WriteRelationships - Write a Relation to repository
(_ context.Context, tenantID string, tupleCollection *database.TupleCollection, attributesCollection *database.AttributeCollection)
| 33 | |
| 34 | // WriteRelationships - Write a Relation to repository |
| 35 | func (w *DataWriter) Write(_ context.Context, tenantID string, tupleCollection *database.TupleCollection, attributesCollection *database.AttributeCollection) (token.EncodedSnapToken, error) { |
| 36 | var err error |
| 37 | |
| 38 | tupleIterator := tupleCollection.CreateTupleIterator() |
| 39 | attributeIterator := attributesCollection.CreateAttributeIterator() |
| 40 | if !tupleIterator.HasNext() && !attributeIterator.HasNext() { |
| 41 | return token.NewNoopToken().Encode(), nil |
| 42 | } |
| 43 | |
| 44 | txn := w.database.DB.Txn(true) |
| 45 | defer txn.Abort() |
| 46 | |
| 47 | for tupleIterator.HasNext() { |
| 48 | bt := tupleIterator.GetNext() |
| 49 | srelation := bt.GetSubject().GetRelation() |
| 50 | if srelation == tuple.ELLIPSIS { |
| 51 | srelation = "" |
| 52 | } |
| 53 | if err = txn.Insert(constants.RelationTuplesTable, storage.RelationTuple{ |
| 54 | ID: w.database.RelationTupleID(), |
| 55 | TenantID: tenantID, |
| 56 | EntityType: bt.GetEntity().GetType(), |
| 57 | EntityID: bt.GetEntity().GetId(), |
| 58 | Relation: bt.GetRelation(), |
| 59 | SubjectType: bt.GetSubject().GetType(), |
| 60 | SubjectID: bt.GetSubject().GetId(), |
| 61 | SubjectRelation: srelation, |
| 62 | }); err != nil { |
| 63 | return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | for attributeIterator.HasNext() { |
| 68 | at := attributeIterator.GetNext() |
| 69 | if err = txn.Insert(constants.AttributesTable, storage.Attribute{ |
| 70 | ID: w.database.AttributeID(), |
| 71 | TenantID: tenantID, |
| 72 | EntityType: at.GetEntity().GetType(), |
| 73 | EntityID: at.GetEntity().GetId(), |
| 74 | Attribute: at.GetAttribute(), |
| 75 | Value: at.GetValue(), |
| 76 | }); err != nil { |
| 77 | return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | txn.Commit() |
| 82 | return snapshot.NewToken(time.Now()).Encode(), nil |
| 83 | } |
| 84 | |
| 85 | // Delete - Delete relationship from repository |
| 86 | func (w *DataWriter) Delete(_ context.Context, tenantID string, tupleFilter *base.TupleFilter, attributeFilter *base.AttributeFilter) (token.EncodedSnapToken, error) { |
nothing calls this directly
no test coverage detected