Rewrite keys into concrete types, remove key package

This commit is contained in:
Barak Michener 2015-02-21 15:51:25 -05:00
parent 969aa1a6c3
commit cf3e286d15
9 changed files with 98 additions and 96 deletions

View file

@ -29,7 +29,6 @@ import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator" "github.com/google/cayley/graph/iterator"
"github.com/google/cayley/keys"
"github.com/google/cayley/quad" "github.com/google/cayley/quad"
) )
@ -43,7 +42,6 @@ var (
} }
hashSize = sha1.Size hashSize = sha1.Size
localFillPercent = 0.7 localFillPercent = 0.7
) )
type Token struct { type Token struct {
@ -128,7 +126,7 @@ func (qs *QuadStore) Size() int64 {
} }
func (qs *QuadStore) Horizon() graph.PrimaryKey { func (qs *QuadStore) Horizon() graph.PrimaryKey {
return keys.NewSequentialKey(qs.horizon) return graph.NewSequentialKey(qs.horizon)
} }
func (qs *QuadStore) createDeltaKeyFor(id int64) []byte { 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 { for _, d := range deltas {
err := qs.buildQuadWrite(tx, d.Quad, d.ID.Int(), d.Action == graph.Add) err := qs.buildQuadWrite(tx, d.Quad, d.ID.Int(), d.Action == graph.Add)
if err != nil { if err != nil {
if err == graph.ErrQuadExists && ignoreOpts.IgnoreDup{ if err == graph.ErrQuadExists && ignoreOpts.IgnoreDup {
continue continue
} }
if err == graph.ErrQuadNotExist && ignoreOpts.IgnoreMissing{ if err == graph.ErrQuadNotExist && ignoreOpts.IgnoreMissing {
continue continue
} }
return err return err

View file

@ -16,7 +16,6 @@ package iterator
import ( import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
"github.com/google/cayley/keys"
"github.com/google/cayley/quad" "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) 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() {} func (qs *store) DebugPrint() {}

View file

@ -170,8 +170,8 @@ func TestLoadDatabase(t *testing.T) {
//Test horizon //Test horizon
horizon := qs.Horizon() horizon := qs.Horizon()
if horizon.Int() != 1 { if horizon.Int() != 0 {
t.Errorf("Unexpected horizon value, got:%d expect:1", horizon.Int()) t.Errorf("Unexpected horizon value, got:%d expect:0", horizon.Int())
} }
w.AddQuadSet(makeQuadSet()) w.AddQuadSet(makeQuadSet())
@ -182,8 +182,8 @@ func TestLoadDatabase(t *testing.T) {
t.Errorf("Unexpected quadstore size, got:%d expect:5", s) t.Errorf("Unexpected quadstore size, got:%d expect:5", s)
} }
horizon = qs.Horizon() horizon = qs.Horizon()
if horizon.Int() != 12 { if horizon.Int() != 11 {
t.Errorf("Unexpected horizon value, got:%d expect:12", horizon.Int()) t.Errorf("Unexpected horizon value, got:%d expect:11", horizon.Int())
} }
w.RemoveQuad(quad.Quad{ w.RemoveQuad(quad.Quad{

View file

@ -31,7 +31,6 @@ import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator" "github.com/google/cayley/graph/iterator"
"github.com/google/cayley/keys"
"github.com/google/cayley/quad" "github.com/google/cayley/quad"
) )
@ -49,7 +48,6 @@ var (
New: func() interface{} { return sha1.New() }, New: func() interface{} { return sha1.New() },
} }
hashSize = sha1.Size hashSize = sha1.Size
) )
type Token []byte type Token []byte
@ -137,7 +135,7 @@ func (qs *QuadStore) Size() int64 {
} }
func (qs *QuadStore) Horizon() graph.PrimaryKey { func (qs *QuadStore) Horizon() graph.PrimaryKey {
return keys.NewSequentialKey(qs.horizon) return graph.NewSequentialKey(qs.horizon)
} }
func hashOf(s string) []byte { func hashOf(s string) []byte {

View file

@ -24,7 +24,6 @@ import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator" "github.com/google/cayley/graph/iterator"
"github.com/google/cayley/graph/memstore/b" "github.com/google/cayley/graph/memstore/b"
"github.com/google/cayley/keys"
"github.com/google/cayley/quad" "github.com/google/cayley/quad"
) )
@ -106,12 +105,12 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta, ignoreOpts graph.IgnoreOp
switch d.Action { switch d.Action {
case graph.Add: case graph.Add:
err = qs.AddDelta(d) err = qs.AddDelta(d)
if err != nil && ignoreOpts.IgnoreDup{ if err != nil && ignoreOpts.IgnoreDup {
err = nil err = nil
} }
case graph.Delete: case graph.Delete:
err = qs.RemoveDelta(d) err = qs.RemoveDelta(d)
if err != nil && ignoreOpts.IgnoreMissing{ if err != nil && ignoreOpts.IgnoreMissing {
err = nil err = nil
} }
default: default:
@ -229,7 +228,7 @@ func (qs *QuadStore) QuadIterator(d quad.Direction, value graph.Value) graph.Ite
} }
func (qs *QuadStore) Horizon() graph.PrimaryKey { 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 { func (qs *QuadStore) Size() int64 {

View file

@ -27,7 +27,6 @@ import (
"github.com/barakmich/glog" "github.com/barakmich/glog"
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator" "github.com/google/cayley/graph/iterator"
"github.com/google/cayley/keys"
"github.com/google/cayley/quad" "github.com/google/cayley/quad"
) )
@ -228,7 +227,7 @@ func (qs *QuadStore) ApplyDeltas(in []graph.Delta, ignoreOpts graph.IgnoreOpts)
if qs.checkValid(key) { if qs.checkValid(key) {
if ignoreOpts.IgnoreDup { if ignoreOpts.IgnoreDup {
continue continue
}else{ } else {
return graph.ErrQuadExists return graph.ErrQuadExists
} }
} }
@ -236,7 +235,7 @@ func (qs *QuadStore) ApplyDeltas(in []graph.Delta, ignoreOpts graph.IgnoreOpts)
if !qs.checkValid(key) { if !qs.checkValid(key) {
if ignoreOpts.IgnoreMissing { if ignoreOpts.IgnoreMissing {
continue continue
}else{ } else {
return graph.ErrQuadNotExist return graph.ErrQuadNotExist
} }
} }
@ -334,11 +333,11 @@ func (qs *QuadStore) Horizon() graph.PrimaryKey {
err := qs.db.C("log").Find(nil).Sort("-LogID").One(&log) err := qs.db.C("log").Find(nil).Sort("-LogID").One(&log)
if err != nil { if err != nil {
if err == mgo.ErrNotFound { if err == mgo.ErrNotFound {
return keys.NewSequentialKey(0) return graph.NewSequentialKey(0)
} }
glog.Errorf("Could not get Horizon from Mongo: %v", err) 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 { func (qs *QuadStore) FixedIterator() graph.FixedIterator {

View file

@ -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"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -14,15 +14,75 @@
package graph package graph
import (
"encoding/json"
"strconv"
"sync"
)
// Defines the PrimaryKey interface, this abstracts the generation of IDs // Defines the PrimaryKey interface, this abstracts the generation of IDs
type PrimaryKey interface { type primaryKeyType uint8
// Returns a new unique primary key
Next() PrimaryKey
// Get the integer format if possible, otherwise logs an error and returns -1 const (
Int() int64 none primaryKeyType = iota
sequential
)
// Get the string format type PrimaryKey struct {
String() string 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)
} }

View file

@ -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)
}

View file

@ -26,32 +26,32 @@ func init() {
} }
type Single struct { type Single struct {
currentID graph.PrimaryKey currentID graph.PrimaryKey
qs graph.QuadStore qs graph.QuadStore
ignoreOpts graph.IgnoreOpts ignoreOpts graph.IgnoreOpts
} }
func NewSingleReplication(qs graph.QuadStore, opts graph.Options) (graph.QuadWriter, error) { func NewSingleReplication(qs graph.QuadStore, opts graph.Options) (graph.QuadWriter, error) {
var ignoreMissing, ignoreDuplicate bool var ignoreMissing, ignoreDuplicate bool
if *graph.IgnoreMissing{ if *graph.IgnoreMissing {
ignoreMissing = true ignoreMissing = true
}else{ } else {
ignoreMissing,_ = opts.BoolKey("ignore_missing") ignoreMissing, _ = opts.BoolKey("ignore_missing")
} }
if *graph.IgnoreDup{ if *graph.IgnoreDup {
ignoreDuplicate = true ignoreDuplicate = true
}else{ } else {
ignoreDuplicate,_ = opts.BoolKey("ignore_duplicate") ignoreDuplicate, _ = opts.BoolKey("ignore_duplicate")
} }
return &Single{ return &Single{
currentID: qs.Horizon(), currentID: qs.Horizon(),
qs: qs, qs: qs,
ignoreOpts: graph.IgnoreOpts{ ignoreOpts: graph.IgnoreOpts{
IgnoreDup: ignoreDuplicate, IgnoreDup: ignoreDuplicate,
IgnoreMissing:ignoreMissing, IgnoreMissing: ignoreMissing,
}, },
}, nil }, nil
} }
@ -77,7 +77,7 @@ func (s *Single) AddQuadSet(set []quad.Quad) error {
Timestamp: time.Now(), Timestamp: time.Now(),
} }
} }
return s.qs.ApplyDeltas(deltas, s.ignoreOpts) return s.qs.ApplyDeltas(deltas, s.ignoreOpts)
} }