-
Notifications
You must be signed in to change notification settings - Fork 0
/
opget.go
181 lines (161 loc) · 4.83 KB
/
opget.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package edb
import (
"reflect"
)
func Reload[Row any](txh Txish, row *Row) *Row {
tx := txh.DBTx()
tbl := tx.Schema().TableByRow((*Row)(nil))
rowVal := reflect.ValueOf(row)
keyVal := tbl.RowKeyVal(rowVal)
newRowVal, _, err := tx.getRowValByKeyVal(tbl, keyVal, true)
if err != nil {
panic(err)
}
if !newRowVal.IsValid() {
return nil
}
return newRowVal.Interface().(*Row)
}
func Get[Row any](txh Txish, key any) *Row {
tx := txh.DBTx()
tbl := tx.Schema().TableByRow((*Row)(nil))
row, _ := tx.Get(tbl, key)
if row == nil {
return nil
}
return row.(*Row)
}
func GetByKeyRaw[Row any](txh Txish, keyRaw []byte) *Row {
tx := txh.DBTx()
tbl := tx.Schema().TableByRow((*Row)(nil))
row, _ := tx.GetByKeyRaw(tbl, keyRaw)
if row == nil {
return nil
}
return row.(*Row)
}
func Exists[Row any](txh Txish, key any) bool {
tx := txh.DBTx()
tbl := tx.Schema().TableByRow((*Row)(nil))
return tx.Exists(tbl, key)
}
func (tx *Tx) Get(tbl *Table, key any) (any, ValueMeta) {
return tx.GetByKeyVal(tbl, reflect.ValueOf(key))
}
func (tx *Tx) TryGet(tbl *Table, key any) (any, ValueMeta, error) {
return tx.TryGetByKeyVal(tbl, reflect.ValueOf(key))
}
func (tx *Tx) GetByKeyVal(tbl *Table, keyVal reflect.Value) (any, ValueMeta) {
if tbl == nil {
panic("tbl == nil")
}
rowVal, rowMeta, err := tx.getRowValByKeyVal(tbl, keyVal, true)
if err != nil {
panic(err)
}
if !rowVal.IsValid() {
return nil, ValueMeta{}
}
return rowVal.Interface(), rowMeta
}
func (tx *Tx) TryGetByKeyVal(tbl *Table, keyVal reflect.Value) (any, ValueMeta, error) {
if tbl == nil {
panic("tbl == nil")
}
rowVal, rowMeta, err := tx.getRowValByKeyVal(tbl, keyVal, true)
if err != nil {
return nil, rowMeta, err
}
if !rowVal.IsValid() {
return nil, ValueMeta{}, nil
}
return rowVal.Interface(), rowMeta, nil
}
func (tx *Tx) GetByKeyRaw(tbl *Table, keyRaw []byte) (any, ValueMeta) {
if tbl == nil {
panic("tbl == nil")
}
rowVal, rowMeta, err := tx.getRowValByKeyRaw(tbl, keyRaw, true, hexBytes(keyRaw))
if err != nil {
panic(err)
}
if !rowVal.IsValid() {
return nil, ValueMeta{}
}
return rowVal.Interface(), rowMeta
}
func (tx *Tx) GetMeta(tbl *Table, key any) ValueMeta {
return tx.GetMetaByKeyVal(tbl, reflect.ValueOf(key))
}
func (tx *Tx) GetMetaByKeyVal(tbl *Table, keyVal reflect.Value) ValueMeta {
_, rowMeta, err := tx.getRowValByKeyVal(tbl, keyVal, false)
if err != nil {
panic(err)
}
return rowMeta
}
func (tx *Tx) Exists(tbl *Table, key any) bool {
return tx.existsByKeyVal(tbl, reflect.ValueOf(key))
}
func (tx *Tx) getRowValByKeyVal(tbl *Table, keyVal reflect.Value, includeRow bool) (reflect.Value, ValueMeta, error) {
keyVal = tbl.ensureCorrectKeyType(keyVal)
keyBuf := keyBytesPool.Get().([]byte)
keyRaw := tbl.encodeKeyVal(keyBuf, keyVal, true)
defer keyBytesPool.Put(keyBuf[:0])
return tx.getRowValByKeyRaw(tbl, keyRaw, includeRow, keyVal.Interface())
}
func (tx *Tx) getRowValByKeyRaw(tbl *Table, keyRaw []byte, includeRow bool, keyValueForLogging any) (reflect.Value, ValueMeta, error) {
val, valMeta, err := tx.getRowValByRawKey(tbl, keyRaw, includeRow)
if tx.db.verbose {
if includeRow {
if val.IsValid() {
tx.db.logf("db: GET %s/%v => %v", tbl.name, keyValueForLogging, loggableRowVal(tbl, val))
} else {
tx.db.logf("db: GET.NOTFOUND %s/%v", tbl.name, keyValueForLogging)
}
} else {
if val.IsValid() {
tx.db.logf("db: META %s/%v => %v", tbl.name, keyValueForLogging, loggableRowVal(tbl, val))
} else {
tx.db.logf("db: META.NOTFOUND %s/%v", tbl.name, keyValueForLogging)
}
}
}
return val, valMeta, err
}
func (tx *Tx) existsByKeyVal(tbl *Table, keyVal reflect.Value) bool {
keyVal = tbl.ensureCorrectKeyType(keyVal)
keyBuf := keyBytesPool.Get().([]byte)
keyRaw := tbl.encodeKeyVal(keyBuf, keyVal, true)
defer keyBytesPool.Put(keyBuf[:0])
found := (tx.getRawByRawKey(tbl, keyRaw) != nil)
if tx.db.verbose {
tx.db.logf("db: EXISTS.%s %s/%v", map[bool]string{false: "NO", true: "YES"}[found], tbl.name, keyVal.Interface())
}
return found
}
func (tx *Tx) ExistsByKeyRaw(tbl *Table, keyRaw []byte) bool {
found := (tx.getRawByRawKey(tbl, keyRaw) != nil)
if tx.db.verbose {
tx.db.logf("db: EXISTS.%s %s/%x", map[bool]string{false: "NO", true: "YES"}[found], tbl.name, keyRaw)
}
return found
}
func (tx *Tx) getRowValByRawKey(tbl *Table, keyRaw []byte, includeRow bool) (reflect.Value, ValueMeta, error) {
valueRaw := tx.getRawByRawKey(tbl, keyRaw)
if valueRaw == nil {
return reflect.Value{}, ValueMeta{}, nil
}
if includeRow {
return decodeTableRow(tbl, keyRaw, valueRaw, tx)
} else {
var vle value
decodeTableValue(&vle, tbl, keyRaw, valueRaw)
return reflect.Value{}, vle.ValueMeta(), nil
}
}
func (tx *Tx) getRawByRawKey(tbl *Table, keyRaw []byte) []byte {
tableBuck := nonNil(tx.btx.Bucket(tbl.buck.Raw()))
dataBuck := nonNil(tableBuck.Bucket(dataBucket.Raw()))
return dataBuck.Get(keyRaw)
}