本文整理汇总了Golang中github.com/tecbot/gorocksdb.NewWriteBatch函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriteBatch函数的具体用法?Golang NewWriteBatch怎么用?Golang NewWriteBatch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewWriteBatch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: populateDB
func populateDB(tb testing.TB, kvSize int, totalKeys int, keyPrefix string) {
dbWrapper := db.NewTestDBWrapper()
dbWrapper.CreateFreshDB(tb)
batch := gorocksdb.NewWriteBatch()
for i := 0; i < totalKeys; i++ {
key := []byte(keyPrefix + strconv.Itoa(i))
value := testutil.ConstructRandomBytes(tb, kvSize-len(key))
batch.Put(key, value)
if i%1000 == 0 {
dbWrapper.WriteToDB(tb, batch)
batch = gorocksdb.NewWriteBatch()
}
}
dbWrapper.CloseDB(tb)
}
示例2: _list_doMerge
func (rh *RocksDBHandler) _list_doMerge(key []byte, value interface{}, opCode string, start, end int) error {
var values [][]byte
if d1Slice, ok := value.([]byte); ok {
values = [][]byte{d1Slice}
}
if d2Slice, ok := value.([][]byte); ok {
values = d2Slice
}
if values == nil || len(values) == 0 {
return ErrWrongArgumentsCount
}
options := rocks.NewDefaultWriteOptions()
defer options.Destroy()
batch := rocks.NewWriteBatch()
defer batch.Destroy()
batch.Put(rh.getTypeKey(key), []byte(kRedisList))
for _, dValue := range values {
operand := ListOperand{opCode, start, end, dValue}
if data, err := encode(operand); err == nil {
batch.Merge(key, data)
} else {
return err
}
}
return rh.db.Write(options, batch)
}
示例3: CommitTxBatch
// CommitTxBatch - gets invoked when the current transaction-batch needs to be committed
// This function returns successfully iff the transactions details and state changes (that
// may have happened during execution of this transaction-batch) have been committed to permanent storage
func (ledger *Ledger) CommitTxBatch(id interface{}, transactions []*protos.Transaction, proof []byte) error {
err := ledger.checkValidIDCommitORRollback(id)
if err != nil {
return err
}
success := true
defer ledger.resetForNextTxGroup(success)
defer ledger.blockchain.blockPersistenceStatus(success)
stateHash, err := ledger.state.GetHash()
if err != nil {
success = false
return err
}
writeBatch := gorocksdb.NewWriteBatch()
block := protos.NewBlock(transactions)
newBlockNumber, err := ledger.blockchain.addPersistenceChangesForNewBlock(context.TODO(), block, stateHash, writeBatch)
if err != nil {
success = false
return err
}
ledger.state.AddChangesForPersistence(newBlockNumber, writeBatch)
opt := gorocksdb.NewDefaultWriteOptions()
dbErr := db.GetDBHandle().DB.Write(opt, writeBatch)
if dbErr != nil {
success = false
return dbErr
}
producer.Send(producer.CreateBlockEvent(block))
return nil
}
示例4: RedisDel
func (rh *RocksDBHandler) RedisDel(key []byte, keys ...[]byte) (int, error) {
if rh.db == nil {
return 0, ErrRocksIsDead
}
if key == nil || len(key) == 0 {
return 0, ErrWrongArgumentsCount
}
keyData := append([][]byte{key}, keys...)
count := 0
readOptions := rocks.NewDefaultReadOptions()
writeOptions := rocks.NewDefaultWriteOptions()
defer readOptions.Destroy()
defer writeOptions.Destroy()
for _, dKey := range keyData {
_, err := rh.loadRedisObject(readOptions, dKey)
if err == nil {
batch := rocks.NewWriteBatch()
batch.Delete(rh.getTypeKey(dKey))
batch.Delete(dKey)
if err := rh.db.Write(writeOptions, batch); err == nil {
count++
}
batch.Destroy()
}
}
return count, nil
}
示例5: persistAndClearInMemoryChanges
func (testWrapper *stateTestWrapper) persistAndClearInMemoryChanges(blockNumber uint64) {
writeBatch := gorocksdb.NewWriteBatch()
defer writeBatch.Destroy()
testWrapper.state.AddChangesForPersistence(blockNumber, writeBatch)
testDBWrapper.WriteToDB(testWrapper.t, writeBatch)
testWrapper.state.ClearInMemoryChanges(true)
}
示例6: TestDBStatsOversizedKV
func TestDBStatsOversizedKV(t *testing.T) {
dbTestWrapper := db.NewTestDBWrapper()
dbTestWrapper.CleanDB(t)
defer dbTestWrapper.CloseDB(t)
defer deleteTestDBDir()
openchainDB := db.GetDBHandle()
writeBatch := gorocksdb.NewWriteBatch()
writeBatch.PutCF(openchainDB.BlockchainCF, []byte("key1"), []byte("value1"))
writeBatch.PutCF(openchainDB.BlockchainCF, []byte("key2"), generateOversizedValue(0))
writeBatch.PutCF(openchainDB.BlockchainCF, []byte("key3"), generateOversizedValue(100))
writeBatch.PutCF(openchainDB.BlockchainCF, []byte("key4"), []byte("value4"))
dbTestWrapper.WriteToDB(t, writeBatch)
totalKVs, numOverSizedKVs := scan(openchainDB, "blockchainCF", openchainDB.BlockchainCF, testDetailPrinter)
if totalKVs != 4 {
t.Fatalf("totalKVs is not correct. Expected [%d], found [%d]", 4, totalKVs)
}
if numOverSizedKVs != 2 {
t.Fatalf("numOverSizedKVs is not correct. Expected [%d], found [%d]", 2, numOverSizedKVs)
}
if numOversizedKeyValues != 2 {
t.Fatalf("numOversizedKeyValues is not correct. Expected [%d], found [%d]", 2, numOversizedKeyValues)
}
}
示例7: pop
// true for LPop(), false for RPop()
func (l *ListElement) pop(left bool) ([]byte, error) {
l.mu.Lock()
defer l.mu.Unlock()
x, y := l.leftIndex(), l.rightIndex()
size := y - x + 1
if size == 0 {
return nil, nil
}
var idxkey []byte
if left {
idxkey = l.indexKey(x)
} else {
idxkey = l.indexKey(y)
}
val, err := l.db.RawGet(idxkey)
if err != nil {
return nil, err
}
if size > 1 {
return val, l.db.RawDelete(idxkey)
} else if size == 1 {
batch := gorocksdb.NewWriteBatch()
defer batch.Destroy()
batch.Delete(l.rawKey())
batch.Delete(idxkey)
return val, l.db.WriteBatch(batch)
} else {
return nil, errors.New("size less than 0")
}
}
示例8: NewBatch
func (w *Writer) NewBatch() store.KVBatch {
rv := Batch{
w: w,
batch: gorocksdb.NewWriteBatch(),
}
return &rv
}
示例9: Add
// http://redis.io/commands/zadd#return-value
func (s *SortedSetElement) Add(scoreMembers ...[]byte) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
count := len(scoreMembers)
if count < 2 || count%2 != 0 {
return 0, errors.New("invalid score/member pairs")
}
batch := gorocksdb.NewWriteBatch()
defer batch.Destroy()
added := 0
for i := 0; i < count; i += 2 {
score, member := scoreMembers[i], scoreMembers[i+1]
skey, mkey := s.scoreKey(score, member), s.memberKey(member)
// remove old score key
oldscore, err := s.db.RawGet(mkey)
if err != nil {
return 0, err
} else if oldscore != nil {
batch.Delete(skey)
} else {
added++
}
// put new value
batch.Put(mkey, score)
batch.Put(skey, nil)
}
return added, s.db.WriteBatch(batch)
}
示例10: Remove
func (h *HashElement) Remove(fields ...[]byte) error {
h.mu.Lock()
defer h.mu.Unlock()
batch := gorocksdb.NewWriteBatch()
defer batch.Destroy()
dict := make(map[string]bool)
for _, field := range fields {
dict[string(field)] = true
batch.Delete(h.fieldKey(field))
}
deleteAll := true
h.db.PrefixEnumerate(h.fieldPrefix(), IterForward, func(i int, key, value []byte, quit *bool) {
field := h.fieldInKey(key)
if _, ok := dict[string(field)]; !ok { // wouldn't delete raw key
deleteAll = false
*quit = true
}
})
if deleteAll {
batch.Delete(h.rawKey())
}
return h.db.WriteBatch(batch)
}
示例11: persistRawBlock
func (blockchain *blockchain) persistRawBlock(block *protos.Block, blockNumber uint64) error {
blockBytes, blockBytesErr := block.Bytes()
if blockBytesErr != nil {
return blockBytesErr
}
writeBatch := gorocksdb.NewWriteBatch()
defer writeBatch.Destroy()
writeBatch.PutCF(db.GetDBHandle().BlockchainCF, encodeBlockNumberDBKey(blockNumber), blockBytes)
blockHash, err := block.GetHash()
if err != nil {
return err
}
// Need to check as we suport out of order blocks in cases such as block/state synchronization. This is
// really blockchain height, not size.
if blockchain.getSize() < blockNumber+1 {
sizeBytes := encodeUint64(blockNumber + 1)
writeBatch.PutCF(db.GetDBHandle().BlockchainCF, blockCountKey, sizeBytes)
blockchain.size = blockNumber + 1
blockchain.previousBlockHash = blockHash
}
if blockchain.indexer.isSynchronous() {
blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch)
}
opt := gorocksdb.NewDefaultWriteOptions()
defer opt.Destroy()
err = db.GetDBHandle().DB.Write(opt, writeBatch)
if err != nil {
return err
}
return nil
}
示例12: PersistChangesAndResetInMemoryChanges
func (stateTrieTestWrapper *stateTrieTestWrapper) PersistChangesAndResetInMemoryChanges() {
writeBatch := gorocksdb.NewWriteBatch()
defer writeBatch.Destroy()
stateTrieTestWrapper.AddChangesForPersistence(writeBatch)
testDBWrapper.WriteToDB(stateTrieTestWrapper.t, writeBatch)
stateTrieTestWrapper.stateTrie.ClearWorkingSet(true)
}
示例13: addNewBlock
func (testWrapper *blockchainTestWrapper) addNewBlock(block *protos.Block, stateHash []byte) uint64 {
writeBatch := gorocksdb.NewWriteBatch()
newBlockNumber, err := testWrapper.blockchain.addPersistenceChangesForNewBlock(context.TODO(), block, stateHash, writeBatch)
testutil.AssertNoError(testWrapper.t, err, "Error while adding a new block")
testDBWrapper.WriteToDB(testWrapper.t, writeBatch)
testWrapper.blockchain.blockPersistenceStatus(true)
return newBlockNumber
}
示例14: CommitStateDelta
// CommitStateDelta commits the changes from state.ApplyStateDelta to the
// DB.
func (state *State) CommitStateDelta() error {
if state.updateStateImpl {
state.stateImpl.PrepareWorkingSet(state.stateDelta)
state.updateStateImpl = false
}
writeBatch := gorocksdb.NewWriteBatch()
state.stateImpl.AddChangesForPersistence(writeBatch)
opt := gorocksdb.NewDefaultWriteOptions()
return db.GetDBHandle().DB.Write(opt, writeBatch)
}
示例15: Enqueue
func (q *Queue) Enqueue(data []byte) (uint64, error) {
id := atomic.AddUint64(&q.tail, 1)
wb := rocks.NewWriteBatch()
defer wb.Destroy()
wb.MergeCF(q.cfHandle, q.metaKey("tail"), oneBinary)
wb.PutCF(q.cfHandle, q.key(id), data)
err := q.store.Write(q.store.wo, wb)
log.Debugf("[Queue] Enqueued data id=%d, err=%v", id, err)
return id, err
}