surrealpatch/db/document.go

445 lines
9.5 KiB
Go
Raw Normal View History

2017-11-16 20:53:39 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"fmt"
2017-11-16 20:53:39 +00:00
"context"
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data"
"github.com/abcum/surreal/util/indx"
"github.com/abcum/surreal/util/keys"
)
type document struct {
i *iterator
id *sql.Thing
enc []byte
2017-11-16 20:53:39 +00:00
key *keys.Thing
val kvs.KV
2018-04-28 19:32:05 +00:00
lck bool
2017-11-16 20:53:39 +00:00
doc *data.Doc
initial *data.Doc
current *data.Doc
changed bool
2017-11-16 20:53:39 +00:00
}
func newDocument(i *iterator, key *keys.Thing, val kvs.KV, doc *data.Doc) (d *document) {
d = documentPool.Get().(*document)
d.i = i
d.id = nil
d.enc = nil
2017-11-16 20:53:39 +00:00
d.key = key
d.val = val
d.doc = doc
2018-04-28 19:32:05 +00:00
d.lck = false
2017-11-16 20:53:39 +00:00
return
}
func (d *document) close() {
documentPool.Put(d)
}
func (d *document) erase() (err error) {
d.changed, d.current = true, data.Consume(nil)
2017-11-16 20:53:39 +00:00
return
}
func (d *document) query(ctx context.Context, stm sql.Statement) (val interface{}, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
if err, ok = r.(error); !ok {
err = fmt.Errorf("%v", r)
}
}
d.ulock(ctx)
d.close()
}()
2017-11-16 20:53:39 +00:00
switch stm := stm.(type) {
default:
return nil, nil
case *sql.SelectStatement:
return d.runSelect(ctx, stm)
case *sql.CreateStatement:
return d.runCreate(ctx, stm)
case *sql.UpdateStatement:
return d.runUpdate(ctx, stm)
case *sql.DeleteStatement:
return d.runDelete(ctx, stm)
case *sql.RelateStatement:
return d.runRelate(ctx, stm)
case *sql.InsertStatement:
return d.runInsert(ctx, stm)
case *sql.UpsertStatement:
return d.runUpsert(ctx, stm)
}
}
func (d *document) init(ctx context.Context) (err error) {
2017-11-16 20:53:39 +00:00
// A table of records were requested
// so we have the values, but no key
// yet, so we need to decode the KV
// store key into a Thing key.
if d.key == nil && d.val != nil {
d.enc = d.val.Key()
2019-11-20 13:20:27 +00:00
if val, ok := keyCache.Get(d.val.Key()); ok {
d.key = val.(*keys.Thing)
} else {
d.key = &keys.Thing{}
d.key.Decode(d.enc)
keyCache.Set(d.val.Key(), d.key, 0)
}
2017-11-16 20:53:39 +00:00
}
return
}
2018-04-28 19:32:05 +00:00
func (d *document) lock(ctx context.Context) (err error) {
if d.key != nil {
2018-04-28 19:32:05 +00:00
d.lck = true
d.i.e.lock.Lock(ctx, d.key)
}
return
}
func (d *document) ulock(ctx context.Context) (err error) {
2018-04-28 19:32:05 +00:00
if d.key != nil && d.lck {
d.lck = false
d.i.e.lock.Unlock(ctx, d.key)
}
return
}
func (d *document) setup(ctx context.Context) (err error) {
2017-11-16 20:53:39 +00:00
// A specific record has been requested
// and we have a key, but no value has
// been loaded yet, so the record needs
// to be loaded from the KV store.
if d.key != nil && d.val == nil {
d.enc = d.key.Encode()
2019-11-20 13:20:27 +00:00
d.val, err = d.i.e.tx.Get(ctx, d.i.versn, d.enc)
2017-11-16 20:53:39 +00:00
if err != nil {
return
}
}
// A subquery or data param has been
// loaded, and we might not have a key
// or a value, so let's load the data
// into a document, so that we can
// maniuplate the virtual document.
if d.doc != nil {
2019-11-20 13:20:27 +00:00
d.initial = d.doc
d.current = d.doc
2017-11-16 20:53:39 +00:00
}
// The requested record has been loaded
// from the KV store (and not from a
// subquery or data variable), but does
// not exist. So we'll create a document
// for processing any record changes.
if d.doc == nil && d.val != nil && d.val.Exi() == false {
d.initial = data.New()
d.current = data.New()
}
// The requested record has been loaded
// from the KV store (and not from a
// subquery or data variable). So we'll
// load the KV data into a document for
// processing any record changes.
if d.doc == nil && d.val != nil && d.val.Exi() == true {
2019-11-20 13:20:27 +00:00
if val, ok := valCache.Get(d.val.Val()); ok {
d.initial = val.(*data.Doc)
d.current = d.initial
} else {
d.initial = data.New().Decode(d.val.Val())
d.current = d.initial
valCache.Set(d.val.Val(), d.current, 0)
}
2017-11-16 20:53:39 +00:00
}
// Finally if we are dealing with a record
// which is not data from the result of a
// subquery, then generate the ID.
2017-11-16 20:53:39 +00:00
if d.key != nil {
d.id = sql.NewThing(d.key.TB, d.key.ID)
}
return
}
func (d *document) forced(ctx context.Context) bool {
if val := ctx.Value(ctxKeyForce); val != nil {
return val.(bool)
}
return false
}
func (d *document) hasChanged(ctx context.Context) bool {
return d.initial.Same(d.current) == false
}
func (d *document) shouldDrop(ctx context.Context) (bool, error) {
2017-11-16 20:53:39 +00:00
// Check whether it is specified
// that the table should drop
// writes, and if so, then return.
2019-11-20 13:20:27 +00:00
tb, err := d.i.e.tx.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
2017-11-16 20:53:39 +00:00
if err != nil {
return false, err
}
return tb.Drop, err
}
2018-08-20 06:51:02 +00:00
func (d *document) shouldVersn(ctx context.Context) (bool, error) {
// Check whether it is specified
2018-12-30 22:02:12 +00:00
// that the table should keep
// all document versions.
2018-08-20 06:51:02 +00:00
2019-11-20 13:20:27 +00:00
tb, err := d.i.e.tx.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
2018-08-20 06:51:02 +00:00
if err != nil {
return false, err
}
return tb.Vers, err
}
func (d *document) storeThing(ctx context.Context) (err error) {
defer d.ulock(ctx)
2017-11-16 20:53:39 +00:00
// Check that the record has been
2017-11-16 20:53:39 +00:00
// changed, and if not, return.
if !d.changed {
2017-11-16 20:53:39 +00:00
return
}
2018-04-24 23:00:36 +00:00
// Check that the table should
// drop data being written.
if ok, err := d.shouldDrop(ctx); ok {
2018-04-24 23:00:36 +00:00
return err
}
2017-11-16 20:53:39 +00:00
// Write the value to the data
// layer and return any errors.
2018-08-20 06:51:02 +00:00
if ok, err := d.shouldVersn(ctx); err != nil {
return err
} else if ok == true {
2019-11-20 13:20:27 +00:00
_, err = d.i.e.tx.Put(ctx, d.i.e.time.UnixNano(), d.enc, d.current.Encode())
2018-08-20 06:51:02 +00:00
} else if ok == false {
2019-11-20 13:20:27 +00:00
_, err = d.i.e.tx.Put(ctx, 0, d.enc, d.current.Encode())
2018-08-20 06:51:02 +00:00
}
2017-11-16 20:53:39 +00:00
return
}
func (d *document) purgeThing(ctx context.Context) (err error) {
defer d.ulock(ctx)
2017-11-16 20:53:39 +00:00
// Check that the table should
// drop data being written.
if ok, err := d.shouldDrop(ctx); ok {
2017-11-16 20:53:39 +00:00
return err
}
// Reset the item by writing a
// nil value to the storage.
2018-08-20 06:51:02 +00:00
if ok, err := d.shouldVersn(ctx); err != nil {
return err
} else if ok == true {
2019-11-20 13:20:27 +00:00
_, err = d.i.e.tx.Put(ctx, d.i.e.time.UnixNano(), d.enc, nil)
2018-08-20 06:51:02 +00:00
} else if ok == false {
2019-11-20 13:20:27 +00:00
_, err = d.i.e.tx.Clr(ctx, d.enc)
2018-08-20 06:51:02 +00:00
}
2017-11-16 20:53:39 +00:00
return
}
func (d *document) storeIndex(ctx context.Context) (err error) {
2017-11-16 20:53:39 +00:00
// Check if this query has been run
// in forced mode, or return.
forced := d.forced(ctx)
// Check that the rcord has been
// changed, and if not, return.
if !forced && !d.changed {
return
}
2017-11-16 20:53:39 +00:00
// Check that the table should
// drop data being written.
if ok, err := d.shouldDrop(ctx); ok {
2017-11-16 20:53:39 +00:00
return err
}
// Get the index values specified
// for this table, loop through
// them, and compute the changes.
2019-11-20 13:20:27 +00:00
ixs, err := d.i.e.tx.AllIX(ctx, d.key.NS, d.key.DB, d.key.TB)
2017-11-16 20:53:39 +00:00
if err != nil {
return err
}
for _, ix := range ixs {
del := indx.Build(ix.Cols, d.initial)
add := indx.Build(ix.Cols, d.current)
if !forced {
del, add = indx.Diff(del, add)
}
2017-11-16 20:53:39 +00:00
if ix.Uniq == true {
for _, f := range del {
enfd := data.Consume(f).Encode()
didx := &keys.Index{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd}
2019-11-20 16:14:15 +00:00
d.i.e.tx.DelC(ctx, 0, didx.Encode(), d.id.Bytes())
2017-11-16 20:53:39 +00:00
}
for _, f := range add {
enfd := data.Consume(f).Encode()
aidx := &keys.Index{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd}
2019-11-20 13:20:27 +00:00
if _, err = d.i.e.tx.PutC(ctx, 0, aidx.Encode(), d.id.Bytes(), nil); err != nil {
return &IndexError{tb: d.key.TB, name: ix.Name, cols: ix.Cols, vals: f}
2017-11-16 20:53:39 +00:00
}
}
}
if ix.Uniq == false {
for _, f := range del {
enfd := data.Consume(f).Encode()
didx := &keys.Point{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd, ID: d.key.ID}
2019-11-20 16:14:15 +00:00
d.i.e.tx.DelC(ctx, 0, didx.Encode(), d.id.Bytes())
2017-11-16 20:53:39 +00:00
}
for _, f := range add {
enfd := data.Consume(f).Encode()
aidx := &keys.Point{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd, ID: d.key.ID}
2019-11-20 13:20:27 +00:00
if _, err = d.i.e.tx.PutC(ctx, 0, aidx.Encode(), d.id.Bytes(), nil); err != nil {
return &IndexError{tb: d.key.TB, name: ix.Name, cols: ix.Cols, vals: f}
2017-11-16 20:53:39 +00:00
}
}
}
}
return
}
func (d *document) purgeIndex(ctx context.Context) (err error) {
2017-11-16 20:53:39 +00:00
// Check if this query has been run
// in forced mode, or return.
forced := d.forced(ctx)
// Check that the rcord has been
// changed, and if not, return.
if !forced && !d.changed {
return
}
2017-11-16 20:53:39 +00:00
// Check that the table should
// drop data being written.
if ok, err := d.shouldDrop(ctx); ok {
2017-11-16 20:53:39 +00:00
return err
}
// Get the index values specified
// for this table, loop through
// them, and compute the changes.
2019-11-20 13:20:27 +00:00
ixs, err := d.i.e.tx.AllIX(ctx, d.key.NS, d.key.DB, d.key.TB)
2017-11-16 20:53:39 +00:00
if err != nil {
return err
}
for _, ix := range ixs {
del := indx.Build(ix.Cols, d.initial)
2017-11-16 20:53:39 +00:00
if ix.Uniq == true {
for _, f := range del {
enfd := data.Consume(f).Encode()
didx := &keys.Index{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd}
d.i.e.tx.DelC(ctx, 0, didx.Encode(), d.id.Bytes())
2017-11-16 20:53:39 +00:00
}
}
if ix.Uniq == false {
for _, f := range del {
enfd := data.Consume(f).Encode()
aidx := &keys.Point{KV: d.key.KV, NS: d.key.NS, DB: d.key.DB, TB: d.key.TB, IX: ix.Name.VA, FD: enfd, ID: d.key.ID}
d.i.e.tx.DelC(ctx, 0, aidx.Encode(), d.id.Bytes())
2017-11-16 20:53:39 +00:00
}
}
}
return
}