-
Notifications
You must be signed in to change notification settings - Fork 5
/
apiget.go
55 lines (40 loc) · 1.37 KB
/
apiget.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
package tormenta
import (
"time"
"github.com/jpincas/gouuidv6"
)
var noCTX = make(map[string]interface{})
// Get retrieves an entity, either according to the ID set on the entity,
// or using a separately specified ID (optional, takes priority)
func (db DB) Get(entity Record, ids ...gouuidv6.UUID) (bool, error) {
return db.GetWithContext(entity, noCTX, ids...)
}
// GetWithContext retrieves an entity, either according to the ID set on the entity,
// or using a separately specified ID (optional, takes priority), and allows the passing of a non-empty context.
func (db DB) GetWithContext(entity Record, ctx map[string]interface{}, ids ...gouuidv6.UUID) (bool, error) {
t := time.Now()
txn := db.KV.NewTransaction(false)
defer txn.Discard()
ok, err := db.get(txn, entity, ctx, ids...)
if db.Options.DebugMode {
var n int
if ok {
n = 1
}
debugLogGet(entity, t, n, err, ids...)
}
return ok, err
}
func (db DB) GetIDs(target interface{}, ids ...gouuidv6.UUID) (int, error) {
return db.GetIDsWithContext(target, noCTX, ids...)
}
func (db DB) GetIDsWithContext(target interface{}, ctx map[string]interface{}, ids ...gouuidv6.UUID) (int, error) {
t := time.Now()
txn := db.KV.NewTransaction(false)
defer txn.Discard()
n, err := db.getIDsWithContext(txn, target, ctx, ids...)
if db.Options.DebugMode {
debugLogGet(target, t, n, err, ids...)
}
return n, err
}