-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
159 lines (137 loc) · 3.16 KB
/
index.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
package main
import "strings"
type UpdateOpts struct {
current *Log
previous *Log
}
type InvertedIndex struct {
keyToEntries map[string][]LogID
entryToKeys map[LogID][]string
}
func getNewIndex() InvertedIndex {
return InvertedIndex{
keyToEntries: map[string][]LogID{},
entryToKeys: map[LogID][]string{},
}
}
func (i *InvertedIndex) update(opts UpdateOpts) {
i.updateEntries(opts.current)
if opts.previous != nil {
i.removeMappings(opts.previous, opts.current)
}
}
func (i *InvertedIndex) removeMappings(prev, current *Log) {
prevWords := getWordsFromData(prev.Data)
currWords := getWordsFromData(current.Data)
keysDelta := getWordsDelta(prevWords, currWords)
i.removeKeysFromEntry(keysDelta, prev.ID)
i.removeEntryFromKeys(keysDelta, prev.ID)
}
func (i *InvertedIndex) updateEntries(log *Log) {
if log == nil {
return
}
words := getWordsFromData(log.Data)
for _, word := range words {
i.updateEntry(word, log.ID)
}
}
func (i *InvertedIndex) updateEntry(key string, id LogID) {
_, found := i.keyToEntries[key]
if !found {
i.keyToEntries[key] = []LogID{}
}
_, found = i.entryToKeys[id]
if !found {
i.entryToKeys[id] = []string{}
}
entryExists := false
for _, entry := range i.keyToEntries[key] {
if entry == id {
entryExists = true
break
}
}
if !entryExists {
i.keyToEntries[key] = append(i.keyToEntries[key], id)
}
keyExists := false
for _, storedKey := range i.entryToKeys[id] {
if storedKey == key {
keyExists = true
break
}
}
if !keyExists {
i.entryToKeys[id] = append(i.entryToKeys[id], key)
}
}
func (i *InvertedIndex) getByKey(key string) []LogID {
entries, found := i.keyToEntries[key]
if !found {
return nil
}
return entries
}
func (i *InvertedIndex) deletedByLogId(id LogID) {
keys, found := i.entryToKeys[id]
if !found {
return
}
delete(i.entryToKeys, id)
i.removeEntryFromKeys(keys, id)
}
func (i *InvertedIndex) removeEntryFromKeys(keys []string, id LogID) {
for _, key := range keys {
if _, found := i.keyToEntries[key]; !found {
continue
}
filteredEntries := []LogID{}
for _, logId := range i.keyToEntries[key] {
if logId != id {
filteredEntries = append(filteredEntries, logId)
}
}
i.keyToEntries[key] = filteredEntries
}
}
func (i *InvertedIndex) removeKeysFromEntry(keysToBeRemoved []string, id LogID) {
storedKeys, found := i.entryToKeys[id]
if !found {
return
}
shouldBeRemoved := map[string]struct{}{}
for _, key := range keysToBeRemoved {
shouldBeRemoved[key] = struct{}{}
}
filteredKeys := []string{}
for _, key := range storedKeys {
if _, yes := shouldBeRemoved[key]; !yes {
filteredKeys = append(filteredKeys, key)
}
}
i.entryToKeys[id] = filteredKeys
}
func getWordsFromData(data string) []string {
tokens := strings.Split(data, " ")
words := []string{}
for _, token := range tokens {
if token != "" {
words = append(words, token)
}
}
return words
}
func getWordsDelta(prev, curr []string) []string {
delta := []string{}
existing := map[string]struct{}{}
for _, word := range curr {
existing[word] = struct{}{}
}
for _, word := range prev {
if _, found := existing[word]; !found {
delta = append(delta, word)
}
}
return delta
}