From e13e65d09bf8eecdb405368668607c82d8e298cd Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Sun, 27 Jul 2014 15:39:45 -0400 Subject: [PATCH] single writer --- graph/replication.go | 2 +- graph/triplestore.go | 2 +- replication/local.go | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/graph/replication.go b/graph/replication.go index d890219..65e560e 100644 --- a/graph/replication.go +++ b/graph/replication.go @@ -38,7 +38,7 @@ type Transaction struct { ID int64 Triple *Triple Action Procedure - Timestamp *time.Time + Timestamp time.Time } type TripleWriter interface { diff --git a/graph/triplestore.go b/graph/triplestore.go index 9927f4c..360dbc9 100644 --- a/graph/triplestore.go +++ b/graph/triplestore.go @@ -39,7 +39,7 @@ type Value interface{} type TripleStore interface { // The only way in is through building a transaction, which // is done by a replication strategy. - ApplyTransactions([]*Transaction) + ApplyTransactions([]*Transaction) error // Given an opaque token, returns the triple for that token from the store. Triple(Value) *Triple diff --git a/replication/local.go b/replication/local.go index 5e3f60b..01eb644 100644 --- a/replication/local.go +++ b/replication/local.go @@ -16,6 +16,7 @@ package replication import ( "sync" + "time" "github.com/google/cayley/graph" ) @@ -42,10 +43,42 @@ func (s *Single) AcquireNextId() int64 { return id } -func AddTriple(*graph.Triple) error { +func (s *Single) AddTriple(t *graph.Triple) error { + trans := make([]*graph.Transaction, 1) + trans[0] = &graph.Transaction{ + ID: s.AcquireNextId(), + Triple: t, + Action: graph.Add, + Timestamp: time.Now(), + } + return s.ts.ApplyTransactions(trans) +} + +func (s *Single) AddTripleSet(set []*graph.Triple) error { + trans := make([]*graph.Transaction, len(set)) + for i, t := range set { + trans[i] = &graph.Transaction{ + ID: s.AcquireNextId(), + Triple: t, + Action: graph.Add, + Timestamp: time.Now(), + } + } + s.ts.ApplyTransactions(trans) return nil } +func (s *Single) RemoveTriple(t *graph.Triple) error { + trans := make([]*graph.Transaction, 1) + trans[0] = &graph.Transaction{ + ID: s.AcquireNextId(), + Triple: t, + Action: graph.Delete, + Timestamp: time.Now(), + } + return s.ts.ApplyTransactions(trans) +} + func init() { graph.RegisterWriter("single", NewSingleReplication) }