-
Notifications
You must be signed in to change notification settings - Fork 4
/
entry_collection.go
156 lines (130 loc) · 3.26 KB
/
entry_collection.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
package dropbag
import "github.com/insolar/assured-ledger/ledger-core/vanilla/longbits"
type FieldId uint16
type FieldOffset int
const (
InvalidField FieldOffset = 0 - iota
AbsentField
SearchField
)
type FieldOffsetMap interface {
FindField(int FieldId) FieldOffset
}
type StorageLocId uint32
type EntryPosition struct {
StorageLoc StorageLocId
SequenceId uint32
StorageOfs int64
}
func (v EntryPosition) IsZero() bool {
return v.StorageOfs == 0
}
func (v EntryPosition) NotFound() bool {
return v.IsZero()
}
type KeyEntryCollection struct {
entries map[longbits.ByteString]keyEntry
complete bool
}
type keyEntry struct {
EntryPosition
}
func (p *KeyEntryCollection) FindKey(k longbits.ByteString) (bool, EntryPosition) {
return p.complete, p.entries[k].EntryPosition
}
//type CompleteEntryCollection struct {
// storageLoc StorageLocId
// entries []StructEntry
//}
//
//func (p CompleteEntryCollection) GetEntryBySeq(seq uint32) (bool, StructEntry) {
// if seq >= uint32(len(p.entries)) {
// return true, StructEntry{}
// }
// return true, p.entries[seq]
//}
//
//func (p CompleteEntryCollection) GetEntryByPos(pos EntryPosition) (bool, StructEntry) {
// switch {
// case pos.NotFound():
// panic("illegal value")
// case pos.StorageLoc != p.storageLoc:
// panic("illegal value")
// }
// return p.GetEntryBySeq(pos.SequenceId)
//}
//
type LazyEntryCollection struct {
storageLoc StorageLocId
entries []StructEntry
loadInfo []lazyEntry
totalCount uint32
loadedCount uint32
}
type StructEntry struct {
content []byte
fieldMap FieldOffsetMap
}
func (v StructEntry) NotFound() bool {
return v.content == nil && v.fieldMap == nil
}
type lazyEntry struct {
}
func (p *LazyEntryCollection) isComplete() bool {
return p.totalCount == p.loadedCount
}
func (p *LazyEntryCollection) GetEntryBySeq(seq uint32) (bool, StructEntry) {
if seq >= uint32(len(p.entries)) {
return p.isComplete(), StructEntry{}
}
return p.isComplete(), p.entries[seq]
}
func (p *LazyEntryCollection) GetEntryByPos(pos EntryPosition) (bool, StructEntry) {
switch {
case pos.NotFound():
panic("illegal value")
case pos.StorageLoc != p.storageLoc:
panic("illegal value")
}
return p.GetEntryBySeq(pos.SequenceId)
}
func (p *LazyEntryCollection) LoadEntries(posList []EntryPosition, loadFn func(EntryPosition) StructEntry) {
maxSeq := uint32(0)
for _, pos := range posList {
switch {
case pos.NotFound():
panic("illegal value")
case pos.StorageLoc != p.storageLoc:
panic("illegal value")
case pos.SequenceId >= p.totalCount:
panic("illegal value")
case pos.SequenceId > maxSeq:
maxSeq = pos.SequenceId
}
}
if p.isComplete() {
return
}
switch n := uint32(len(p.entries)); {
case n < maxSeq:
case n == maxSeq:
p.entries = append(p.entries, StructEntry{})
p.loadInfo = append(p.loadInfo, lazyEntry{})
default:
n = maxSeq - n + 1
p.entries = append(p.entries, make([]StructEntry, n)...)
p.loadInfo = append(p.loadInfo, make([]lazyEntry, n)...)
}
for _, pos := range posList {
le := &p.entries[pos.SequenceId]
if !le.NotFound() {
continue
}
// TODO le.StructEntry = loadFn(pos)
}
if p.totalCount == p.loadedCount {
p.loadInfo = nil
}
}
func (p LazyEntryCollection) loadEntry(pos EntryPosition, loadFn func(EntryPosition) StructEntry) {
}