From 46f987ca51e1ee2ba8d05c625692a6e2a1037416 Mon Sep 17 00:00:00 2001 From: kortschak Date: Tue, 27 Jan 2015 08:39:50 +1030 Subject: [PATCH] Make graph.Delta zero state invalid for use Previously, an incorrectly initialised Delta (omission of Action) would result in an Add operation. Make that detectable and return an error. --- graph/bolt/quadstore.go | 4 ++++ graph/leveldb/quadstore.go | 3 +++ graph/memstore/quadstore.go | 8 ++++++-- graph/mongo/quadstore.go | 4 ++++ graph/quadwriter.go | 6 +++--- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/graph/bolt/quadstore.go b/graph/bolt/quadstore.go index 2a76d8e..98c2a86 100644 --- a/graph/bolt/quadstore.go +++ b/graph/bolt/quadstore.go @@ -19,6 +19,7 @@ import ( "crypto/sha1" "encoding/binary" "encoding/json" + "errors" "fmt" "hash" "sync" @@ -192,6 +193,9 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error { resizeMap := make(map[string]int64) sizeChange := int64(0) for _, d := range deltas { + if d.Action != graph.Add && d.Action != graph.Delete { + return errors.New("bolt: invalid action") + } bytes, err := json.Marshal(d) if err != nil { return err diff --git a/graph/leveldb/quadstore.go b/graph/leveldb/quadstore.go index f335f35..a5cfb35 100644 --- a/graph/leveldb/quadstore.go +++ b/graph/leveldb/quadstore.go @@ -185,6 +185,9 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error { resizeMap := make(map[string]int64) sizeChange := int64(0) for _, d := range deltas { + if d.Action != graph.Add && d.Action != graph.Delete { + return errors.New("leveldb: invalid action") + } bytes, err := json.Marshal(d) if err != nil { return err diff --git a/graph/memstore/quadstore.go b/graph/memstore/quadstore.go index bd7581c..6b11e4e 100644 --- a/graph/memstore/quadstore.go +++ b/graph/memstore/quadstore.go @@ -15,6 +15,7 @@ package memstore import ( + "errors" "fmt" "time" @@ -102,10 +103,13 @@ func newQuadStore() *QuadStore { func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error { for _, d := range deltas { var err error - if d.Action == graph.Add { + switch d.Action { + case graph.Add: err = qs.AddDelta(d) - } else { + case graph.Delete: err = qs.RemoveDelta(d) + default: + err = errors.New("memstore: invalid action") } if err != nil { return err diff --git a/graph/mongo/quadstore.go b/graph/mongo/quadstore.go index 9d7af09..f2bcf51 100644 --- a/graph/mongo/quadstore.go +++ b/graph/mongo/quadstore.go @@ -17,6 +17,7 @@ package mongo import ( "crypto/sha1" "encoding/hex" + "errors" "hash" "sync" @@ -218,6 +219,9 @@ func (qs *QuadStore) ApplyDeltas(in []graph.Delta) error { ids := make(map[string]int) // Pre-check the existence condition. for _, d := range in { + if d.Action != graph.Add && d.Action != graph.Delete { + return errors.New("mongo: invalid action") + } key := qs.getIDForQuad(d.Quad) switch d.Action { case graph.Add: diff --git a/graph/quadwriter.go b/graph/quadwriter.go index 5e95539..5124d60 100644 --- a/graph/quadwriter.go +++ b/graph/quadwriter.go @@ -28,12 +28,12 @@ import ( "github.com/google/cayley/quad" ) -type Procedure byte +type Procedure int8 // The different types of actions a transaction can do. const ( - Add Procedure = iota - Delete + Add Procedure = +1 + Delete Procedure = -1 ) type Delta struct {