Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit fa02b97

Browse files
authored
Merge pull request #586 from erizocosmico/fix/lookup-names
sql/index/pilosa: return all index ids in lookups
2 parents 1bdf44b + 71a114a commit fa02b97

File tree

3 files changed

+130
-8
lines changed

3 files changed

+130
-8
lines changed

sql/index/pilosa/index.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func (idx *pilosaIndex) Get(keys ...interface{}) (sql.IndexLookup, error) {
108108
mapping: idx.mapping,
109109
keys: keys,
110110
expressions: idx.expressions,
111+
indexes: map[string]struct{}{
112+
idx.ID(): struct{}{},
113+
},
111114
}, nil
112115
}
113116

@@ -165,6 +168,9 @@ func (idx *pilosaIndex) AscendGreaterOrEqual(keys ...interface{}) (sql.IndexLook
165168
mapping: idx.mapping,
166169
keys: keys,
167170
expressions: idx.expressions,
171+
indexes: map[string]struct{}{
172+
idx.ID(): struct{}{},
173+
},
168174
}, keys, nil), nil
169175
}
170176

@@ -179,6 +185,9 @@ func (idx *pilosaIndex) AscendLessThan(keys ...interface{}) (sql.IndexLookup, er
179185
mapping: idx.mapping,
180186
keys: keys,
181187
expressions: idx.expressions,
188+
indexes: map[string]struct{}{
189+
idx.ID(): struct{}{},
190+
},
182191
}, nil, keys), nil
183192
}
184193

@@ -196,6 +205,9 @@ func (idx *pilosaIndex) AscendRange(greaterOrEqual, lessThan []interface{}) (sql
196205
index: idx.index,
197206
mapping: idx.mapping,
198207
expressions: idx.expressions,
208+
indexes: map[string]struct{}{
209+
idx.ID(): struct{}{},
210+
},
199211
}, greaterOrEqual, lessThan), nil
200212
}
201213

@@ -211,6 +223,9 @@ func (idx *pilosaIndex) DescendGreater(keys ...interface{}) (sql.IndexLookup, er
211223
keys: keys,
212224
expressions: idx.expressions,
213225
reverse: true,
226+
indexes: map[string]struct{}{
227+
idx.ID(): struct{}{},
228+
},
214229
}, keys, nil), nil
215230
}
216231

@@ -226,6 +241,9 @@ func (idx *pilosaIndex) DescendLessOrEqual(keys ...interface{}) (sql.IndexLookup
226241
keys: keys,
227242
expressions: idx.expressions,
228243
reverse: true,
244+
indexes: map[string]struct{}{
245+
idx.ID(): struct{}{},
246+
},
229247
}, nil, keys), nil
230248
}
231249

@@ -244,6 +262,9 @@ func (idx *pilosaIndex) DescendRange(lessOrEqual, greaterThan []interface{}) (sq
244262
mapping: idx.mapping,
245263
expressions: idx.expressions,
246264
reverse: true,
265+
indexes: map[string]struct{}{
266+
idx.ID(): struct{}{},
267+
},
247268
}, greaterThan, lessOrEqual), nil
248269
}
249270

@@ -258,6 +279,9 @@ func (idx *pilosaIndex) Not(keys ...interface{}) (sql.IndexLookup, error) {
258279
mapping: idx.mapping,
259280
keys: keys,
260281
expressions: idx.expressions,
282+
indexes: map[string]struct{}{
283+
idx.ID(): struct{}{},
284+
},
261285
}, nil
262286
}
263287

sql/index/pilosa/lookup.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/gob"
66
"io"
7+
"sort"
78
"strings"
89
"time"
910

@@ -63,6 +64,7 @@ type (
6364
keys []interface{}
6465
expressions []string
6566
operations []*lookupOperation
67+
indexes map[string]struct{}
6668
}
6769

6870
lookupOperation struct {
@@ -165,7 +167,7 @@ func (l *indexLookup) Values(p sql.Partition) (sql.IndexValueIter, error) {
165167
}
166168

167169
func (l *indexLookup) Indexes() []string {
168-
return []string{l.id}
170+
return sortedIndexes(l.indexes)
169171
}
170172

171173
// IsMergeable implements sql.Mergeable interface.
@@ -181,6 +183,9 @@ func (l *indexLookup) IsMergeable(lookup sql.IndexLookup) bool {
181183
func (l *indexLookup) Intersection(lookups ...sql.IndexLookup) sql.IndexLookup {
182184
lookup := *l
183185
for _, li := range lookups {
186+
for _, idx := range li.Indexes() {
187+
lookup.indexes[idx] = struct{}{}
188+
}
184189
lookup.operations = append(lookup.operations, &lookupOperation{li, intersect})
185190
}
186191

@@ -191,6 +196,9 @@ func (l *indexLookup) Intersection(lookups ...sql.IndexLookup) sql.IndexLookup {
191196
func (l *indexLookup) Union(lookups ...sql.IndexLookup) sql.IndexLookup {
192197
lookup := *l
193198
for _, li := range lookups {
199+
for _, idx := range li.Indexes() {
200+
lookup.indexes[idx] = struct{}{}
201+
}
194202
lookup.operations = append(lookup.operations, &lookupOperation{li, union})
195203
}
196204

@@ -201,6 +209,9 @@ func (l *indexLookup) Union(lookups ...sql.IndexLookup) sql.IndexLookup {
201209
func (l *indexLookup) Difference(lookups ...sql.IndexLookup) sql.IndexLookup {
202210
lookup := *l
203211
for _, li := range lookups {
212+
for _, idx := range li.Indexes() {
213+
lookup.indexes[idx] = struct{}{}
214+
}
204215
lookup.operations = append(lookup.operations, &lookupOperation{li, difference})
205216
}
206217

@@ -214,6 +225,7 @@ type filteredLookup struct {
214225
keys []interface{}
215226
expressions []string
216227
operations []*lookupOperation
228+
indexes map[string]struct{}
217229

218230
reverse bool
219231
filter func(int, []byte) (bool, error)
@@ -320,7 +332,7 @@ func (l *filteredLookup) Values(p sql.Partition) (sql.IndexValueIter, error) {
320332
}
321333

322334
func (l *filteredLookup) Indexes() []string {
323-
return []string{l.id}
335+
return sortedIndexes(l.indexes)
324336
}
325337

326338
// IsMergeable implements sql.Mergeable interface.
@@ -335,6 +347,9 @@ func (l *filteredLookup) IsMergeable(lookup sql.IndexLookup) bool {
335347
func (l *filteredLookup) Intersection(lookups ...sql.IndexLookup) sql.IndexLookup {
336348
lookup := *l
337349
for _, li := range lookups {
350+
for _, idx := range li.Indexes() {
351+
lookup.indexes[idx] = struct{}{}
352+
}
338353
lookup.operations = append(lookup.operations, &lookupOperation{li, intersect})
339354
}
340355

@@ -345,6 +360,9 @@ func (l *filteredLookup) Intersection(lookups ...sql.IndexLookup) sql.IndexLooku
345360
func (l *filteredLookup) Union(lookups ...sql.IndexLookup) sql.IndexLookup {
346361
lookup := *l
347362
for _, li := range lookups {
363+
for _, idx := range li.Indexes() {
364+
lookup.indexes[idx] = struct{}{}
365+
}
348366
lookup.operations = append(lookup.operations, &lookupOperation{li, union})
349367
}
350368

@@ -355,6 +373,9 @@ func (l *filteredLookup) Union(lookups ...sql.IndexLookup) sql.IndexLookup {
355373
func (l *filteredLookup) Difference(lookups ...sql.IndexLookup) sql.IndexLookup {
356374
lookup := *l
357375
for _, li := range lookups {
376+
for _, idx := range li.Indexes() {
377+
lookup.indexes[idx] = struct{}{}
378+
}
358379
lookup.operations = append(lookup.operations, &lookupOperation{li, difference})
359380
}
360381

@@ -379,6 +400,7 @@ type negateLookup struct {
379400
mapping *mapping
380401
keys []interface{}
381402
expressions []string
403+
indexes map[string]struct{}
382404
operations []*lookupOperation
383405
}
384406

@@ -491,7 +513,7 @@ func (l *negateLookup) Values(p sql.Partition) (sql.IndexValueIter, error) {
491513
}
492514

493515
func (l *negateLookup) Indexes() []string {
494-
return []string{l.id}
516+
return sortedIndexes(l.indexes)
495517
}
496518

497519
// IsMergeable implements sql.Mergeable interface.
@@ -507,6 +529,9 @@ func (l *negateLookup) IsMergeable(lookup sql.IndexLookup) bool {
507529
func (l *negateLookup) Intersection(lookups ...sql.IndexLookup) sql.IndexLookup {
508530
lookup := *l
509531
for _, li := range lookups {
532+
for _, idx := range li.Indexes() {
533+
lookup.indexes[idx] = struct{}{}
534+
}
510535
lookup.operations = append(lookup.operations, &lookupOperation{li, intersect})
511536
}
512537

@@ -517,6 +542,9 @@ func (l *negateLookup) Intersection(lookups ...sql.IndexLookup) sql.IndexLookup
517542
func (l *negateLookup) Union(lookups ...sql.IndexLookup) sql.IndexLookup {
518543
lookup := *l
519544
for _, li := range lookups {
545+
for _, idx := range li.Indexes() {
546+
lookup.indexes[idx] = struct{}{}
547+
}
520548
lookup.operations = append(lookup.operations, &lookupOperation{li, union})
521549
}
522550

@@ -527,6 +555,9 @@ func (l *negateLookup) Union(lookups ...sql.IndexLookup) sql.IndexLookup {
527555
func (l *negateLookup) Difference(lookups ...sql.IndexLookup) sql.IndexLookup {
528556
lookup := *l
529557
for _, li := range lookups {
558+
for _, idx := range li.Indexes() {
559+
lookup.indexes[idx] = struct{}{}
560+
}
530561
lookup.operations = append(lookup.operations, &lookupOperation{li, difference})
531562
}
532563

@@ -596,7 +627,7 @@ func compare(a, b interface{}) (int, error) {
596627
return 0, nil
597628
}
598629

599-
if !a {
630+
if a == false {
600631
return -1, nil
601632
}
602633

@@ -734,3 +765,13 @@ func compare(a, b interface{}) (int, error) {
734765
return 0, errUnknownType.New(a)
735766
}
736767
}
768+
769+
func sortedIndexes(indexes map[string]struct{}) []string {
770+
var result = make([]string, 0, len(indexes))
771+
for idx := range indexes {
772+
result = append(result, idx)
773+
}
774+
775+
sort.Strings(result)
776+
return result
777+
}

sql/index/pilosa/lookup_test.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,20 @@ func TestMergeable(t *testing.T) {
204204

205205
func TestIndexes(t *testing.T) {
206206
testCases := []sql.IndexLookup{
207-
&indexLookup{id: "foo"},
208-
&negateLookup{id: "foo"},
209-
&ascendLookup{filteredLookup: &filteredLookup{id: "foo"}},
210-
&descendLookup{filteredLookup: &filteredLookup{id: "foo"}},
207+
&indexLookup{id: "foo", indexes: map[string]struct{}{"foo": struct{}{}}},
208+
&negateLookup{id: "foo", indexes: map[string]struct{}{"foo": struct{}{}}},
209+
&ascendLookup{
210+
filteredLookup: &filteredLookup{
211+
id: "foo",
212+
indexes: map[string]struct{}{"foo": struct{}{}},
213+
},
214+
},
215+
&descendLookup{
216+
filteredLookup: &filteredLookup{
217+
id: "foo",
218+
indexes: map[string]struct{}{"foo": struct{}{}},
219+
},
220+
},
211221
}
212222

213223
for _, tt := range testCases {
@@ -216,3 +226,50 @@ func TestIndexes(t *testing.T) {
216226
})
217227
}
218228
}
229+
230+
func TestLookupIndexes(t *testing.T) {
231+
require := require.New(t)
232+
233+
lookups := []sql.IndexLookup{
234+
&indexLookup{
235+
id: "1",
236+
indexes: map[string]struct{}{"1": struct{}{}},
237+
},
238+
&negateLookup{
239+
id: "2",
240+
indexes: map[string]struct{}{"2": struct{}{}},
241+
},
242+
&ascendLookup{filteredLookup: &filteredLookup{
243+
id: "3",
244+
indexes: map[string]struct{}{"3": struct{}{}},
245+
}},
246+
&descendLookup{filteredLookup: &filteredLookup{
247+
id: "4",
248+
indexes: map[string]struct{}{"4": struct{}{}},
249+
}},
250+
&filteredLookup{
251+
id: "5",
252+
indexes: map[string]struct{}{"5": struct{}{}},
253+
},
254+
}
255+
256+
expected := []string{"1", "2", "3", "4", "5"}
257+
258+
// All possible permutations of operations between all the different kinds
259+
// of lookups are tested.
260+
for i := 0; i < len(lookups); i++ {
261+
var op sql.SetOperations
262+
var others []sql.IndexLookup
263+
for j := 0; j < len(lookups); j++ {
264+
if i == j {
265+
op = lookups[i].(sql.SetOperations)
266+
} else {
267+
others = append(others, lookups[j])
268+
}
269+
}
270+
271+
require.Equal(expected, op.Union(others...).Indexes())
272+
require.Equal(expected, op.Difference(others...).Indexes())
273+
require.Equal(expected, op.Intersection(others...).Indexes())
274+
}
275+
}

0 commit comments

Comments
 (0)