-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazydict_i64i64set.go
228 lines (166 loc) · 4.66 KB
/
lazydict_i64i64set.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
package gokvdb
import (
"os"
"fmt"
)
type LazyI64I64SetDict struct {
storage *Storage
dbName string
ixName string
internalPager IPager
treeFactory *BranchI64BTreeFactory
ctxByKey map[int64]*LazyI64I64SetContext
}
type LazyI64I64SetContext struct {
pid uint32
key int64
set *LazyI64Set
isChanged bool
}
func (self *LazyI64I64SetDict) ToString() string {
return fmt.Sprintf("<LazyI64I64SetDict db=%v name=%v %v>", self.dbName, self.ixName, self.treeFactory.ToString())
}
func (self *LazyI64I64SetContext) ToString() string {
return fmt.Sprintf("<LazyI64I64SetContext key=%v pid=%v>", self.key, self.pid)
}
func (self *LazyI64I64SetDict) ReleaseCache() {
var keys []int64
for key, ctx := range self.ctxByKey {
if !ctx.isChanged {
keys = append(keys, key)
}
}
for _, key := range keys {
ctx, _ := self.ctxByKey[key]
fmt.Println("ReleaseCache", ctx.ToString())
delete(self.ctxByKey, key)
ctx = nil
}
}
func (self *LazyI64I64SetDict) Save(commit bool) {
for _, ctx := range self.ctxByKey {
if ctx.isChanged {
ctx.isChanged = false
ctxData := ctx.set.Save()
self.internalPager.WritePayloadData(ctx.pid, ctxData)
//fmt.Println("LazyI64I64SetDict SAVE ctx", ctx.ToString(), "len", len(ctxData))
}
}
db, err := self.storage.DB(self.dbName)
if err != nil {
fmt.Println("error dbName", self.dbName)
os.Exit(1)
}
treeFactoryMeta := self.treeFactory.Save()
internalPagerMeta := self.internalPager.Save()
metaW := NewDataStream()
metaW.WriteChunk(internalPagerMeta)
metaW.WriteChunk(treeFactoryMeta)
metaBytes := metaW.ToBytes()
db.SetMeta(self.ixName, metaBytes)
if commit {
self.storage.Save()
}
}
func NewLazyI64I64SetDict(storage *Storage, dbName string, ixName string) *LazyI64I64SetDict {
self := new(LazyI64I64SetDict)
self.storage = storage
self.dbName = dbName
self.ixName = ixName
self.ctxByKey = make(map[int64]*LazyI64I64SetContext)
db, err := storage.DB(dbName)
if err != nil {
fmt.Println("error dbName", dbName)
os.Exit(1)
}
metaData, ok := db.GetMeta(ixName)
var internalPagerMeta []byte
var treeFactoryMeta []byte
if ok {
rd := NewDataStreamFromBuffer(metaData)
internalPagerMeta = rd.ReadChunk()
treeFactoryMeta = rd.ReadChunk()
}
internalPageSize := uint16(128)
internalPager := NewInternalPager(storage.pager, internalPageSize, internalPagerMeta)
self.internalPager = internalPager
self.treeFactory = NewBranchI64BTreeFactory(internalPager, treeFactoryMeta, 3)
return self
}
func (self *LazyI64I64SetDict) NewContext(pid uint32, key int64, data []byte) *LazyI64I64SetContext {
ctx := new(LazyI64I64SetContext)
ctx.pid = pid
ctx.key = key
ctx.set = NewLazyI64Set(self.internalPager, data)
ctx.isChanged = false
return ctx
}
type LazyI64I64SetItem struct {
key int64
ctxPageId uint32
i64i64set *LazyI64I64SetDict
}
func (self *LazyI64I64SetItem) Key() int64 {
return self.key
}
func (self *LazyI64I64SetItem) Values() chan int64 {
ctx, ok := self.i64i64set.ctxByKey[self.key]
if !ok {
ctx = self.i64i64set.LoadContext(self.ctxPageId, self.key)
}
return ctx.set.Values()
}
func (self *LazyI64I64SetDict) LoadContext(ctxPageId uint32, key int64) *LazyI64I64SetContext {
setData, _ := self.internalPager.ReadPayloadData(ctxPageId)
ctx := self.NewContext(ctxPageId, key, setData)
return ctx
}
func (self *LazyI64I64SetDict) Items() chan LazyI64I64SetItem {
q := make(chan LazyI64I64SetItem)
go func(ch chan LazyI64I64SetItem) {
for _item := range self.treeFactory.Items() {
key := _item.Key()
ctxPageId := uint32(_item.Value())
item := LazyI64I64SetItem{key: key, ctxPageId:ctxPageId, i64i64set:self}
ch <- item
}
close(ch)
}(q)
return q
}
func (self *LazyI64I64SetDict) Get(key int64) (LazyI64I64SetItem, bool) {
page := self.treeFactory.GetPage(key)
if page != nil {
_ctxPageId, ok := page.Get(key)
if ok {
ctxPageId := uint32(_ctxPageId)
item := LazyI64I64SetItem{key: key, ctxPageId:ctxPageId, i64i64set:self}
return item, true
}
}
return LazyI64I64SetItem{}, false
}
func (self *LazyI64I64SetDict) Add(key int64, value int64) {
var ctx *LazyI64I64SetContext
var ok bool
ctx, ok = self.ctxByKey[key]
//fmt.Println("LazyI64I64Set Add", "key=", key, ctx, ok)
if !ok {
page := self.treeFactory.GetOrCreatePage(key)
var ctxPageId uint32
_ctxPageId, ok := page.Get(key)
if ok {
ctxPageId = uint32(_ctxPageId)
ctx = self.LoadContext(ctxPageId, key)
} else {
ctxPageId = self.internalPager.CreatePageId()
page.Set(key, int64(ctxPageId))
ctx = self.NewContext(ctxPageId, key, nil)
ctx.isChanged = true
//ctx.isChanged = true
}
self.ctxByKey[key] = ctx
}
ctx.set.Add(value)
ctx.isChanged = true
}