1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-02 12:20:03 +08:00

Merge pull request #1161 from ipfs/blocks-put-exists

blocks: Don't re-Put blocks we already have
This commit is contained in:
Juan Batiz-Benet
2015-04-28 16:42:06 -07:00
4 changed files with 16 additions and 7 deletions

View File

@ -64,10 +64,11 @@ func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
} }
func (bs *blockstore) Put(block *blocks.Block) error { func (bs *blockstore) Put(block *blocks.Block) error {
// Has is cheaper than
k := block.Key().DsKey() k := block.Key().DsKey()
// Has is cheaper than Put, so see if we already have it
exists, err := bs.datastore.Has(k) exists, err := bs.datastore.Has(k)
if err != nil && exists { if err == nil && exists {
return nil // already stored. return nil // already stored.
} }
return bs.datastore.Put(k, block.Data) return bs.datastore.Put(k, block.Data)

View File

@ -116,7 +116,6 @@ func main() {
} }
} }
// ok now handle parse error (which means cli input was wrong, // ok now handle parse error (which means cli input was wrong,
// e.g. incorrect number of args, or nonexistent subcommand) // e.g. incorrect number of args, or nonexistent subcommand)
if parseErr != nil { if parseErr != nil {

View File

@ -40,11 +40,20 @@ Please run the ipfs migration tool before continuing.
` + migrationInstructions ` + migrationInstructions
var ( var (
ErrNoRepo = func (path string) error { return fmt.Errorf("no ipfs repo found in '%s'. please run: ipfs init ", path) }
ErrNoVersion = errors.New("no version file found, please run 0-to-1 migration tool.\n" + migrationInstructions) ErrNoVersion = errors.New("no version file found, please run 0-to-1 migration tool.\n" + migrationInstructions)
ErrOldRepo = errors.New("ipfs repo found in old '~/.go-ipfs' location, please run migration tool.\n" + migrationInstructions) ErrOldRepo = errors.New("ipfs repo found in old '~/.go-ipfs' location, please run migration tool.\n" + migrationInstructions)
) )
type NoRepoError struct {
Path string
}
var _ error = NoRepoError{}
func (err NoRepoError) Error() string {
return fmt.Sprintf("no ipfs repo found in '%s'. please run: ipfs init ", err.Path)
}
const ( const (
leveldbDirectory = "datastore" leveldbDirectory = "datastore"
flatfsDirectory = "blocks" flatfsDirectory = "blocks"
@ -172,7 +181,7 @@ func checkInitialized(path string) error {
if isInitializedUnsynced(alt) { if isInitializedUnsynced(alt) {
return ErrOldRepo return ErrOldRepo
} }
return ErrNoRepo(path) return NoRepoError{Path: path}
} }
return nil return nil
} }