File: batch.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (53 lines) | stat: -rw-r--r-- 930 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package etcddb

import (
	"github.com/aptly-dev/aptly/database"
	clientv3 "go.etcd.io/etcd/client/v3"
)

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

type WriteOptions struct {
	NoWriteMerge bool
	Sync         bool
}

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) {
	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
}

// batch should implement database.Batch
var (
	_ database.Batch = &EtcDBatch{}
)