-
Notifications
You must be signed in to change notification settings - Fork 23
/
drop_index_command.go
165 lines (142 loc) · 3.88 KB
/
drop_index_command.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
160
161
162
163
164
165
package command
import (
"github.com/squareup/pranadb/cluster"
"strings"
"sync"
"github.com/squareup/pranadb/command/parser"
"github.com/squareup/pranadb/common"
"github.com/squareup/pranadb/errors"
)
type DropIndexCommand struct {
lock sync.Mutex
e *Executor
schemaName string
sql string
tableName string
indexName string
indexInfo *common.IndexInfo
toDeleteBatch *cluster.ToDeleteBatch
}
func (d *DropIndexCommand) CommandType() DDLCommandType {
return DDLCommandTypeDropIndex
}
func (d *DropIndexCommand) SchemaName() string {
return d.schemaName
}
func (d *DropIndexCommand) SQL() string {
return d.sql
}
func (d *DropIndexCommand) TableSequences() []uint64 {
return nil
}
func (d *DropIndexCommand) Cancel() {
}
func NewOriginatingDropIndexCommand(e *Executor, schemaName string, sql string, tableName string, indexName string) *DropIndexCommand {
return &DropIndexCommand{
e: e,
schemaName: schemaName,
sql: sql,
tableName: strings.ToLower(tableName),
indexName: strings.ToLower(indexName),
}
}
func NewDropIndexCommand(e *Executor, schemaName string, sql string) *DropIndexCommand {
return &DropIndexCommand{
e: e,
schemaName: schemaName,
sql: sql,
}
}
func (d *DropIndexCommand) Before() error {
d.lock.Lock()
defer d.lock.Unlock()
indexInfo, err := d.getIndexInfo()
if err != nil {
return errors.WithStack(err)
}
d.indexInfo = indexInfo
return nil
}
func (d *DropIndexCommand) OnPhase(phase int32) error {
switch phase {
case 0:
return d.onPhase0()
case 1:
return d.onPhase1()
default:
panic("invalid phase")
}
}
func (d *DropIndexCommand) NumPhases() int {
return 2
}
func (d *DropIndexCommand) onPhase0() error {
d.lock.Lock()
defer d.lock.Unlock()
if d.indexInfo == nil {
indexInfo, err := d.getIndexInfo()
if err != nil {
return errors.WithStack(err)
}
d.indexInfo = indexInfo
}
if err := d.e.metaController.UnregisterIndex(d.schemaName, d.indexInfo.TableName, d.indexInfo.Name); err != nil {
return errors.WithStack(err)
}
return nil
}
func (d *DropIndexCommand) onPhase1() error {
d.lock.Lock()
defer d.lock.Unlock()
// This disconnects and removes the data for the index (data removal only happens on the originating node)
return d.e.pushEngine.RemoveIndex(d.indexInfo)
}
func (d *DropIndexCommand) AfterPhase(phase int32) error {
d.lock.Lock()
defer d.lock.Unlock()
switch phase {
case 0:
// We record prefixes in the to_delete table - this makes sure index data is deleted on restart if failure occurs
// after this
var err error
d.toDeleteBatch, err = storeToDeleteBatch(d.indexInfo.ID, d.e.cluster)
if err != nil {
return err
}
// Delete the index info from the indexes table - we must do this before we start deleting the actual index data
// otherwise we can end up with a partial index if failure occurs
if err := d.e.metaController.DeleteIndex(d.indexInfo.ID); err != nil {
return err
}
case 1:
// Now delete rows from the to_delete table
return d.e.cluster.RemoveToDeleteBatch(d.toDeleteBatch)
}
return nil
}
func (d *DropIndexCommand) Cleanup() {
}
func (d *DropIndexCommand) getIndexInfo() (*common.IndexInfo, error) {
if d.indexName == "" {
ast, err := parser.Parse(d.sql)
if err != nil {
return nil, errors.WithStack(err)
}
if ast.Drop == nil && !ast.Drop.Index {
return nil, errors.Errorf("not a drop index command %s", d.sql)
}
d.indexName = strings.ToLower(ast.Drop.Name)
d.tableName = strings.ToLower(ast.Drop.TableName)
}
if d.tableName == "" {
return nil, errors.NewInvalidStatementError("Drop index requires a table")
}
indexInfo, ok := d.e.metaController.GetIndex(d.schemaName, d.tableName, d.indexName)
if !ok {
return nil, errors.NewUnknownIndexError(d.schemaName, d.tableName, d.indexName)
}
return indexInfo, nil
}
func (d *DropIndexCommand) GetExtraData() []byte {
return nil
}