-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
341 lines (275 loc) · 7.66 KB
/
list.go
File metadata and controls
341 lines (275 loc) · 7.66 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package mind
import (
"errors"
"fmt"
"strings"
"sync"
)
// List is a fast in-memory store, which is extended by Indices for fast finding Items.
//
// WARNING: If T is a pointer type, modifying the items returned by Get() or Query()
// will corrupt the database indexes. Always use Update() to modify data.
type List[T any, ID comparable] struct {
list FreeList[T]
indexMap indexMap[T, ID]
lock sync.RWMutex
}
// NewList create a new List
func NewList[T any]() *List[T, struct{}] {
return &List[T, struct{}]{
list: NewFreeList[T](),
indexMap: newIndexMap[T, struct{}](nil),
}
}
// NewList create a new List with an ID-Index
func NewListWithID[T any, ID comparable](fieldIDGetFn func(*T) ID) *List[T, ID] {
return &List[T, ID]{
list: NewFreeList[T](),
indexMap: newIndexMap(newIDMapIndex(fieldIDGetFn)),
}
}
// CreateIndex create a new Index:
// - fieldName: a name for a field of the saved Item
// - fieldGetFn: a function, which returns the value of an field
// - Index: a impl of the Index interface
//
// Hint: empty field-name or the field-name ID are not allowed!
func (l *List[T, ID]) CreateIndex(fieldName string, index Index[T]) error {
if fieldName == "" {
return fmt.Errorf("empty fieldName is not allowed")
}
if strings.ToLower(fieldName) == IDIndexFieldName {
return fmt.Errorf("ID is a reserved field name")
}
l.lock.Lock()
defer l.lock.Unlock()
if _, exist := l.indexMap.index[fieldName]; exist {
return fmt.Errorf("field-name: %s already exists", fieldName)
}
for idx, item := range l.list.Iter() {
index.Set(item, uint32(idx))
}
l.indexMap.index[fieldName] = index
return nil
}
// RemoveIndex removed a the Index with the given field-name (what the name of the Index is)
// With the field-name: ID you can remove the ID-Index
func (l *List[T, ID]) RemoveIndex(fieldName string) {
if fieldName == "" {
return
}
l.lock.Lock()
defer l.lock.Unlock()
if strings.ToUpper(fieldName) == "ID" {
l.indexMap.idIndex = nil
return
}
delete(l.indexMap.index, fieldName)
}
// InitialBulkInsert can be used for a more performant inserting of initial values.
// The List must be empty!
func (l *List[T, ID]) InitialBulkInsert(values FreeList[T]) error {
if l.list.count > 0 {
return errors.New("can not execute bulk insert for a non empty list")
}
l.lock.Lock()
defer l.lock.Unlock()
// update all indexes
l.indexMap.bulkInsert(values.Iter())
l.list = values
return nil
}
// Insert add the given Item to the list,
// There is NO check, for existing this Item in the list, it will ALWAYS inserting!
func (l *List[T, ID]) Insert(item T) int {
l.lock.Lock()
defer l.lock.Unlock()
idx := l.list.Insert(item)
l.indexMap.insert(&item, idx)
return idx
}
// Update replaces an item and consistently updates all registered indexes.
func (l *List[T, ID]) Update(item T) error {
l.lock.Lock()
defer l.lock.Unlock()
id, idx, err := l.indexMap.getIDByItem(&item)
if err != nil {
return err
}
// overwrite the data in the main list
oldItem, ok := l.list.Set(int(idx), item)
if !ok {
return ValueNotFoundError{id}
}
// update all indexes: re-index
l.indexMap.update(&oldItem, &item, int(idx))
return nil
}
// Remove an item by the given ID.
// This works ONLY, if an ID is defined (with calling: NewListWithID)
// errors:
// - wrong datatype
// - ID not found
// - no ID defined
func (l *List[T, ID]) Remove(id ID) (bool, error) {
l.lock.Lock()
defer l.lock.Unlock()
idx, err := l.indexMap.getListIdxByID(id)
if err != nil {
return false, err
}
return l.removeByIdxNoLock(int(idx)), nil
}
//go:inline
func (l *List[T, ID]) removeByIdxNoLock(index int) (removed bool) {
item, found := l.list.Get(index)
if !found {
return found
}
removed = l.list.Remove(index)
l.indexMap.delete(&item, index)
return removed
}
// Get returns an item by the given ID.
// This works ONLY, if an ID is defined (with calling: NewListWithID)
// errors:
// - wrong datatype
// - ID not found
// - no ID defined
func (l *List[T, ID]) Get(id ID) (T, error) {
l.lock.RLock()
defer l.lock.RUnlock()
idx, err := l.indexMap.getListIdxByID(id)
if err != nil {
var null T
return null, err
}
// not found should be possible
item, _ := l.list.Get(int(idx))
return item, nil
}
// ContainsID check, is this ID found in the list.
func (l *List[T, ID]) Contains(id ID) bool {
l.lock.RLock()
defer l.lock.RUnlock()
_, err := l.indexMap.getListIdxByID(id)
return err == nil
}
// Count the Items, which in this list exist
func (l *List[T, ID]) Count() int {
l.lock.RLock()
defer l.lock.RUnlock()
return l.list.Count()
}
type QueryOption struct {
withOptimizer bool
tracer *Tracer
}
type Opion func(*QueryOption)
func NoOptimizer() Opion { return func(o *QueryOption) { o.withOptimizer = false } }
func WithTracer(t *Tracer) Opion { return func(o *QueryOption) { o.tracer = t } }
// QueryStr execute the given Query-string.
func (l *List[T, ID]) QueryStr(queryStr string, opts ...Opion) *QueryResult[T, ID] {
ast, err := Parse(queryStr)
if err != nil {
var query Query
return &QueryResult[T, ID]{list: l, query: query, err: err}
}
return l.Query(ast, opts...)
}
// Query execute the given Query.
func (l *List[T, ID]) Query(query Expr, opts ...Opion) *QueryResult[T, ID] {
opt := QueryOption{withOptimizer: true}
for _, o := range opts {
o(&opt)
}
if opt.withOptimizer {
query = query.Optimize()
}
return &QueryResult[T, ID]{list: l, query: query.Compile(opt.tracer)}
}
type QueryResult[T any, ID comparable] struct {
list *List[T, ID]
query Query
err error
}
// Count of the Query result
func (qr *QueryResult[T, ID]) Count() (int, error) {
if qr.err != nil {
return 0, qr.err
}
qr.list.lock.RLock()
defer qr.list.lock.RUnlock()
bs, _, err := qr.query(qr.list.indexMap.FilterByName, qr.list.indexMap.allIDs)
if err != nil {
return 0, err
}
return bs.Count(), nil
}
// Values the result values of the Query
func (qr *QueryResult[T, ID]) Values() ([]T, error) {
result, _, err := qr.exec(Paginate{})
return result, err
}
// Paginate the result values of the Query, but in Pagination
func (qr *QueryResult[T, ID]) Paginate(offset, limit uint32) ([]T, PageInfo, error) {
return qr.exec(Paginate{Offset: offset, Limit: limit})
}
type PageInfo struct {
Offset uint32
Limit uint32
Count uint32
Total uint32
}
type Paginate struct {
Offset uint32
Limit uint32
}
// QueryPage executes the query with optional pagination.
// If opts is nil, it returns all matching results.
func (qr *QueryResult[T, ID]) exec(p Paginate) ([]T, PageInfo, error) {
if qr.err != nil {
return nil, PageInfo{}, qr.err
}
qr.list.lock.RLock()
defer qr.list.lock.RUnlock()
rids, _, err := qr.query(qr.list.indexMap.FilterByName, qr.list.indexMap.allIDs)
if err != nil {
return nil, PageInfo{}, err
}
total := uint32(rids.Count())
offset := p.Offset
limit := total // default to "all"
// if limit is provided and not zero, use it; otherwise stay at "total"
if p.Limit > 0 {
limit = p.Limit
}
pi := PageInfo{Offset: offset, Limit: limit, Total: total}
// bound check
if offset >= total {
return []T{}, pi, nil
}
// adjust limit if it exceeds the remaining items
if offset+limit > total {
limit = total - offset
}
pi.Count = limit
startIndex := uint32(0)
if offset > 0 {
idx, found := rids.ValueOnIndex(offset)
if !found {
return []T{}, pi, nil
}
startIndex = idx
}
// the theoretical maximum bit index for the "to" parameter
result := make([]T, 0, limit)
maxBitIndex := uint32(rids.Max())
rids.Range(startIndex, maxBitIndex, func(idx uint32) bool {
item, _ := qr.list.list.Get(int(idx))
result = append(result, item)
// run only until reach the limit
return uint32(len(result)) < limit
})
return result, pi, nil
}