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/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

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

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

@ -34,16 +34,16 @@ type Single struct {
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{
@ -51,7 +51,7 @@ func NewSingleReplication(qs graph.QuadStore, opts graph.Options) (graph.QuadWri
qs: qs,
ignoreOpts: graph.IgnoreOpts{
IgnoreDup: ignoreDuplicate,
IgnoreMissing:ignoreMissing,
IgnoreMissing: ignoreMissing,
},
}, nil
}