-
Notifications
You must be signed in to change notification settings - Fork 24
/
index_exec.go
54 lines (49 loc) · 1.53 KB
/
index_exec.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
package exec
import (
"github.com/squareup/pranadb/cluster"
"github.com/squareup/pranadb/common"
"github.com/squareup/pranadb/errors"
"github.com/squareup/pranadb/table"
)
// IndexExecutor maintains a secondary index
type IndexExecutor struct {
pushExecutorBase
IndexInfo *common.IndexInfo
TableInfo *common.TableInfo // The table info of the table (source or MV) that we are creating the index on
store cluster.Cluster
}
func NewIndexExecutor(tableInfo *common.TableInfo, indexInfo *common.IndexInfo, store cluster.Cluster) *IndexExecutor {
return &IndexExecutor{
pushExecutorBase: pushExecutorBase{
rowsFactory: common.NewRowsFactory(tableInfo.ColumnTypes),
},
TableInfo: tableInfo,
IndexInfo: indexInfo,
store: store,
}
}
func (t *IndexExecutor) ReCalcSchemaFromChildren() error {
return nil
}
func (t *IndexExecutor) HandleRows(rowsBatch RowsBatch, ctx *ExecutionContext) error {
numEntries := rowsBatch.Len()
for i := 0; i < numEntries; i++ {
prevRow := rowsBatch.PreviousRow(i)
currentRow := rowsBatch.CurrentRow(i)
if currentRow != nil {
keyBuff, valueBuff, err := table.EncodeIndexKeyValue(t.TableInfo, t.IndexInfo, ctx.WriteBatch.ShardID, currentRow)
if err != nil {
return errors.WithStack(err)
}
ctx.WriteBatch.AddPut(keyBuff, valueBuff)
} else {
// It's a delete
keyBuff, _, err := table.EncodeIndexKeyValue(t.TableInfo, t.IndexInfo, ctx.WriteBatch.ShardID, prevRow)
if err != nil {
return errors.WithStack(err)
}
ctx.WriteBatch.AddDelete(keyBuff)
}
}
return nil
}