-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbatch.go
61 lines (51 loc) · 1.19 KB
/
batch.go
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
54
55
56
57
58
59
60
61
package bitcask
type Batch struct {
records []*Record
// the total number of bytes of serialized records
byteSize int
}
func NewBatch() *Batch {
return &Batch{}
}
func (b *Batch) Put(ns, key, val []byte, meta *Meta) {
record := &Record{
Ns: ns,
Key: key,
Meta: meta,
Value: val,
Deleted: false,
}
b.records = append(b.records, record)
b.byteSize += record.ApproximateSize()
}
func (b *Batch) Delete(ns, key []byte) {
record := &Record{
Ns: ns,
Key: key,
// the deletion operation will carry tombstone flag, and store in database
// at the same time, the related index in memory will be removed. so the key will
// not be found. the record with tombstone flag will be removed in compaction
Meta: NewMetaWithTombstone(),
Value: nil,
Deleted: true,
}
b.records = append(b.records, record)
b.byteSize += record.ApproximateSize()
}
func (b *Batch) Clear() {
b.byteSize = 0
b.records = nil
}
func (b *Batch) Append(batch *Batch) {
if batch == nil {
return
}
b.records = append(b.records, batch.records...)
b.byteSize += batch.byteSize
}
func (b *Batch) Size() int {
return len(b.records)
}
func (b *Batch) ByteSize() int {
return b.byteSize
}