sophia

package module
v0.0.0-...-afcd224 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: BSD-2-Clause Imports: 6 Imported by: 0

README

Build Status Coverage Status Go Report Card codebeat badge GoDoc

go-sophia

go-sophia is a Go (golang) binding to the Sophia key-value database (http://sophia.systems/)

#Installation The sophia sources are bundled with the go-sophia, so you should make only go get github.com/pzhin/go-sophia to install it.

#Library information Used Sophia v2.2 (commit 1419633)

Documentation

Index

Constants

View Source
const (
	// CursorPrefix uses for setting cursor prefix
	CursorPrefix = "prefix"
	// CursorOrder uses for setting cursor order
	CursorOrder = "order"
)
View Source
const EnvironmentPath = "sophia.path"

Variables

View Source
var ErrCursorClosed = errors.New("usage of closed Cursor")

ErrCursorClosed will be returned in case of closed cursor usage

View Source
var ErrEnvironmentClosed = errors.New("usage of closed environment")
View Source
var ErrNotFound = errors.New("document not found")

ErrNotFound error constant for 'NotFount' cases

Functions

This section is empty.

Types

type CompressionType

type CompressionType byte

CompressionType type of compression for content

const (
	CompressionTypeNone CompressionType = iota
	CompressionTypeLZ4
	CompressionTypeZSTD
)

CompressionType constants for different types of compression

func (CompressionType) String

func (t CompressionType) String() string

type Cursor

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

Cursor iterates over key-values in a database.

func (*Cursor) Close

func (cur *Cursor) Close() error

Close closes the cursor. If a cursor is not closed, future operations on the database can hang indefinitely. Cursor won't be accessible after this

func (*Cursor) Next

func (cur *Cursor) Next() Document

Next fetches the next row for the cursor Returns next row if it exists else it will return nil

type Database

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

Database is used for accessing a database. Take it's name from sophia. Usually object with same features is called 'table'.

func (*Database) Cursor

func (db *Database) Cursor(doc Document) (*Cursor, error)

Cursor returns a Cursor for iterating over rows in the database

func (Database) Delete

func (d Database) Delete(doc Document) error

Delete deletes row with specified set of keys.

func (*Database) Document

func (db *Database) Document() Document

Document creates a Document for a single or multi-statement transactions

func (Database) Get

func (d Database) Get(doc Document) (Document, error)

Get retrieves the row for the set of keys.

func (Database) Set

func (d Database) Set(doc Document) error

Set sets the row of the set of keys.

func (Database) Upsert

func (d Database) Upsert(doc Document) error

Upsert sets the row of the set of keys.

type DatabaseConfig

type DatabaseConfig struct {
	// Name of database.
	// It will be used to set and get values specific to this base.
	Name string
	// Schema of database.
	// It is used to describe the keys and values that will be stored in the database.
	Schema *Schema
	// CacheSize precalculated memory usage (cache size) for expected storage capacity and write rates.
	// See more http://sophia.systems/v2.2/admin/memory_requirements.html
	CompactionCacheSize int64
	// CompactionNodeSize set a node file size in bytes.
	// Node file can grow up to two times the size before the old node file is being split.
	CompactionNodeSize int64
	// CompactionNodeSize set size of a page to use.
	CompactionPageSize int64
	// CompactionPageChecksum check checksum during compaction.
	DisableCompactionPageChecksum bool
	// CompactionExpirePeriod set expire check process period in seconds.
	CompactionExpirePeriod int64
	// CompactionGcWm when this value reaches a compaction, operation is scheduled.
	// Garbage collection starts when watermark value reaches a certain percent of duplicates.
	CompactionGCWatermark int64
	// CompactionGCPeriod run check for a gc every CompactionGCPeriod seconds.
	CompactionGCPeriod int64
	// DisableMmapMode can be set to disable mmap mode.
	// By default Sophia uses pread(2) to read data from disk.
	// Using mmap mode, Sophia handles all requests by directly accessing memory-mapped node files memory.
	//
	// It is a good idea to try this mode, even if your dataset is rather small
	// or you need to handle a large ratio of read request with a predictable pattern.
	//
	// Disadvantage of mmap mode, in comparison to RAM Storage,
	// is a possible unpredictable latency behaviour and a OS cache warmup period after recovery.
	DisableMmapMode bool
	// DirectIO can be set to enable O_DIRECT to see what actual read
	// performance might be, if we avoid using file system cache.
	//
	// When a database size is lower then RAM, it probably sits in file system cache and all operations do very little actual IO.
	// In some sense, when a database grows in size load scenario might change from from CPU bound to IO bound.
	// It is possible to set DirectIO=true and MmapMode=false to get it.
	// DirectIO=true and MmapMode=true will cause panic.
	DirectIO bool
	// DisableSync can be set to disable sync node file on compaction completion.
	DisableSync bool
	// Expire can be set to enable or disable key expire.
	Expire bool
	// Compression specify compression driver. Supported: lz4, zstd, none (default).
	Compression CompressionType
	// Upsert is a function that will be called on every upsert operation.
	// If it was not set during the configuration database, upsert operation will not be available
	Upsert UpsertFunc
	// UpsertArg an argument which is additionally passed every call
	UpsertArg interface{}
}

DatabaseConfig a structure for the description of the database to be created.

type Document

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

Document is a representation of a row in a database. Destroy should be called after Document usage.

func (*Document) Destroy

func (d *Document) Destroy() error

Destroy call C function that releases all resources associated with the Document

func (*Document) Free

func (s *Document) Free()

Free frees allocated memory for all C variables, that were in this store This always should be called to prevent memory leaks

func (*Document) Get

func (s *Document) Get(path string, size *int) unsafe.Pointer

func (*Document) GetInt

func (s *Document) GetInt(path string) int64

func (*Document) GetObject

func (s *Document) GetObject(path string) unsafe.Pointer

func (*Document) GetString

func (s *Document) GetString(path string, size *int) string

GetString returns string without extra allocations. We can use C pointer to string to make Go string without allocation. C memory will be freed on Document Destroy() call. So for long-term usage you should to make copy of string to avoid data corruption.

func (*Document) IsEmpty

func (s *Document) IsEmpty() bool

func (*Document) Set

func (s *Document) Set(path string, val interface{}) bool

TODO :: implement custom types

func (*Document) SetInt

func (s *Document) SetInt(path string, val int64) bool

func (*Document) SetString

func (s *Document) SetString(path, val string) bool

type Environment

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

Environment is used to configure the database before opening. Take it's name from sophia Usually object with same features are called 'database'

func NewEnvironment

func NewEnvironment() (*Environment, error)

NewEnvironment creates a new environment for opening a database. Receivers must call Close() on the returned Environment.

func (*Environment) BeginTx

func (env *Environment) BeginTx() (*Transaction, error)

BeginTx starts an Transaction Commit() or Rollback() should be called to release resources.

func (*Environment) Close

func (env *Environment) Close() error

Close closes the environment and frees its associated memory. You must call Close on any Environment created with NewEnvironment.

func (*Environment) Error

func (env *Environment) Error() error

Error returns last received error

func (*Environment) Free

func (s *Environment) Free()

Free frees allocated memory for all C variables, that were in this store This always should be called to prevent memory leaks

func (*Environment) Get

func (s *Environment) Get(path string, size *int) unsafe.Pointer

func (*Environment) GetInt

func (s *Environment) GetInt(path string) int64

func (*Environment) GetObject

func (s *Environment) GetObject(path string) unsafe.Pointer

func (*Environment) GetString

func (s *Environment) GetString(path string, size *int) string

GetString returns string without extra allocations. We can use C pointer to string to make Go string without allocation. C memory will be freed on Document Destroy() call. So for long-term usage you should to make copy of string to avoid data corruption.

func (*Environment) IsEmpty

func (s *Environment) IsEmpty() bool

func (*Environment) NewDatabase

func (env *Environment) NewDatabase(config DatabaseConfig) (*Database, error)

NewDatabase creates new database in environment with given configuration. At least database's name should be defined. Another options aren't required. Database configuration can't be changed after Environment's Open() was called.

func (*Environment) Open

func (env *Environment) Open() error

Open opens environment At a minimum path must be specified and one db declared

func (*Environment) Set

func (s *Environment) Set(path string, val interface{}) bool

TODO :: implement custom types

func (*Environment) SetInt

func (s *Environment) SetInt(path string, val int64) bool

func (*Environment) SetString

func (s *Environment) SetString(path, val string) bool

type FieldType

type FieldType byte

FieldType type of key or value in a row

const (
	FieldTypeUInt8 FieldType = iota
	FieldTypeUInt16
	FieldTypeUInt32
	FieldTypeUInt64
	FieldTypeUInt8Rev
	FieldTypeUInt16Rev
	FieldTypeUInt32Rev
	FieldTypeUInt64Rev
	FieldTypeString
)

FieldType constants for different data types

func (FieldType) String

func (t FieldType) String() string

type Order

type Order string

Order string type of sophia cursor order

const (
	GreaterThan      Order = ">"
	GT               Order = GreaterThan
	GreaterThanEqual Order = ">="
	GTE              Order = GreaterThanEqual
	LessThan         Order = "<"
	LT               Order = LessThan
	LessThanEqual    Order = "<="
	LTE              Order = LessThanEqual
)

Constants for sophia cursor order They are used while creating cursor to select it's direction

type Schema

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

Schema is a structure for configuring fields which record will contain

func (*Schema) AddKey

func (s *Schema) AddKey(name string, typ FieldType) error

AddKey adds new key field for record. If record have already had field with such name error will be returned

func (*Schema) AddValue

func (s *Schema) AddValue(name string, typ FieldType) error

AddKey adds new value field for record. If record have already had field with such name error will be returned

type Transaction

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

Transaction multi-statement transaction is automatically processed when Set(), Delete(), Upsert(), Get() are used on a transactional object. The BeginTx() function is used to start a multi-statement transaction. During transaction, no updates are written to the database files until a Commit() is called. On commit, all modifications that were made are written to the log file in a single batch. To discard any changes made during transaction operation, Rollback() function should be used. No nested transactions are supported. There are no limit on a number of concurrent transactions. Any number of databases can be involved in a multi-statement transaction.

func (*Transaction) Commit

func (tx *Transaction) Commit() TxStatus

Commit commits the transaction and returns it's status. Any error happened during multi-statement transaction does not rollback a transaction.

func (Transaction) Delete

func (d Transaction) Delete(doc Document) error

Delete deletes row with specified set of keys.

func (Transaction) Get

func (d Transaction) Get(doc Document) (Document, error)

Get retrieves the row for the set of keys.

func (*Transaction) Rollback

func (tx *Transaction) Rollback() error

Rollback rollbacks transaction and destroy transaction object.

func (Transaction) Set

func (d Transaction) Set(doc Document) error

Set sets the row of the set of keys.

func (Transaction) Upsert

func (d Transaction) Upsert(doc Document) error

Upsert sets the row of the set of keys.

type TxStatus

type TxStatus int

TxStatus transactional status

const (
	// TxError means that transaction has been completed with error
	TxError TxStatus = -1
	// TxOk means that transaction has been completed
	TxOk TxStatus = 0
	// TxRollback status means that transaction has been rollbacked by another concurrent transaction
	TxRollback TxStatus = 1
	// TxLock status means that transaction is not finished and waiting for concurrent transaction to complete.
	// In that case commit should be retried later or transaction can be rollbacked.
	TxLock TxStatus = 2
)

type UpsertFunc

type UpsertFunc func(count int,
	src []unsafe.Pointer, srcSize []uint32,
	upsert []unsafe.Pointer, upsertSize []uint32,
	result []unsafe.Pointer, resultSize []uint32,
	arg unsafe.Pointer) int

UpsertFunc golang equivalent of upsert_callback. Should return 0 in case of success, otherwise -1.

Directories

Path Synopsis
manual_tests

Jump to

Keyboard shortcuts

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