storage

package
v0.12.4 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: ISC Imports: 15 Imported by: 0

Documentation

Overview

Package storage - maintain the on-disk data store

maintain separate pools of a number of elements in key->value form

This maintains a LevelDB database split into a series of tables. Each table is defined by a prefix byte that is obtained from the prefix tag in the struct defining the available tables.

Notes: 1. each separate pool has a single byte prefix (to spread the keys in LevelDB) 2. ⧺ = concatenation of byte data 3. BN = block number as 8 byte big endian (uint64) 4. txId = transaction digest as 32 byte SHA3-256(data) 5. asset id = fingerprint digest as 64 byte SHA3-512(data) 6. count = successive index value as 8 byte big endian (uint64) 7. owner = bitmark account (prefix ⧺ public key ≡ 33 bytes if Ed25519) 8. 00 = single byte values 00..ff 9. value = balance quantity value as 8 byte big endian (uint64) 10. *others* = byte values of various length

Blocks:

B ⧺ BN               - block store
                       data: header ⧺ (concat transactions)
2 ⧺ BN               - block Argon2 hashes
                       data: hash of block
H ⧺ BN               - current block currencies
                       data: map(currency → currency address)
I ⧺ txId             - current block owner transaction index
                       data: BN

Transactions:

T ⧺ txId             - confirmed transactions
                       data: BN ⧺ packed transaction data

Assets:

A ⧺ asset id         - confirmed asset identifier
                       data: BN ⧺ packed asset data

Ownership:

N ⧺ owner            - next count value to use for appending to owned items
                       data: count
L ⧺ owner ⧺ count    - list of owned items
                       data: txId
D ⧺ owner ⧺ txId     - position in list of owned items, for delete after transfer
                       data: count
P ⧺ txId             - owner data (00=asset, 01=block, 02=share) head of provenance chain
                       data: 00 ⧺ transfer BN ⧺ issue txId ⧺ issue BN ⧺ asset id
                       data: 01 ⧺ transfer BN ⧺ issue txId ⧺ issue BN ⧺ owned BN
                       data: 02 ⧺ transfer BN ⧺ issue txId ⧺ issue BN ⧺ asset id

Bitmark Shares (txId ≡ share id)

F ⧺ txId             - share total value (constant)
                       data: value ⧺ txId
Q ⧺ owner ⧺ txId     - current balance quantity of shares (ShareId) for each owner (deleted if value becomes zero)
                       data: value

Testing:

Z ⧺ key              - testing data

Index

Constants

View Source
const (
	ReadOnly  = true
	ReadWrite = false
)

pool access modes

Variables

View Source
var CheckpointKey = []byte("checkpoint")
View Source
var PaymentStorage struct {
	Btc P2PStorage
	Ltc P2PStorage
}
View Source
var Pool pools

Pool - the set of exported pools

Functions

func Finalise

func Finalise()

Finalise - close the database connection

func Initialise

func Initialise(dbPrefix string, readOnly bool) error

Initialise - open up the database connection

this must be called before any pool is accessed

func IsMigrationNeed added in v0.12.0

func IsMigrationNeed() bool

IsMigrationNeed - check if bitmarks database needs migration

Types

type Access added in v0.12.0

type Access interface {
	Abort()
	Begin() error
	Commit() error
	Delete([]byte)
	DumpTx() []byte
	Get([]byte) ([]byte, error)
	Has([]byte) (bool, error)
	InUse() bool
	Iterator(*ldb_util.Range) iterator.Iterator
	Put([]byte, []byte)
}

for Database

type AccessData added in v0.12.0

type AccessData struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*AccessData) Abort added in v0.12.0

func (d *AccessData) Abort()

func (*AccessData) Begin added in v0.12.0

func (d *AccessData) Begin() error

func (*AccessData) Commit added in v0.12.0

func (d *AccessData) Commit() error

func (*AccessData) Delete added in v0.12.0

func (d *AccessData) Delete(key []byte)

func (*AccessData) DumpTx added in v0.12.0

func (d *AccessData) DumpTx() []byte

func (*AccessData) Get added in v0.12.0

func (d *AccessData) Get(key []byte) ([]byte, error)

func (*AccessData) Has added in v0.12.0

func (d *AccessData) Has(key []byte) (bool, error)

func (*AccessData) InUse added in v0.12.0

func (d *AccessData) InUse() bool

func (*AccessData) Iterator added in v0.12.0

func (d *AccessData) Iterator(searchRange *ldb_util.Range) iterator.Iterator

func (*AccessData) Put added in v0.12.0

func (d *AccessData) Put(key []byte, value []byte)

type Cache added in v0.11.0

type Cache interface {
	Get(string) ([]byte, bool)
	Set(int, string, []byte)
	Clear()
}

type Element

type Element struct {
	Key   []byte
	Value []byte
}

Element - a binary data item

type FetchCursor

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

FetchCursor - cursor structure

func (*FetchCursor) Fetch

func (cursor *FetchCursor) Fetch(count int) ([]Element, error)

Fetch - return some elements starting from key

