From 768ca5c36f3956b8de43dda36f1b724b06f61320 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Tue, 22 Jul 2014 21:26:30 -0400 Subject: [PATCH] add replication registry and local replication --- graph/replication.go | 34 +++++++++++++++++++++++++++++++--- replication/local.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 replication/local.go diff --git a/graph/replication.go b/graph/replication.go index 57a4b2e..ab86ffe 100644 --- a/graph/replication.go +++ b/graph/replication.go @@ -21,6 +21,10 @@ package graph // instances. The simplest case is to keep an append-only log of triple // changes. +import ( + "errors" +) + type Procedure byte const ( @@ -47,7 +51,31 @@ type Replication interface { // Attempt to acquire the given range of triples from other replicated sources. RequestTransactionRange(start int64, end int64) - - // Opens the replication interface. - Open(TripleStore, Options) +} + +type NewReplicationFunc func(TripleStore, Options) (Replication, error) + +var replicationRegistry = make(map[string]NewReplicationFunc) + +func RegisterReplication(name string, newFunc NewReplicationFunc) { + if _, found := replicationRegistry[name]; found { + panic("already registered Replication " + name) + } + replicationRegistry[name] = newFunc +} + +func NewReplication(name string, ts TripleStore, opts Options) (Replication, error) { + newFunc, hasNew := replicationRegistry[name] + if !hasNew { + return nil, errors.New("replication: name '" + name + "' is not registered") + } + return newFunc(ts, opts) +} + +func ReplicationMethods() []string { + t := make([]string, 0, len(replicationRegistry)) + for n := range replicationRegistry { + t = append(t, n) + } + return t } diff --git a/replication/local.go b/replication/local.go new file mode 100644 index 0000000..0749d55 --- /dev/null +++ b/replication/local.go @@ -0,0 +1,52 @@ +// 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 local + +import ( + "github.com/google/cayley/graph" + "sync" +) + +type LocalReplication struct { + lastId int64 + ts graph.TripleStore + mut sync.Mutex +} + +func NewLocalReplication(ts graph.TripleStore, opts graph.Options) (*LocalReplication, error) { + rep := &LocalReplication{lastId: ts.Horizon(), ts: ts} + ts.SetReplication(rep) + return rep, nil +} + +func (l *LocalReplication) AcquireNextIds(size int64) (start int64, end int64) { + l.mut.Lock() + defer l.mut.Unlock() + start = l.lastId + 1 + end = l.lastId + size + l.lastId += size + return +} + +func (l *LocalReplication) GetLastId() int64 { + return l.lastId +} + +func (l *LocalReplication) Replicate([]*graph.Transaction) {} +func (l *LocalReplication) RequestTransactionRange(int64, int64) {} + +func init() { + graph.RegisterReplication("local", NewLocalReplication) +}