-
Notifications
You must be signed in to change notification settings - Fork 27
/
rdatatype.go
183 lines (165 loc) · 3.67 KB
/
rdatatype.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package riak
import (
"errors"
"github.com/tpjg/goriakpbc/pb"
)
const (
TYPE_COUNTER = 1
TYPE_SET = 2
TYPE_MAP = 3
)
type RDataTypeObject struct {
Bucket *Bucket
Key string
Options []map[string]uint32
Context []uint8
}
type RDataType interface {
Store() error
Destroy() error
}
func (b *Bucket) FetchCounter(key string, options ...map[string]uint32) (obj *RDtCounter, err error) {
o, err := b.fetch(key, options...)
if o != nil {
obj = o.(*RDtCounter)
}
return
}
func (b *Bucket) FetchSet(key string, options ...map[string]uint32) (obj *RDtSet, err error) {
o, err := b.fetch(key, options...)
if o != nil {
obj = o.(*RDtSet)
}
return
}
func (b *Bucket) FetchMap(key string, options ...map[string]uint32) (obj *RDtMap, err error) {
o, err := b.fetch(key, options...)
if o != nil {
obj = o.(*RDtMap)
}
return
}
func (b *Bucket) fetch(key string, options ...map[string]uint32) (obj RDataType, err error) {
t := true
req := &pb.DtFetchReq{
Type: []byte(b.bucket_type),
Bucket: []byte(b.name),
Key: []byte(key),
NotfoundOk: &t,
}
for _, omap := range options {
for k, v := range omap {
switch k {
case "r":
req.R = &v
case "pr":
req.Pr = &v
case "include_context":
include_context := v == 1
req.IncludeContext = &include_context
}
}
}
err, conn := b.client.request(req, dtFetchReq)
if err != nil {
return nil, err
}
resp := &pb.DtFetchResp{}
err = b.client.response(conn, resp)
if err != nil {
return nil, err
}
// Create a new object (even if only for storing the returned Vclock)
switch *resp.Type {
case TYPE_COUNTER:
obj = &RDtCounter{RDataTypeObject: RDataTypeObject{Key: key, Bucket: b, Options: options, Context: resp.Context}}
case TYPE_SET:
obj = &RDtSet{RDataTypeObject: RDataTypeObject{Key: key, Bucket: b, Options: options, Context: resp.Context}}
case TYPE_MAP:
obj = &RDtMap{RDataTypeObject: RDataTypeObject{Key: key, Bucket: b, Options: options, Context: resp.Context}}
obj.(*RDtMap).Init(nil)
default:
return nil, errors.New("Type mismatch")
}
// If no Content is returned then the object was not found
if resp.Value == nil {
return obj, NotFound
}
switch dt := obj.(type) {
case *RDtCounter:
dt.Value = resp.Value.CounterValue
case *RDtSet:
dt.Value = resp.Value.SetValue
case *RDtMap:
dt.Init(resp.Value.MapValue)
}
return obj, nil
}
func (m RDataTypeObject) store(op *pb.DtOp) (err error) {
req := &pb.DtUpdateReq{
Type: []byte(m.Bucket.bucket_type),
Bucket: []byte(m.Bucket.name),
Context: m.Context,
Key: []byte(m.Key),
Op: op,
}
// Add the options
for _, omap := range m.Options {
for k, v := range omap {
switch k {
case "w":
req.W = &v
case "dw":
req.Dw = &v
case "pw":
req.Pw = &v
}
}
}
// Send the request
err, conn := m.Bucket.client.request(req, dtUpdateReq)
if err != nil {
return err
}
// Get response, ReturnHead is true, so we can store the vclock
resp := &pb.DtUpdateResp{}
err = m.Bucket.client.response(conn, resp)
if err != nil {
return err
}
return nil
}
func (obj *RDataTypeObject) Destroy() (err error) {
req := &pb.RpbDelReq{
Type: []byte(obj.Bucket.bucket_type),
Bucket: []byte(obj.Bucket.name),
Key: []byte(obj.Key),
}
for _, omap := range obj.Options {
for k, v := range omap {
switch k {
case "r":
req.R = &v
case "pr":
req.Pr = &v
case "rw":
req.Rw = &v
case "w":
req.W = &v
case "dw":
req.Dw = &v
case "pw":
req.Pw = &v
}
}
}
err, conn := obj.Bucket.client.request(req, rpbDelReq)
if err != nil {
return err
}
err = obj.Bucket.client.response(conn, req)
if err != nil {
return err
}
return nil
}