func (*FetchCursor) Map added in v0.8.0

func (cursor *FetchCursor) Map(f func(key []byte, value []byte) error) error

Map - run a function on all elements in the range

func (*FetchCursor) Seek

func (cursor *FetchCursor) Seek(key []byte) *FetchCursor

Seek - move cursor to specific key position

type Handle added in v0.11.0

type Handle interface {
	Begin()
	Commit() error
	Query
	Retrieve
	Update
}

Handle - interface for storage operations

type LevelDBPaymentStore added in v0.12.0

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

func NewLevelDBPaymentStore added in v0.12.0

func NewLevelDBPaymentStore(db *leveldb.DB) *LevelDBPaymentStore

func (*LevelDBPaymentStore) Close added in v0.12.0

func (l *LevelDBPaymentStore) Close() error

func (*LevelDBPaymentStore) DB added in v0.12.0

func (l *LevelDBPaymentStore) DB() *leveldb.DB

func (*LevelDBPaymentStore) GetCheckpoint added in v0.12.0

func (l *LevelDBPaymentStore) GetCheckpoint() (*chainhash.Hash, error)

GetCheckpoint returns the hash of the last saved checkpoint in the storage

func (*LevelDBPaymentStore) GetHash added in v0.12.0

func (l *LevelDBPaymentStore) GetHash(height int32) (*chainhash.Hash, error)

GetHash returns the hash of a give height

func (*LevelDBPaymentStore) GetHeight added in v0.12.0

func (l *LevelDBPaymentStore) GetHeight(hash *chainhash.Hash) (int32, error)

GetHeight returns height for a specific hash

func (*LevelDBPaymentStore) RollbackTo added in v0.12.0

func (l *LevelDBPaymentStore) RollbackTo(deleteFrom, deleteTo int32) error

RollbackTo deletes blocks from a block down a another

func (*LevelDBPaymentStore) SetCheckpoint added in v0.12.0

func (l *LevelDBPaymentStore) SetCheckpoint(height int32) error

SetCheckpoint saves the hash of a give height as a checkpoint

func (*LevelDBPaymentStore) StoreBlock added in v0.12.0

func (l *LevelDBPaymentStore) StoreBlock(height int32, hash *chainhash.Hash) error

StoreBlock saves a pair of hash and its height

func (*LevelDBPaymentStore) Table added in v0.12.0

func (l *LevelDBPaymentStore) Table(tableName string) *PaymentTable

type P2PStorage added in v0.12.0

type P2PStorage interface {
	DB() *leveldb.DB
	GetHash(height int32) (*chainhash.Hash, error)
	GetHeight(hash *chainhash.Hash) (int32, error)
	StoreBlock(height int32, hash *chainhash.Hash) error
	GetCheckpoint() (*chainhash.Hash, error)
	SetCheckpoint(height int32) error
	RollbackTo(deleteFrom, deleteTo int32) error
	Close() error
}

type PaymentTable added in v0.12.0

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

func (*PaymentTable) Delete added in v0.12.0

func (p *PaymentTable) Delete(key []byte) error

remove a key from the database

func (*PaymentTable) Get added in v0.12.0

func (p *PaymentTable) Get(key []byte) []byte

read a value for a given key

this returns the actual element - copy the result if it must be preserved

func (*PaymentTable) Has added in v0.12.0

func (p *PaymentTable) Has(key []byte) bool

Check if a key exists

func (*PaymentTable) Put added in v0.12.0

func (p *PaymentTable) Put(key []byte, value []byte) error

store a key/value bytes pair to the database

type PoolHandle

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

PoolHandle - the structure of a pool handle

func (*PoolHandle) Begin added in v0.11.0

func (p *PoolHandle) Begin()

func (*PoolHandle) Commit added in v0.11.0

func (p *PoolHandle) Commit() error

func (*PoolHandle) Get

func (p *PoolHandle) Get(key []byte) []byte

Get - read a value for a given key

this returns the actual element - copy the result if it must be preserved

func (*PoolHandle) GetN added in v0.8.0

func (p *PoolHandle) GetN(key []byte) (uint64, bool)

GetN - read a record and decode first 8 bytes as big endian uint64

second parameter is false if record was not found panics if not 8 (or more) bytes in the record

func (*PoolHandle) GetNB added in v0.8.0

func (p *PoolHandle) GetNB(key []byte) (uint64, []byte)

GetNB - read a record and decode first 8 bytes as big endian uint64 and return the rest of the record as byte slice

second parameter is nil if record was not found panics if not 9 (or more) bytes in the record this returns the actual element in the second parameter - copy the result if it must be preserved

func (*PoolHandle) Has

func (p *PoolHandle) Has(key []byte) bool

Has - Check if a key exists

func (*PoolHandle) LastElement

func (p *PoolHandle) LastElement() (Element, bool)

LastElement - get the last element in a pool

func (*PoolHandle) NewFetchCursor

func (p *PoolHandle) NewFetchCursor() *FetchCursor

NewFetchCursor - initialise a cursor to the start of a key range

