reldb

package
v0.0.0-...-ee222a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AutoKeyBuffer = 100

	PreAutoKavlb = "tank%avlb_"
	PreAutoKused = "tank%used_"
	PrefixLink   = "link%"
	PrefixTable  = "tbl%"

	Delimiter     = "_"
	LinkDelimiter = "@"
)

Functions

func Count

func Count[T IObject](db IRelationalDB) int

func DeepDelete

func DeepDelete[T IObject](db IRelationalDB, id string) error

func Delete

func Delete[T IObject](db IRelationalDB, id string) error

func Encode

func Encode(obj *IObject) []byte

func Exist

func Exist[T IObject](db IRelationalDB, id string) bool

func Foreach

func Foreach[T IObject](db IRelationalDB, do func(id string, value *T))

func MakeKey

func MakeKey(tableName, id string) []byte

func MakeLinkKey

func MakeLinkKey(tableName string, id string, targetName string, targetId string) []string

func MakePrefix

func MakePrefix(tableName string) string

func NameOfField

func NameOfField(parent interface{}, field interface{}) (string, error)

func NameOfStruct

func NameOfStruct[T any]() string

func ToString

func ToString(v any) string

func Type

func Type[T IObject]() *T

Types

type AbstractRelDB

type AbstractRelDB struct{ IRelationalDB }

AbstractRelDB pre-implement IRelationalDB, you need to set value of interface: methods implemented are used in abstract.

func (*AbstractRelDB) CleanUnusedKey

func (db *AbstractRelDB) CleanUnusedKey()

func (*AbstractRelDB) Count

func (db *AbstractRelDB) Count(prefix string) int

func (*AbstractRelDB) DeepDelete

func (db *AbstractRelDB) DeepDelete(tableName string, id string) error

func (*AbstractRelDB) Delete

func (db *AbstractRelDB) Delete(tableName string, id string) error

func (*AbstractRelDB) FindAll

func (db *AbstractRelDB) FindAll(tableName string, predicate func(id string, value *IObject) bool) ([]string, []*IObject)

func (*AbstractRelDB) FindFirst

func (db *AbstractRelDB) FindFirst(tableName string, predicate func(id string, value *IObject) bool) (string, *IObject)

func (*AbstractRelDB) Foreach

func (db *AbstractRelDB) Foreach(tableName string, do func(id string, value *IObject))

func (*AbstractRelDB) FreeKey

func (db *AbstractRelDB) FreeKey(keys ...string) []error

func (*AbstractRelDB) Get

func (db *AbstractRelDB) Get(tableName string, id string) *IObject

func (*AbstractRelDB) GetNextKey

func (db *AbstractRelDB) GetNextKey() string

func (*AbstractRelDB) Insert

func (db *AbstractRelDB) Insert(object IObject) string

func (*AbstractRelDB) Set

func (db *AbstractRelDB) Set(id string, object IObject) error

func (*AbstractRelDB) Update

func (db *AbstractRelDB) Update(tableName string, id string, editor func(value IObject) IObject) *IObject

type DBObject

type DBObject struct{ IObject }

func (DBObject) Equals

func (o DBObject) Equals(v IObject) bool

func (DBObject) Hash

func (o DBObject) Hash() string

type IObject

type IObject interface {
	Equals(v IObject) bool
	Hash() string
	ToString() string
	TableName() string
}

func Decode

func Decode(value []byte) *IObject

type IRelationalDB

