forked from c4pt0r/kvql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_plan.go
77 lines (67 loc) · 1.35 KB
/
delete_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
package kvql
import "fmt"
type DeletePlan struct {
Txn Txn
ChildPlan Plan
executed bool
}
func (p *DeletePlan) Init() error {
p.executed = false
return p.ChildPlan.Init()
}
func (p *DeletePlan) String() string {
return fmt.Sprintf("DeletePlan{}")
}
func (p *DeletePlan) Explain() []string {
ret := []string{p.String()}
for _, plan := range p.ChildPlan.Explain() {
ret = append(ret, plan)
}
return ret
}
func (p *DeletePlan) FieldNameList() []string {
return []string{"Rows"}
}
func (p *DeletePlan) FieldTypeList() []Type {
return []Type{TNUMBER}
}
func (p *DeletePlan) 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 *DeletePlan) 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 *DeletePlan) execute(ctx *ExecuteCtx) (int, error) {
count := 0
for {
ctx.Clear()
rows, err := p.ChildPlan.Batch(ctx)
if err != nil {
return count, err
}
nrows := len(rows)
if nrows == 0 {
return count, nil
}
keys := make([][]byte, nrows)
for i, kv := range rows {
keys[i] = kv.Key
}
err = p.Txn.BatchDelete(keys)
if err != nil {
return count, err
}
count += nrows
}
}