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/kvs"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data" "github.com/abcum/surreal/util/data"
"github.com/abcum/surreal/util/diff"
"github.com/abcum/surreal/util/indx" "github.com/abcum/surreal/util/indx"
"github.com/abcum/surreal/util/keys" "github.com/abcum/surreal/util/keys"
) )
@ -36,6 +35,7 @@ type document struct {
doc *data.Doc doc *data.Doc
initial *data.Doc initial *data.Doc
current *data.Doc current *data.Doc
changed bool
} }
func newDocument(i *iterator, key *keys.Thing, val kvs.KV, doc *data.Doc) (d *document) { 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) { func (d *document) erase() (err error) {
d.current = data.Consume(nil) d.changed, d.current = true, data.Consume(nil)
return return
} }
@ -205,13 +205,6 @@ func (d *document) forced(ctx context.Context) bool {
return false 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) { func (d *document) shouldDrop(ctx context.Context) (bool, error) {
// Check whether it is specified // Check whether it is specified
@ -249,7 +242,7 @@ func (d *document) storeThing(ctx context.Context) (err error) {
// Check that the record has been // Check that the record has been
// changed, and if not, return. // changed, and if not, return.
if ok := d.changed(ctx); !ok { if !d.changed {
return return
} }
@ -311,7 +304,7 @@ func (d *document) storeIndex(ctx context.Context) (err error) {
// Check that the rcord has been // Check that the rcord has been
// changed, and if not, return. // changed, and if not, return.
if !forced && !d.changed(ctx) { if !forced && !d.changed {
return return
} }
@ -386,7 +379,7 @@ func (d *document) purgeIndex(ctx context.Context) (err error) {
// Check that the rcord has been // Check that the rcord has been
// changed, and if not, return. // changed, and if not, return.
if !forced && !d.changed(ctx) { if !forced && !d.changed {
return 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 // then there is no need to perform
// any registered events. // any registered events.
if !forced && !d.changed(ctx) { if !forced && !d.changed {
return nil 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 // then there is no need to update
// any registered live queries. // any registered live queries.
if !forced && !d.changed(ctx) { if !forced && !d.changed {
return nil return nil
} }

View file

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