2016-11-21 18:47:23 +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 mem
|
|
|
|
|
|
|
|
import (
|
2017-11-16 20:51:39 +00:00
|
|
|
"sync"
|
2016-11-21 18:47:23 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
"context"
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
|
|
|
"github.com/abcum/surreal/kvs"
|
2016-11-21 18:47:23 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
2017-02-12 11:56:46 +00:00
|
|
|
"github.com/abcum/surreal/util/keys"
|
2016-11-21 18:47:23 +00:00
|
|
|
)
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
type Cache struct {
|
|
|
|
kvs.TX
|
2018-04-24 16:11:12 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
data map[string]interface{}
|
|
|
|
locks struct {
|
|
|
|
ns sync.RWMutex
|
|
|
|
db sync.RWMutex
|
|
|
|
tb sync.RWMutex
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
func New() (c *Cache) {
|
|
|
|
return &Cache{
|
|
|
|
data: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
2016-11-23 15:42:32 +00:00
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
func NewWithTX(tx kvs.TX) (c *Cache) {
|
|
|
|
return &Cache{
|
|
|
|
TX: tx,
|
|
|
|
data: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) Reset() {
|
|
|
|
c.TX = nil
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
func (c *Cache) get(key keys.Key) (out interface{}, ok bool) {
|
2017-11-16 20:51:39 +00:00
|
|
|
c.lock.RLock()
|
2018-04-24 16:11:12 +00:00
|
|
|
out, ok = c.data[key.String()]
|
2017-11-16 20:51:39 +00:00
|
|
|
c.lock.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
func (c *Cache) put(key keys.Key, val interface{}) {
|
2017-11-16 20:51:39 +00:00
|
|
|
c.lock.Lock()
|
2018-04-24 16:11:12 +00:00
|
|
|
c.data[key.String()] = val
|
2017-11-16 20:51:39 +00:00
|
|
|
c.lock.Unlock()
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
func (c *Cache) del(key keys.Key) {
|
2017-11-16 20:51:39 +00:00
|
|
|
c.lock.Lock()
|
2018-04-24 16:11:12 +00:00
|
|
|
delete(c.data, key.String())
|
2017-11-16 20:51:39 +00:00
|
|
|
c.lock.Unlock()
|
2016-11-23 15:42:32 +00:00
|
|
|
}
|
2016-11-21 18:47:23 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllNS(ctx context.Context) (out []*sql.DefineNamespaceStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
c.locks.ns.RLock()
|
|
|
|
defer c.locks.ns.RUnlock()
|
|
|
|
|
|
|
|
key := &keys.NS{KV: cnf.Settings.DB.Base, NS: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineNamespaceStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineNamespaceStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetNS(ctx context.Context, ns string) (val *sql.DefineNamespaceStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
c.locks.ns.RLock()
|
|
|
|
defer c.locks.ns.RUnlock()
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
key := &keys.NS{KV: cnf.Settings.DB.Base, NS: ns}
|
|
|
|
|
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineNamespaceStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorNSNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineNamespaceStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AddNS(ctx context.Context, ns string) (val *sql.DefineNamespaceStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
c.locks.ns.Lock()
|
|
|
|
defer c.locks.ns.Unlock()
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
key := &keys.NS{KV: cnf.Settings.DB.Base, NS: ns}
|
|
|
|
|
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineNamespaceStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, _ = c.TX.Get(ctx, 0, key.Encode()); kv.Exi() {
|
2018-04-24 16:11:12 +00:00
|
|
|
val = &sql.DefineNamespaceStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
c.put(key, val)
|
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
val = &sql.DefineNamespaceStatement{Name: sql.NewIdent(ns)}
|
2018-05-02 02:05:03 +00:00
|
|
|
c.TX.PutC(ctx, 0, key.Encode(), val.Encode(), nil)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelNS(ns string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.NS{KV: cnf.Settings.DB.Base, NS: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.NS{KV: cnf.Settings.DB.Base, NS: ns})
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllNT(ctx context.Context, ns string) (out []*sql.DefineTokenStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kvs []kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.NT{KV: cnf.Settings.DB.Base, NS: ns, TK: keys.Ignore}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineTokenStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetNT(ctx context.Context, ns, tk string) (val *sql.DefineTokenStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kv kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.NT{KV: cnf.Settings.DB.Base, NS: ns, TK: tk}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorNTNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineTokenStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllNU(ctx context.Context, ns string) (out []*sql.DefineLoginStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kvs []kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.NU{KV: cnf.Settings.DB.Base, NS: ns, US: keys.Ignore}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineLoginStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetNU(ctx context.Context, ns, us string) (val *sql.DefineLoginStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kv kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.NU{KV: cnf.Settings.DB.Base, NS: ns, US: us}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorNUNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineLoginStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllDB(ctx context.Context, ns string) (out []*sql.DefineDatabaseStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
c.locks.db.RLock()
|
|
|
|
defer c.locks.db.RUnlock()
|
|
|
|
|
|
|
|
key := &keys.DB{KV: cnf.Settings.DB.Base, NS: ns, DB: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineDatabaseStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineDatabaseStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetDB(ctx context.Context, ns, db string) (val *sql.DefineDatabaseStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
c.locks.db.RLock()
|
|
|
|
defer c.locks.db.RUnlock()
|
|
|
|
|
|
|
|
key := &keys.DB{KV: cnf.Settings.DB.Base, NS: ns, DB: db}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineDatabaseStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorDBNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineDatabaseStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AddDB(ctx context.Context, ns, db string) (val *sql.DefineDatabaseStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if _, err = c.AddNS(ctx, ns); err != nil {
|
2018-04-24 16:11:12 +00:00
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.locks.db.Lock()
|
|
|
|
defer c.locks.db.Unlock()
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.DB{KV: cnf.Settings.DB.Base, NS: ns, DB: db}
|
2018-04-24 16:11:12 +00:00
|
|
|
|
|
|
|
if out, ok := c.get(key); ok {
|
|
|
|
return out.(*sql.DefineDatabaseStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, _ = c.TX.Get(ctx, 0, key.Encode()); kv.Exi() {
|
2018-04-24 16:11:12 +00:00
|
|
|
val = &sql.DefineDatabaseStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
c.put(key, val)
|
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
val = &sql.DefineDatabaseStatement{Name: sql.NewIdent(db)}
|
2018-05-02 02:05:03 +00:00
|
|
|
c.TX.PutC(ctx, 0, key.Encode(), val.Encode(), nil)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
|
|
|
|
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelDB(ns, db string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.DB{KV: cnf.Settings.DB.Base, NS: ns, DB: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.DB{KV: cnf.Settings.DB.Base, NS: ns, DB: db})
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllDT(ctx context.Context, ns, db string) (out []*sql.DefineTokenStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kvs []kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.DT{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TK: keys.Ignore}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
2016-11-23 15:42:32 +00:00
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineTokenStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetDT(ctx context.Context, ns, db, tk string) (val *sql.DefineTokenStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kv kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.DT{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TK: tk}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorDTNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineTokenStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllDU(ctx context.Context, ns, db string) (out []*sql.DefineLoginStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kvs []kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.DU{KV: cnf.Settings.DB.Base, NS: ns, DB: db, US: keys.Ignore}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineLoginStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetDU(ctx context.Context, ns, db, us string) (val *sql.DefineLoginStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kv kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.DU{KV: cnf.Settings.DB.Base, NS: ns, DB: db, US: us}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorDUNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineLoginStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllSC(ctx context.Context, ns, db string) (out []*sql.DefineScopeStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kvs []kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.SC{KV: cnf.Settings.DB.Base, NS: ns, DB: db, SC: keys.Ignore}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineScopeStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetSC(ctx context.Context, ns, db, sc string) (val *sql.DefineScopeStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kv kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.SC{KV: cnf.Settings.DB.Base, NS: ns, DB: db, SC: sc}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorSCNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineScopeStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllST(ctx context.Context, ns, db, sc string) (out []*sql.DefineTokenStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kvs []kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.ST{KV: cnf.Settings.DB.Base, NS: ns, DB: db, SC: sc, TK: keys.Ignore}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineTokenStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetST(ctx context.Context, ns, db, sc, tk string) (val *sql.DefineTokenStatement, err error) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
var kv kvs.KV
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
key := &keys.ST{KV: cnf.Settings.DB.Base, NS: ns, DB: db, SC: sc, TK: tk}
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorSTNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineTokenStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllTB(ctx context.Context, ns, db string) (out []*sql.DefineTableStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
c.locks.tb.RLock()
|
|
|
|
defer c.locks.tb.RUnlock()
|
|
|
|
|
|
|
|
key := &keys.TB{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineTableStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineTableStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetTB(ctx context.Context, ns, db, tb string) (val *sql.DefineTableStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
c.locks.tb.RLock()
|
|
|
|
defer c.locks.tb.RUnlock()
|
|
|
|
|
|
|
|
key := &keys.TB{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineTableStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorTBNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineTableStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AddTB(ctx context.Context, ns, db, tb string) (val *sql.DefineTableStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if _, err = c.AddDB(ctx, ns, db); err != nil {
|
2018-04-24 16:11:12 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.locks.tb.Lock()
|
|
|
|
defer c.locks.tb.Unlock()
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
key := &keys.TB{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
|
|
|
return out.(*sql.DefineTableStatement), nil
|
2017-11-16 20:51:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, _ = c.TX.Get(ctx, 0, key.Encode()); kv.Exi() {
|
2018-04-24 16:11:12 +00:00
|
|
|
val = &sql.DefineTableStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
c.put(key, val)
|
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
val = &sql.DefineTableStatement{Name: sql.NewIdent(tb)}
|
2018-05-02 02:05:03 +00:00
|
|
|
c.TX.PutC(ctx, 0, key.Encode(), val.Encode(), nil)
|
2018-04-24 16:11:12 +00:00
|
|
|
|
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
return
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelTB(ns, db, tb string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.TB{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.TB{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllEV(ctx context.Context, ns, db, tb string) (out []*sql.DefineEventStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
key := &keys.EV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, EV: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineEventStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-11-16 20:51:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineEventStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetEV(ctx context.Context, ns, db, tb, ev string) (val *sql.DefineEventStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
key := &keys.EV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, EV: ev}
|
|
|
|
|
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineEventStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
|
|
|
return nil, ErrorEVNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineEventStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:39 +00:00
|
|
|
func (c *Cache) DelEV(ns, db, tb, ev string) {
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.EV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, EV: keys.Ignore})
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.EV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, EV: ev})
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllFD(ctx context.Context, ns, db, tb string) (out []*sql.DefineFieldStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
key := &keys.FD{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FD: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineFieldStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineFieldStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetFD(ctx context.Context, ns, db, tb, fd string) (val *sql.DefineFieldStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
key := &keys.FD{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FD: fd}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineFieldStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorFDNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineFieldStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelFD(ns, db, tb, fd string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.FD{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FD: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.FD{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FD: fd})
|
2017-02-12 11:56:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllIX(ctx context.Context, ns, db, tb string) (out []*sql.DefineIndexStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
key := &keys.IX{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, IX: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineIndexStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineIndexStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetIX(ctx context.Context, ns, db, tb, ix string) (val *sql.DefineIndexStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
key := &keys.IX{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, IX: ix}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineIndexStatement), nil
|
|
|
|
}
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-02-12 11:56:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, ErrorIXNotFound
|
2017-02-12 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineIndexStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelIX(ns, db, tb, ix string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.IX{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, IX: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.IX{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, IX: ix})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllFT(ctx context.Context, ns, db, tb string) (out []*sql.DefineTableStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
|
|
|
|
|
|
|
key := &keys.FT{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FT: keys.Ignore}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.DefineTableStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-11-16 20:51:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.DefineTableStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetFT(ctx context.Context, ns, db, tb, ft string) (val *sql.DefineTableStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
key := &keys.FT{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FT: ft}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.DefineTableStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
|
|
|
return nil, ErrorFTNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.DefineTableStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelFT(ns, db, tb, ft string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.FT{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FT: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.FT{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, FT: ft})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) AllLV(ctx context.Context, ns, db, tb string) (out []*sql.LiveStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kvs []kvs.KV
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
key := &keys.LV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, LV: keys.Ignore}
|
|
|
|
|
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.([]*sql.LiveStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kvs, err = c.TX.GetP(ctx, 0, key.Encode(), 0); err != nil {
|
2017-11-16 20:51:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range kvs {
|
|
|
|
val := &sql.LiveStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
out = append(out, val)
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, out)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
func (c *Cache) GetLV(ctx context.Context, ns, db, tb, lv string) (val *sql.LiveStatement, err error) {
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
var kv kvs.KV
|
|
|
|
|
|
|
|
key := &keys.LV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, LV: lv}
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
if out, ok := c.get(key); ok {
|
2017-11-16 20:51:39 +00:00
|
|
|
return out.(*sql.LiveStatement), nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
if kv, err = c.TX.Get(ctx, 0, key.Encode()); err != nil {
|
2017-11-16 20:51:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Exi() {
|
|
|
|
return nil, ErrorLVNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
val = &sql.LiveStatement{}
|
|
|
|
val.Decode(kv.Val())
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.put(key, val)
|
2017-11-16 20:51:39 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DelLV(ns, db, tb, lv string) {
|
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.LV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, LV: keys.Ignore})
|
2017-11-16 20:51:39 +00:00
|
|
|
|
2018-04-24 16:11:12 +00:00
|
|
|
c.del(&keys.LV{KV: cnf.Settings.DB.Base, NS: ns, DB: db, TB: tb, LV: lv})
|
2017-02-12 11:56:46 +00:00
|
|
|
|
2016-11-21 18:47:23 +00:00
|
|
|
}
|