forked from olliephillips/sett
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitem.go
More file actions
146 lines (135 loc) · 3.16 KB
/
Copy pathitem.go
File metadata and controls
146 lines (135 loc) · 3.16 KB
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
package sett
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
badger "github.com/dgraph-io/badger/v2"
)
const (
STRUCT_TYPE = 1
STRING_TYPE = 2
)
type SettItem struct {
fullKey string
s *Sett
txn *badger.Txn
unlock bool
}
type SettValueItem struct {
V interface{}
Locked bool
}
func NewSettItem(s *Sett, txn *badger.Txn, key string) *SettItem {
k := s.makeKey(key)
return &SettItem{fullKey: k, s: s, txn: txn, unlock: false}
}
func (si *SettItem) Unlock(u bool) {
si.unlock = u
}
func (si *SettItem) GetStructValue() (*SettValueItem, error) {
item, err := si.txn.Get([]byte(si.fullKey))
if err != nil {
return nil, err
}
meta := item.UserMeta()
if (meta & 0x0F) != STRUCT_TYPE {
return nil, errors.New("Attempt to fetch Struct where item was not struct type")
}
var val []byte
val, err = item.ValueCopy(nil)
if err != nil {
return nil, err
}
var container genericContainer
err = gob.NewDecoder(bytes.NewBuffer(val)).Decode(&container)
if err != nil {
return nil, err
}
var locked bool = false
if (meta & 0x80) != 0 {
locked = true
}
ret := &SettValueItem{V: container.V, Locked: locked}
return ret, nil
}
func (si *SettItem) IsLocked() bool {
item, err := si.txn.Get([]byte(si.fullKey))
if err != nil {
return false
}
if (item.UserMeta() & 0x80) != 0 {
return true
}
return false
}
func (si *SettItem) Lock() error {
item, err := si.txn.Get([]byte(si.fullKey))
if err != nil {
return err
}
meta := item.UserMeta()
if (meta & 0x80) != 0 {
return fmt.Errorf("The item was already locked")
}
var val []byte
val, err = item.ValueCopy(nil)
if err != nil {
return err
}
e := badger.NewEntry([]byte(si.fullKey), val)
meta = meta | 0x80
err = si.setEntry(e, meta)
return err
}
func (si *SettItem) SetStructValue(val interface{}) error {
if !si.unlock && si.IsLocked() {
return fmt.Errorf("The item with key %s is locked. Can't update now", si.fullKey)
}
var bValue bytes.Buffer
container := genericContainer{V: val}
err := gob.NewEncoder(&bValue).Encode(&container)
if err != nil {
return err
}
e := badger.NewEntry([]byte(si.fullKey), bValue.Bytes())
err = si.setEntry(e, STRUCT_TYPE)
return err
}
func (si *SettItem) setEntry(e *badger.Entry, vtype byte) error {
if si.s.ttl > 0 {
e.WithTTL(si.s.ttl)
}
e.WithMeta(vtype)
return si.txn.SetEntry(e)
}
func (si *SettItem) SetStringValue(val string) error {
if !si.unlock && si.IsLocked() {
return fmt.Errorf("The item with key %s is locked. Can't update now", si.fullKey)
}
e := badger.NewEntry([]byte(si.fullKey), []byte(val))
err := si.setEntry(e, STRING_TYPE)
return err
}
func (si *SettItem) GetStringValue() (string, error) {
item, err := si.txn.Get([]byte(si.fullKey))
if err != nil {
return "", err
}
meta := item.UserMeta()
if (meta & 0x0F) != STRING_TYPE {
return "", errors.New("Attempt to fetch Struct where item was not struct type")
}
var val []byte
val, err = item.ValueCopy(nil)
if err != nil {
return "", err
}
return string(val), nil
}
func (si *SettItem) Delete() error {
if !si.unlock && si.IsLocked() {
return fmt.Errorf("The item with key %s is locked. Can't delete now", si.fullKey)
}
return si.txn.Delete([]byte(si.fullKey))
}