Merge pull request #217 from barakmich/primarykey

Rewrite keys into concrete types, remove key package
This commit is contained in:
Barak Michener 2015-02-21 16:37:55 -05:00
commit 2c74cb1657
9 changed files with 96 additions and 93 deletions

View file

@ -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"
)
@ -127,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 {

View file

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

View file

@ -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{

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

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");
// 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)
}