From f19a0f174476639c3c9dc994d094fefad93fedbd Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 1 Feb 2019 00:20:16 +0000 Subject: [PATCH] Improve detecting document changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- db/document.go | 17 +++++------------ db/event.go | 2 +- db/lives.go | 2 +- db/merge.go | 5 +++-- db/table.go | 2 +- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/db/document.go b/db/document.go index a9b905f1..67d4dcc5 100644 --- a/db/document.go +++ b/db/document.go @@ -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 } diff --git a/db/event.go b/db/event.go index 7cf30780..19921e4c 100644 --- a/db/event.go +++ b/db/event.go @@ -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 } diff --git a/db/lives.go b/db/lives.go index cae04fa6..20f3b857 100644 --- a/db/lives.go +++ b/db/lives.go @@ -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 } diff --git a/db/merge.go b/db/merge.go index d0ed585e..7311896e 100644 --- a/db/merge.go +++ b/db/merge.go @@ -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) diff --git a/db/table.go b/db/table.go index 9b6063ec..89ff7a78 100644 --- a/db/table.go +++ b/db/table.go @@ -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 }