-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexes.go
70 lines (62 loc) · 1.31 KB
/
indexes.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
package odb
import (
"bytes"
"fmt"
"github.com/cznic/kv"
)
var (
oidKey = []byte("oid")
)
type OidIndex struct {
*kv.DB
}
func (o *OidIndex) Name() string {
return "core_oid"
}
func (o *OidIndex) incOid() (int64, error) {
return o.Inc(oidKey, 1)
}
func (o *OidIndex) Write(dbe *DBEntry) error {
if dbe.LocalId() == 0 {
nid, err := o.incOid()
if err != nil {
return err
}
dbe.SetLocalId(nid)
}
buf := &bytes.Buffer{}
buf.Grow(8)
bw := &BinaryBuffer{buf, nil}
bw.WriteInt64(dbe.Oid())
bw.WriteTypedMap(&dbe.TypedMap)
bin := buf.Bytes()
return o.Set(bin[:8], bin[8:])
}
func (o *OidIndex) ExplainError(err error, isKey bool) error {
return fmt.Errorf("error %v (was key? %v)", err, isKey)
}
func (o *OidIndex) Find(values ...interface{}) (*DBEntry, error) {
buf := &bytes.Buffer{}
bw := BinaryBuffer{buf, nil}
if len(values) != 1 {
return nil, errInvalidIndexFind
}
if k, ok := values[0].(int64); ok {
bw.WriteInt64(k)
val, err := o.Get(nil, buf.Bytes())
if err != nil {
return nil, newError(UnableToReadStorage, "unable to read storage. cause: %v", err)
}
buf = bytes.NewBuffer(val)
bw.SwitchToReader(buf)
out := &DBEntry{
Object: NewObject(),
}
err = bw.ReadTypedMap(&out.TypedMap)
if err != nil {
return nil, err
}
return out, nil
}
return nil, errInvalidIndexFind
}