-
Notifications
You must be signed in to change notification settings - Fork 2
/
relation.go
747 lines (683 loc) · 22 KB
/
relation.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
package stored
import (
"fmt"
"reflect"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
"github.com/apple/foundationdb/bindings/go/src/fdb/subspace"
"github.com/apple/foundationdb/bindings/go/src/fdb/tuple"
)
// RelationN2N is main type of relation
var RelationN2N = 1
// list of service keys for shorter
var keyRelHostCount = "a"
var keyRelClientCount = "b"
// Relation is main struct to represent Relation
type Relation struct {
name string
host *Object
client *Object
hostDir directory.DirectorySubspace
clientDir directory.DirectorySubspace
infoDir directory.DirectorySubspace
kind int
counter bool
counterClient *Field
hostDataField *Field
clientDataField *Field
}
func (r *Relation) init(kind int, host *Object, client *Object) {
r.kind = kind
r.host = host
r.client = client
var err error
dir := host.directory.Subspace
db := host.db
fdbPath := []string{"rel", host.name, client.name}
if r.name != "" {
fdbPath = append(fdbPath, r.name)
}
r.infoDir, err = dir.CreateOrOpen(db, fdbPath, nil)
if err != nil {
panic(err)
}
r.hostDir, err = r.infoDir.CreateOrOpen(db, []string{"host"}, nil)
if err != nil {
panic(err)
}
if host == client { // if same object use same directory
r.clientDir = r.hostDir
} else {
r.clientDir, err = r.infoDir.CreateOrOpen(db, []string{"client"}, nil)
if err != nil {
panic(err)
}
}
host.Relations = append(host.Relations, r)
}
func (r *Relation) panic(text string) {
panic("relation between «" + r.host.name + "» and «" + r.client.name + "»: " + text)
}
// Counter will start count objects count within relation
func (r *Relation) Counter(on bool) {
r.counter = on
}
// CounterClient will set external field
func (r *Relation) CounterClient(object *ObjectBuilder, fieldName string) {
field := object.object.field(fieldName)
if field.Kind != reflect.Int64 {
r.panic("field «" + fieldName + "» should be int64 to work as counter")
}
r.counterClient = field
}
// return primary values, no masser object wass passed or primary value
func (r *Relation) getPrimary(hostObject interface{}, clientObject interface{}) (tuple.Tuple, tuple.Tuple) {
//hostPrimary := r.host.GetPrimaryField().fromAnyInterface(hostObject)
hostPrimary := r.host.getPrimaryTuple(hostObject)
//clientPrimary := r.client.GetPrimaryField().fromAnyInterface(clientObject)
clientPrimary := r.client.getPrimaryTuple(clientObject)
return hostPrimary, clientPrimary
}
// getHostData fetches data from object
func (r *Relation) getHostDataBytes(hostObj interface{}) (hostVal []byte, err error) {
if r.hostDataField != nil {
hostVal, err = r.hostDataField.BytesFromObject(hostObj)
if err != nil {
return
}
} else {
hostVal = []byte{}
}
return
}
// getClientData fetches data from object
func (r *Relation) getClientDataBytes(clientObj interface{}) (clientVal []byte, err error) {
if r.clientDataField != nil {
clientVal, err = r.clientDataField.BytesFromObject(clientObj)
if err != nil {
return
}
} else {
clientVal = []byte{}
}
return
}
// getData fetches data from object
func (r *Relation) getData(hostObj interface{}, clientObj interface{}) (hostVal, clientVal []byte, err error) {
hostVal, err = r.getHostDataBytes(hostObj)
if err != nil {
return
}
clientVal, err = r.getClientDataBytes(clientObj)
if err != nil {
return
}
return
}
// HostData will set host data field name
func (r *Relation) HostData(fieldName string) {
field := r.host.field(fieldName)
r.hostDataField = field
}
// ClientData will set cliet data field name
func (r *Relation) ClientData(fieldName string) {
field := r.client.field(fieldName)
r.clientDataField = field
}
func (r *Relation) incClientCounter(clientPrimary tuple.Tuple, tr fdb.Transaction, incVal []byte) {
if r.counterClient != nil {
sub := r.counterClient.object.sub(clientPrimary)
tr.Add(r.counterClient.getKey(sub), incVal)
} else {
tr.Add(r.infoDir.Sub(keyRelClientCount).Pack(clientPrimary), incVal)
}
}
func (r *Relation) getClientCounter(clientPrimary tuple.Tuple, tr fdb.ReadTransaction) fdb.FutureByteSlice {
if r.counterClient != nil {
sub := r.counterClient.object.sub(clientPrimary)
return tr.Get(r.counterClient.getKey(sub))
}
return tr.Get(r.infoDir.Sub(keyRelClientCount).Pack(clientPrimary))
}
// Add writes new relation beween objects (return ErrAlreadyExist if exists)
func (r *Relation) Add(hostOrID interface{}, clientOrID interface{}) *PromiseErr {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
p := r.host.promiseErr()
p.do(func() Chain {
val, err := p.tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary)).Get()
if err != nil {
p.fail(err)
}
if val != nil { // already exists
p.fail(ErrAlreadyExist)
}
if r.counter { // increment if not exists
p.tr.Add(r.infoDir.Sub(keyRelHostCount).Pack(hostPrimary), countInc)
r.incClientCounter(clientPrimary, p.tr, countInc)
}
// getting data to store inside relation kv
hostVal, clientVal, dataErr := r.getData(hostOrID, clientOrID)
if dataErr != nil {
p.fail(dataErr)
}
p.tr.Set(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary), clientVal)
p.tr.Set(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary), hostVal)
return p.ok()
})
return p
}
// Set writes new relation beween objects, you could use objects or values with same types as primary key
func (r *Relation) Set(hostOrID interface{}, clientOrID interface{}) *PromiseErr {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
p := r.host.promiseErr()
p.do(func() Chain {
if r.counter { // increment if not exists
val, err := p.tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary)).Get()
if err != nil {
return p.fail(err)
}
if val == nil { // not exists increment here
p.tr.Add(r.infoDir.Sub(keyRelHostCount).Pack(hostPrimary), countInc)
//tr.Add(r.infoDir.Sub(keyRelClientCount).Pack(clientPrimary), countInc)
r.incClientCounter(clientPrimary, p.tr, countInc)
}
}
// getting data to store inside relation kv
hostVal, clientVal, dataErr := r.getData(hostOrID, clientOrID)
if dataErr != nil {
return p.fail(dataErr)
}
p.tr.Set(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary), clientVal)
p.tr.Set(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary), hostVal)
return p.ok()
})
return p
}
// Delete removes relation between objects
func (r *Relation) Delete(hostOrID interface{}, clientOrID interface{}) *PromiseErr {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
p := r.host.promiseErr()
p.do(func() Chain {
if r.counter { // increment if not exists
val, err := p.tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary)).Get()
if err != nil {
return p.fail(err)
}
if val != nil { // exists decrement here
p.tr.Add(r.infoDir.Sub(keyRelHostCount).Pack(hostPrimary), countDec)
//tr.Add(r.infoDir.Sub(keyRelClientCount).Pack(clientPrimary), countDec)
r.incClientCounter(clientPrimary, p.tr, countDec)
}
}
p.tr.Clear(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary))
p.tr.Clear(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary))
return p.ok()
})
return p
}
// GetHostsCount fetches counter
func (r *Relation) GetHostsCount(clientOrID interface{}) *Promise {
if !r.counter {
r.panic("counter is off, use rel.Counter(true)")
}
p := r.host.promiseInt64()
p.doRead(func() Chain {
clientPrimary := r.client.getPrimaryTuple(clientOrID)
//row, err := r.host.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
//row, err := p.readTr.Get(r.infoDir.Sub(keyRelClientCount).Pack(clientPrimary)).Get()
row, err := r.getClientCounter(clientPrimary, p.readTr).Get()
if row == nil {
return p.fail(ErrNotFound)
//return nil, ErrNotFound
}
if err != nil {
return p.fail(err)
}
return p.done(ToInt64(row))
})
return p
}
// SetHostsCounterUnsafe set hosts counter unsafely. User with care
func (r *Relation) SetHostsCounterUnsafe(clientObject interface{}, count int64) error {
clientPrimary := r.client.getPrimaryTuple(clientObject)
_, err := r.client.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
if r.counterClient != nil {
sub := r.counterClient.object.sub(clientPrimary)
tr.Set(r.counterClient.getKey(sub), Int64(count))
} else {
tr.Set(r.infoDir.Sub(keyRelClientCount).Pack(clientPrimary), Int64(count))
}
return
})
return err
}
// GetClientsCount fetches counter
func (r *Relation) GetClientsCount(hostOrID interface{}) *Promise {
if !r.counter {
r.panic("counter is off, use rel.Counter(true)")
}
p := r.host.promiseInt64()
p.doRead(func() Chain {
hostPrimary := r.host.getPrimaryTuple(hostOrID)
//row, err := r.host.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
row, err := p.readTr.Get(r.infoDir.Sub(keyRelHostCount).Pack(hostPrimary)).Get()
if row == nil {
return p.fail(ErrNotFound)
//return nil, ErrNotFound
}
if err != nil {
return p.fail(err)
}
return p.done(ToInt64(row))
})
return p
}
func (r *Relation) getHostsOrClients(objOrID interface{}, from interface{}, hosts bool) *PromiseSlice {
var obj *Object
var opposite *Object
var oppositeDir directory.DirectorySubspace
if hosts {
obj = r.host
opposite = r.client
oppositeDir = r.clientDir
} else {
obj = r.client
opposite = r.host
oppositeDir = r.hostDir
}
p := r.host.promiseSlice()
p.doRead(func() Chain {
primaryKey := opposite.getPrimaryTuple(objOrID)
key := oppositeDir.Sub(primaryKey...)
start, end := key.FDBRangeKeys()
if from != nil {
start = key.Pack(obj.getPrimaryTuple(from)) // add the last key fetched
}
iterator := p.readTr.GetRange(fdb.KeyRange{Begin: start, End: end}, fdb.RangeOptions{
Limit: p.limit,
Reverse: p.reverse,
}).Iterator()
indexData := [][]byte{}
needed := []*needObject{}
for iterator.Advance() {
kv, err := iterator.Get()
if err != nil {
fmt.Printf("Unable to read next value: %v\n", err)
return p.fail(err)
}
keyTuple, err := key.Unpack(kv.Key)
if err != nil {
fmt.Printf("Unable to unpack index key: %v\n", err)
return p.fail(err)
}
l := len(keyTuple)
if l < 1 {
panic("empty key")
}
//obj.need(tr, obj.sub(keyTuple))
needed = append(needed, obj.need(p.readTr, obj.sub(keyTuple)))
indexData = append(indexData, kv.Value)
}
slice := r.host.wrapRange(needed)
if hosts {
if r.hostDataField != nil {
slice.fillFieldData(r.hostDataField, indexData)
}
} else {
if r.clientDataField != nil {
slice.fillFieldData(r.clientDataField, indexData)
}
}
p.done(slice)
if p.onDone != nil {
p.onDone()
}
return nil
})
return p
}
// GetClients fetch slice of client objects using host
func (r *Relation) GetClients(objOrID interface{}, from interface{}) *PromiseSlice {
return r.getHostsOrClients(objOrID, from, false)
}
func (r *Relation) getSliceIDs(objFrom *Object, objRet *Object, dataField *Field, sub subspace.Subspace, from interface{}, limit int) *SliceIDs {
resp, err := objFrom.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
start, end := sub.FDBRangeKeys()
if from != nil {
start = sub.Pack(objRet.getPrimaryTuple(from)) // add the last key fetched
}
iterator := tr.GetRange(fdb.KeyRange{Begin: start, End: end}, fdb.RangeOptions{
Limit: limit,
}).Iterator()
slice := SliceIDs{}
slice.init(objRet)
slice.dataField = dataField
for iterator.Advance() {
kv, err := iterator.Get()
if err != nil {
fmt.Printf("Unable to read next value: %v\n", err)
return nil, err
}
keyTuple, err := sub.Unpack(kv.Key)
if err != nil {
fmt.Printf("Unable to unpack index key: %v\n", err)
return nil, err
}
slice.push(keyTuple, kv.Value)
}
return &slice, nil
})
if err != nil {
return &SliceIDs{
object: objRet,
err: err,
}
}
return resp.(*SliceIDs)
}
// GetClientIDs will fetch only primary values of client objects
func (r *Relation) GetClientIDs(objOrID interface{}, from interface{}, limit int) *SliceIDs {
hostPrimary := r.host.getPrimaryTuple(objOrID)
sub := r.hostDir.Sub(hostPrimary...)
return r.getSliceIDs(r.host, r.client, r.clientDataField, sub, from, limit)
}
// GetHostIDs will fetch only primary values of host objects
func (r *Relation) GetHostIDs(objOrID interface{}, from interface{}, limit int) *SliceIDs {
clientPrimary := r.client.getPrimaryTuple(objOrID)
sub := r.clientDir.Sub(clientPrimary...)
return r.getSliceIDs(r.client, r.host, r.hostDataField, sub, from, limit)
}
// GetHosts fetch slice of client objects using host
func (r *Relation) GetHosts(objOrID interface{}, from interface{}) *PromiseSlice {
return r.getHostsOrClients(objOrID, from, true)
}
// SetHostData writed new data from host object (host data could be )
func (r *Relation) SetHostData(hostObj interface{}, clientOrID interface{}) *Promise {
hostPrimary, clientPrimary := r.getPrimary(hostObj, clientOrID)
p := r.client.promise()
p.do(func() Chain {
//_, err := r.host.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
dataGet := p.tr.Get(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary))
row, err := dataGet.Get()
if err != nil {
return p.fail(err)
}
if row == nil {
return p.fail(ErrNotFound)
}
// getting data to store inside relation kv
hostVal, dataErr := r.getHostDataBytes(hostObj)
if dataErr != nil {
return p.fail(dataErr)
}
p.tr.Set(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary), hostVal)
return p.done(nil)
})
return p
}
// SetClientData writed new data from host object (host data could be )
func (r *Relation) SetClientData(hostOrID interface{}, clientObj interface{}) *Promise {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientObj)
p := r.client.promise()
p.do(func() Chain {
dataGet := p.tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary))
row, err := dataGet.Get()
if err != nil {
return p.fail(err)
}
if row == nil {
fmt.Println("Catched error")
return p.fail(ErrNotFound)
}
// getting data to store inside relation kv
clientVal, dataErr := r.getClientDataBytes(clientObj)
if dataErr != nil {
return p.fail(dataErr)
}
//fmt.Println("[CLIENT DATA]", hostPrimary, clientPrimary, "=>", clientVal)
p.tr.Set(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary), clientVal)
return p.done(nil)
})
return p
}
func (r *Relation) doUpdateData(hostObj interface{}, clientObj interface{}, callback func(), doHost, doClient bool) *Promise {
hostEditable := structEditable(hostObj)
clientEditable := structEditable(clientObj)
hostPrimary := hostEditable.getPrimary(r.host)
clientPrimary := clientEditable.getPrimary(r.client)
p := r.client.promise()
p.do(func() Chain {
var hostDataGet, clientDataGet fdb.FutureByteSlice
if doHost {
hostDataGet = p.tr.Get(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary))
}
if doClient {
clientDataGet = p.tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary))
}
return func() Chain {
var err error
var hostData, clientData []byte
if doHost {
hostData, err = hostDataGet.Get()
if err != nil {
return p.fail(err)
}
if hostData == nil {
return p.fail(ErrNotFound)
}
hostEditable.setField(r.hostDataField, hostData)
}
if doClient {
clientData, err = clientDataGet.Get()
if err != nil {
return p.fail(err)
}
if clientData == nil {
return p.fail(ErrNotFound)
}
clientEditable.setField(r.clientDataField, clientData)
}
callback()
if doHost {
hostVal := hostEditable.GetBytes(r.hostDataField)
p.tr.Set(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary), hostVal)
}
if doClient {
clientVal := clientEditable.GetBytes(r.clientDataField)
p.tr.Set(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary), clientVal)
}
return p.done(nil)
}
})
return p
}
// UpdateData will atomicly update host and client index storage data using callback
func (r *Relation) UpdateData(hostObj interface{}, clientObj interface{}, callback func()) *Promise {
return r.doUpdateData(hostObj, clientObj, callback, true, true)
}
// UpdateClientData will atomicly update only client index storage data using callback
func (r *Relation) UpdateClientData(hostObj interface{}, clientObj interface{}, callback func()) *Promise {
return r.doUpdateData(hostObj, clientObj, callback, false, true)
}
// UpdateHostData will atomicly update only host index storage data using callback
func (r *Relation) UpdateHostData(hostObj interface{}, clientObj interface{}, callback func()) *Promise {
return r.doUpdateData(hostObj, clientObj, callback, true, false)
}
// SetData writed new data for both host and client index storages, return fail if object nor exist
func (r *Relation) SetData(hostObj interface{}, clientObj interface{}) *Promise {
hostPrimary, clientPrimary := r.getPrimary(hostObj, clientObj)
p := r.client.promise()
p.do(func() Chain {
dataGet := p.tr.Get(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary))
return func() Chain {
row, err := dataGet.Get()
if err != nil {
return p.fail(err)
}
if row == nil {
return p.fail(ErrNotFound)
}
// getting data to store inside relation kv
hostVal, dataErr := r.getHostDataBytes(hostObj)
if dataErr != nil {
return p.fail(dataErr)
}
p.tr.Set(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary), hostVal)
clientVal, dataErr := r.getClientDataBytes(clientObj)
if dataErr != nil {
return p.fail(dataErr)
}
p.tr.Set(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary), clientVal)
return p.done(nil)
}
})
return p
}
// Check return true if relation is set (false) if not set
func (r *Relation) Check(hostOrID interface{}, clientOrID interface{}) *Promise {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
p := r.host.promise()
p.doRead(func() Chain {
checkGet := p.readTr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary))
return func() Chain {
val, err := checkGet.Get()
if err != nil {
return p.fail(err)
}
if val == nil { // not exists increment here
return p.done(false)
}
return p.done(true)
}
})
return p
/*val, err := r.host.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
val, err := tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary)).Get()
if err != nil {
return false, err
}
if val == nil { // not exists increment here
return false, nil
}
return true, nil
})
return val.(bool), err*/
}
// GetClientDataIDs returns client data bytes
func (r *Relation) GetClientDataIDs(hostOrID interface{}, clientOrID interface{}) ([]byte, error) {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
val, err := r.host.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
val, err := tr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary)).Get()
if err != nil {
return Nan, err
}
if val == nil { // not exists increment here
return Nan, ErrNotFound
}
return val, nil
})
return val.([]byte), err
}
// FillClientData fetch client data, so it will be written into the data field of client object
func (r *Relation) FillClientData(hostOrID interface{}, client interface{}) *PromiseErr {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, client)
p := r.host.promiseErr()
p.doRead(func() Chain {
fetching := p.readTr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary))
return func() Chain {
val, err := fetching.Get()
if err != nil {
return p.fail(err)
}
//fmt.Println("CLIENT DATA", hostPrimary, clientPrimary, "=>", val)
if val == nil { // not exists increment here
return p.fail(ErrNotFound)
}
return func() Chain {
value := p.getValueField(r.client, r.clientDataField, val)
value.Scan(client)
return p.done(nil)
}
}
})
return p
}
// GetClientData fetch client data
func (r *Relation) GetClientData(hostOrID interface{}, clientOrID interface{}) *PromiseValue {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
p := r.host.promiseValue()
p.doRead(func() Chain {
fetching := p.readTr.Get(r.hostDir.Sub(hostPrimary...).Pack(clientPrimary))
return func() Chain {
val, err := fetching.Get()
if err != nil {
return p.fail(err)
}
//fmt.Println("CLIENT DATA", hostPrimary, clientPrimary, "=>", val)
if val == nil { // not exists increment here
return p.fail(ErrNotFound)
}
raw := valueRaw{}
if r.clientDataField != nil {
raw[r.clientDataField.Name] = val
}
value := Value{
object: r.client,
raw: raw,
}
return p.done(&value)
}
})
return p
}
// FillHostData fetch client data and fill it to host object
func (r *Relation) FillHostData(host interface{}, clientOrID interface{}) *PromiseErr {
hostPrimary, clientPrimary := r.getPrimary(host, clientOrID)
p := r.host.promiseErr()
p.doRead(func() Chain {
val, err := p.readTr.Get(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary)).Get()
if err != nil {
return p.fail(err)
}
if val == nil { // not exists increment here
return p.fail(ErrNotFound)
}
return func() Chain {
value := p.getValueField(r.host, r.hostDataField, val)
value.Scan(host)
return p.done(nil)
}
})
return p
}
// GetHostData fetch client data
func (r *Relation) GetHostData(hostOrID interface{}, clientOrID interface{}) *PromiseValue {
hostPrimary, clientPrimary := r.getPrimary(hostOrID, clientOrID)
p := r.host.promiseValue()
p.doRead(func() Chain {
val, err := p.readTr.Get(r.clientDir.Sub(clientPrimary...).Pack(hostPrimary)).Get()
if err != nil {
return p.fail(err)
}
if val == nil { // not exists increment here
return p.fail(ErrNotFound)
}
return func() Chain {
return p.done(p.getValueField(r.host, r.hostDataField, val))
}
})
return p
}
// Clear will remove all data from relation
func (r *Relation) Clear() error {
_, err := r.host.db.Transact(func(tr fdb.Transaction) (ret interface{}, e error) {
start, end := r.hostDir.FDBRangeKeys()
tr.ClearRange(fdb.KeyRange{Begin: start, End: end})
start, end = r.clientDir.FDBRangeKeys()
tr.ClearRange(fdb.KeyRange{Begin: start, End: end})
start, end = r.infoDir.FDBRangeKeys()
tr.ClearRange(fdb.KeyRange{Begin: start, End: end})
return
})
return err
}