pttdb

package
v0.0.0-...-516970c Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: LGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Code using batches should try to add this much data to the batch.
	// The value was determined empirically.
	IdealBatchSize = 100 * 1024

	InfiniteQuota = 0

	SizeDBKeyPrefix          = 5
	OffsetDBKeyPrefixPostfix = 3
)
View Source
const (
	OpenFileLimit = 64
)

Variables

View Source
var (
	ErrInvalidPrefix   = errors.New("invalid prefix")
	ErrInvalidDBable   = errors.New("invalid dbable (no UT)")
	ErrInvalidUpdateTS = errors.New("invalid UT")
	ErrInvalidLock     = errors.New("invalid db lock")
	ErrBusy            = errors.New("db busy")
	ErrInvalidKeys     = errors.New("invalid db keys")
	ErrInvalidIndex    = errors.New("invalid db index")
)
View Source
var (
	ValueTrue = []byte{1}
)

Functions

func GetFuncIter

func GetFuncIter(iter iterator.Iterator, listOrder ListOrder) func() bool

func GetIterByID

func GetIterByID(db *LDBDatabase, prefix []byte, idxPrefix []byte, startID *types.PttID, listOrder ListOrder) (iterator.Iterator, error)

func IndexGetKey

func IndexGetKey(theBytes []byte) ([]byte, error)

func IndexGetKeys

func IndexGetKeys(theBytes []byte) ([][]byte, error)

Types

type Batch

type Batch interface {
	Putter
	Deleter

	ValueSize() int // amount of data in the batch
	Write() error
	// Reset resets the batch for reuse
	Reset()
}

Batch is a write-only database that commits changes to its host database when Write is called. Batch cannot be used concurrently.

func NewTableBatch

func NewTableBatch(db Database, prefix string) Batch

NewTableBatch returns a Batch object which prefixes all keys with a given string.

type DBable

type DBable struct {
	UpdateTS types.Timestamp `json:"UT"`
}

func (*DBable) Marshal

func (d *DBable) Marshal() ([]byte, error)

func (*DBable) Unmarshal

func (d *DBable) Unmarshal(data []byte) error

type Database

type Database interface {
	Putter
	Getter
	Deleter

	Close()
	NewBatch() Batch
}

Database wraps all database operations. All methods are safe for concurrent use.

func NewTable

func NewTable(db Database, prefix string) Database

NewTable returns a Database object that prefixes all keys with a given string.

type Deleter

type Deleter interface {
	Delete(key []byte) error
}

type Getter

type Getter interface {
	Get(key []byte) ([]byte, error)
	Has(key []byte) (bool, error)
}

type Index

type Index struct {
	Keys     [][]byte        `json:"K"`
	UpdateTS types.Timestamp `json:"UT"`
}

Index stores the information of index for ts-based records.

The 0th key is the key for the record.

The other keys are the secondary index-keys

func (*Index) Marshal

func (i *Index) Marshal() ([]byte, error)

func (*Index) Unmarshal

func (i *Index) Unmarshal(data []byte) error

type IndexWithStatus

type IndexWithStatus struct {
	Keys     [][]byte        `json:"K"`
	UpdateTS types.Timestamp `json:"UT"`
	Status   types.Status    `json:"S"`
}

func (*IndexWithStatus) Marshal

func (i *IndexWithStatus) Marshal() ([]byte, error)

func (*IndexWithStatus) Unmarshal

func (i *IndexWithStatus) Unmarshal(data []byte) error

type KeyVal

type KeyVal struct {
	K []byte
	V []byte
}

type LDBBatch

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

func NewLDBBatch

func NewLDBBatch(db *LDBDatabase) (*LDBBatch, error)

func (LDBBatch) DB

func (b LDBBatch) DB() *LDBDatabase

func (LDBBatch) DBDelete

func (b LDBBatch) DBDelete(key []byte) error

func (LDBBatch) DBGet

func (b LDBBatch) DBGet(key []byte) ([]byte, error)

func (LDBBatch) Delete

func (b LDBBatch) Delete(key []byte) error

func (*LDBBatch) DeleteAll

func (b *LDBBatch) DeleteAll(idxKey []byte) error

DeleteAll deletes all the records, including value and 2nd-indexing of the idxKey.

func (*LDBBatch) DeleteAllKeys

func (b *LDBBatch) DeleteAllKeys(keys [][]byte) error

func (*LDBBatch) ForcePutAll

func (b *LDBBatch) ForcePutAll(idxKey []byte, idx *Index, kvs []*KeyVal) ([][]byte, error)

ForcePutAll tries to put all the key-vals with comparing the updateTS of the 1st item.

This is used when the real content is stored in ts-based key, but we want to refer the content directly from id-key.

This assumes 1-record per id-key. The original data will be deleted if id-key already exists.

idxKey, idx, and kvs intends to implement 2nd-indexing in leveldb.

Given the id-key and the data with updateTS, idxKey is the id-key, key is the key-with-updateTS, value is the corresponding data. Besides, we also have the 2nd-idx-keys to index by the 2nd-idx-keys. The value of the 2nd-idx-keys are key.

idx includes the key (in the 0th of idx.Keys) and 2nd-idx-keys kvs are the corresponding key-vals based on the order of idx.

func (*LDBBatch) GetBy2ndIdxKey

func (b *LDBBatch) GetBy2ndIdxKey(idxKey []byte) ([]byte, error)

GetBy2ndIdxKey gets the value based on the 2nd-idx-key.

func (*LDBBatch) GetByIdxKey

func (b *LDBBatch) GetByIdxKey(idxKey []byte, idx int) ([]byte, error)

GetByIdxKey gets the value (idx as 0) or the value of the corresponding 2nd-idx-key (should be the key).

func (*LDBBatch) GetKeyBy2ndIdxKey

func (b *LDBBatch) GetKeyBy2ndIdxKey(idxKey []byte) ([]byte, error)

GetKeyBy2ndIdxKey gets the key based on the 2nd-idx-key.

func (*LDBBatch) GetKeyByIdxKey

func (b *LDBBatch) GetKeyByIdxKey(idxKey []byte, idx int) ([]byte, error)

GetKeyByIdxKey gets the key (idx as 0) or the corresponding 2nd-idx-key.

func (LDBBatch) Put

func (b LDBBatch) Put(key, value []byte) error

func (LDBBatch) Reset

func (b LDBBatch) Reset()

func (*LDBBatch) TryPutAll

func (b *LDBBatch) TryPutAll(idxKey []byte, idx *Index, kvs []*KeyVal, isDeleteOrig bool, isGetOrig bool) ([]*KeyVal, error)

TryPutAll tries to put all the key-vals with comparing the updateTS of the 1st item.

This is used when the real content is stored in ts-based key, but we want to refer the content directly from id-key.

This assumes 1-record per id-key. The old data will be deleted if id-key already exists.

idxKey, idx, and kvs intends to implement 2nd-indexing in leveldb.

Given the id-key and the data with updateTS, idxKey is the id-key, key is the key-with-updateTS, value is the corresponding data. Besides, we also have the 2nd-idx-keys to index by the 2nd-idx-keys. The value of the 2nd-idx-keys are key.

idx includes the key (in the 0th of idx.Keys) and 2nd-idx-keys kvs are the corresponding key-vals based on the order of idx.

func (LDBBatch) ValueSize

func (b LDBBatch) ValueSize() int

func (LDBBatch) Write

func (b LDBBatch) Write() error

type LDBDatabase

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

func NewLDBDatabase

func NewLDBDatabase(file string, dataDir string, cache int, handles int) (*LDBDatabase, error)

NewLDBDatabase returns a LevelDB wrapped object.

func (*LDBDatabase) Close

func (db *LDBDatabase) Close()

func (*LDBDatabase) Delete

func (db *LDBDatabase) Delete(key []byte) error

Delete deletes the key from the queue and database

func (*LDBDatabase) Get

func (db *LDBDatabase) Get(key []byte) ([]byte, error)

Get returns the given key if it's present.

func (*LDBDatabase) Has

func (db *LDBDatabase) Has(key []byte) (bool, error)

func (*LDBDatabase) LDB

func (db *LDBDatabase) LDB() *leveldb.DB

func (*LDBDatabase) Meter

func (db *LDBDatabase) Meter(prefix string)

Meter configures the database metrics collectors and

func (*LDBDatabase) Name

func (db *LDBDatabase) Name() string

func (*LDBDatabase) NewBatch

func (db *LDBDatabase) NewBatch() Batch

func (*LDBDatabase) NewIterator

func (db *LDBDatabase) NewIterator(listOrder ListOrder) iterator.Iterator

func (*LDBDatabase) NewIteratorWithPrefix

func (db *LDBDatabase) NewIteratorWithPrefix(start []byte, prefix []byte, listOrder ListOrder) (iterator.Iterator, error)

NewIteratorWithPrefix returns a iterator to iterate over subset of database content with a particular prefix.

func (*LDBDatabase) NewIteratorWithRange

func (db *LDBDatabase) NewIteratorWithRange(r *util.Range, listOrder ListOrder) iterator.Iterator

func (*LDBDatabase) Path

func (db *LDBDatabase) Path() string

Path returns the path to the database directory.

func (*LDBDatabase) Pop

func (db *LDBDatabase) Pop(key []byte) ([]byte, error)

Delete With Get

func (*LDBDatabase) Put

func (db *LDBDatabase) Put(key []byte, value []byte) error

Put puts the given key / value to the queue

func (*LDBDatabase) RUnlockMap

func (db *LDBDatabase) RUnlockMap(key []byte) error

func (*LDBDatabase) TryLockMap

func (db *LDBDatabase) TryLockMap(key []byte) error

func (*LDBDatabase) TryPut

func (db *LDBDatabase) TryPut(key []byte, value []byte, updateTS types.Timestamp) ([]byte, error)

TryPut tries to put the key/val based on the updateTS of val.

Value is the jsonified bytes. the obj of the bytes needs to include UpdateTS.

func (*LDBDatabase) TryRLockMap

func (db *LDBDatabase) TryRLockMap(key []byte) error

func (*LDBDatabase) UnlockMap

func (db *LDBDatabase) UnlockMap(key []byte) error

type ListOrder

type ListOrder int

list-order

const (
	ListOrderPrev ListOrder
	ListOrderNext
)

type MemDatabase

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

* This is a test memory database. Do not use for any production it does not get persisted

func NewMemDatabase

func NewMemDatabase() *MemDatabase

func NewMemDatabaseWithCap

func NewMemDatabaseWithCap(size int) *MemDatabase

func (*MemDatabase) Close

func (db *MemDatabase) Close()

func (*MemDatabase) Delete

func (db *MemDatabase) Delete(key []byte) error

func (*MemDatabase) Get

func (db *MemDatabase) Get(key []byte) ([]byte, error)

func (*MemDatabase) Has

func (db *MemDatabase) Has(key []byte) (bool, error)

func (*MemDatabase) Keys

func (db *MemDatabase) Keys() [][]byte

func (*MemDatabase) Len

func (db *MemDatabase) Len() int

func (*MemDatabase) NewBatch

func (db *MemDatabase) NewBatch() Batch

func (*MemDatabase) Put

func (db *MemDatabase) Put(key []byte, value []byte) error

type Putter

type Putter interface {
	Put(key []byte, value []byte) error
}

Putter wraps the database write operation supported by both batches and regular databases.

Jump to

Keyboard shortcuts

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