-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.go
238 lines (187 loc) · 4.89 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
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
package itisadb
import (
"context"
"fmt"
"sync/atomic"
"github.com/egorgasay/gost"
api "github.com/egorgasay/itisadb-shared-proto/go"
)
type Object struct {
name string
server atomic.Int32
cl api.ItisaDBClient
}
func (c *Client) Object(name string) *Object {
return &Object{
name: name,
cl: c.cl,
}
}
func (o *Object) Create(ctx context.Context, opts ...ObjectOptions) (res gost.Result[*Object]) {
opt := ObjectOptions{}
if len(opts) > 0 {
opt = opts[0]
}
r, err := o.cl.Object(withAuth(ctx), &api.ObjectRequest{
Name: o.name,
Options: &api.ObjectRequest_Options{
Server: opt.Server,
Level: api.Level(opt.Level),
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
o.server.Store(r.GetServer())
return res.Ok(o)
}
// Server returns the server ID of the object.
func (o *Object) Server() int32 {
return o.server.Load()
}
// Set sets the value for the key in the specified object.
func (o *Object) Set(ctx context.Context, key, value string, opts ...SetToObjectOptions) (res gost.Result[int32]) {
opt := SetToObjectOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
r, err := o.cl.SetToObject(withAuth(ctx), &api.SetToObjectRequest{
Key: key,
Value: value,
Object: o.name,
Options: &api.SetToObjectRequest_Options{
Server: opt.Server,
ReadOnly: opt.ReadOnly,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok(r.SavedTo)
}
// Get gets the value for the key from the specified object.
func (o *Object) Get(ctx context.Context, key string, opts ...GetFromObjectOptions) (res gost.Result[string]) {
opt := GetFromObjectOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
r, err := o.cl.GetFromObject(withAuth(ctx), &api.GetFromObjectRequest{
Key: key,
Object: o.name,
Options: &api.GetFromObjectRequest_Options{
Server: opt.Server,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok(r.Value)
}
// Object returns a new or an existing object.
func (o *Object) Object(name string) *Object {
return &Object{
name: fmt.Sprint(o.name, ObjectSeparator, name),
cl: o.cl,
}
}
// Name returns the name of the object.
func (o *Object) Name() string {
return o.name
}
// JSON returns the object in JSON.
func (o *Object) JSON(ctx context.Context, opts ...ObjectToJSONOptions) (res gost.Result[string]) {
opt := ObjectToJSONOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
r, err := o.cl.ObjectToJSON(withAuth(ctx), &api.ObjectToJSONRequest{
Name: o.name,
Options: &api.ObjectToJSONRequest_Options{
Server: opt.Server,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok(r.Object)
}
// Size returns the size of the object.
func (o *Object) Size(ctx context.Context, opts ...SizeOptions) (res gost.Result[uint64]) {
opt := SizeOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
r, err := o.cl.Size(withAuth(ctx), &api.ObjectSizeRequest{
Name: o.name,
Options: &api.ObjectSizeRequest_Options{
Server: opt.Server,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok(r.Size)
}
// DeleteObject deletes the object.
func (o *Object) DeleteObject(ctx context.Context, opts ...DeleteObjectOptions) (res gost.ResultN) {
opt := DeleteObjectOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
_, err := o.cl.DeleteObject(withAuth(ctx), &api.DeleteObjectRequest{
Object: o.name,
Options: &api.DeleteObjectRequest_Options{
Server: opt.Server,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok()
}
// Attach attaches the object to another object.
func (o *Object) Attach(ctx context.Context, name string, opts ...AttachToObjectOptions) (res gost.ResultN) {
opt := AttachToObjectOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
_, err := o.cl.AttachToObject(withAuth(ctx), &api.AttachToObjectRequest{
Dst: o.name,
Src: name,
Options: &api.AttachToObjectRequest_Options{
Server: opt.Server,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok()
}
// DeleteKey deletes the attribute from the object.
func (o *Object) DeleteKey(ctx context.Context, key string, opts ...DeleteKeyOptions) (res gost.ResultN) {
opt := DeleteKeyOptions{} // Server: o.opt.Server
if len(opts) > 0 {
opt = opts[0]
}
_, err := o.cl.DeleteAttr(withAuth(ctx), &api.DeleteAttrRequest{
Object: o.name,
Key: key,
Options: &api.DeleteAttrRequest_Options{
Server: opt.Server,
},
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok()
}
// Is checks if it is an object or not.
func (o *Object) Is(ctx context.Context) (res gost.Result[bool]) {
r, err := o.cl.IsObject(withAuth(ctx), &api.IsObjectRequest{
Name: o.name,
})
if err != nil {
return res.Err(errFromGRPCError(err))
}
return res.Ok(r.Ok)
}