-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazydict_i64str.go
277 lines (206 loc) · 5.62 KB
/
lazydict_i64str.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package gokvdb
import (
"fmt"
"os"
"sort"
//"hash/fnv"
)
type LazyI64StrDict struct {
dbName string
dictName string
storage *Storage
contextByBranchKey map[int64]*LazyI64StrContext
//bt *BTreeIndex
internalPager IPager
keyFactory *BranchI64BTreeFactory
//bt *BTreeBlobMap
isChanged bool
}
type LazyI64StrContext struct {
pid uint32
branchKey int64
getValueByKey map[int64]string
isChanged bool
dict *LazyI64StrDict
}
func NewI64StrDict(s *Storage, dbName string, dictName string) *LazyI64StrDict {
self := new(LazyI64StrDict)
self.dbName = dbName
self.dictName = dictName
self.storage = s
self.contextByBranchKey = make(map[int64]*LazyI64StrContext)
db, err := s.DB(dbName)
if err != nil {
fmt.Println("error dbName", dbName)
os.Exit(1)
}
metaData, ok := db.GetMeta(dictName)
var internalPagerMeta []byte
var keyFactoryMeta []byte
if ok {
rd := NewDataStreamFromBuffer(metaData)
internalPagerMeta = rd.ReadChunk()
keyFactoryMeta = rd.ReadChunk()
//fmt.Println("NewI64StrDict keyFactoryMeta", keyFactoryMeta)
}
internalPageSize := uint16(128)
internalPager := NewInternalPager(s.pager, internalPageSize, internalPagerMeta)
self.internalPager = internalPager
self.keyFactory = NewBranchI64BTreeFactory(internalPager, keyFactoryMeta, 3)
return self
}
func (d *LazyI64StrDict) ToString() string {
return fmt.Sprintf("<LazyI64StrDict db=%v name=%v>", d.dbName, d.dictName)
}
func (c *LazyI64StrContext) ToString() string {
return fmt.Sprintf("<LazyI64StrContext pid=%v branchKey=%v>", c.pid, c.branchKey)
}
func (d *LazyI64StrDict) _GetBranchKey(key int64) int64 {
return key / 4096
}
type LazyI64StrDictItem struct {
key int64
value string
}
func (i *LazyI64StrDictItem) Key() int64 {
return i.key
}
func (i *LazyI64StrDictItem) Value() string {
return i.value
}
func (self *LazyI64StrDict) Items() chan LazyI64StrDictItem {
q := make(chan LazyI64StrDictItem)
go func(ch chan LazyI64StrDictItem) {
for _item := range self.keyFactory.Items() {
branchKey := _item.Key()
ctxPageId := uint32(_item.Value())
ctx, ok := self.contextByBranchKey[branchKey]
if !ok {
ctx = self._ReadContext(ctxPageId, branchKey)
}
var keys I64Array
for k, _ := range ctx.getValueByKey {
keys = append(keys, k)
}
sort.Sort(keys)
for _, k := range keys {
v, _ := ctx.getValueByKey[k]
ch <- LazyI64StrDictItem{key: k, value: v}
}
}
close(ch)
}(q)
return q
}
func (self *LazyI64StrDict) Set(key int64, value string) {
branchKey := self._GetBranchKey(key)
ctx := self._GetContextByBranchKey(branchKey)
if ctx == nil {
ctxPageId := self.internalPager.CreatePageId()
page := self.keyFactory.GetOrCreatePage(branchKey)
page.Set(branchKey, int64(ctxPageId))
ctx = self._NewContext(ctxPageId, branchKey)
ctx.isChanged = true
self.contextByBranchKey[branchKey] = ctx
}
//fmt.Println(d.ToString(), "SET", key, value, ctx.ToString())
ctx.getValueByKey[key] = value
ctx.isChanged = true
}
func (self *LazyI64StrDict) Get(key int64) (string, bool) {
//bt := d._GetBt()
branchKey := self._GetBranchKey(key)
ctx := self._GetContextByBranchKey(branchKey)
if ctx != nil {
val, ok := ctx.getValueByKey[key]
if ok {
return val, true
}
}
return "", false
}
func (self *LazyI64StrDict) ReleaseCache() {
var keys []int64
for key, ctx := range self.contextByBranchKey {
if !ctx.isChanged {
keys = append(keys, key)
}
}
for _, key := range keys {
ctx, _ := self.contextByBranchKey[key]
fmt.Println("[ReleaseCache]", ctx.ToString())
delete(self.contextByBranchKey, key)
ctx = nil
}
}
func (self *LazyI64StrDict) Save(commit bool) {
//fmt.Println("Save", d.ToString())
//isChanged := false
for _, ctx := range self.contextByBranchKey {
if ctx.isChanged {
w := NewDataStream()
w.WriteUInt24(uint32(len(ctx.getValueByKey)))
for k, v := range ctx.getValueByKey {
w.WriteUInt64(uint64(k))
w.WriteChunk([]byte(v))
//fmt.Println("SAVE CONTEXT", k, v)
}
ctxData := w.ToBytes()
self.internalPager.WritePayloadData(ctx.pid, ctxData)
ctx.isChanged = false
//isChanged = true
//fmt.Println("Save", ctx.ToString(), "bytes", len(ctxData))
}
}
db, err := self.storage.DB(self.dbName)
if err != nil {
fmt.Println("error dbName", self.dbName)
os.Exit(1)
}
keyMeta := self.keyFactory.Save()
internalPagerMeta := self.internalPager.Save()
metaW := NewDataStream()
metaW.WriteChunk(internalPagerMeta)
metaW.WriteChunk(keyMeta)
metaBytes := metaW.ToBytes()
db.SetMeta(self.dictName, metaBytes)
if commit {
self.storage.Save()
}
}
func (d *LazyI64StrDict) _NewContext(pid uint32, branchKey int64) *LazyI64StrContext {
ctx := new(LazyI64StrContext)
ctx.pid = pid
ctx.branchKey = branchKey
ctx.getValueByKey = make(map[int64]string)
ctx.isChanged = false
return ctx
}
func (self *LazyI64StrDict) _ReadContext(pid uint32, branchKey int64) *LazyI64StrContext {
ctx := self._NewContext(pid, branchKey)
data, _ := self.internalPager.ReadPayloadData(pid)
rd := NewDataStreamFromBuffer(data)
rowsCount := int(rd.ReadUInt24())
for i:=0; i<rowsCount; i++ {
key := int64(rd.ReadUInt64())
valChunk := rd.ReadChunk()
ctx.getValueByKey[key] = string(valChunk)
}
return ctx
}
func (self *LazyI64StrDict) _GetContextByBranchKey(branchKey int64) *LazyI64StrContext {
ctx, ok := self.contextByBranchKey[branchKey]
if !ok {
page := self.keyFactory.GetPage(branchKey)
if page != nil {
_ctxPageId, ok := page.Get(branchKey)
if ok {
ctxPageId := uint32(_ctxPageId)
ctx = self._ReadContext(ctxPageId, branchKey)
self.contextByBranchKey[branchKey] = ctx
return ctx
}
}
}
return ctx
}