forked from go-bond/bond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_unsafe.go
103 lines (86 loc) · 2.26 KB
/
table_unsafe.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package bond
import (
"bytes"
"context"
"fmt"
"golang.org/x/exp/maps"
)
// TableUnsafeUpdater provides access to UnsafeUpdate method that allows
// to reduce number of database calls if you already have original version
// of the entry.
//
// Warning: If you provide outdated rows the table indexes may be corrupted.
type TableUnsafeUpdater[T any] interface {
UnsafeUpdate(ctx context.Context, trs []T, oldTrs []T, optBatch ...Batch) error
}
func (t *_table[T]) UnsafeUpdate(ctx context.Context, trs []T, oldTrs []T, optBatch ...Batch) error {
if len(trs) != len(oldTrs) {
return fmt.Errorf("params need to be of equal size")
}
t.mutex.RLock()
indexes := make(map[IndexID]*Index[T])
maps.Copy(indexes, t.secondaryIndexes)
t.mutex.RUnlock()
var batch Batch
var externalBatch = len(optBatch) > 0 && optBatch[0] != nil
if externalBatch {
batch = optBatch[0]
} else {
batch = t.db.Batch()
}
// key
var (
keyBuffer = t.db.getKeyBufferPool().Get()[:0]
indexKeyBuffer = t.db.getKeyBufferPool().Get()[:0]
indexKeyBuffer2 = t.db.getKeyBufferPool().Get()[:0]
)
defer t.db.getKeyBufferPool().Put(keyBuffer[:0])
defer t.db.getKeyBufferPool().Put(indexKeyBuffer[:0])
defer t.db.getKeyBufferPool().Put(indexKeyBuffer2[:0])
// value
value := t.db.getValueBufferPool().Get()[:0]
valueBuffer := bytes.NewBuffer(value)
defer t.db.getValueBufferPool().Put(value)
// serializer
var serialize = t.serializer.Serializer.Serialize
if sw, ok := t.serializer.Serializer.(SerializerWithBuffer[any]); ok {
serialize = sw.SerializeFuncWithBuffer(valueBuffer)
}
for i := 0; i < len(trs); i++ {
tr := trs[i]
oldTr := oldTrs[i]
select {
case <-ctx.Done():
return fmt.Errorf("context done: %w", ctx.Err())
default:
}
// update key
key := t.key(tr, keyBuffer[:0])
// serialize
data, err := serialize(&tr)
if err != nil {
return err
}
// update entry
err = batch.Set(key, data, Sync)
if err != nil {
_ = batch.Close()
return err
}
// update indexes
for _, idx := range indexes {
err = idx.OnUpdate(t, oldTr, tr, batch, indexKeyBuffer[:0], indexKeyBuffer2[:0])
if err != nil {
return err
}
}
}
if !externalBatch {
err := batch.Commit(Sync)
if err != nil {
_ = batch.Close()
return err
}
}
return nil
}