From 672d298e7ebfef127771cecb6de0f051b6032f11 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 25 Apr 2018 00:00:36 +0100 Subject: [PATCH] Make db package more efficient --- db/db.go | 7 +------ db/document.go | 14 +++++++------- db/lives.go | 12 ++++++------ db/yield.go | 40 ++++++++++++++++++++-------------------- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/db/db.go b/db/db.go index 10a93418..c8e7a2d7 100644 --- a/db/db.go +++ b/db/db.go @@ -255,12 +255,7 @@ func Process(fib *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o if !open { return } - out = append(out, &Response{ - Time: res.Time, - Status: res.Status, - Detail: res.Detail, - Result: res.Result, - }) + out = append(out, res) } } diff --git a/db/document.go b/db/document.go index 51ff52e7..4f6f94aa 100644 --- a/db/document.go +++ b/db/document.go @@ -364,13 +364,6 @@ func (d *document) storeThing(ctx context.Context) (err error) { defer d.ulock(ctx) - // Check that the table should - // drop data being written. - - if ok, err := d.shouldDrop(); ok { - return err - } - // Check that the rcord has been // changed, and if not, return. @@ -378,6 +371,13 @@ func (d *document) storeThing(ctx context.Context) (err error) { return } + // Check that the table should + // drop data being written. + + if ok, err := d.shouldDrop(); ok { + return err + } + // Write the value to the data // layer and return any errors. diff --git a/db/lives.go b/db/lives.go index 50f9f81c..1c7e0418 100644 --- a/db/lives.go +++ b/db/lives.go @@ -24,12 +24,6 @@ import ( // this table, and executes them in name order. func (d *document) lives(ctx context.Context, when method) (err error) { - // Get the ID of the current fibre - // connection so that we can check - // against the ID of live queries. - - id := ctx.Value(ctxKeyId).(string) - // If this document has not changed // then there is no need to update // any registered live queries. @@ -38,6 +32,12 @@ func (d *document) lives(ctx context.Context, when method) (err error) { return nil } + // Get the ID of the current fibre + // connection so that we can check + // against the ID of live queries. + + id := ctx.Value(ctxKeyId).(string) + // Get the foreign read-only tables // specified for this table, and // update values which have changed. diff --git a/db/yield.go b/db/yield.go index 038a1681..76f80b6c 100644 --- a/db/yield.go +++ b/db/yield.go @@ -25,48 +25,48 @@ import ( func (d *document) cold(ctx context.Context) (doc *data.Doc, err error) { - // If we are authenticated using DB, NS, - // or KV permissions level, then we can - // return the document without copying. + // If we are not authenticated using DB, + // NS, or KV level, then we need to check + // document permissions for this query. if k, ok := ctx.Value(ctxKeyKind).(cnf.Kind); ok { - if k < cnf.AuthSC { - return d.initial, nil + if k == cnf.AuthSC { + if err = d.perms(ctx, d.initial); err != nil { + return nil, err + } } } - // Otherwise, we need to create a copy - // of the document so that we can add - // and remove fields before outputting. + // We need to copy the document so that + // we can add and remove the fields which + // are relevant to the particular query. doc = d.initial.Copy() - err = d.perms(ctx, doc) - return } func (d *document) cnow(ctx context.Context) (doc *data.Doc, err error) { - // If we are authenticated using DB, NS, - // or KV permissions level, then we can - // return the document without copying. + // If we are not authenticated using DB, + // NS, or KV level, then we need to check + // document permissions for this query. if k, ok := ctx.Value(ctxKeyKind).(cnf.Kind); ok { - if k < cnf.AuthSC { - return d.current, nil + if k == cnf.AuthSC { + if err = d.perms(ctx, d.current); err != nil { + return nil, err + } } } - // Otherwise, we need to create a copy - // of the document so that we can add - // and remove fields before outputting. + // We need to copy the document so that + // we can add and remove the fields which + // are relevant to the particular query. doc = d.current.Copy() - err = d.perms(ctx, doc) - return }