Skip to content

Commit

Permalink
etcd: implement batch operations
Browse files Browse the repository at this point in the history
- cache the operations internally in a list
- Write() applies the list to etcd
  • Loading branch information
neolynx committed Jul 31, 2024
1 parent 9768ece commit 7a01c9c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions database/etcddb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,44 @@ import (
)

type EtcDBatch struct {
db *clientv3.Client
s *EtcDStorage
ops []clientv3.Op
}

type WriteOptions struct {
NoWriteMerge bool
Sync bool
}

func (b *EtcDBatch) Put(key, value []byte) (err error) {
_, err = b.db.Put(Ctx, string(key), string(value))
func (b *EtcDBatch) Put(key []byte, value []byte) (err error) {
b.ops = append(b.ops, clientv3.OpPut(string(key), string(value)))
return
}

func (b *EtcDBatch) Delete(key []byte) (err error) {
_, err = b.db.Delete(Ctx, string(key))
b.ops = append(b.ops, clientv3.OpDelete(string(key)))
return
}

func (b *EtcDBatch) Write() (err error) {
kv := clientv3.NewKV(b.s.db)

batchSize := 128
for i := 0; i < len(b.ops); i += batchSize {
txn := kv.Txn(Ctx)
end := i + batchSize
if end > len(b.ops) {
end = len(b.ops)
}

batch := b.ops[i:end]
txn.Then(batch...)
_, err = txn.Commit()
if err != nil {
panic(err)
}
}

return
}

Expand Down
2 changes: 1 addition & 1 deletion database/etcddb/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (s *EtcDStorage) CreateBatch() database.Batch {
return nil
}
return &EtcDBatch{
db: s.db,
s: s,
}
}

Expand Down

0 comments on commit 7a01c9c

Please sign in to comment.