From 1b24d66d8a4c3a2ba0f43a4c3af9931b168138b1 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Sun, 27 Jul 2014 15:41:52 -0400 Subject: [PATCH] rename --- graph/replication.go | 81 ------------------------------------------------- graph/triplewriter.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ replication/local.go | 84 --------------------------------------------------- writer/single.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+), 165 deletions(-) delete mode 100644 graph/replication.go create mode 100644 graph/triplewriter.go delete mode 100644 replication/local.go create mode 100644 writer/single.go diff --git a/graph/replication.go b/graph/replication.go deleted file mode 100644 index 65e560e..0000000 --- a/graph/replication.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2014 The Cayley Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package graph - -// Defines the interface for consistent replication of a graph instance. -// -// Separate from the backend, this dictates how individual triples get -// identified and replicated consistently across (potentially) multiple -// instances. The simplest case is to keep an append-only log of triple -// changes. - -import ( - "errors" - "time" -) - -type Procedure byte - -// The different types of actions a transaction can do. -const ( - Add Procedure = iota - Delete -) - -type Transaction struct { - ID int64 - Triple *Triple - Action Procedure - Timestamp time.Time -} - -type TripleWriter interface { - // Add a triple to the store. - AddTriple(*Triple) error - - // Add a set of triples to the store, atomically if possible. - AddTripleSet([]*Triple) error - - // Removes a triple matching the given one from the database, - // if it exists. Does nothing otherwise. - RemoveTriple(*Triple) error -} - -type NewTripleWriterFunc func(TripleStore, Options) (TripleWriter, error) - -var writerRegistry = make(map[string]NewTripleWriterFunc) - -func RegisterWriter(name string, newFunc NewTripleWriterFunc) { - if _, found := writerRegistry[name]; found { - panic("already registered TripleWriter " + name) - } - writerRegistry[name] = newFunc -} - -func NewTripleWriter(name string, ts TripleStore, opts Options) (TripleWriter, error) { - newFunc, hasNew := writerRegistry[name] - if !hasNew { - return nil, errors.New("replication: name '" + name + "' is not registered") - } - return newFunc(ts, opts) -} - -func WriterMethods() []string { - t := make([]string, 0, len(writerRegistry)) - for n := range writerRegistry { - t = append(t, n) - } - return t -} diff --git a/graph/triplewriter.go b/graph/triplewriter.go new file mode 100644 index 0000000..65e560e --- /dev/null +++ b/graph/triplewriter.go @@ -0,0 +1,81 @@ +// Copyright 2014 The Cayley Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package graph + +// Defines the interface for consistent replication of a graph instance. +// +// Separate from the backend, this dictates how individual triples get +// identified and replicated consistently across (potentially) multiple +// instances. The simplest case is to keep an append-only log of triple +// changes. + +import ( + "errors" + "time" +) + +type Procedure byte + +// The different types of actions a transaction can do. +const ( + Add Procedure = iota + Delete +) + +type Transaction struct { + ID int64 + Triple *Triple + Action Procedure + Timestamp time.Time +} + +type TripleWriter interface { + // Add a triple to the store. + AddTriple(*Triple) error + + // Add a set of triples to the store, atomically if possible. + AddTripleSet([]*Triple) error + + // Removes a triple matching the given one from the database, + // if it exists. Does nothing otherwise. + RemoveTriple(*Triple) error +} + +type NewTripleWriterFunc func(TripleStore, Options) (TripleWriter, error) + +var writerRegistry = make(map[string]NewTripleWriterFunc) + +func RegisterWriter(name string, newFunc NewTripleWriterFunc) { + if _, found := writerRegistry[name]; found { + panic("already registered TripleWriter " + name) + } + writerRegistry[name] = newFunc +} + +func NewTripleWriter(name string, ts TripleStore, opts Options) (TripleWriter, error) { + newFunc, hasNew := writerRegistry[name] + if !hasNew { + return nil, errors.New("replication: name '" + name + "' is not registered") + } + return newFunc(ts, opts) +} + +func WriterMethods() []string { + t := make([]string, 0, len(writerRegistry)) + for n := range writerRegistry { + t = append(t, n) + } + return t +} diff --git a/replication/local.go b/replication/local.go deleted file mode 100644 index 01eb644..0000000 --- a/replication/local.go +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2014 The Cayley Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package replication - -import ( - "sync" - "time" - - "github.com/google/cayley/graph" -) - -type Single struct { - nextID int64 - ts graph.TripleStore - mut sync.Mutex -} - -func NewSingleReplication(ts graph.TripleStore, opts graph.Options) (graph.TripleWriter, error) { - rep := &Single{nextID: ts.Horizon(), ts: ts} - if rep.nextID == -1 { - rep.nextID = 1 - } - return rep, nil -} - -func (s *Single) AcquireNextId() int64 { - s.mut.Lock() - defer s.mut.Unlock() - id := s.nextID - s.nextID += 1 - return id -} - -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) -} diff --git a/writer/single.go b/writer/single.go new file mode 100644 index 0000000..6687055 --- /dev/null +++ b/writer/single.go @@ -0,0 +1,84 @@ +// Copyright 2014 The Cayley Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package writer + +import ( + "sync" + "time" + + "github.com/google/cayley/graph" +) + +type Single struct { + nextID int64 + ts graph.TripleStore + mut sync.Mutex +} + +func NewSingleReplication(ts graph.TripleStore, opts graph.Options) (graph.TripleWriter, error) { + rep := &Single{nextID: ts.Horizon(), ts: ts} + if rep.nextID == -1 { + rep.nextID = 1 + } + return rep, nil +} + +func (s *Single) AcquireNextId() int64 { + s.mut.Lock() + defer s.mut.Unlock() + id := s.nextID + s.nextID += 1 + return id +} + +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) +}