Improve detecting document changes

Instead of computing a diff to detect whether the document has changed, the document is marked as ‘changed’ if the initial document is different from the current document after being updated, or after being deleted.
This commit is contained in:
Tobie Morgan Hitchcock 2019-02-01 00:20:16 +00:00
parent a00a7f17ef
commit f19a0f1744
5 changed files with 11 additions and 17 deletions

View file

@ -22,7 +22,6 @@ import (
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data"
"github.com/abcum/surreal/util/diff"
"github.com/abcum/surreal/util/indx"
"github.com/abcum/surreal/util/keys"
)
@ -36,6 +35,7 @@ type document struct {
doc *data.Doc
initial *data.Doc
current *data.Doc
changed bool
}
func newDocument(i *iterator, key *keys.Thing, val kvs.KV, doc *data.Doc) (d *document) {
@ -58,7 +58,7 @@ func (d *document) close() {
}
func (d *document) erase() (err error) {
d.current = data.Consume(nil)
d.changed, d.current = true, data.Consume(nil)
return
}
@ -205,13 +205,6 @@ func (d *document) forced(ctx context.Context) bool {
return false
}
func (d *document) changed(ctx context.Context) bool {
a, _ := d.initial.Data().(map[string]interface{})
b, _ := d.current.Data().(map[string]interface{})
c := diff.Diff(a, b)
return len(c) > 0
}
func (d *document) shouldDrop(ctx context.Context) (bool, error) {
// Check whether it is specified
@ -249,7 +242,7 @@ func (d *document) storeThing(ctx context.Context) (err error) {
// Check that the record has been
// changed, and if not, return.
if ok := d.changed(ctx); !ok {
if !d.changed {
return
}
@ -311,7 +304,7 @@ func (d *document) storeIndex(ctx context.Context) (err error) {
// Check that the rcord has been
// changed, and if not, return.
if !forced && !d.changed(ctx) {
if !forced && !d.changed {
return
}
@ -386,7 +379,7 @@ func (d *document) purgeIndex(ctx context.Context) (err error) {
// Check that the rcord has been
// changed, and if not, return.
if !forced && !d.changed(ctx) {
if !forced && !d.changed {
return
}

View file

@ -39,7 +39,7 @@ func (d *document) event(ctx context.Context, met method) (err error) {
// then there is no need to perform
// any registered events.
if !forced && !d.changed(ctx) {
if !forced && !d.changed {
return nil
}

View file

@ -34,7 +34,7 @@ func (d *document) lives(ctx context.Context, when method) (err error) {
// then there is no need to update
// any registered live queries.
if !forced && !d.changed(ctx) {
if !forced && !d.changed {
return nil
}

View file

@ -18,6 +18,7 @@ import (
"sort"
"context"
"reflect"
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/sql"
@ -74,6 +75,8 @@ func (d *document) merge(ctx context.Context, met method, data sql.Expr) (err er
return
}
d.changed = !reflect.DeepEqual(d.initial, d.current)
return
}
@ -287,8 +290,6 @@ func (d *document) mrgFld(ctx context.Context, met method) (err error) {
// using json, there is no specific type
// for a 'datetime' and 'record'.
// IMPORTANT remove this, and put it in SQL parser
d.current.Each(func(key string, val interface{}) (err error) {
if val, ok := conv.MightBe(val); ok {
d.current.Iff(val, key)

View file

@ -43,7 +43,7 @@ func (d *document) table(ctx context.Context, when method) (err error) {
// then there is no need to update
// any registered foreign tables.
if !forced && !d.changed(ctx) {
if !forced && !d.changed {
return nil
}