-
Notifications
You must be signed in to change notification settings - Fork 0
/
opget.go
113 lines (99 loc) · 3.21 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
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, _ := tx.getRowValByKeyVal(tbl, keyVal, true)
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 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) GetByKeyVal(tbl *Table, keyVal reflect.Value) (any, ValueMeta) {
rowVal, rowMeta := tx.getRowValByKeyVal(tbl, keyVal, true)
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 := tx.getRowValByKeyVal(tbl, keyVal, false)
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) {
keyVal = tbl.ensureCorrectKeyType(keyVal)
keyBuf := keyBytesPool.Get().([]byte)
keyRaw := tbl.encodeKeyVal(keyBuf, keyVal, true)
defer keyBytesPool.Put(keyBuf[:0])
val, valMeta := tx.getRowValByRawKey(tbl, keyRaw, includeRow)
if tx.db.verbose {
if includeRow {
if val.IsValid() {
tx.db.logf("db: GET %s/%v => %v", tbl.name, keyVal.Interface(), loggableRowVal(val))
} else {
tx.db.logf("db: GET.NOTFOUND %s/%v", tbl.name, keyVal.Interface())
}
} else {
if val.IsValid() {
tx.db.logf("db: META %s/%v => %v", tbl.name, keyVal.Interface(), loggableRowVal(val))
} else {
tx.db.logf("db: META.NOTFOUND %s/%v", tbl.name, keyVal.Interface())
}
}
}
return val, valMeta
}
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) getRowValByRawKey(tbl *Table, keyRaw []byte, includeRow bool) (reflect.Value, ValueMeta) {
valueRaw := tx.getRawByRawKey(tbl, keyRaw)
if valueRaw == nil {
return reflect.Value{}, ValueMeta{}
}
if includeRow {
rowVal, rowMeta := decodeTableRow(tbl, keyRaw, valueRaw, tx)
return rowVal, rowMeta
} else {
vle := decodeTableValue(tbl, keyRaw, valueRaw)
return reflect.Value{}, vle.ValueMeta()
}
}
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)
}