-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.obx.go
387 lines (339 loc) · 13.5 KB
/
task.obx.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Code generated by ObjectBox; DO NOT EDIT.
// Learn more about defining entities and generating this file - visit https://golang.objectbox.io/entity-annotations
package model
import (
"errors"
"github.com/google/flatbuffers/go"
"github.com/objectbox/objectbox-go/objectbox"
"github.com/objectbox/objectbox-go/objectbox/fbutils"
)
type task_EntityInfo struct {
objectbox.Entity
Uid uint64
}
var TaskBinding = task_EntityInfo{
Entity: objectbox.Entity{
Id: 1,
},
Uid: 6645479796472661392,
}
// Task_ contains type-based Property helpers to facilitate some common operations such as Queries.
var Task_ = struct {
Id *objectbox.PropertyUint64
Text *objectbox.PropertyString
DateCreated *objectbox.PropertyInt64
DateFinished *objectbox.PropertyInt64
}{
Id: &objectbox.PropertyUint64{
BaseProperty: &objectbox.BaseProperty{
Id: 1,
Entity: &TaskBinding.Entity,
},
},
Text: &objectbox.PropertyString{
BaseProperty: &objectbox.BaseProperty{
Id: 2,
Entity: &TaskBinding.Entity,
},
},
DateCreated: &objectbox.PropertyInt64{
BaseProperty: &objectbox.BaseProperty{
Id: 4,
Entity: &TaskBinding.Entity,
},
},
DateFinished: &objectbox.PropertyInt64{
BaseProperty: &objectbox.BaseProperty{
Id: 5,
Entity: &TaskBinding.Entity,
},
},
}
// GeneratorVersion is called by ObjectBox to verify the compatibility of the generator used to generate this code
func (task_EntityInfo) GeneratorVersion() int {
return 6
}
// AddToModel is called by ObjectBox during model build
func (task_EntityInfo) AddToModel(model *objectbox.Model) {
model.Entity("Task", 1, 6645479796472661392)
model.Property("Id", 6, 1, 9211738071025439652)
model.PropertyFlags(1)
model.Property("Text", 9, 2, 8804670454579230281)
model.Property("DateCreated", 10, 4, 1260602348787983453)
model.Property("DateFinished", 10, 5, 6240065879507520219)
model.EntityLastPropertyId(5, 6240065879507520219)
}
// GetId is called by ObjectBox during Put operations to check for existing ID on an object
func (task_EntityInfo) GetId(object interface{}) (uint64, error) {
return object.(*Task).Id, nil
}
// SetId is called by ObjectBox during Put to update an ID on an object that has just been inserted
func (task_EntityInfo) SetId(object interface{}, id uint64) error {
object.(*Task).Id = id
return nil
}
// PutRelated is called by ObjectBox to put related entities before the object itself is flattened and put
func (task_EntityInfo) PutRelated(ob *objectbox.ObjectBox, object interface{}, id uint64) error {
return nil
}
// Flatten is called by ObjectBox to transform an object to a FlatBuffer
func (task_EntityInfo) Flatten(object interface{}, fbb *flatbuffers.Builder, id uint64) error {
obj := object.(*Task)
var propDateCreated int64
{
var err error
propDateCreated, err = objectbox.TimeInt64ConvertToDatabaseValue(obj.DateCreated)
if err != nil {
return errors.New("converter objectbox.TimeInt64ConvertToDatabaseValue() failed on Task.DateCreated: " + err.Error())
}
}
var propDateFinished int64
{
var err error
propDateFinished, err = objectbox.TimeInt64ConvertToDatabaseValue(obj.DateFinished)
if err != nil {
return errors.New("converter objectbox.TimeInt64ConvertToDatabaseValue() failed on Task.DateFinished: " + err.Error())
}
}
var offsetText = fbutils.CreateStringOffset(fbb, obj.Text)
// build the FlatBuffers object
fbb.StartObject(5)
fbutils.SetUint64Slot(fbb, 0, id)
fbutils.SetUOffsetTSlot(fbb, 1, offsetText)
fbutils.SetInt64Slot(fbb, 3, propDateCreated)
fbutils.SetInt64Slot(fbb, 4, propDateFinished)
return nil
}
// Load is called by ObjectBox to load an object from a FlatBuffer
func (task_EntityInfo) Load(ob *objectbox.ObjectBox, bytes []byte) (interface{}, error) {
if len(bytes) == 0 { // sanity check, should "never" happen
return nil, errors.New("can't deserialize an object of type 'Task' - no data received")
}
var table = &flatbuffers.Table{
Bytes: bytes,
Pos: flatbuffers.GetUOffsetT(bytes),
}
var propId = table.GetUint64Slot(4, 0)
propDateCreated, err := objectbox.TimeInt64ConvertToEntityProperty(fbutils.GetInt64Slot(table, 10))
if err != nil {
return nil, errors.New("converter objectbox.TimeInt64ConvertToEntityProperty() failed on Task.DateCreated: " + err.Error())
}
propDateFinished, err := objectbox.TimeInt64ConvertToEntityProperty(fbutils.GetInt64Slot(table, 12))
if err != nil {
return nil, errors.New("converter objectbox.TimeInt64ConvertToEntityProperty() failed on Task.DateFinished: " + err.Error())
}
return &Task{
Id: propId,
Text: fbutils.GetStringSlot(table, 6),
DateCreated: propDateCreated,
DateFinished: propDateFinished,
}, nil
}
// MakeSlice is called by ObjectBox to construct a new slice to hold the read objects
func (task_EntityInfo) MakeSlice(capacity int) interface{} {
return make([]*Task, 0, capacity)
}
// AppendToSlice is called by ObjectBox to fill the slice of the read objects
func (task_EntityInfo) AppendToSlice(slice interface{}, object interface{}) interface{} {
if object == nil {
return append(slice.([]*Task), nil)
}
return append(slice.([]*Task), object.(*Task))
}
// Box provides CRUD access to Task objects
type TaskBox struct {
*objectbox.Box
}
// BoxForTask opens a box of Task objects
func BoxForTask(ob *objectbox.ObjectBox) *TaskBox {
return &TaskBox{
Box: ob.InternalBox(1),
}
}
// Put synchronously inserts/updates a single object.
// In case the Id is not specified, it would be assigned automatically (auto-increment).
// When inserting, the Task.Id property on the passed object will be assigned the new ID as well.
func (box *TaskBox) Put(object *Task) (uint64, error) {
return box.Box.Put(object)
}
// Insert synchronously inserts a single object. As opposed to Put, Insert will fail if given an ID that already exists.
// In case the Id is not specified, it would be assigned automatically (auto-increment).
// When inserting, the Task.Id property on the passed object will be assigned the new ID as well.
func (box *TaskBox) Insert(object *Task) (uint64, error) {
return box.Box.Insert(object)
}
// Update synchronously updates a single object.
// As opposed to Put, Update will fail if an object with the same ID is not found in the database.
func (box *TaskBox) Update(object *Task) error {
return box.Box.Update(object)
}
// PutAsync asynchronously inserts/updates a single object.
// Deprecated: use box.Async().Put() instead
func (box *TaskBox) PutAsync(object *Task) (uint64, error) {
return box.Box.PutAsync(object)
}
// PutMany inserts multiple objects in single transaction.
// In case Ids are not set on the objects, they would be assigned automatically (auto-increment).
//
// Returns: IDs of the put objects (in the same order).
// When inserting, the Task.Id property on the objects in the slice will be assigned the new IDs as well.
//
// Note: In case an error occurs during the transaction, some of the objects may already have the Task.Id assigned
// even though the transaction has been rolled back and the objects are not stored under those IDs.
//
// Note: The slice may be empty or even nil; in both cases, an empty IDs slice and no error is returned.
func (box *TaskBox) PutMany(objects []*Task) ([]uint64, error) {
return box.Box.PutMany(objects)
}
// Get reads a single object.
//
// Returns nil (and no error) in case the object with the given ID doesn't exist.
func (box *TaskBox) Get(id uint64) (*Task, error) {
object, err := box.Box.Get(id)
if err != nil {
return nil, err
} else if object == nil {
return nil, nil
}
return object.(*Task), nil
}
// GetMany reads multiple objects at once.
// If any of the objects doesn't exist, its position in the return slice is nil
func (box *TaskBox) GetMany(ids ...uint64) ([]*Task, error) {
objects, err := box.Box.GetMany(ids...)
if err != nil {
return nil, err
}
return objects.([]*Task), nil
}
// GetManyExisting reads multiple objects at once, skipping those that do not exist.
func (box *TaskBox) GetManyExisting(ids ...uint64) ([]*Task, error) {
objects, err := box.Box.GetManyExisting(ids...)
if err != nil {
return nil, err
}
return objects.([]*Task), nil
}
// GetAll reads all stored objects
func (box *TaskBox) GetAll() ([]*Task, error) {
objects, err := box.Box.GetAll()
if err != nil {
return nil, err
}
return objects.([]*Task), nil
}
// Remove deletes a single object
func (box *TaskBox) Remove(object *Task) error {
return box.Box.Remove(object)
}
// RemoveMany deletes multiple objects at once.
// Returns the number of deleted object or error on failure.
// Note that this method will not fail if an object is not found (e.g. already removed).
// In case you need to strictly check whether all of the objects exist before removing them,
// you can execute multiple box.Contains() and box.Remove() inside a single write transaction.
func (box *TaskBox) RemoveMany(objects ...*Task) (uint64, error) {
var ids = make([]uint64, len(objects))
for k, object := range objects {
ids[k] = object.Id
}
return box.Box.RemoveIds(ids...)
}
// Creates a query with the given conditions. Use the fields of the Task_ struct to create conditions.
// Keep the *TaskQuery if you intend to execute the query multiple times.
// Note: this function panics if you try to create illegal queries; e.g. use properties of an alien type.
// This is typically a programming error. Use QueryOrError instead if you want the explicit error check.
func (box *TaskBox) Query(conditions ...objectbox.Condition) *TaskQuery {
return &TaskQuery{
box.Box.Query(conditions...),
}
}
// Creates a query with the given conditions. Use the fields of the Task_ struct to create conditions.
// Keep the *TaskQuery if you intend to execute the query multiple times.
func (box *TaskBox) QueryOrError(conditions ...objectbox.Condition) (*TaskQuery, error) {
if query, err := box.Box.QueryOrError(conditions...); err != nil {
return nil, err
} else {
return &TaskQuery{query}, nil
}
}
// Async provides access to the default Async Box for asynchronous operations. See TaskAsyncBox for more information.
func (box *TaskBox) Async() *TaskAsyncBox {
return &TaskAsyncBox{AsyncBox: box.Box.Async()}
}
// TaskAsyncBox provides asynchronous operations on Task objects.
//
// Asynchronous operations are executed on a separate internal thread for better performance.
//
// There are two main use cases:
//
// 1) "execute & forget:" you gain faster put/remove operations as you don't have to wait for the transaction to finish.
//
// 2) Many small transactions: if your write load is typically a lot of individual puts that happen in parallel,
// this will merge small transactions into bigger ones. This results in a significant gain in overall throughput.
//
// In situations with (extremely) high async load, an async method may be throttled (~1ms) or delayed up to 1 second.
// In the unlikely event that the object could still not be enqueued (full queue), an error will be returned.
//
// Note that async methods do not give you hard durability guarantees like the synchronous Box provides.
// There is a small time window in which the data may not have been committed durably yet.
type TaskAsyncBox struct {
*objectbox.AsyncBox
}
// AsyncBoxForTask creates a new async box with the given operation timeout in case an async queue is full.
// The returned struct must be freed explicitly using the Close() method.
// It's usually preferable to use TaskBox::Async() which takes care of resource management and doesn't require closing.
func AsyncBoxForTask(ob *objectbox.ObjectBox, timeoutMs uint64) *TaskAsyncBox {
var async, err = objectbox.NewAsyncBox(ob, 1, timeoutMs)
if err != nil {
panic("Could not create async box for entity ID 1: %s" + err.Error())
}
return &TaskAsyncBox{AsyncBox: async}
}
// Put inserts/updates a single object asynchronously.
// When inserting a new object, the Id property on the passed object will be assigned the new ID the entity would hold
// if the insert is ultimately successful. The newly assigned ID may not become valid if the insert fails.
func (asyncBox *TaskAsyncBox) Put(object *Task) (uint64, error) {
return asyncBox.AsyncBox.Put(object)
}
// Insert a single object asynchronously.
// The Id property on the passed object will be assigned the new ID the entity would hold if the insert is ultimately
// successful. The newly assigned ID may not become valid if the insert fails.
// Fails silently if an object with the same ID already exists (this error is not returned).
func (asyncBox *TaskAsyncBox) Insert(object *Task) (id uint64, err error) {
return asyncBox.AsyncBox.Insert(object)
}
// Update a single object asynchronously.
// The object must already exists or the update fails silently (without an error returned).
func (asyncBox *TaskAsyncBox) Update(object *Task) error {
return asyncBox.AsyncBox.Update(object)
}
// Remove deletes a single object asynchronously.
func (asyncBox *TaskAsyncBox) Remove(object *Task) error {
return asyncBox.AsyncBox.Remove(object)
}
// Query provides a way to search stored objects
//
// For example, you can find all Task which Id is either 42 or 47:
//
// box.Query(Task_.Id.In(42, 47)).Find()
type TaskQuery struct {
*objectbox.Query
}
// Find returns all objects matching the query
func (query *TaskQuery) Find() ([]*Task, error) {
objects, err := query.Query.Find()
if err != nil {
return nil, err
}
return objects.([]*Task), nil
}
// Offset defines the index of the first object to process (how many objects to skip)
func (query *TaskQuery) Offset(offset uint64) *TaskQuery {
query.Query.Offset(offset)
return query
}
// Limit sets the number of elements to process by the query
func (query *TaskQuery) Limit(limit uint64) *TaskQuery {
query.Query.Limit(limit)
return query
}