-
Notifications
You must be signed in to change notification settings - Fork 7
/
object.go
136 lines (126 loc) · 3.04 KB
/
object.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
package riaken_core
import (
"errors"
"github.com/golang/protobuf/proto"
"github.com/riaken/riaken-core/rpb"
)
type Object struct {
bucket *Bucket // bucket this object is associated with
key string // key this object is associated with
vclock []byte // vector clock
opts interface{}
ct []byte
}
func (o *Object) reset() {
o.opts = nil
o.ct = nil
}
func (o *Object) GetOpts() interface{} {
return o.opts
}
// Do allows opts to be passed to a method. This call should be chained.
func (o *Object) Do(opts interface{}) *Object {
o.opts = opts
return o
}
// ContentType sets the default content type for this object.
func (o *Object) ContentType(ct []byte) {
o.ct = ct
}
// Fetch returns the data for this object at key.
func (o *Object) Fetch() (*rpb.RpbGetResp, error) {
defer o.reset()
opts := new(rpb.RpbGetReq)
if o.opts != nil {
if _, ok := o.opts.(*rpb.RpbGetReq); !ok {
return nil, errors.New("Called Do() with wrong opts. Should be RpbGetReq")
} else {
opts = o.opts.(*rpb.RpbGetReq)
}
}
opts.Bucket = []byte(o.bucket.name)
opts.Key = []byte(o.key)
if opts.Type == nil {
opts.Type = o.bucket.btype
}
in, err := proto.Marshal(opts)
if err != nil {
return nil, err
}
out, err := o.bucket.session.execute(Messages["GetReq"], in)
if err != nil {
return nil, err
}
o.vclock = out.(*rpb.RpbGetResp).Vclock
return out.(*rpb.RpbGetResp), nil
}
// Store adds or replaces data for this object at key.
//
// It is up to the caller to make sure data is converted to []byte format.
func (o *Object) Store(data []byte) (*rpb.RpbPutResp, error) {
defer o.reset()
opts := new(rpb.RpbPutReq)
if o.opts != nil {
if _, ok := o.opts.(*rpb.RpbPutReq); !ok {
return nil, errors.New("Called Do() with wrong opts. Should be RpbPutReq")
} else {
opts = o.opts.(*rpb.RpbPutReq)
}
}
opts.Bucket = []byte(o.bucket.name)
if o.key != "" {
opts.Key = []byte(o.key)
}
if opts.Type == nil {
opts.Type = o.bucket.btype
}
if opts.Content == nil {
opts.Content = &rpb.RpbContent{
Value: data,
ContentType: o.ct,
}
} else {
opts.Content.Value = data
}
if opts.Vclock == nil {
opts.Vclock = o.vclock
}
in, err := proto.Marshal(opts)
if err != nil {
return nil, err
}
out, err := o.bucket.session.execute(Messages["PutReq"], in)
if err != nil {
return nil, err
}
return out.(*rpb.RpbPutResp), nil
}
// Delete removes the both the data and key for this object.
func (o *Object) Delete() (bool, error) {
defer o.reset()
opts := new(rpb.RpbDelReq)
if o.opts != nil {
if _, ok := o.opts.(*rpb.RpbDelReq); !ok {
return false, errors.New("Called Do() with wrong opts. Should be RpbDelReq")
} else {
opts = o.opts.(*rpb.RpbDelReq)
}
}
opts.Bucket = []byte(o.bucket.name)
opts.Key = []byte(o.key)
if opts.Type == nil {
opts.Type = o.bucket.btype
}
if opts.Vclock == nil {
opts.Vclock = o.vclock
}
in, err := proto.Marshal(opts)
if err != nil {
return false, err
}
out, err := o.bucket.session.execute(Messages["DelReq"], in)
if err != nil {
return false, err
}
return out.(bool), nil
}