database: Ensure that quads in a tx are applied in the desired order.
This commit is contained in:
parent
635f4ec76f
commit
8aacc8bfdc
2
Godeps/Godeps.json
generated
2
Godeps/Godeps.json
generated
@ -42,7 +42,7 @@
|
|||||||
{
|
{
|
||||||
"ImportPath": "github.com/google/cayley",
|
"ImportPath": "github.com/google/cayley",
|
||||||
"Comment": "v0.4.1-160-gcdf0154",
|
"Comment": "v0.4.1-160-gcdf0154",
|
||||||
"Rev": "6027e4c48b3385307bd4bea462149fd544577165"
|
"Rev": "cfbc0b364910ab69bbced5d2f7913ee7e80a0c80"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/julienschmidt/httprouter",
|
"ImportPath": "github.com/julienschmidt/httprouter",
|
||||||
|
61
vendor/github.com/google/cayley/graph/transaction.go
generated
vendored
61
vendor/github.com/google/cayley/graph/transaction.go
generated
vendored
@ -18,32 +18,28 @@ import "github.com/google/cayley/quad"
|
|||||||
|
|
||||||
// Transaction stores a bunch of Deltas to apply atomatically on the database.
|
// Transaction stores a bunch of Deltas to apply atomatically on the database.
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
Deltas map[Delta]struct{}
|
// Deltas stores the deltas in the right order
|
||||||
|
Deltas []Delta
|
||||||
|
// deltas stores the deltas in a map to avoid duplications
|
||||||
|
deltas map[Delta]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransaction initialize a new transaction.
|
// NewTransaction initialize a new transaction.
|
||||||
func NewTransaction() *Transaction {
|
func NewTransaction() *Transaction {
|
||||||
return &Transaction{Deltas: make(map[Delta]struct{}, 100)}
|
return &Transaction{Deltas: make([]Delta, 0, 10), deltas: make(map[Delta]struct{}, 10)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddQuad adds a new quad to the transaction if it is not already present in it.
|
// AddQuad adds a new quad to the transaction if it is not already present in it.
|
||||||
// If there is a 'remove' delta for that quad, it will remove that delta from
|
// If there is a 'remove' delta for that quad, it will remove that delta from
|
||||||
// the transaction instead of actually addind the quad.
|
// the transaction instead of actually addind the quad.
|
||||||
func (t *Transaction) AddQuad(q quad.Quad) {
|
func (t *Transaction) AddQuad(q quad.Quad) {
|
||||||
ad := Delta{
|
ad, rd := createDeltas(q)
|
||||||
Quad: q,
|
|
||||||
Action: Add,
|
|
||||||
}
|
|
||||||
rd := Delta{
|
|
||||||
Quad: q,
|
|
||||||
Action: Delete,
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, adExists := t.Deltas[ad]; !adExists {
|
if _, adExists := t.deltas[ad]; !adExists {
|
||||||
if _, rdExists := t.Deltas[rd]; rdExists {
|
if _, rdExists := t.deltas[rd]; rdExists {
|
||||||
delete(t.Deltas, rd)
|
t.deleteDelta(rd)
|
||||||
} else {
|
} else {
|
||||||
t.Deltas[ad] = struct{}{}
|
t.addDelta(ad)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -52,14 +48,41 @@ func (t *Transaction) AddQuad(q quad.Quad) {
|
|||||||
// The quad will be removed from the database if it is not present in the
|
// The quad will be removed from the database if it is not present in the
|
||||||
// transaction, otherwise it simply remove it from the transaction.
|
// transaction, otherwise it simply remove it from the transaction.
|
||||||
func (t *Transaction) RemoveQuad(q quad.Quad) {
|
func (t *Transaction) RemoveQuad(q quad.Quad) {
|
||||||
ad := Delta{
|
ad, rd := createDeltas(q)
|
||||||
|
|
||||||
|
if _, adExists := t.deltas[ad]; adExists {
|
||||||
|
t.deleteDelta(ad)
|
||||||
|
} else {
|
||||||
|
if _, rdExists := t.deltas[rd]; !rdExists {
|
||||||
|
t.addDelta(rd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDeltas(q quad.Quad) (ad, rd Delta) {
|
||||||
|
ad = Delta{
|
||||||
Quad: q,
|
Quad: q,
|
||||||
Action: Add,
|
Action: Add,
|
||||||
}
|
}
|
||||||
|
rd = Delta{
|
||||||
|
Quad: q,
|
||||||
|
Action: Delete,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if _, adExists := t.Deltas[ad]; adExists {
|
func (t *Transaction) addDelta(d Delta) {
|
||||||
delete(t.Deltas, ad)
|
t.Deltas = append(t.Deltas, d)
|
||||||
} else {
|
t.deltas[d] = struct{}{}
|
||||||
t.Deltas[Delta{Quad: q, Action: Delete}] = struct{}{}
|
}
|
||||||
|
|
||||||
|
func (t *Transaction) deleteDelta(d Delta) {
|
||||||
|
delete(t.deltas, d)
|
||||||
|
|
||||||
|
for i, id := range t.Deltas {
|
||||||
|
if id == d {
|
||||||
|
t.Deltas = append(t.Deltas[:i], t.Deltas[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
vendor/github.com/google/cayley/writer/single.go
generated
vendored
10
vendor/github.com/google/cayley/writer/single.go
generated
vendored
@ -109,11 +109,9 @@ func (s *Single) Close() error {
|
|||||||
|
|
||||||
func (s *Single) ApplyTransaction(t *graph.Transaction) error {
|
func (s *Single) ApplyTransaction(t *graph.Transaction) error {
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
deltas := make([]graph.Delta, 0, len(t.Deltas))
|
for i := 0; i < len(t.Deltas); i++ {
|
||||||
for d := range t.Deltas {
|
t.Deltas[i].ID = s.currentID.Next()
|
||||||
d.ID = s.currentID.Next()
|
t.Deltas[i].Timestamp = ts
|
||||||
d.Timestamp = ts
|
|
||||||
deltas = append(deltas, d)
|
|
||||||
}
|
}
|
||||||
return s.qs.ApplyDeltas(deltas, s.ignoreOpts)
|
return s.qs.ApplyDeltas(t.Deltas, s.ignoreOpts)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user