forked from Mintegral-official/juno
/
inverted_index_impl.go
142 lines (129 loc) · 3 KB
/
inverted_index_impl.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
package index
import (
"github.com/MintegralTech/juno/datastruct"
"github.com/MintegralTech/juno/debug"
"github.com/MintegralTech/juno/document"
"github.com/MintegralTech/juno/helpers"
"strconv"
"strings"
"sync"
)
type InvertedIndexer struct {
data sync.Map
aDebug *debug.Debug
}
func NewInvertedIndexer() *InvertedIndexer {
return &InvertedIndexer{
data: sync.Map{},
}
}
func (i *InvertedIndexer) Count() (count int) {
count = 0
i.data.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
func (i *InvertedIndexer) Range(f func(key, value interface{}) bool) {
i.data.Range(f)
}
func (i *InvertedIndexer) GetValueById(id document.DocId) map[string][]string {
var res = make(map[string][]string, 16)
i.data.Range(func(key, value interface{}) bool {
v, ok := value.(*datastruct.SkipList)
if !ok {
return true
}
e := v.Iterator().GetGE(id)
if e == nil {
return true
}
if e.Key() != id {
return true
}
keys := strings.Split(key.(string), SEP)
if len(keys) == 2 {
res[keys[0]] = append(res[keys[0]], keys[1])
}
return true
})
return res
}
func (i *InvertedIndexer) Add(fieldName string, id document.DocId) (err error) {
v, ok := i.data.Load(fieldName)
if !ok {
sl := datastruct.NewSkipList(datastruct.DefaultMaxLevel)
sl.Add(id, nil)
i.data.Store(fieldName, sl)
return err
}
sl, ok := v.(*datastruct.SkipList)
if !ok {
err = helpers.ParseError
return err
}
sl.Add(id, nil)
return err
}
func (i *InvertedIndexer) Del(fieldName string, id document.DocId) (ok bool) {
v, ok := i.data.Load(fieldName)
if !ok {
return ok
}
if sl, ok := v.(*datastruct.SkipList); ok {
sl.Del(id)
i.data.Store(fieldName, sl)
return ok
}
return ok
}
func (i *InvertedIndexer) Update(fieldName string, ids []document.DocId) {
v, ok := i.data.Load(fieldName)
if !ok {
sl := datastruct.NewSkipList(datastruct.DefaultMaxLevel)
for _, id := range ids {
sl.Add(id, nil)
}
i.data.Store(fieldName, sl)
return
}
if sl, ok := v.(*datastruct.SkipList); ok {
sl = datastruct.NewSkipList(datastruct.DefaultMaxLevel)
for _, id := range ids {
sl.Add(id, nil)
}
i.data.Store(fieldName, sl)
}
}
func (i *InvertedIndexer) Delete(fieldName string) {
i.data.Delete(fieldName)
}
func (i *InvertedIndexer) Iterator(name, value string) datastruct.Iterator {
var fieldName = name + SEP + value
if v, ok := i.data.Load(fieldName); ok {
sl, ok := v.(*datastruct.SkipList)
if ok {
if i.aDebug != nil {
i.aDebug.AddDebugMsg("index[" + fieldName + "] len: " + strconv.Itoa(sl.Len()))
}
iter := sl.Iterator()
iter.FieldName = fieldName
return iter
}
}
if i.aDebug != nil {
i.aDebug.AddDebugMsg("index: " + fieldName + " is nil")
}
sl := datastruct.NewSkipList(datastruct.DefaultMaxLevel).Iterator()
sl.FieldName = fieldName
return sl
}
func (i *InvertedIndexer) DebugInfo() *debug.Debug {
return i.aDebug
}
func (i *InvertedIndexer) SetDebug(level int) {
if i.aDebug == nil {
i.aDebug = debug.NewDebug(level, "invert index")
}
}