Primary key refactoring re. Un/Marshaling of JSON and the unique keys, plus the ApplyDelta() and 'defensive' changes introduced by Kortshack, also did some tidying up.

This commit is contained in:
panamafrancis 2015-02-25 10:40:27 +01:00
parent 299cca54cf
commit 55c1fe9e57
6 changed files with 186 additions and 51 deletions

View file

@ -15,46 +15,64 @@
package graph
import (
"code.google.com/p/go-uuid/uuid"
"encoding/json"
"errors"
"github.com/barakmich/glog"
"strconv"
"sync"
)
// Defines the PrimaryKey interface, this abstracts the generation of IDs
type primaryKeyType uint8
const (
none primaryKeyType = iota
sequential
unique
)
type PrimaryKey struct {
keyType primaryKeyType
KeyType primaryKeyType
mut sync.Mutex
sequentialID int64
SequentialID int64
UniqueID string
}
func NewSequentialKey(horizon int64) PrimaryKey {
return PrimaryKey{
keyType: sequential,
sequentialID: horizon,
KeyType: sequential,
SequentialID: horizon,
}
}
func NewUniqueKey(horizon string) PrimaryKey {
id := uuid.Parse(horizon)
if id == nil {
id = uuid.NewUUID()
}
return PrimaryKey{
KeyType: unique,
UniqueID: id.String(),
}
}
func (p *PrimaryKey) Next() PrimaryKey {
switch p.keyType {
switch p.KeyType {
case sequential:
p.mut.Lock()
defer p.mut.Unlock()
p.sequentialID++
if p.sequentialID <= 0 {
p.sequentialID = 1
p.SequentialID++
if p.SequentialID <= 0 {
p.SequentialID = 1
}
return PrimaryKey{
keyType: sequential,
sequentialID: p.sequentialID,
KeyType: sequential,
SequentialID: p.SequentialID,
}
case unique:
id := uuid.NewUUID()
p.UniqueID = id.String()
return *p
case none:
panic("Calling next() on a none PrimaryKey")
}
@ -62,27 +80,50 @@ func (p *PrimaryKey) Next() PrimaryKey {
}
func (p *PrimaryKey) Int() int64 {
switch p.keyType {
switch p.KeyType {
case sequential:
return p.sequentialID
return p.SequentialID
case unique:
glog.Fatal("UUID cannot be cast to an int64")
return -1
}
return -1
}
func (p *PrimaryKey) String() string {
// More options for more keyTypes
return strconv.FormatInt(p.sequentialID, 10)
switch p.KeyType {
case sequential:
return strconv.FormatInt(p.SequentialID, 10)
case unique:
return p.UniqueID
case none:
panic("Calling String() on a none PrimaryKey")
}
return ""
}
func (p *PrimaryKey) MarshalJSON() ([]byte, error) {
return json.Marshal(p.sequentialID)
if p.KeyType == none {
return nil, errors.New("Cannot marshal PrimaryKey with KeyType of 'none'")
}
return json.Marshal(*p)
}
//To avoid recursion in the implmentation of the UnmarshalJSON interface below
type primaryKey PrimaryKey
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)
temp := primaryKey{}
if err := json.Unmarshal(bytes, &temp); err != nil {
return err
}
*p = (PrimaryKey)(temp)
if p.KeyType == none {
return errors.New("Could not properly unmarshal primary key, 'none' keytype detected")
}
return nil
}