Skip to content

Commit f22e4ff

Browse files
committed
sql: implement index interfaces
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
1 parent b4c3bf9 commit f22e4ff

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

sql/core.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ type Table interface {
101101
Node
102102
}
103103

104+
// Indexable represents a table that supports being indexed and receiving
105+
// indexes to be able to speed up its execution.
106+
type Indexable interface {
107+
// IndexKeyValueIter returns an iterator with the values of each row in
108+
// the table for the given column names.
109+
IndexKeyValueIter(colNames []string) (IndexKeyValueIter, error)
110+
// WithProjectFiltersAndIndex is meant to be called instead of RowIter
111+
// method of the table. Returns a new iterator given the columns,
112+
// filters and the index so the table can improve its speed instead of
113+
// making a full scan.
114+
WithProjectFiltersAndIndex(columns, filters []Expression, index IndexValueIter) (RowIter, error)
115+
}
116+
104117
// PushdownProjectionTable is a table that can produce a specific RowIter
105118
// that's more optimized given the columns that are projected.
106119
type PushdownProjectionTable interface {

sql/index.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package sql
2+
3+
import "io"
4+
5+
// IndexKeyValueIter is an iterator of index key values, that is, a tuple of
6+
// the values that will be index keys.
7+
type IndexKeyValueIter interface {
8+
// Next returns the next tuple of index key values. The length of the
9+
// returned slice will be the same as the number of columns used to
10+
// create this iterator.
11+
Next() ([]interface{}, error)
12+
io.Closer
13+
}
14+
15+
// IndexValueIter is an iterator of index values.
16+
type IndexValueIter interface {
17+
// Next returns the next index value.
18+
Next() (interface{}, error)
19+
io.Closer
20+
}
21+
22+
// Index is the basic representation of an index. It can be extended with
23+
// more functionality by implementing more specific interfaces.
24+
type Index interface {
25+
// Get returns an IndexLookup for the given key in the index.
26+
Get(key interface{}) (IndexLookup, error)
27+
// Has checks if the given key is present in the index.
28+
Has(key interface{}) (bool, error)
29+
// ID returns the identifier of the index.
30+
ID() string
31+
// Expression returns the indexed expression.
32+
Expression() Expression
33+
}
34+
35+
// AscendIndex is an index that is sorted in ascending order.
36+
type AscendIndex interface {
37+
// AscendGreaterOrEqual returns an IndexLookup for keys that are greater
38+
// or equal to the given key.
39+
AscendGreaterOrEqual(key interface{}) (IndexLookup, error)
40+
// AscendLessThan returns an IndexLookup for keys that are less than the
41+
// given key.
42+
AscendLessThan(key interface{}) (IndexLookup, error)
43+
// AscendRange returns an IndexLookup for keys that are within the given
44+
// range.
45+
AscendRange(greaterOrEqual, lessThan interface{}) (IndexLookup, error)
46+
}
47+
48+
// DescendIndex is an index that is sorted in descending order.
49+
type DescendIndex interface {
50+
// DescendGreater returns an IndexLookup for keys that are greater
51+
// than the given key.
52+
DescendGreater(key interface{}) (IndexLookup, error)
53+
// DescendLessOrEqual returns an IndexLookup for keys that are less than or
54+
// equal to the given key.
55+
DescendLessOrEqual(key interface{}) (IndexLookup, error)
56+
// DescendRange returns an IndexLookup for keys that are within the given
57+
// range.
58+
DescendRange(lessOrEqual, greaterThan interface{}) (IndexLookup, error)
59+
}
60+
61+
// IndexLookup is a subset of an index. More specific interfaces can be
62+
// implemented to grant more capabilities to the index lookup.
63+
type IndexLookup interface {
64+
// Values returns the values in the subset of the index.
65+
Values() IndexValueIter
66+
}
67+
68+
// SetOperations is a specialization of IndexLookup that enables set operations
69+
// between several IndexLookups.
70+
type SetOperations interface {
71+
// Intersection returns a new index subset with the intersection of the
72+
// current IndexLookup and the ones given.
73+
Intersection(...IndexLookup) IndexLookup
74+
// Union returns a new index subset with the union of the current
75+
// IndexLookup and the ones given.
76+
Union(...IndexLookup) IndexLookup
77+
// Difference returns a new index subset with the difference between the
78+
// current IndexLookup and the ones given.
79+
Difference(...IndexLookup) IndexLookup
80+
}
81+
82+
// Mergeable is a specialization of IndexLookup to check if an IndexLookup can
83+
// be merged with another one.
84+
type Mergeable interface {
85+
// IsMergeable checks whether the current IndexLookup can be merged with
86+
// the given one.
87+
IsMergeable(IndexLookup) bool
88+
}
89+
90+
// IndexLoader is the piece that loads indexes from disk.
91+
type IndexLoader interface {
92+
// ID returns the unique name of the index loader.
93+
ID() string
94+
// Load the index at the given path.
95+
Load(path string) (Index, error)
96+
}
97+
98+
// IndexSaver is the piece that stores indexes in disk.
99+
type IndexSaver interface {
100+
// ID returns the unique name of the index saver.
101+
ID() string
102+
// Save the given index at the given path.
103+
Save(index Index, path string) error
104+
}

0 commit comments

Comments
 (0)