forked from tpjg/goriakpbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robject.go
328 lines (304 loc) · 8 KB
/
robject.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package riak
import (
"errors"
"github.com/tpjg/goriakpbc/pb"
)
// A Riak link
type Link struct {
Bucket string
Key string
Tag string
}
// An object van have siblings that can each have their own content
type Sibling struct {
ContentType string
Data []byte
Links []Link
Meta map[string]string
Indexes map[string][]string
Vtag string
LastMod uint32
LastModUsecs uint32
}
// An RObject is an object or document that is or can be stored in Riak
type RObject struct {
Bucket *Bucket
Vclock []byte
Key string
ContentType string
Data []byte
Links []Link
Meta map[string]string
Indexes map[string][]string
Vtag string
LastMod uint32
LastModUsecs uint32
conflict bool
Siblings []Sibling
Options []map[string]uint32
}
// Error definitions
var (
NotFound = errors.New("Object not found")
)
// Store an RObject
func (obj *RObject) Store() (err error) {
// Create base pb.RpbPutReq
t := true
req := &pb.RpbPutReq{
Type: []byte(obj.Bucket.bucket_type),
Bucket: []byte(obj.Bucket.name),
Content: &pb.RpbContent{
Value: []byte(obj.Data),
ContentType: []byte(obj.ContentType),
},
ReturnHead: &t,
}
if obj.Key != "" {
req.Key = []byte(obj.Key)
}
if len(obj.Vclock) > 0 {
req.Vclock = obj.Vclock
}
// Add the links
req.Content.Links = make([]*pb.RpbLink, len(obj.Links))
for i, v := range obj.Links {
req.Content.Links[i] = &pb.RpbLink{Bucket: []byte(v.Bucket),
Key: []byte(v.Key),
Tag: []byte(v.Tag)}
}
// Add the user metadata
req.Content.Usermeta = make([]*pb.RpbPair, len(obj.Meta))
i := 0
for k, v := range obj.Meta {
req.Content.Usermeta[i] = &pb.RpbPair{Key: []byte(k), Value: []byte(v)}
i += 1
}
// Add the indexes
for k, idx := range obj.Indexes {
for _, v := range idx {
req.Content.Indexes = append(req.Content.Indexes, &pb.RpbPair{Key: []byte(k), Value: []byte(v)})
}
}
// Add the options
for _, omap := range obj.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 := obj.Bucket.client.request(req, rpbPutReq)
if err != nil {
return err
}
// Get response, ReturnHead is true, so we can store the vclock
resp := &pb.RpbPutResp{}
err = obj.Bucket.client.response(conn, resp)
if err != nil {
return err
}
obj.Vclock = resp.Vclock
// If applicable, store the key
if obj.Key == "" {
obj.Key = string(resp.Key)
}
return nil
}
// Delete the object from Riak
func (obj *RObject) Destroy() (err error) {
req := &pb.RpbDelReq{
Type: []byte(obj.Bucket.bucket_type),
Bucket: []byte(obj.Bucket.name),
Key: []byte(obj.Key),
Vclock: obj.Vclock}
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
}
// Returns true if the object was fetched with multiple siblings (AllowMult=true on the bucket)
func (obj *RObject) Conflict() bool {
return obj.conflict
}
// Sets the values that returned from a pb.RpbGetResp in the RObject
func (obj *RObject) setContent(resp *pb.RpbGetResp) {
// Check if there are siblings
if len(resp.Content) > 1 {
// Mark as conflict, set fields
obj.conflict = true
obj.Siblings = make([]Sibling, len(resp.Content))
for i, content := range resp.Content {
obj.Siblings[i].ContentType = string(content.ContentType)
obj.Siblings[i].Data = content.Value
obj.Siblings[i].Vtag = string(content.Vtag)
obj.Siblings[i].LastMod = *content.LastMod
obj.Siblings[i].LastModUsecs = *content.LastModUsecs
obj.Siblings[i].Links = make([]Link, len(content.Links))
for j, link := range content.Links {
obj.Siblings[i].Links[j] = Link{string(link.Bucket),
string(link.Key),
string(link.Tag)}
}
obj.Siblings[i].Meta = make(map[string]string)
for _, meta := range content.Usermeta {
obj.Siblings[i].Meta[string(meta.Key)] = string(meta.Value)
}
// Indexes (can contain multiple values)
obj.Siblings[i].Indexes = make(map[string][]string)
for _, index := range content.Indexes {
obj.Siblings[i].Indexes[string(index.Key)] = append(obj.Siblings[i].Indexes[string(index.Key)], string(index.Value))
}
}
} else if len(resp.Content) == 1 {
// No conflict, set the fields in object directly
obj.conflict = false
obj.ContentType = string(resp.Content[0].ContentType)
obj.Data = resp.Content[0].Value
obj.Links = make([]Link, len(resp.Content[0].Links))
for j, link := range resp.Content[0].Links {
obj.Links[j] = Link{string(link.Bucket),
string(link.Key),
string(link.Tag)}
}
obj.Meta = make(map[string]string)
for _, meta := range resp.Content[0].Usermeta {
obj.Meta[string(meta.Key)] = string(meta.Value)
}
// Indexes (can contain multiple values)
obj.Indexes = make(map[string][]string)
for _, index := range resp.Content[0].Indexes {
obj.Indexes[string(index.Key)] = append(obj.Indexes[string(index.Key)], string(index.Value))
}
obj.Vtag = string(resp.Content[0].Vtag)
obj.LastMod = *resp.Content[0].LastMod
obj.LastModUsecs = *resp.Content[0].LastModUsecs
}
}
// Add a link to another object (does not store the object, must explicitly call "Store()")
func (obj *RObject) LinkTo(target *RObject, tag string) {
if target.Bucket.name != "" && target.Key != "" {
obj.Links = append(obj.Links, Link{target.Bucket.name, target.Key, tag})
}
}
// Add a link if it is not already in the Links slics, returns false if already present
func (obj *RObject) AddLink(link Link) bool {
for _, el := range obj.Links {
if el.Bucket == link.Bucket && el.Key == link.Key && el.Tag == link.Tag {
return false
}
}
obj.Links = append(obj.Links, link)
return true
}
// Get an object
func (b *Bucket) Get(key string, options ...map[string]uint32) (obj *RObject, err error) {
t := true
req := &pb.RpbGetReq{
Type: []byte(b.bucket_type),
Bucket: []byte(b.name),
Key: []byte(key),
NotfoundOk: &t,
Deletedvclock: &t,
}
for _, omap := range options {
for k, v := range omap {
switch k {
case "r":
req.R = &v
case "pr":
req.Pr = &v
}
}
}
err, conn := b.client.request(req, rpbGetReq)
if err != nil {
return nil, err
}
resp := &pb.RpbGetResp{}
err = b.client.response(conn, resp)
if err != nil {
return nil, err
}
// Create a new object (even if only for storing the returned Vclock)
obj = &RObject{Key: key, Bucket: b, Vclock: resp.Vclock, Options: options}
// If no Content is returned then the object was not found
if len(resp.Content) == 0 {
return obj, NotFound
}
// Set the fields
obj.setContent(resp)
return obj, nil
}
// Get directly from a bucket, without creating a bucket first
func (c *Client) GetFrom(bucketname string, key string, options ...map[string]uint32) (obj *RObject, err error) {
var bucket *Bucket
bucket, err = c.Bucket(bucketname)
if err != nil {
return
}
return bucket.Get(key, options...)
}
// Reload an object if it has changed (new Vclock)
func (obj *RObject) Reload() (err error) {
req := &pb.RpbGetReq{
Type: []byte(obj.Bucket.bucket_type),
Bucket: []byte(obj.Bucket.name),
Key: []byte(obj.Key),
IfModified: obj.Vclock}
for _, omap := range obj.Options {
for k, v := range omap {
switch k {
case "r":
req.R = &v
case "pr":
req.Pr = &v
}
}
}
err, conn := obj.Bucket.client.request(req, rpbGetReq)
if err != nil {
return err
}
resp := &pb.RpbGetResp{}
err = obj.Bucket.client.response(conn, resp)
if err != nil {
return err
}
if resp.Unchanged != nil && *resp.Unchanged == true {
return nil
}
// Object has new content, reload object
obj.Vclock = resp.Vclock
obj.setContent(resp)
return nil
}