forked from c4pt0r/kvql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_plan.go
94 lines (82 loc) · 1.71 KB
/
remove_plan.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
package kvql
import (
"fmt"
"strings"
)
type RemovePlan struct {
Txn Txn
Keys []Expression
executed bool
}
func (p *RemovePlan) Init() error {
p.executed = false
return nil
}
func (p *RemovePlan) Explain() []string {
return []string{p.String()}
}
func (p *RemovePlan) String() string {
keys := make([]string, len(p.Keys))
for i, k := range p.Keys {
keys[i] = k.String()
}
return fmt.Sprintf("RemovePlan{Keys = [%s]}", strings.Join(keys, ", "))
}
func (p *RemovePlan) Next(ctx *ExecuteCtx) ([]Column, error) {
if !p.executed {
n, err := p.execute(ctx)
p.executed = true
return []Column{n}, err
}
return nil, nil
}
func (p *RemovePlan) Batch(ctx *ExecuteCtx) ([][]Column, error) {
if !p.executed {
n, err := p.execute(ctx)
p.executed = true
row := []Column{n}
return [][]Column{row}, err
}
return nil, nil
}
func (p *RemovePlan) FieldNameList() []string {
return []string{"Rows"}
}
func (p *RemovePlan) FieldTypeList() []Type {
return []Type{TNUMBER}
}
func (p *RemovePlan) processKey(ekvp KVPair, ctx *ExecuteCtx, kexpr Expression) ([]byte, error) {
rkey, err := kexpr.Execute(ekvp, ctx)
if err != nil {
return nil, err
}
key := []byte(toString(rkey))
return key, nil
}
func (p *RemovePlan) execute(ctx *ExecuteCtx) (int, error) {
nks := len(p.Keys)
keys := make([][]byte, nks)
ekvp := NewKVPStr("", "")
for i, kexpr := range p.Keys {
key, err := p.processKey(ekvp, ctx, kexpr)
if err != nil {
return 0, err
}
keys[i] = key
}
if nks == 0 {
return 0, nil
} else if nks == 1 {
err := p.Txn.Delete(keys[0])
if err != nil {
return 0, err
}
return 1, nil
} else {
err := p.Txn.BatchDelete(keys)
if err != nil {
return 0, err
}
return nks, nil
}
}