database

package
v1.2.101 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: MIT Imports: 14 Imported by: 6

Documentation

Overview

Package database

General interface for distributed cache and data structure store (e.g. Redis)

Index

Constants

View Source
const (
	NOT_IMPLEMENTED  = "not implemented"
	NOT_SUPPORTED    = "not supported"
	TABLE_NOT_EXISTS = "table does not exist"
)
View Source
const (
	Eq       QueryOperator = "="
	Neq                    = "!"
	Like                   = "~"
	Gt                     = ">"
	Gte                    = ">="
	Lt                     = "<"
	Lte                    = "<="
	In                     = "*"
	NotIn                  = "-"
	Between                = "#"
	Contains               = "@"
)
View Source
const (
	INDEX_NOT_EXISTS = "index does not exist"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AggFunc added in v1.2.82

type AggFunc string
const (
	COUNT AggFunc = "count"
	SUM   AggFunc = "sum"
	AVG   AggFunc = "avg"
	MIN   AggFunc = "min"
	MAX   AggFunc = "max"
)

type FilterFunction

type FilterFunction func(raw map[string]any, filter QueryFilter) bool

FilterFunction signature of a filter function

type IDataCache

type IDataCache interface {

	// Closer includes method Close()
	io.Closer

	// Ping tests connectivity for retries number of time with time interval (in seconds) between retries
	Ping(retries uint, intervalInSeconds uint) error

	// CloneDataCache Returns a clone (copy) of the instance
	CloneDataCache() (IDataCache, error)

	// Get the value of a key
	Get(factory EntityFactory, key string) (Entity, error)

	// GetRaw gets the raw value of a key
	GetRaw(key string) ([]byte, error)

	// GetKeys Get the value of all the given keys
	GetKeys(factory EntityFactory, keys ...string) ([]Entity, error)

	// GetRawKeys gets the raw value of all the given keys
	GetRawKeys(keys ...string) ([]Tuple[string, []byte], error)

	// Set value of key with optional expiration
	Set(key string, entity Entity, expiration ...time.Duration) error

	// SetRaw sets the raw value of key with optional expiration
	SetRaw(key string, bytes []byte, expiration ...time.Duration) error

	// SetNX sets value of key only if it is not exist with optional expiration, return false if the key exists
	SetNX(key string, entity Entity, expiration ...time.Duration) (bool, error)

	// SetRawNX sets the raw value of key only if it is not exist with optional expiration, return false if the key exists
	SetRawNX(key string, bytes []byte, expiration ...time.Duration) (bool, error)

	// Add Set the value of a key only if the key does not exist
	Add(key string, entity Entity, expiration time.Duration) (bool, error)

	// AddRaw sets the raw value of a key only if the key does not exist
	AddRaw(key string, bytes []byte, expiration time.Duration) (bool, error)

	// Del Delete keys
	Del(keys ...string) (err error)

	// Rename a key
	Rename(key string, newKey string) (err error)

	// Exists Check if key exists
	Exists(key string) (result bool, err error)

	// Scan keys from the provided cursor
	Scan(from uint64, match string, count int64) (keys []string, cursor uint64, err error)

	// HGet gets the value of a hash field
	HGet(factory EntityFactory, key, field string) (result Entity, err error)

	// HGetRaw gets the raw value of a hash field
	HGetRaw(key, field string) ([]byte, error)

	// HKeys gets all the fields in a hash
	HKeys(key string) (fields []string, err error)

	// HGetAll gets all the fields and values in a hash
	HGetAll(factory EntityFactory, key string) (result map[string]Entity, err error)

	// HGetRawAll gets all the fields and raw values in a hash
	HGetRawAll(key string) (result map[string][]byte, err error)

	// HSet sets the value of a hash field
	HSet(key, field string, entity Entity) (err error)

	// HSetRaw sets the raw value of a hash field
	HSetRaw(key, field string, bytes []byte) (err error)

	// HSetNX Set value of key only if it is not exist with optional expiration, return false if the key exists
	HSetNX(key, field string, entity Entity) (result bool, err error)

	// HSetRawNX sets the raw value of key only if it is not exist with optional expiration, return false if the key exists
	HSetRawNX(key, field string, bytes []byte) (result bool, err error)

	// HDel delete one or more hash fields
	HDel(key string, fields ...string) (err error)

	// HAdd sets the value of a key only if the key does not exist
	HAdd(key, field string, entity Entity) (result bool, err error)

	// HAddRaw sets the raw value of a key only if the key does not exist
	HAddRaw(key, field string, bytes []byte) (result bool, err error)

	// HExists Check if key exists
	HExists(key, field string) (result bool, err error)

	// RPush Append one or multiple values to a list
	RPush(key string, value ...Entity) (err error)

	// LPush Prepend one or multiple values to a list
	LPush(key string, value ...Entity) (err error)

	// RPop Remove and get the last element in a list
	RPop(factory EntityFactory, key string) (entity Entity, err error)

	// LPop Remove and get the first element in a list
	LPop(factory EntityFactory, key string) (entity Entity, err error)

	// BRPop Remove and get the last element in a list or block until one is available
	BRPop(factory EntityFactory, timeout time.Duration, keys ...string) (key string, entity Entity, err error)

	// BLPop Remove and get the first element in a list or block until one is available
	BLPop(factory EntityFactory, timeout time.Duration, keys ...string) (key string, entity Entity, err error)

	// LRange Get a range of elements from list
	LRange(factory EntityFactory, key string, start, stop int64) (result []Entity, err error)

	// LLen Get the length of a list
	LLen(key string) (result int64)

	// ObtainLocker tries to obtain a new lock using a key with the given TTL
	ObtainLocker(key string, ttl time.Duration) (ILocker, error)
}

IDataCache DataCache interface

func NewInMemoryDataCache

func NewInMemoryDataCache() (dc IDataCache, err error)

NewInMemoryDataCache is a factory method for DB store

type IDatabase

type IDatabase interface {

	// Closer includes method Close()
	io.Closer

	// Ping Test database connectivity for retries number of time with time interval (in seconds) between retries
	Ping(retries uint, intervalInSeconds uint) error

	// CloneDatabase Returns a clone (copy) of the database instance
	CloneDatabase() (IDatabase, error)

	// Get a single entity by ID
	Get(factory EntityFactory, entityID string, keys ...string) (result Entity, err error)

	// List Get multiple entities by IDs
	List(factory EntityFactory, entityIDs []string, keys ...string) (list []Entity, err error)

	// Exists Check if entity exists by ID
	Exists(factory EntityFactory, entityID string, keys ...string) (result bool, err error)

	// Insert new entity
	Insert(entity Entity) (added Entity, err error)

	// Update existing entity
	Update(entity Entity) (updated Entity, err error)

	// Upsert Update entity or create it if it does not exist
	Upsert(entity Entity) (updated Entity, err error)

	// Delete entity by id and shard (key)
	Delete(factory EntityFactory, entityID string, keys ...string) (err error)

	// BulkInsert Insert multiple entities
	BulkInsert(entities []Entity) (affected int64, err error)

	// BulkUpdate Update multiple entities
	BulkUpdate(entities []Entity) (affected int64, err error)

	// BulkUpsert Update or insert multiple entities
	BulkUpsert(entities []Entity) (affected int64, err error)

	// BulkDelete Delete multiple entities by IDs
	BulkDelete(factory EntityFactory, entityIDs []string, keys ...string) (affected int64, err error)

	// SetField Update single field of the document in a single transaction (eliminates the need to fetch - change - update)
	SetField(factory EntityFactory, entityID string, field string, value any, keys ...string) (err error)

	// SetFields Update some fields of the document in a single transaction (eliminates the need to fetch - change - update)
	SetFields(factory EntityFactory, entityID string, fields map[string]any, keys ...string) (err error)

	// BulkSetFields Update specific field of multiple entities in a single transaction (eliminates the need to fetch - change - update)
	// The field is the name of the field, values is a map of entityId -> field value
	BulkSetFields(factory EntityFactory, field string, values map[string]any, keys ...string) (affected int64, err error)

	// Query Utility struct method to build a query
	Query(factory EntityFactory) IQuery

	// ExecuteDDL Execute DDL - create table and indexes
	ExecuteDDL(ddl map[string][]string) (err error)

	// ExecuteSQL Execute SQL - execute SQL command
	ExecuteSQL(sql string, args ...any) (affected int64, err error)

	// ExecuteQuery Execute native SQL query
	ExecuteQuery(sql string, args ...any) ([]Json, error)

	// DropTable Drop table and indexes
	DropTable(table string) (err error)

	// PurgeTable Fast delete table content (truncate)
	PurgeTable(table string) (err error)
}

IDatabase Database interface

func NewInMemoryDatabase

func NewInMemoryDatabase() (dbs IDatabase, err error)

NewInMemoryDatabase Factory method for database

type IDatastore

type IDatastore interface {

	// Closer includes method Close()
	io.Closer

	// Ping tests database connectivity for retries number of time with time interval (in seconds) between retries
	Ping(retries uint, intervalInSeconds uint) error

	// CloneDatastore Returns a clone (copy) of the instance
	CloneDatastore() (IDatastore, error)

	// Get a single entity by ID
	Get(factory EntityFactory, entityID string, keys ...string) (result Entity, err error)

	// List gets multiple entities by IDs
	List(factory EntityFactory, entityIDs []string, keys ...string) (list []Entity, err error)

	// Exists checks if entity exists by ID
	Exists(factory EntityFactory, entityID string, keys ...string) (result bool, err error)

	// Insert a new entity
	Insert(entity Entity) (added Entity, err error)

	// Update an existing entity
	Update(entity Entity) (updated Entity, err error)

	// Upsert update entity or create it if it does not exist
	Upsert(entity Entity) (updated Entity, err error)

	// Delete entity by id and shard (key)
	Delete(factory EntityFactory, entityID string, keys ...string) (err error)

	// BulkInsert inserts multiple entities
	BulkInsert(entities []Entity) (affected int64, err error)

	// BulkUpdate updates multiple entities
	BulkUpdate(entities []Entity) (affected int64, err error)

	// BulkUpsert update or insert multiple entities
	BulkUpsert(entities []Entity) (affected int64, err error)

	// BulkDelete delete multiple entities by IDs
	BulkDelete(factory EntityFactory, entityIDs []string, keys ...string) (affected int64, err error)

	// SetField update a single field of the document in a single transaction (eliminates the need to fetch - change - update)
	SetField(factory EntityFactory, entityID string, field string, value any, keys ...string) (err error)

	// SetFields update some fields of the document in a single transaction (eliminates the need to fetch - change - update)
	SetFields(factory EntityFactory, entityID string, fields map[string]any, keys ...string) (err error)

	// Query is a factory method for query builder Utility
	Query(factory EntityFactory) IQuery

	// IndexExists tests if index exists
	IndexExists(indexName string) (exists bool)

	// CreateIndex creates an index (without mapping)
	CreateIndex(indexName string) (name string, err error)

	// CreateEntityIndex creates an index of entity and add entity field mapping
	CreateEntityIndex(factory EntityFactory, key string) (name string, err error)

	// ListIndices returns a list of all indices matching the pattern
	ListIndices(pattern string) (map[string]int, error)

	// DropIndex drops an index
	DropIndex(indexName string) (ack bool, err error)

	// ExecuteQuery Execute native KQL query
	ExecuteQuery(query string, args ...any) ([]Json, error)
}

IDatastore interface for NoSQL Big Data wrapper implementations

func NewInMemoryDatastore

func NewInMemoryDatastore() (dbs IDatastore, err error)

NewInMemoryDatastore Factory method for Datastore

type ILocker added in v1.2.54

type ILocker interface {
	// Key returns the locker key
	Key() string

	// Token returns the token value set by the lock.
	Token() string

	// TTL returns the remaining time-to-live. Returns 0 if the lock has expired.
	TTL(ctx context.Context) (time.Duration, error)

	// Refresh extends the lock with a new TTL.
	Refresh(ctx context.Context, ttl time.Duration) error

	// Release manually releases the lock.
	Release(ctx context.Context) error
}

ILocker represents distributed lock

type IQuery

type IQuery interface {

	// Apply adds a callback to apply on each result entity in the query
	Apply(cb func(in Entity) Entity) IQuery

	// Filter Add single field filter
	Filter(filter QueryFilter) IQuery

	// Range add time frame filter on specific time field
	Range(field string, from Timestamp, to Timestamp) IQuery

	// MatchAll Add list of filters, all of them should be satisfied (AND)
	MatchAll(filters ...QueryFilter) IQuery

	// MatchAny Add list of filters, any of them should be satisfied (OR)
	MatchAny(filters ...QueryFilter) IQuery

	// Sort Add sort order by field,  expects sort parameter in the following form: field_name (Ascending) or field_name- (Descending)
	Sort(sort string) IQuery

	// Page Set page number (for pagination)
	Page(page int) IQuery

	// Limit Set page size limit (for pagination)
	Limit(page int) IQuery

	// List Execute a query to get list of entities by IDs (the criteria is ignored)
	List(entityIDs []string, keys ...string) (out []Entity, err error)

	// Find Execute the query based on the criteria, order and pagination
	Find(keys ...string) (out []Entity, total int64, err error)

	// Select is similar to find but with ability to retrieve specific fields
	Select(fields ...string) ([]Json, error)

	// Count Execute the query based on the criteria, order and pagination and return only the count of matching rows
	Count(keys ...string) (total int64, err error)

	// Aggregation Execute the query based on the criteria, order and pagination and return the provided aggregation function on the field
	// supported functions: count : avg, sum, min, max
	Aggregation(field string, function AggFunc, keys ...string) (value float64, err error)

	// GroupCount Execute the query based on the criteria, grouped by field and return count per group
	GroupCount(field string, keys ...string) (out map[any]int64, total int64, err error)

	// GroupAggregation Execute the query based on the criteria, order and pagination and return the aggregated value per group
	// the data point is a calculation of the provided function on the selected field, each data point includes the number of documents and the calculated value
	// the total is the sum of all calculated values in all the buckets
	// supported functions: count : avg, sum, min, max
	GroupAggregation(field string, function AggFunc, keys ...string) (out map[any]Tuple[int64, float64], total float64, err error)

	// Histogram returns a time series data points based on the time field, supported intervals: Minute, Hour, Day, week, month
	// the data point is a calculation of the provided function on the selected field, each data point includes the number of documents and the calculated value
	// the total is the sum of all calculated values in all the buckets
	// supported functions: count : avg, sum, min, max
	Histogram(field string, function AggFunc, timeField string, interval time.Duration, keys ...string) (out map[Timestamp]Tuple[int64, float64], total float64, err error)

	// Histogram2D returns a two-dimensional time series data points based on the time field, supported intervals: Minute, Hour, Day, week, month
	// the data point is a calculation of the provided function on the selected field
	// supported functions: count : avg, sum, min, max
	Histogram2D(field string, function AggFunc, dim, timeField string, interval time.Duration, keys ...string) (out map[Timestamp]map[any]Tuple[int64, float64], total float64, err error)

	// FindSingle Execute query based on the where criteria to get a single (the first) result
	FindSingle(keys ...string) (entity Entity, err error)

	// GetMap Execute query based on the criteria, order and pagination and return the results as a map of id->Entity
	GetMap(keys ...string) (out map[string]Entity, err error)

	// GetIDs executes a query based on the where criteria, order and pagination and return the results as a list of Ids
	GetIDs(keys ...string) (out []string, err error)

	// Delete the entities satisfying the criteria
	Delete(keys ...string) (total int64, err error)

	// SetField Update single field of all the documents meeting the criteria in a single transaction
	SetField(field string, value any, keys ...string) (total int64, err error)

	// SetFields Update multiple fields of all the documents meeting the criteria in a single transaction
	SetFields(fields map[string]any, keys ...string) (total int64, err error)

	// ToString Get the string representation of the query
	ToString() string
}

IQuery Database Query interface

type ITable

type ITable interface {

	// Get single entity by ID
	Get(entityID string) (entity Entity, err error)

	// Exists checks if entity exists by ID
	Exists(entityID string) (result bool, err error)

	// Insert entity
	Insert(entity Entity) (added Entity, err error)

	// Update entity
	Update(entity Entity) (added Entity, err error)

	// Upsert update entity or insert if not found
	Upsert(entity Entity) (added Entity, err error)

	// Delete entity
	Delete(entityID string) (err error)

	// Table get access to the underlying data structure
	Table() (result map[string]Entity)
}

ITable is a database table interface

func NewInMemTable

func NewInMemTable() ITable

NewInMemTable factory method

type InMemoryDataCache

type InMemoryDataCache struct {
	// contains filtered or unexported fields
}

InMemoryDataCache represent in memory data cache

func (*InMemoryDataCache) Add added in v1.2.1

func (dc *InMemoryDataCache) Add(key string, entity Entity, expiration time.Duration) (result bool, err error)

Add Set the value of a key only if the key does not exist

func (*InMemoryDataCache) AddRaw added in v1.2.54

func (dc *InMemoryDataCache) AddRaw(key string, bytes []byte, expiration time.Duration) (result bool, err error)

AddRaw sets the raw value of a key only if the key does not exist

func (*InMemoryDataCache) BLPop

func (dc *InMemoryDataCache) BLPop(factory EntityFactory, timeout time.Duration, keys ...string) (key string, entity Entity, err error)

BLPop Remove and get the first element in a list or block until one is available

func (*InMemoryDataCache) BRPop

func (dc *InMemoryDataCache) BRPop(factory EntityFactory, timeout time.Duration, keys ...string) (key string, value Entity, err error)

BRPop Remove and get the last element in a list or block until one is available

func (*InMemoryDataCache) CloneDataCache added in v1.2.62

func (dc *InMemoryDataCache) CloneDataCache() (IDataCache, error)

CloneDataCache Returns a clone (copy) of the instance

func (*InMemoryDataCache) Close

func (dc *InMemoryDataCache) Close() error

Close cache and free resources

func (*InMemoryDataCache) Del

func (dc *InMemoryDataCache) Del(keys ...string) (err error)

Del Delete keys

func (*InMemoryDataCache) Exists

func (dc *InMemoryDataCache) Exists(key string) (result bool, err error)

Exists checks if key exists

func (*InMemoryDataCache) Get

func (dc *InMemoryDataCache) Get(factory EntityFactory, key string) (result Entity, err error)

Get the value of a key

func (*InMemoryDataCache) GetKeys added in v1.2.1

func (dc *InMemoryDataCache) GetKeys(factory EntityFactory, keys ...string) (results []Entity, err error)

GetKeys Get the value of all the given keys

func (*InMemoryDataCache) GetRaw added in v1.2.54

func (dc *InMemoryDataCache) GetRaw(key string) (res []byte, err error)

GetRaw gets the raw value of a key

func (*InMemoryDataCache) GetRawKeys added in v1.2.54

func (dc *InMemoryDataCache) GetRawKeys(keys ...string) ([]Tuple[string, []byte], error)

GetRawKeys gets the raw value of all the given keys

func (*InMemoryDataCache) HAdd added in v1.2.1

func (dc *InMemoryDataCache) HAdd(key, field string, entity Entity) (result bool, err error)

HAdd sets the value of a key only if the key does not exist

func (*InMemoryDataCache) HAddRaw added in v1.2.54

func (dc *InMemoryDataCache) HAddRaw(key, field string, bytes []byte) (result bool, err error)

HAddRaw sets the raw value of a key only if the key does not exist

func (*InMemoryDataCache) HDel

func (dc *InMemoryDataCache) HDel(key string, fields ...string) (err error)

HDel delete one or more hash fields

func (*InMemoryDataCache) HExists

func (dc *InMemoryDataCache) HExists(key, field string) (result bool, err error)

HExists Check if key exists

func (*InMemoryDataCache) HGet

func (dc *InMemoryDataCache) HGet(factory EntityFactory, key, field string) (result Entity, err error)

HGet gets the value of a hash field

func (*InMemoryDataCache) HGetAll

func (dc *InMemoryDataCache) HGetAll(factory EntityFactory, key string) (result map[string]Entity, err error)

HGetAll gets all the fields and values in a hash

func (*InMemoryDataCache) HGetRaw added in v1.2.54

func (dc *InMemoryDataCache) HGetRaw(key, field string) ([]byte, error)

HGetRaw gets the raw value of a hash field

func (*InMemoryDataCache) HGetRawAll added in v1.2.54

func (dc *InMemoryDataCache) HGetRawAll(key string) (result map[string][]byte, err error)

HGetRawAll gets all the fields and raw values in a hash

func (*InMemoryDataCache) HKeys

func (dc *InMemoryDataCache) HKeys(key string) (fields []string, err error)

HKeys get all the fields in a hash

func (*InMemoryDataCache) HSet

func (dc *InMemoryDataCache) HSet(key, field string, entity Entity) (err error)

HSet sets the value of a hash field

func (*InMemoryDataCache) HSetNX

func (dc *InMemoryDataCache) HSetNX(key, field string, entity Entity) (bool, error)

HSetNX Set value of key only if it is not exist with optional expiration, return false if the key exists

func (*InMemoryDataCache) HSetRaw added in v1.2.54

func (dc *InMemoryDataCache) HSetRaw(key, field string, bytes []byte) (err error)

HSetRaw sets the raw value of a hash field

func (*InMemoryDataCache) HSetRawNX added in v1.2.54

func (dc *InMemoryDataCache) HSetRawNX(key, field string, bytes []byte) (bool, error)

HSetRawNX sets the raw value of key only if it is not exist with optional expiration, return false if the key exists

func (*InMemoryDataCache) LLen

func (dc *InMemoryDataCache) LLen(key string) (result int64)

LLen Get the length of a list

func (*InMemoryDataCache) LPop

func (dc *InMemoryDataCache) LPop(factory EntityFactory, key string) (entity Entity, err error)

LPop Remove and get the first element in a list

func (*InMemoryDataCache) LPush

func (dc *InMemoryDataCache) LPush(key string, value ...Entity) (err error)

LPush Prepend (add to the left) one or multiple values to a list

func (*InMemoryDataCache) LRange

func (dc *InMemoryDataCache) LRange(factory EntityFactory, key string, start, stop int64) (result []Entity, err error)

LRange Get a range of elements from list

func (*InMemoryDataCache) ObtainLocker added in v1.2.54

func (dc *InMemoryDataCache) ObtainLocker(key string, ttl time.Duration) (ILocker, error)

ObtainLocker tries to obtain a new lock using a key with the given TTL

func (*InMemoryDataCache) Ping

func (dc *InMemoryDataCache) Ping(retries uint, interval uint) error

Ping tests connectivity for retries number of time with time interval (in seconds) between retries

func (*InMemoryDataCache) RPop

func (dc *InMemoryDataCache) RPop(factory EntityFactory, key string) (entity Entity, err error)

RPop Remove and get the last element in a list

func (*InMemoryDataCache) RPush

func (dc *InMemoryDataCache) RPush(key string, value ...Entity) (err error)

RPush append (add to the right) one or multiple values to a list

func (*InMemoryDataCache) Rename

func (dc *InMemoryDataCache) Rename(key string, newKey string) (err error)

Rename a key

func (*InMemoryDataCache) Scan

func (dc *InMemoryDataCache) Scan(from uint64, match string, count int64) (keys []string, cursor uint64, err error)

Scan keys from the provided cursor

func (*InMemoryDataCache) Set

func (dc *InMemoryDataCache) Set(key string, entity Entity, expiration ...time.Duration) (err error)

Set value of key with optional expiration

func (*InMemoryDataCache) SetNX

func (dc *InMemoryDataCache) SetNX(key string, entity Entity, expiration ...time.Duration) (bool, error)

SetNX Set value of key only if it is not exist with optional expiration, return false if the key exists

func (*InMemoryDataCache) SetRaw added in v1.2.54

func (dc *InMemoryDataCache) SetRaw(key string, bytes []byte, expiration ...time.Duration) (err error)

SetRaw sets the raw value of key with optional expiration

func (*InMemoryDataCache) SetRawNX added in v1.2.54

func (dc *InMemoryDataCache) SetRawNX(key string, bytes []byte, expiration ...time.Duration) (bool, error)

SetRawNX sets the raw value of key only if it is not exist with optional expiration, return false if the key exists

type InMemoryDatabase

type InMemoryDatabase struct {
	// contains filtered or unexported fields
}

InMemoryDatabase represents in memory database with tables

func (*InMemoryDatabase) BulkDelete

func (dbs *InMemoryDatabase) BulkDelete(factory EntityFactory, entityIDs []string, keys ...string) (affected int64, err error)

BulkDelete delete multiple entities by id list

func (*InMemoryDatabase) BulkInsert

func (dbs *InMemoryDatabase) BulkInsert(entities []Entity) (affected int64, err error)

BulkInsert adds multiple entities to data store (all must be of the same type)

func (*InMemoryDatabase) BulkSetFields added in v1.2.51

func (dbs *InMemoryDatabase) BulkSetFields(factory EntityFactory, field string, values map[string]any, keys ...string) (affected int64, error error)

BulkSetFields Update specific field of multiple entities in a single transaction (eliminates the need to fetch - change - update) The field is the name of the field, values is a map of entityId -> field value

func (*InMemoryDatabase) BulkUpdate

func (dbs *InMemoryDatabase) BulkUpdate(entities []Entity) (affected int64, err error)

BulkUpdate updates multiple entities in the data store (all must be of the same type)

func (*InMemoryDatabase) BulkUpsert

func (dbs *InMemoryDatabase) BulkUpsert(entities []Entity) (affected int64, err error)

BulkUpsert update or insert multiple entities in the data store (all must be of the same type)

func (*InMemoryDatabase) CloneDatabase added in v1.2.62

func (dbs *InMemoryDatabase) CloneDatabase() (IDatabase, error)

CloneDatabase Returns a clone (copy) of the database instance

func (*InMemoryDatabase) Close

func (dbs *InMemoryDatabase) Close() error

Close DB and free resources

func (*InMemoryDatabase) Delete

func (dbs *InMemoryDatabase) Delete(factory EntityFactory, entityID string, keys ...string) (err error)

Delete entity by id

func (*InMemoryDatabase) DropTable

func (dbs *InMemoryDatabase) DropTable(table string) (err error)

DropTable drop a table and its related indexes

func (*InMemoryDatabase) ExecuteDDL

func (dbs *InMemoryDatabase) ExecuteDDL(ddl map[string][]string) (err error)

ExecuteDDL create table and indexes The ddl parameter is a map of strings (table names) to array of strings (list of fields to index)

func (*InMemoryDatabase) ExecuteQuery added in v1.2.64

func (dbs *InMemoryDatabase) ExecuteQuery(sql string, args ...any) ([]Json, error)

ExecuteQuery Execute native SQL query

func (*InMemoryDatabase) ExecuteSQL

func (dbs *InMemoryDatabase) ExecuteSQL(sql string, args ...any) (affected int64, err error)

ExecuteSQL execute raw SQL command

func (*InMemoryDatabase) Exists

func (dbs *InMemoryDatabase) Exists(factory EntityFactory, entityID string, keys ...string) (result bool, err error)

Exists checks if entity exists by ID

func (*InMemoryDatabase) Get

func (dbs *InMemoryDatabase) Get(factory EntityFactory, entityID string, keys ...string) (result Entity, err error)

Get single entity by ID

func (*InMemoryDatabase) Insert

func (dbs *InMemoryDatabase) Insert(entity Entity) (added Entity, err error)

Insert Add new entity

func (*InMemoryDatabase) List

func (dbs *InMemoryDatabase) List(factory EntityFactory, entityIDs []string, keys ...string) (list []Entity, err error)

List get a list of entities by IDs

func (*InMemoryDatabase) Ping

func (dbs *InMemoryDatabase) Ping(retries uint, interval uint) error

Ping Test database connectivity @param retries - how many retries are required (max 10) @param interval - time interval (in seconds) between retries (max 60)

func (*InMemoryDatabase) PurgeTable

func (dbs *InMemoryDatabase) PurgeTable(table string) (err error)

PurgeTable fast delete table content (truncate)

func (*InMemoryDatabase) Query

func (dbs *InMemoryDatabase) Query(factory EntityFactory) IQuery

Query is a builder method to construct query

func (*InMemoryDatabase) SetField

func (dbs *InMemoryDatabase) SetField(factory EntityFactory, entityID string, field string, value any, keys ...string) (err error)

SetField updates single field of the document in a single transaction (eliminates the need to fetch - change - update)

func (*InMemoryDatabase) SetFields

func (dbs *InMemoryDatabase) SetFields(factory EntityFactory, entityID string, fields map[string]any, keys ...string) (err error)

SetFields Updates some numeric fields of the document in a single transaction (eliminates the need to fetch - change - update)

func (*InMemoryDatabase) Update

func (dbs *InMemoryDatabase) Update(entity Entity) (updated Entity, err error)

Update existing entity in the data store

func (*InMemoryDatabase) Upsert

func (dbs *InMemoryDatabase) Upsert(entity Entity) (updated Entity, err error)

Upsert updates existing entity in the data store or add it if it does not exist

type InMemoryDatastore

type InMemoryDatastore struct {
	// contains filtered or unexported fields
}

InMemoryDatastore Represent a db with tables

func (*InMemoryDatastore) BulkDelete

func (dbs *InMemoryDatastore) BulkDelete(factory EntityFactory, entityIDs []string, keys ...string) (affected int64, err error)

BulkDelete delete multiple entities by IDs

func (*InMemoryDatastore) BulkInsert

func (dbs *InMemoryDatastore) BulkInsert(entities []Entity) (affected int64, err error)

BulkInsert inserts multiple entities

func (*InMemoryDatastore) BulkSetFields added in v1.2.51

func (dbs *InMemoryDatastore) BulkSetFields(factory EntityFactory, field string, values map[string]any, keys ...string) (affected int64, error error)

BulkSetFields Update specific field of multiple entities in a single transaction (eliminates the need to fetch - change - update) The field is the name of the field, values is a map of entityId -> field value

func (*InMemoryDatastore) BulkUpdate

func (dbs *InMemoryDatastore) BulkUpdate(entities []Entity) (affected int64, err error)

BulkUpdate updates multiple entities

func (*InMemoryDatastore) BulkUpsert

func (dbs *InMemoryDatastore) BulkUpsert(entities []Entity) (affected int64, err error)

BulkUpsert update or insert multiple entities

func (*InMemoryDatastore) CloneDatastore added in v1.2.62

func (dbs *InMemoryDatastore) CloneDatastore() (IDatastore, error)

CloneDatastore Returns a clone (copy) of the instance

func (*InMemoryDatastore) Close

func (dbs *InMemoryDatastore) Close() error

Close Datastore and free resources

func (*InMemoryDatastore) CreateEntityIndex

func (dbs *InMemoryDatastore) CreateEntityIndex(factory EntityFactory, key string) (name string, err error)

CreateEntityIndex creates an index of entity and add entity field mapping

func (*InMemoryDatastore) CreateIndex

func (dbs *InMemoryDatastore) CreateIndex(indexName string) (name string, err error)

CreateIndex creates an index (without mapping)

func (*InMemoryDatastore) Delete

func (dbs *InMemoryDatastore) Delete(factory EntityFactory, entityID string, keys ...string) (err error)

Delete entity by id and shard (key)

func (*InMemoryDatastore) DropIndex

func (dbs *InMemoryDatastore) DropIndex(indexName string) (ack bool, err error)

DropIndex drops an index

func (*InMemoryDatastore) ExecuteQuery added in v1.2.80

func (dbs *InMemoryDatastore) ExecuteQuery(query string, args ...any) ([]Json, error)

ExecuteQuery Execute native KQL query

func (*InMemoryDatastore) Exists

func (dbs *InMemoryDatastore) Exists(factory EntityFactory, entityID string, keys ...string) (result bool, err error)

Exists checks if entity exists by ID

func (*InMemoryDatastore) Get

func (dbs *InMemoryDatastore) Get(factory EntityFactory, entityID string, keys ...string) (result Entity, err error)

Get a single entity by ID

func (*InMemoryDatastore) IndexExists

func (dbs *InMemoryDatastore) IndexExists(indexName string) (exists bool)

IndexExists tests if index exists

func (*InMemoryDatastore) Insert

func (dbs *InMemoryDatastore) Insert(entity Entity) (added Entity, err error)

Insert a new entity

func (*InMemoryDatastore) List

func (dbs *InMemoryDatastore) List(factory EntityFactory, entityIDs []string, keys ...string) (list []Entity, err error)

List gets multiple entities by IDs

func (*InMemoryDatastore) ListIndices added in v1.2.71

func (dbs *InMemoryDatastore) ListIndices(pattern string) (map[string]int, error)

ListIndices returns a list of all indices matching the pattern

func (*InMemoryDatastore) Ping

func (dbs *InMemoryDatastore) Ping(retries uint, interval uint) error

Ping tests database connectivity for retries number of time with time interval (in seconds) between retries

func (*InMemoryDatastore) Query

func (dbs *InMemoryDatastore) Query(factory EntityFactory) IQuery

Query is a factory method for query builder Utility

func (*InMemoryDatastore) SetField

func (dbs *InMemoryDatastore) SetField(factory EntityFactory, entityID string, field string, value any, keys ...string) (err error)

SetField update a single field of the document in a single transaction (eliminates the need to fetch - change - update)

func (*InMemoryDatastore) SetFields

func (dbs *InMemoryDatastore) SetFields(factory EntityFactory, entityID string, fields map[string]any, keys ...string) (err error)

SetFields update some fields of the document in a single transaction (eliminates the need to fetch - change - update)

func (*InMemoryDatastore) Update

func (dbs *InMemoryDatastore) Update(entity Entity) (updated Entity, err error)

Update an existing entity

func (*InMemoryDatastore) Upsert

func (dbs *InMemoryDatastore) Upsert(entity Entity) (updated Entity, err error)

Upsert update entity or create it if it does not exist

type InMemoryTable

type InMemoryTable struct {
	// contains filtered or unexported fields
}

InMemoryTable represents a table in the DB

func (*InMemoryTable) Delete

func (tbl *InMemoryTable) Delete(entityID string) (err error)

Delete entity

func (*InMemoryTable) Exists

func (tbl *InMemoryTable) Exists(entityID string) (result bool, err error)

Exists checks if entity exists by ID

func (*InMemoryTable) Get

func (tbl *InMemoryTable) Get(entityID string) (entity Entity, err error)

Get single entity by ID

func (*InMemoryTable) Insert

func (tbl *InMemoryTable) Insert(entity Entity) (added Entity, err error)

Insert entity

func (*InMemoryTable) Table

func (tbl *InMemoryTable) Table() (result map[string]Entity)

Table get access to the underlying data structure

func (*InMemoryTable) Update

func (tbl *InMemoryTable) Update(entity Entity) (added Entity, err error)

Update entity

func (*InMemoryTable) Upsert

func (tbl *InMemoryTable) Upsert(entity Entity) (added Entity, err error)

Upsert update entity or insert if not found

type QueryFilter

type QueryFilter interface {

	// Eq - Equal
	Eq(value any) QueryFilter

	// Neq - Not equal
	Neq(value any) QueryFilter

	// Like - similar
	Like(value string) QueryFilter

	// Gt - Greater than
	Gt(value any) QueryFilter

	// Gte - Greater or equal
	Gte(value any) QueryFilter

	// Lt - Less than
	Lt(value any) QueryFilter

	// Lte - Less or equal
	Lte(value any) QueryFilter

	// In - mach one of the values
	In(values ...any) QueryFilter

	// NotIn - Not In
	NotIn(values ...any) QueryFilter

	// Between - equal or greater than the lower boundary and equal or less than the upper boundary
	Between(value1, value2 any) QueryFilter

	// If - Include this filter only if condition is true
	If(value bool) QueryFilter

	// IsActive Include this filter only if condition is true
	IsActive() bool

	// GetField Get the field name
	GetField() string

	// GetOperator Get the criteria operator
	GetOperator() QueryOperator

	// GetValues Get the criteria values
	GetValues() []any

	// GetStringValue Get string representation of the value
	GetStringValue(index int) string
}

QueryFilter Query filter interface

func F added in v1.2.13

func F(field string) QueryFilter

F filter by field (synonym to Filter)

func Filter

func Filter(field string) QueryFilter

Filter by field

type QueryOperator

type QueryOperator string

Jump to

Keyboard shortcuts

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