-
Notifications
You must be signed in to change notification settings - Fork 3
/
put_plan.go
100 lines (88 loc) · 1.89 KB
/
put_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
95
96
97
98
99
100
package kvql
import (
"fmt"
"strings"
)
type PutPlan struct {
Storage Storage
KVPairs []*PutKVPair
executed bool
}
func (p *PutPlan) Init() error {
p.executed = false
return nil
}
func (p *PutPlan) Explain() []string {
return []string{p.String()}
}
func (p *PutPlan) String() string {
kvps := make([]string, len(p.KVPairs))
for i, kvp := range p.KVPairs {
kvps[i] = kvp.String()
}
return fmt.Sprintf("PutPlan{KVPairs = [%s]}", strings.Join(kvps, ", "))
}
func (p *PutPlan) 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 *PutPlan) 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 *PutPlan) FieldNameList() []string {
return []string{"Rows"}
}
func (p *PutPlan) FieldTypeList() []Type {
return []Type{TNUMBER}
}
func (p *PutPlan) processKVPair(ctx *ExecuteCtx, kvp *PutKVPair) ([]byte, []byte, error) {
ekvp := NewKVPStr("", "")
rkey, err := kvp.Key.Execute(ekvp, ctx)
if err != nil {
return nil, nil, err
}
key := []byte(toString(rkey))
ekvp.Key = key
rvalue, err := kvp.Value.Execute(ekvp, ctx)
if err != nil {
return nil, nil, err
}
value := []byte(toString(rvalue))
return key, value, nil
}
func (p *PutPlan) execute(ctx *ExecuteCtx) (int, error) {
nkvps := len(p.KVPairs)
kvps := make([]KVPair, nkvps)
for i, kvp := range p.KVPairs {
key, value, err := p.processKVPair(ctx, kvp)
if err != nil {
return 0, err
}
kvps[i] = NewKVP(key, value)
}
if nkvps == 0 {
return 0, nil
} else if nkvps == 1 {
err := p.Storage.Put(kvps[0].Key, kvps[0].Value)
if err != nil {
return 0, err
}
return 1, nil
} else {
err := p.Storage.BatchPut(kvps)
if err != nil {
return 0, err
}
return nkvps, nil
}
}