diff --git a/graph/bolt/quadstore.go b/graph/bolt/quadstore.go index 265f948..14f2102 100644 --- a/graph/bolt/quadstore.go +++ b/graph/bolt/quadstore.go @@ -29,7 +29,6 @@ import ( "github.com/google/cayley/graph" "github.com/google/cayley/graph/iterator" - "github.com/google/cayley/keys" "github.com/google/cayley/quad" ) @@ -43,7 +42,6 @@ var ( } hashSize = sha1.Size localFillPercent = 0.7 - ) type Token struct { @@ -128,7 +126,7 @@ func (qs *QuadStore) Size() int64 { } func (qs *QuadStore) Horizon() graph.PrimaryKey { - return keys.NewSequentialKey(qs.horizon) + return graph.NewSequentialKey(qs.horizon) } func (qs *QuadStore) createDeltaKeyFor(id int64) []byte { @@ -209,10 +207,10 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta, ignoreOpts graph.IgnoreOp for _, d := range deltas { err := qs.buildQuadWrite(tx, d.Quad, d.ID.Int(), d.Action == graph.Add) if err != nil { - if err == graph.ErrQuadExists && ignoreOpts.IgnoreDup{ + if err == graph.ErrQuadExists && ignoreOpts.IgnoreDup { continue } - if err == graph.ErrQuadNotExist && ignoreOpts.IgnoreMissing{ + if err == graph.ErrQuadNotExist && ignoreOpts.IgnoreMissing { continue } return err diff --git a/graph/iterator/mock_ts_test.go b/graph/iterator/mock_ts_test.go index 6e2b00d..59e3bdf 100644 --- a/graph/iterator/mock_ts_test.go +++ b/graph/iterator/mock_ts_test.go @@ -16,7 +16,6 @@ package iterator import ( "github.com/google/cayley/graph" - "github.com/google/cayley/keys" "github.com/google/cayley/quad" ) @@ -57,7 +56,7 @@ func (qs *store) NameOf(v graph.Value) string { func (qs *store) Size() int64 { return 0 } -func (qs *store) Horizon() graph.PrimaryKey { return keys.NewSequentialKey(0) } +func (qs *store) Horizon() graph.PrimaryKey { return graph.NewSequentialKey(0) } func (qs *store) DebugPrint() {} diff --git a/graph/leveldb/leveldb_test.go b/graph/leveldb/leveldb_test.go index 0711f25..292b7dd 100644 --- a/graph/leveldb/leveldb_test.go +++ b/graph/leveldb/leveldb_test.go @@ -170,8 +170,8 @@ func TestLoadDatabase(t *testing.T) { //Test horizon horizon := qs.Horizon() - if horizon.Int() != 1 { - t.Errorf("Unexpected horizon value, got:%d expect:1", horizon.Int()) + if horizon.Int() != 0 { + t.Errorf("Unexpected horizon value, got:%d expect:0", horizon.Int()) } w.AddQuadSet(makeQuadSet()) @@ -182,8 +182,8 @@ func TestLoadDatabase(t *testing.T) { t.Errorf("Unexpected quadstore size, got:%d expect:5", s) } horizon = qs.Horizon() - if horizon.Int() != 12 { - t.Errorf("Unexpected horizon value, got:%d expect:12", horizon.Int()) + if horizon.Int() != 11 { + t.Errorf("Unexpected horizon value, got:%d expect:11", horizon.Int()) } w.RemoveQuad(quad.Quad{ diff --git a/graph/leveldb/quadstore.go b/graph/leveldb/quadstore.go index 0a7fe99..9f04b3d 100644 --- a/graph/leveldb/quadstore.go +++ b/graph/leveldb/quadstore.go @@ -31,7 +31,6 @@ import ( "github.com/google/cayley/graph" "github.com/google/cayley/graph/iterator" - "github.com/google/cayley/keys" "github.com/google/cayley/quad" ) @@ -49,7 +48,6 @@ var ( New: func() interface{} { return sha1.New() }, } hashSize = sha1.Size - ) type Token []byte @@ -137,7 +135,7 @@ func (qs *QuadStore) Size() int64 { } func (qs *QuadStore) Horizon() graph.PrimaryKey { - return keys.NewSequentialKey(qs.horizon) + return graph.NewSequentialKey(qs.horizon) } func hashOf(s string) []byte { diff --git a/graph/memstore/quadstore.go b/graph/memstore/quadstore.go index 4a62ec5..8e8f8c4 100644 --- a/graph/memstore/quadstore.go +++ b/graph/memstore/quadstore.go @@ -24,7 +24,6 @@ import ( "github.com/google/cayley/graph" "github.com/google/cayley/graph/iterator" "github.com/google/cayley/graph/memstore/b" - "github.com/google/cayley/keys" "github.com/google/cayley/quad" ) @@ -106,12 +105,12 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta, ignoreOpts graph.IgnoreOp switch d.Action { case graph.Add: err = qs.AddDelta(d) - if err != nil && ignoreOpts.IgnoreDup{ + if err != nil && ignoreOpts.IgnoreDup { err = nil } case graph.Delete: err = qs.RemoveDelta(d) - if err != nil && ignoreOpts.IgnoreMissing{ + if err != nil && ignoreOpts.IgnoreMissing { err = nil } default: @@ -229,7 +228,7 @@ func (qs *QuadStore) QuadIterator(d quad.Direction, value graph.Value) graph.Ite } func (qs *QuadStore) Horizon() graph.PrimaryKey { - return keys.NewSequentialKey(qs.log[len(qs.log)-1].ID) + return graph.NewSequentialKey(qs.log[len(qs.log)-1].ID) } func (qs *QuadStore) Size() int64 { diff --git a/graph/mongo/quadstore.go b/graph/mongo/quadstore.go index 98a5cc3..636e1d1 100644 --- a/graph/mongo/quadstore.go +++ b/graph/mongo/quadstore.go @@ -27,7 +27,6 @@ import ( "github.com/barakmich/glog" "github.com/google/cayley/graph" "github.com/google/cayley/graph/iterator" - "github.com/google/cayley/keys" "github.com/google/cayley/quad" ) @@ -228,7 +227,7 @@ func (qs *QuadStore) ApplyDeltas(in []graph.Delta, ignoreOpts graph.IgnoreOpts) if qs.checkValid(key) { if ignoreOpts.IgnoreDup { continue - }else{ + } else { return graph.ErrQuadExists } } @@ -236,7 +235,7 @@ func (qs *QuadStore) ApplyDeltas(in []graph.Delta, ignoreOpts graph.IgnoreOpts) if !qs.checkValid(key) { if ignoreOpts.IgnoreMissing { continue - }else{ + } else { return graph.ErrQuadNotExist } } @@ -334,11 +333,11 @@ func (qs *QuadStore) Horizon() graph.PrimaryKey { err := qs.db.C("log").Find(nil).Sort("-LogID").One(&log) if err != nil { if err == mgo.ErrNotFound { - return keys.NewSequentialKey(0) + return graph.NewSequentialKey(0) } glog.Errorf("Could not get Horizon from Mongo: %v", err) } - return keys.NewSequentialKey(log.LogID) + return graph.NewSequentialKey(log.LogID) } func (qs *QuadStore) FixedIterator() graph.FixedIterator { diff --git a/graph/primarykey.go b/graph/primarykey.go index 25d97ba..3ab0260 100644 --- a/graph/primarykey.go +++ b/graph/primarykey.go @@ -1,4 +1,4 @@ -// Copyright 2014 The Cayley Authors. All rights reserved. +// Copyright 2015 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. @@ -14,15 +14,75 @@ package graph +import ( + "encoding/json" + "strconv" + "sync" +) + // Defines the PrimaryKey interface, this abstracts the generation of IDs -type PrimaryKey interface { - // Returns a new unique primary key - Next() PrimaryKey +type primaryKeyType uint8 - // Get the integer format if possible, otherwise logs an error and returns -1 - Int() int64 +const ( + none primaryKeyType = iota + sequential +) - // Get the string format - String() string +type PrimaryKey struct { + keyType primaryKeyType + mut sync.Mutex + sequentialID int64 +} + +func NewSequentialKey(horizon int64) PrimaryKey { + return PrimaryKey{ + keyType: sequential, + sequentialID: horizon, + } +} + +func (p *PrimaryKey) Next() PrimaryKey { + switch p.keyType { + case sequential: + p.mut.Lock() + defer p.mut.Unlock() + p.sequentialID++ + if p.sequentialID <= 0 { + p.sequentialID = 1 + } + return PrimaryKey{ + keyType: sequential, + sequentialID: p.sequentialID, + } + case none: + panic("Calling next() on a none PrimaryKey") + } + return PrimaryKey{} +} + +func (p *PrimaryKey) Int() int64 { + switch p.keyType { + case sequential: + return p.sequentialID + } + return -1 +} + +func (p *PrimaryKey) String() string { + // More options for more keyTypes + return strconv.FormatInt(p.sequentialID, 10) +} + +func (p *PrimaryKey) MarshalJSON() ([]byte, error) { + return json.Marshal(p.sequentialID) +} + +func (p *PrimaryKey) UnmarshalJSON(bytes []byte) error { + /* Careful special casing here. For example, string-related things might begin + if bytes[0] == '"' { + } + */ + p.keyType = sequential + return json.Unmarshal(bytes, &p.sequentialID) } diff --git a/keys/sequentialkey.go b/keys/sequentialkey.go deleted file mode 100644 index 286a61e..0000000 --- a/keys/sequentialkey.go +++ /dev/null @@ -1,51 +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 keys - -import ( - "github.com/google/cayley/graph" - "strconv" - "sync" -) - -type Sequential struct { - nextID int64 - mut sync.Mutex -} - -func NewSequentialKey(horizon int64) graph.PrimaryKey { - if horizon <= 0 { - horizon = 1 - } - return &Sequential{nextID: horizon} -} - -func (s *Sequential) Next() graph.PrimaryKey { - s.mut.Lock() - defer s.mut.Unlock() - s.nextID++ - if s.nextID <= 0 { - s.nextID = 1 - } - return s -} - -func (s *Sequential) Int() int64 { - return s.nextID -} - -func (s *Sequential) String() string { - return strconv.FormatInt(s.nextID, 10) -} diff --git a/writer/single.go b/writer/single.go index 4c386b8..53d46fa 100644 --- a/writer/single.go +++ b/writer/single.go @@ -26,32 +26,32 @@ func init() { } type Single struct { - currentID graph.PrimaryKey - qs graph.QuadStore - ignoreOpts graph.IgnoreOpts + currentID graph.PrimaryKey + qs graph.QuadStore + ignoreOpts graph.IgnoreOpts } func NewSingleReplication(qs graph.QuadStore, opts graph.Options) (graph.QuadWriter, error) { var ignoreMissing, ignoreDuplicate bool - if *graph.IgnoreMissing{ + if *graph.IgnoreMissing { ignoreMissing = true - }else{ - ignoreMissing,_ = opts.BoolKey("ignore_missing") + } else { + ignoreMissing, _ = opts.BoolKey("ignore_missing") } - if *graph.IgnoreDup{ + if *graph.IgnoreDup { ignoreDuplicate = true - }else{ - ignoreDuplicate,_ = opts.BoolKey("ignore_duplicate") + } else { + ignoreDuplicate, _ = opts.BoolKey("ignore_duplicate") } return &Single{ - currentID: qs.Horizon(), - qs: qs, + currentID: qs.Horizon(), + qs: qs, ignoreOpts: graph.IgnoreOpts{ - IgnoreDup: ignoreDuplicate, - IgnoreMissing:ignoreMissing, + IgnoreDup: ignoreDuplicate, + IgnoreMissing: ignoreMissing, }, }, nil } @@ -77,7 +77,7 @@ func (s *Single) AddQuadSet(set []quad.Quad) error { Timestamp: time.Now(), } } - + return s.qs.ApplyDeltas(deltas, s.ignoreOpts) }