func (*PoolHandle) Put

func (p *PoolHandle) Put(key []byte, value []byte, dummy []byte)

Put - store a key/value bytes pair to the database

func (*PoolHandle) PutN added in v0.10.0

func (p *PoolHandle) PutN(key []byte, value uint64)

PutN - store a uint8 as an 8 byte sequence

func (*PoolHandle) Ready added in v0.12.1

func (p *PoolHandle) Ready() bool

Ready - check if db is ready

func (*PoolHandle) Remove added in v0.12.1

func (p *PoolHandle) Remove(key []byte)

type PoolNB added in v0.8.0

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

PoolNB - handle for a storage pool

func (*PoolNB) Begin added in v0.11.0

func (p *PoolNB) Begin()

func (*PoolNB) Commit added in v0.11.0

func (p *PoolNB) Commit() error

func (*PoolNB) Get added in v0.11.0

func (p *PoolNB) Get(key []byte) []byte

Get - for interface

func (*PoolNB) GetN added in v0.11.0

func (p *PoolNB) GetN(key []byte) (uint64, bool)

GetN - for interface only

func (*PoolNB) GetNB added in v0.8.0

func (p *PoolNB) GetNB(key []byte) (uint64, []byte)

GetNB - read a record and decode first 8 bytes as big endian uint64 and return the rest of the record as byte slice

second parameter is nil if record was not found panics if not 9 (or more) bytes in the record this returns the actual element in the second parameter - copy the result if it must be preserved

func (*PoolNB) Has added in v0.8.0

func (p *PoolNB) Has(key []byte) bool

Has - Check if a key exists

func (*PoolNB) LastElement added in v0.12.1

func (p *PoolNB) LastElement() (Element, bool)

LastElement - last element

func (*PoolNB) NewFetchCursor added in v0.8.0

func (p *PoolNB) NewFetchCursor() *FetchCursor

NewFetchCursor - initialise a cursor to the start of a key range

func (*PoolNB) Put added in v0.8.0

func (p *PoolNB) Put(key []byte, nValue []byte, bValue []byte)

Put - store a key/value bytes pair to the database

func (*PoolNB) PutN added in v0.12.1

func (p *PoolNB) PutN(key []byte, value uint64)

func (*PoolNB) Ready added in v0.12.1

func (p *PoolNB) Ready() bool

Ready - check if struct is empty

func (*PoolNB) Remove added in v0.12.1

func (p *PoolNB) Remove(key []byte)

Remove - internally Remove a key from database

type Query added in v0.12.1

type Query interface {
	Has([]byte) bool
	Ready() bool
}

Query - interface for storage query data status

type Retrieve added in v0.12.1

type Retrieve interface {
	Get([]byte) []byte
	GetN([]byte) (uint64, bool)
	GetNB([]byte) (uint64, []byte)
	LastElement() (Element, bool)
	NewFetchCursor() *FetchCursor
}

Retrieve - interface for storage retrieve data

type Transaction added in v0.11.0

type Transaction interface {
	Abort()
	Begin() error
	Commit() error
	Delete(Handle, []byte)
	Get(Handle, []byte) []byte
	GetN(Handle, []byte) (uint64, bool)
	GetNB(Handle, []byte) (uint64, []byte)
	Has(Handle, []byte) bool
	InUse() bool
	Put(Handle, []byte, []byte, []byte)
	PutN(Handle, []byte, uint64)
}

Transaction - concept from RDBMS

func NewDBTransaction added in v0.11.0

func NewDBTransaction() (Transaction, error)

type TransactionData added in v0.12.0

type TransactionData struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*TransactionData) Abort added in v0.12.0

func (t *TransactionData) Abort()

func (*TransactionData) Begin added in v0.12.0

func (t *TransactionData) Begin() error

func (*TransactionData) Commit added in v0.12.0

func (t *TransactionData) Commit() error

func (*TransactionData) Delete added in v0.12.0

func (t *TransactionData) Delete(h Handle, key []byte)

func (*TransactionData) Get added in v0.12.0

func (t *TransactionData) Get(h Handle, key []byte) []byte

func (*TransactionData) GetN added in v0.12.0

func (t *TransactionData) GetN(h Handle, key []byte) (uint64, bool)

func (*TransactionData) GetNB added in v0.12.0

func (t *TransactionData) GetNB(h Handle, key []byte) (uint64, []byte)

func (*TransactionData) Has added in v0.12.0

func (t *TransactionData) Has(h Handle, key []byte) bool

func (*TransactionData) InUse added in v0.12.0

func (t *TransactionData) InUse() bool

func (*TransactionData) Put added in v0.12.0

func (t *TransactionData) Put(
	h Handle,
	key []byte,
	value []byte,
	additional []byte,
)

func (*TransactionData) PutN added in v0.12.0

func (t *TransactionData) PutN(h Handle, key []byte, value uint64)

type Update added in v0.12.1

type Update interface {
	Put([]byte, []byte, []byte)
	PutN([]byte, uint64)
	Remove([]byte)
}

Update - interface for storage update data

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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