Improve speed of document change detection

This commit is contained in:
Tobie Morgan Hitchcock 2019-06-17 07:00:32 +01:00
parent a1b6d24772
commit 304c0458e8
2 changed files with 30 additions and 2 deletions

View file

@ -18,7 +18,6 @@ import (
"fmt" "fmt"
"context" "context"
"reflect"
"github.com/abcum/surreal/kvs" "github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
@ -207,7 +206,7 @@ func (d *document) forced(ctx context.Context) bool {
} }
func (d *document) hasChanged(ctx context.Context) bool { func (d *document) hasChanged(ctx context.Context) bool {
return reflect.DeepEqual(d.initial, d.current) == false return d.initial.Same(d.current) == false
} }
func (d *document) shouldDrop(ctx context.Context) (bool, error) { func (d *document) shouldDrop(ctx context.Context) (bool, error) {

View file

@ -913,6 +913,35 @@ func (d *Doc) Dec(value interface{}, path ...string) (*Doc, error) {
// -------------------------------------------------------------------------------- // --------------------------------------------------------------------------------
func (d *Doc) Same(n *Doc) bool {
switch a := d.data.(type) {
case []interface{}:
switch b := n.data.(type) {
case map[string]interface{}:
return false
case []interface{}:
if len(a) != len(b) {
return false
}
break
}
case map[string]interface{}:
switch b := n.data.(type) {
case []interface{}:
return false
case map[string]interface{}:
if len(a) != len(b) {
return false
}
break
}
}
return reflect.DeepEqual(d, n)
}
func (d *Doc) Diff(n *Doc) map[string]interface{} { func (d *Doc) Diff(n *Doc) map[string]interface{} {
var initial = make(map[string]interface{}) var initial = make(map[string]interface{})