Use caching on database level, not in document class

This commit is contained in:
Tobie Morgan Hitchcock 2019-02-01 00:18:55 +00:00
parent 1d988760b3
commit a00a7f17ef
7 changed files with 14 additions and 128 deletions

View file

@ -74,7 +74,7 @@ func (d *document) grant(ctx context.Context, met method) (ok bool, err error) {
// so we can check if the permissions // so we can check if the permissions
// allow us to view this document. // allow us to view this document.
tb, err := d.getTB(ctx) tb, err := d.i.e.dbo.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -161,7 +161,7 @@ func (d *document) allow(ctx context.Context, met method) (ok bool, err error) {
// so we can check if the permissions // so we can check if the permissions
// allow us to view this document. // allow us to view this document.
tb, err := d.getTB(ctx) tb, err := d.i.e.dbo.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return false, err return false, err
} }

View file

@ -29,9 +29,6 @@ import (
type document struct { type document struct {
i *iterator i *iterator
ns string
db string
tb string
id *sql.Thing id *sql.Thing
key *keys.Thing key *keys.Thing
val kvs.KV val kvs.KV
@ -39,23 +36,6 @@ type document struct {
doc *data.Doc doc *data.Doc
initial *data.Doc initial *data.Doc
current *data.Doc current *data.Doc
store struct {
id int
tb bool
ev bool
fd bool
ix bool
ft bool
lv bool
}
cache struct {
tb *sql.DefineTableStatement
ev []*sql.DefineEventStatement
fd []*sql.DefineFieldStatement
ix []*sql.DefineIndexStatement
ft []*sql.DefineTableStatement
lv []*sql.LiveStatement
}
} }
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) {
@ -77,68 +57,11 @@ func (d *document) close() {
documentPool.Put(d) documentPool.Put(d)
} }
func (d *document) clear() {
d.store.tb = false
d.store.ev = false
d.store.fd = false
d.store.ix = false
d.store.ft = false
d.store.lv = false
}
func (d *document) erase() (err error) { func (d *document) erase() (err error) {
d.current = data.Consume(nil) d.current = data.Consume(nil)
return return
} }
func (d *document) getTB(ctx context.Context) (out *sql.DefineTableStatement, err error) {
if !d.store.tb {
d.store.tb = true
d.cache.tb, err = d.i.e.dbo.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
}
return d.cache.tb, err
}
func (d *document) getEV(ctx context.Context) (out []*sql.DefineEventStatement, err error) {
if !d.store.ev {
d.store.ev = true
d.cache.ev, err = d.i.e.dbo.AllEV(ctx, d.key.NS, d.key.DB, d.key.TB)
}
return d.cache.ev, err
}
func (d *document) getFD(ctx context.Context) (out []*sql.DefineFieldStatement, err error) {
if !d.store.fd {
d.store.fd = true
d.cache.fd, err = d.i.e.dbo.AllFD(ctx, d.key.NS, d.key.DB, d.key.TB)
}
return d.cache.fd, err
}
func (d *document) getIX(ctx context.Context) (out []*sql.DefineIndexStatement, err error) {
if !d.store.ix {
d.store.ix = true
d.cache.ix, err = d.i.e.dbo.AllIX(ctx, d.key.NS, d.key.DB, d.key.TB)
}
return d.cache.ix, err
}
func (d *document) getFT(ctx context.Context) (out []*sql.DefineTableStatement, err error) {
if !d.store.ft {
d.store.ft = true
d.cache.ft, err = d.i.e.dbo.AllFT(ctx, d.key.NS, d.key.DB, d.key.TB)
}
return d.cache.ft, err
}
func (d *document) getLV(ctx context.Context) (out []*sql.LiveStatement, err error) {
if !d.store.lv {
d.store.lv = true
d.cache.lv, err = d.i.e.dbo.AllLV(ctx, d.key.NS, d.key.DB, d.key.TB)
}
return d.cache.lv, err
}
func (d *document) query(ctx context.Context, stm sql.Statement) (val interface{}, err error) { func (d *document) query(ctx context.Context, stm sql.Statement) (val interface{}, err error) {
defer func() { defer func() {
@ -265,47 +188,10 @@ func (d *document) setup(ctx context.Context) (err error) {
// Finally if we are dealing with a record // Finally if we are dealing with a record
// which is not data from the result of a // which is not data from the result of a
// subquery, then generate the ID from the // subquery, then generate the ID.
// key and re-calculate any cached data.
if d.key != nil { if d.key != nil {
// Check that the cached data for the
// current document belongs to the same
// NS, DB, and TB as the pooled document.
// If it doesn't then reset the cached data.
if d.ns != d.key.NS {
d.ns = d.key.NS
d.clear()
}
if d.db != d.key.DB {
d.db = d.key.DB
d.clear()
}
if d.tb != d.key.TB {
d.tb = d.key.TB
d.clear()
}
// Check that the cached data for the
// current document belongs to the same
// iterator as the pooled document. If
// it doesn't then reset the cached data.
if d.i.id != d.store.id {
d.store.id = d.i.id
d.clear()
}
// Finally, let's specify the ID of the
// current document, so we can use it
// for getting and setting data.
d.id = sql.NewThing(d.key.TB, d.key.ID) d.id = sql.NewThing(d.key.TB, d.key.ID)
} }
return return
@ -332,7 +218,7 @@ func (d *document) shouldDrop(ctx context.Context) (bool, error) {
// that the table should drop // that the table should drop
// writes, and if so, then return. // writes, and if so, then return.
tb, err := d.getTB(ctx) tb, err := d.i.e.dbo.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -347,7 +233,7 @@ func (d *document) shouldVersn(ctx context.Context) (bool, error) {
// that the table should keep // that the table should keep
// all document versions. // all document versions.
tb, err := d.getTB(ctx) tb, err := d.i.e.dbo.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -440,7 +326,7 @@ func (d *document) storeIndex(ctx context.Context) (err error) {
// for this table, loop through // for this table, loop through
// them, and compute the changes. // them, and compute the changes.
ixs, err := d.getIX(ctx) ixs, err := d.i.e.dbo.AllIX(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }
@ -515,7 +401,7 @@ func (d *document) purgeIndex(ctx context.Context) (err error) {
// for this table, loop through // for this table, loop through
// them, and compute the changes. // them, and compute the changes.
ixs, err := d.getIX(ctx) ixs, err := d.i.e.dbo.AllIX(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }

View file

@ -47,7 +47,7 @@ func (d *document) event(ctx context.Context, met method) (err error) {
// for this table, loop through // for this table, loop through
// them, and compute the events. // them, and compute the events.
evs, err := d.getEV(ctx) evs, err := d.i.e.dbo.AllEV(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }

View file

@ -42,7 +42,7 @@ func (d *document) lives(ctx context.Context, when method) (err error) {
// specified for this table, and // specified for this table, and
// update values which have changed. // update values which have changed.
lvs, err := d.getLV(ctx) lvs, err := d.i.e.dbo.AllLV(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }

View file

@ -98,7 +98,7 @@ func (d *document) defFld(ctx context.Context, met method) (err error) {
func (d *document) delFld(ctx context.Context, met method) (err error) { func (d *document) delFld(ctx context.Context, met method) (err error) {
tb, err := d.getTB(ctx) tb, err := d.i.e.dbo.GetTB(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }
@ -109,7 +109,7 @@ func (d *document) delFld(ctx context.Context, met method) (err error) {
// Get the defined fields // Get the defined fields
fds, err := d.getFD(ctx) fds, err := d.i.e.dbo.AllFD(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }
@ -267,7 +267,7 @@ func (d *document) mrgSet(ctx context.Context, met method, expr *sql.DataExpress
func (d *document) mrgFld(ctx context.Context, met method) (err error) { func (d *document) mrgFld(ctx context.Context, met method) (err error) {
fds, err := d.getFD(ctx) fds, err := d.i.e.dbo.AllFD(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }

View file

@ -37,7 +37,7 @@ func (d *document) perms(ctx context.Context, doc *data.Doc) (err error) {
// check if the permissions allow us // check if the permissions allow us
// to view each field. // to view each field.
fds, err := d.getFD(ctx) fds, err := d.i.e.dbo.AllFD(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }

View file

@ -51,7 +51,7 @@ func (d *document) table(ctx context.Context, when method) (err error) {
// specified for this table, and // specified for this table, and
// update values which have changed. // update values which have changed.
fts, err := d.getFT(ctx) fts, err := d.i.e.dbo.AllFT(ctx, d.key.NS, d.key.DB, d.key.TB)
if err != nil { if err != nil {
return err return err
} }