Rewrite keys into concrete types, remove key package
This commit is contained in:
parent
969aa1a6c3
commit
cf3e286d15
9 changed files with 98 additions and 96 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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() {}
|
||||
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue