Skip to content

Commit

Permalink
Reduplicative ations with same hash shows in action list 1386 (#1404)
Browse files Browse the repository at this point in the history
* Reduplicative ations with same hash shows in action list 1386
  • Loading branch information
coderbradlee authored and zjshen14 committed Aug 5, 2019
1 parent 51cf380 commit b3421b0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
7 changes: 7 additions & 0 deletions blockchain/indexbuilder.go
Expand Up @@ -181,6 +181,13 @@ func (ib *IndexBuilder) initAndLoadActions() error {
if err != nil {
return err
}
} else {
if err = ib.dao.kvstore.Delete(blockAddressActionMappingNS, nil); err != nil {
return err
}
if err = ib.dao.kvstore.Delete(blockAddressActionCountMappingNS, nil); err != nil {
return err
}
}
zap.L().Info("Loading actions", zap.Uint64("startHeight", startHeight), zap.Uint64("startIndex", startIndex))
batch := db.NewBatch()
Expand Down
23 changes: 16 additions & 7 deletions db/db_bolt.go
Expand Up @@ -90,17 +90,26 @@ func (b *boltDB) Get(namespace string, key []byte) ([]byte, error) {
return nil, errors.Wrap(ErrIO, err.Error())
}

// Delete deletes a record
// Delete deletes a record,if key is nil,this will delete the whole bucket
func (b *boltDB) Delete(namespace string, key []byte) (err error) {
numRetries := b.config.NumRetries
for c := uint8(0); c < numRetries; c++ {
err = b.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
if key == nil {
err = b.db.Update(func(tx *bolt.Tx) error {
if err := tx.DeleteBucket([]byte(namespace)); err != bolt.ErrBucketNotFound {
return err
}
return nil
}
return bucket.Delete(key)
})
})
} else {
err = b.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
return nil
}
return bucket.Delete(key)
})
}
if err == nil {
break
}
Expand Down
37 changes: 37 additions & 0 deletions db/db_test.go
Expand Up @@ -323,3 +323,40 @@ func TestCacheKV(t *testing.T) {
testFunc(NewOnDiskDB(cfg), t)
})
}

func TestDeleteBucket(t *testing.T) {
testFunc := func(kv KVStore, t *testing.T) {
require := require.New(t)

require.Nil(kv.Start(context.Background()))
defer func() {
err := kv.Stop(context.Background())
require.Nil(err)
}()

require.NoError(kv.Put(bucket1, testK1[0], testV1[0]))
v, _ := kv.Get(bucket1, testK1[0])
require.Equal(testV1[0], v)

require.NoError(kv.Put(bucket2, testK1[0], testV1[0]))
v, _ = kv.Get(bucket2, testK1[0])
require.Equal(testV1[0], v)

require.NoError(kv.Delete(bucket1, nil))
v, _ = kv.Get(bucket1, testK1[0])
require.Equal([]uint8([]byte(nil)), v)

v, _ = kv.Get(bucket2, testK1[0])
require.Equal(testV1[0], v)
}

path := "test-cache-kv.bolt"
testFile, _ := ioutil.TempFile(os.TempDir(), path)
testPath := testFile.Name()
cfg.DbPath = testPath
t.Run("Bolt DB", func(t *testing.T) {
testutil.CleanupPath(t, testPath)
defer testutil.CleanupPath(t, testPath)
testFunc(NewBoltDB(cfg), t)
})
}

0 comments on commit b3421b0

Please sign in to comment.