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.
This commit is contained in:
kortschak 2015-01-27 08:39:50 +10:30
parent 9088fe376b
commit 46f987ca51
5 changed files with 20 additions and 5 deletions

View file

@ -19,6 +19,7 @@ import (
"crypto/sha1" "crypto/sha1"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"hash" "hash"
"sync" "sync"
@ -192,6 +193,9 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error {
resizeMap := make(map[string]int64) resizeMap := make(map[string]int64)
sizeChange := int64(0) sizeChange := int64(0)
for _, d := range deltas { for _, d := range deltas {
if d.Action != graph.Add && d.Action != graph.Delete {
return errors.New("bolt: invalid action")
}
bytes, err := json.Marshal(d) bytes, err := json.Marshal(d)
if err != nil { if err != nil {
return err return err

View file

@ -185,6 +185,9 @@ func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error {
resizeMap := make(map[string]int64) resizeMap := make(map[string]int64)
sizeChange := int64(0) sizeChange := int64(0)
for _, d := range deltas { for _, d := range deltas {
if d.Action != graph.Add && d.Action != graph.Delete {
return errors.New("leveldb: invalid action")
}
bytes, err := json.Marshal(d) bytes, err := json.Marshal(d)
if err != nil { if err != nil {
return err return err

View file

@ -15,6 +15,7 @@
package memstore package memstore
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
@ -102,10 +103,13 @@ func newQuadStore() *QuadStore {
func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error { func (qs *QuadStore) ApplyDeltas(deltas []graph.Delta) error {
for _, d := range deltas { for _, d := range deltas {
var err error var err error
if d.Action == graph.Add { switch d.Action {
case graph.Add:
err = qs.AddDelta(d) err = qs.AddDelta(d)
} else { case graph.Delete:
err = qs.RemoveDelta(d) err = qs.RemoveDelta(d)
default:
err = errors.New("memstore: invalid action")
} }
if err != nil { if err != nil {
return err return err

View file

@ -17,6 +17,7 @@ package mongo
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"errors"
"hash" "hash"
"sync" "sync"
@ -218,6 +219,9 @@ func (qs *QuadStore) ApplyDeltas(in []graph.Delta) error {
ids := make(map[string]int) ids := make(map[string]int)
// Pre-check the existence condition. // Pre-check the existence condition.
for _, d := range in { for _, d := range in {
if d.Action != graph.Add && d.Action != graph.Delete {
return errors.New("mongo: invalid action")
}
key := qs.getIDForQuad(d.Quad) key := qs.getIDForQuad(d.Quad)
switch d.Action { switch d.Action {
case graph.Add: case graph.Add:

View file

@ -28,12 +28,12 @@ import (
"github.com/google/cayley/quad" "github.com/google/cayley/quad"
) )
type Procedure byte type Procedure int8
// The different types of actions a transaction can do. // The different types of actions a transaction can do.
const ( const (
Add Procedure = iota Add Procedure = +1
Delete Delete Procedure = -1
) )
type Delta struct { type Delta struct {