type IRelationalDB interface {
	// GetNextKey pick a key in tank of key. If tank is empty, it should be filled with unused key.
	GetNextKey() string
	// FreeKey check if key is used (return error otherwise) and make key available again.
	FreeKey(key ...string) []error
	CleanUnusedKey()

	// RawSet set a value in DB. prefix and key are simply concatenated. Don't care about Key is already in used or not.
	RawSet(prefix string, key string, value []byte)
	// RawGet get a value in DB. prefix and key are simply concatenated. If no value corresponding to this Key,
	//empty slice and false should be returned.
	RawGet(prefix string, key string) ([]byte, bool)
	// RawDelete delete a value in DB. prefix and key are simply concatenated. Return true if value is correctly deleted.
	RawDelete(prefix string, key string) bool
	// RawIterKey iterate in DB when prefix match with Key.
	//The action it called for each Key and the key is truncated with the prefix given.
	//The stop boolean defined if iteration should be stopped. No values are prefetched with this iterator.
	RawIterKey(prefix string, action func(key string) (stop bool))
	// RawIterKV iterate in DB when prefix match with Key.
	//The action it called for each Key and the key is truncated with the prefix given.
	//The stop boolean defined if iteration should be stopped. value is the corresponding value of the key.
	RawIterKV(prefix string, action func(key string, value []byte) (stop bool))

	// Insert create a new entry in storage with IObject passed. TableName is inferred with the IObject.
	Insert(object IObject) string
	// Set write a value for a specific id. TableName is inferred with the IObject. If Key not exist, an error is returned.
	Set(id string, object IObject) error
	// Get retrieve the value for corresponding TableName and ID. Return nil if nothing found.
	Get(tableName string, id string) *IObject
	// Update retrieve the value for corresponding TableName and ID, call the editor et Set the resulted value.
	Update(tableName string, id string, editor func(value IObject) IObject) *IObject
	// Delete remove the value for corresponding TableName and ID. If Key not exist,
	//an error is returned. The link using the object will be also deleted.
	Delete(tableName string, id string) error
	// DeepDelete remove the value for corresponding TableName and ID. If Key not exist,
	//an error is returned. It also recursively remove all values connected with a link.
	DeepDelete(tableName string, id string) error
	// Exist return true if the for corresponding TableName and ID exist in DB.
	Exist(tableName string, id string) bool

	// Count return the count for matching Key prefixed by TableName that exist in DB.
	Count(tableName string) int
	// Foreach call the do function for each value whose key is prefixed by TableName.
	Foreach(tableName string, do func(id string, value *IObject))
	FindFirst(tableName string, predicate func(id string, value *IObject) bool) (string, *IObject)
	FindAll(tableName string, predicate func(id string, value *IObject) bool) ([]string, []*IObject)

	Print(tableName string) error
}

IRelationalDB is a small interface to define some operation with a storage used like a relational DB.

Key

In first, the underlying storage should be work like a KV DB. In consequence, a key is structured to store

  • Key: internalTypePrefix, suffix
  • Key for concrete type : internalTypePrefix, tableName, id

To make uniq key, a tank key system is implemented and can be used with GetNextKey, FreeKey. The global AutoKeyBuffer defined the size of this tank. When value is inserted, a key is pick in tank. When entry is deleted, the key become available again.

Operators

All raw operator must be implemented ; other can be but are already implement in the abstraction AbstractRelDB. Raw Operators probably work directly with the db driver and are used by all other operators.

type ObjWrapper

type ObjWrapper[T IObject] struct {
	ID    string
	Value T
	// contains filtered or unexported fields
}

func FindAll

func FindAll[T IObject](db IRelationalDB, predicate func(id string, value *T) bool) []*ObjWrapper[T]

func FindFirst

func FindFirst[T IObject](db IRelationalDB, predicate func(id string, value *T) bool) *ObjWrapper[T]

func Get

func Get[T IObject](db IRelationalDB, id string) *ObjWrapper[T]

func Insert

func Insert[T IObject](db IRelationalDB, value T) *ObjWrapper[T]

func NewObjWrapper

func NewObjWrapper[T IObject](db IRelationalDB, ID string, value *T) *ObjWrapper[T]

func Set

func Set[T IObject](db IRelationalDB, id string, value T) *ObjWrapper[T]

func Update

func Update[T IObject](db IRelationalDB, id string, editor func(value *T)) *ObjWrapper[T]
func (t *ObjWrapper[IObject]) AllFromLink(tableName string) ([]string, []*IObject)
func (t *ObjWrapper[IObject]) FromLink(tableName string) (string, *IObject)
func (t *ObjWrapper[IObject]) Link(biDirectional bool, tableName string, ids ...string)

func (*ObjWrapper[IObject]) LinkNew

func (t *ObjWrapper[IObject]) LinkNew(biDirectional bool, objs ...IObject) []*ObjWrapper[IObject]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL