database

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2019 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const IdealBatchSize = 100 * 1024

Code using batches should try to add this much data to the batch. The value was determined empirically.

Variables

View Source
var ErrKeyNotFound = errors.New("not found")
View Source
var ErrPathNotFound = errors.New("path not found")

Functions

This section is empty.

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.

type Database

type Database interface {
	Putter
	Deleter
	Get(key []byte) ([]byte, error)
	Has(key []byte) (bool, error)
	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
}

Deleter wraps the database delete operation supported by both batches and regular databases.

type DummyDatabase added in v0.3.1

type DummyDatabase struct {
}

DummyDatabase is a dummy "remote database" which actually save all data to key, i.e. key is data, data is key. It does not save data to any store, but to the returned key. It is used to serve as an extremely stable remote database. For example, PrivateTx could use it as an option of its "remote database" to ensure that it does not depend on any "single point" database.

func (*DummyDatabase) Discard added in v0.3.1

func (d *DummyDatabase) Discard(key []byte) error

func (*DummyDatabase) Get added in v0.3.1

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

func (*DummyDatabase) Has added in v0.3.1

func (d *DummyDatabase) Has(key []byte) bool

func (*DummyDatabase) Put added in v0.3.1

func (d *DummyDatabase) Put(value []byte) ([]byte, error)

type FakeIpfsAdapter

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

FakeIpfsAdapter is a fake IPFS for unit test.

func NewFakeIpfsAdapter

func NewFakeIpfsAdapter() *FakeIpfsAdapter

NewFakeIpfsAdapter creates a new FakeIpfsAdapter instance.

func (*FakeIpfsAdapter) Add

func (adapter *FakeIpfsAdapter) Add(r io.Reader) (string, error)

func (*FakeIpfsAdapter) Cat

func (adapter *FakeIpfsAdapter) Cat(path string) (io.ReadCloser, error)

func (*FakeIpfsAdapter) Unpin

func (adapter *FakeIpfsAdapter) Unpin(path string) error

type IpfsAdapter

type IpfsAdapter interface {
	// Cat reads data from IPFS.
	Cat(path string) (io.ReadCloser, error)
	// Add adds and pin data to IPFS.
	Add(r io.Reader) (string, error)
	// Unpin unpins the data from IPFS, unpinned data may be removed by GC in future.
	Unpin(path string) error
}

IpfsAdapter represents an adapter for IPFS access. It also makes a room for weaving fake IPFS in unit test.

type IpfsDatabase

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

IpfsDatabase is a IPFS-based database.

func NewIpfsDB

func NewIpfsDB(url string) *IpfsDatabase

NewIpfsDB creates a new IpfsDatabase instance with given url which is the IPFS node's API url.

func NewIpfsDbWithAdapter

func NewIpfsDbWithAdapter(adapter IpfsAdapter) *IpfsDatabase

NewIpfsDbWithAdapter creates a new IpfsDatabase instance with given IPFS adapter.

func (*IpfsDatabase) Discard

func (db *IpfsDatabase) Discard(key []byte) error

Discard data from IPFS network.

func (*IpfsDatabase) Get

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

Get retrieves data from IPFS with given key.

func (*IpfsDatabase) Has

func (db *IpfsDatabase) Has(key []byte) bool

Has checks if the data specified by given key can be retrieved.

func (*IpfsDatabase) Put

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

Put saves data to IPFS and return key(internally it is a hash).

type LDBDatabase

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

func NewLDBDatabase

func NewLDBDatabase(file 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) NewBatch

func (db *LDBDatabase) NewBatch() Batch

func (*LDBDatabase) NewIterator

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

func (*LDBDatabase) NewIteratorWithPrefix

func (db *LDBDatabase) NewIteratorWithPrefix(prefix []byte) iterator.Iterator

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

func (*LDBDatabase) Path

func (db *LDBDatabase) Path() string

Path returns the path to the database directory.

func (*LDBDatabase) Put

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

Put puts the given key / value to the queue

type MemDatabase

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

func NewMemDatabase

func NewMemDatabase() *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.

type RemoteDatabase

type RemoteDatabase interface {
	// Get gets data from database with given key.
	Get(key []byte) ([]byte, error)
	// Put puts data to database and returns an address(key/identifier).
	Put(value []byte) ([]byte, error)
	// Discard discards data from database with given key. As remote database system such as ipfs doesn't have explicit
	// data deleting function as its own implementation mechanism, we adopt 'discard' instead of 'delete'.
	Discard(key []byte) error
	// Has checks if the given address/key points to an existent data.
	Has(key []byte) bool
}

RemoteDatabase represents the database interface that offloads data to distributed storage such as ipfs.

Jump to

Keyboard shortcuts

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