storage

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2020 License: GPL-3.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrInit = iota
	ErrNotFound
	ErrUnauthorized
	ErrInvalidValue
	ErrDataOverflow
	ErrNothingToReturn
	ErrInvalidSignature
	ErrNotSynced
)
View Source
const (
	BMTHash     = "BMT"
	SHA3Hash    = "SHA3" // http://golang.org/pkg/hash/#Hash
	DefaultHash = BMTHash
)
View Source
const AddressLength = 32
View Source
const (
	ChunkProcessors = 8
)
View Source
const CurrentDbSchema = DbSchemaHalloween

The DB schema we want to use. The actual/current DB schema might differ until migrations are run.

View Source
const DbSchemaHalloween = "halloween"

"halloween" is here because we had a screw in the garbage collector index. Because of that we had to rebuild the GC index to get rid of erroneous entries and that takes a long time. This schema is used for bookkeeping, so rebuild index will run just once.

View Source
const DbSchemaNone = ""

There was a time when we had no schema at all.

View Source
const DbSchemaPurity = "purity"

"purity" is the first formal schema of LevelDB we release together with Swarm 0.3.5

View Source
const MaxPO = 16

Variables

View Source
var (
	ErrChunkNotFound = errors.New("chunk not found")
	ErrChunkInvalid  = errors.New("invalid chunk")
)
View Source
var (
	ErrDBClosed = errors.New("LDBStore closed")
)
View Source
var ZeroAddr = Address(common.Hash{}.Bytes())

Functions

func BytesToU64

func BytesToU64(data []byte) uint64

func NewChunk

func NewChunk(addr Address, data []byte) *chunk

func NewHasherStore

func NewHasherStore(store ChunkStore, hashFunc SwarmHasher, toEncrypt bool) *hasherStore

NewHasherStore creates a hasherStore object, which implements Putter and Getter interfaces. With the HasherStore you can put and get chunk data (which is just []byte) into a ChunkStore and the hasherStore will take core of encryption/decryption of data if necessary

func Proximity

func Proximity(one, other []byte) (ret int)

Proximity(x, y) returns the proximity order of the MSB distance between x and y

The distance metric MSB(x, y) of two equal length byte sequences x an y is the value of the binary integer cast of the x^y, ie., x and y bitwise xor-ed. the binary cast is big endian: most significant bit first (=MSB).

Proximity(x, y) is a discrete logarithmic scaling of the MSB distance. It is defined as the reverse rank of the integer part of the base 2 logarithm of the distance. It is calculated by counting the number of common leading zeros in the (MSB) binary representation of the x^y.

(0 farthest, 255 closest, 256 self)

func U64ToBytes

func U64ToBytes(val uint64) []byte

Types

type Address

type Address []byte

func PyramidAppend

func PyramidAppend(ctx context.Context, addr Address, reader io.Reader, putter Putter, getter Getter) (Address, func(context.Context) error, error)

func PyramidSplit

func PyramidSplit(ctx context.Context, reader io.Reader, putter Putter, getter Getter) (Address, func(context.Context) error, error)

When splitting, data is given as a SectionReader, and the key is a hashSize long byte slice (Address), the root hash of the entire content will fill this once processing finishes. New chunks to store are store using the putter which the caller provides.

func TreeSplit

func TreeSplit(ctx context.Context, data io.Reader, size int64, putter Putter) (k Address, wait func(context.Context) error, err error)

When splitting, data is given as a SectionReader, and the key is a hashSize long byte slice (Key), the root hash of the entire content will fill this once processing finishes. New chunks to store are store using the putter which the caller provides.

func (Address) Hex

func (a Address) Hex() string

func (Address) Log

func (a Address) Log() string

func (Address) MarshalJSON

func (a Address) MarshalJSON() (out []byte, err error)

func (Address) String

func (a Address) String() string

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(value []byte) error

type AddressCollection

type AddressCollection []Address

func NewAddressCollection

func NewAddressCollection(l int) AddressCollection

func (AddressCollection) Len

func (c AddressCollection) Len() int

func (AddressCollection) Less

func (c AddressCollection) Less(i, j int) bool

func (AddressCollection) Swap

func (c AddressCollection) Swap(i, j int)

type Chunk

type Chunk interface {
	Address() Address
	Data() []byte
}

Chunk interface implemented by context.Contexts and data chunks

func GenerateRandomChunk

func GenerateRandomChunk(dataSize int64) Chunk

func GenerateRandomChunks

func GenerateRandomChunks(dataSize int64, count int) (chunks []Chunk)

type ChunkData

type ChunkData []byte

func (ChunkData) Size

func (c ChunkData) Size() uint64

NOTE: this returns invalid data if chunk is encrypted

type ChunkStore

type ChunkStore interface {
	Put(ctx context.Context, ch Chunk) (err error)
	Get(rctx context.Context, ref Address) (ch Chunk, err error)
	Has(rctx context.Context, ref Address) bool
	Close()
}

type ChunkValidator

type ChunkValidator interface {
	Validate(chunk Chunk) bool
}

type ChunkerParams

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

type ContentAddressValidator

type ContentAddressValidator struct {
	Hasher SwarmHasher
}

Provides method for validation of content address in chunks Holds the corresponding hasher to create the address

func NewContentAddressValidator

func NewContentAddressValidator(hasher SwarmHasher) *ContentAddressValidator

Constructor

func (*ContentAddressValidator) Validate

func (v *ContentAddressValidator) Validate(chunk Chunk) bool

Validate that the given key is a valid content address for the given data

type FakeChunkStore

type FakeChunkStore struct {
}

FakeChunkStore doesn't store anything, just implements the ChunkStore interface It can be used to inject into a hasherStore if you don't want to actually store data just do the hashing

func (*FakeChunkStore) Close

func (f *FakeChunkStore) Close()

Close doesn't store anything it is just here to implement ChunkStore

func (*FakeChunkStore) Get

func (f *FakeChunkStore) Get(_ context.Context, ref Address) (Chunk, error)

Get doesn't store anything it is just here to implement ChunkStore

func (*FakeChunkStore) Has

func (f *FakeChunkStore) Has(_ context.Context, ref Address) bool

Has doesn't do anything it is just here to implement ChunkStore

func (*FakeChunkStore) Put

func (f *FakeChunkStore) Put(_ context.Context, ch Chunk) error

Put doesn't store anything it is just here to implement ChunkStore

type FileStore

type FileStore struct {
	ChunkStore
	// contains filtered or unexported fields
}

func NewFileStore

func NewFileStore(store ChunkStore, params *FileStoreParams) *FileStore

func NewLocalFileStore

func NewLocalFileStore(datadir string, basekey []byte) (*FileStore, error)

for testing locally

func (*FileStore) GetAllReferences

func (f *FileStore) GetAllReferences(ctx context.Context, data io.Reader, toEncrypt bool) (addrs AddressCollection, err error)

GetAllReferences is a public API. This endpoint returns all chunk hashes (only) for a given file

func (*FileStore) HashSize

func (f *FileStore) HashSize() int

func (*FileStore) Retrieve

func (f *FileStore) Retrieve(ctx context.Context, addr Address) (reader *LazyChunkReader, isEncrypted bool)

Retrieve is a public API. Main entry point for document retrieval directly. Used by the FS-aware API and httpaccess Chunk retrieval blocks on netStore requests with a timeout so reader will report error if retrieval of chunks within requested range time out. It returns a reader with the chunk data and whether the content was encrypted

func (*FileStore) Store

func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr Address, wait func(context.Context) error, err error)

Store is a public API. Main entry point for document storage directly. Used by the FS-aware API and httpaccess

type FileStoreParams

type FileStoreParams struct {
	Hash string
}

func NewFileStoreParams

func NewFileStoreParams() *FileStoreParams

type Getter

type Getter interface {
	Get(context.Context, Reference) (ChunkData, error)
}

Getter is an interface to retrieve a chunk's data by its reference

type HashWithLength

type HashWithLength struct {
	hash.Hash
}

func (*HashWithLength) ResetWithLength

func (h *HashWithLength) ResetWithLength(length []byte)

type JoinerParams

type JoinerParams struct {
	ChunkerParams
	// contains filtered or unexported fields
}

type LDBDatabase

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

func NewLDBDatabase

func NewLDBDatabase(file string) (*LDBDatabase, error)

func (*LDBDatabase) Close

func (db *LDBDatabase) Close()

func (*LDBDatabase) Delete

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

func (*LDBDatabase) Get

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

func (*LDBDatabase) NewIterator

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

func (*LDBDatabase) Put

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

func (*LDBDatabase) Write

func (db *LDBDatabase) Write(batch *leveldb.Batch) error

type LDBStore

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

func NewLDBStore

func NewLDBStore(params *LDBStoreParams) (s *LDBStore, err error)

TODO: Instead of passing the distance function, just pass the address from which distances are calculated to avoid the appearance of a pluggable distance metric and opportunities of bugs associated with providing a function different from the one that is actually used.

func NewMockDbStore

func NewMockDbStore(params *LDBStoreParams, mockStore *mock.NodeStore) (s *LDBStore, err error)

NewMockDbStore creates a new instance of DbStore with mockStore set to a provided value. If mockStore argument is nil, this function behaves exactly as NewDbStore.

func (*LDBStore) BinIndex

func (s *LDBStore) BinIndex(po uint8) uint64

func (*LDBStore) CleanGCIndex

func (s *LDBStore) CleanGCIndex() error

CleanGCIndex rebuilds the garbage collector index from scratch, while removing inconsistent elements, e.g., indices with missing data chunks. WARN: it's a pretty heavy, long running function.

func (*LDBStore) Cleanup

func (s *LDBStore) Cleanup(f func(*chunk) bool)

Cleanup iterates over the database and deletes chunks if they pass the `f` condition

func (*LDBStore) Close

func (s *LDBStore) Close()

func (*LDBStore) Delete

func (s *LDBStore) Delete(addr Address) error

Delete is removes a chunk and updates indices. Is thread safe

func (*LDBStore) Export

func (s *LDBStore) Export(out io.Writer) (int64, error)

Export writes all chunks from the store to a tar archive, returning the number of chunks written.

func (*LDBStore) Get

func (s *LDBStore) Get(_ context.Context, addr Address) (chunk Chunk, err error)

Get retrieves the chunk matching the provided key from the database. If the chunk entry does not exist, it returns an error Updates access count and is thread safe

func (*LDBStore) GetSchema

func (s *LDBStore) GetSchema() (string, error)

GetSchema is returning the current named schema of the datastore as read from LevelDB

func (*LDBStore) Has

func (s *LDBStore) Has(_ context.Context, addr Address) bool

Has queries the underlying DB if a chunk with the given address is stored Returns true if the chunk is found, false if not

func (*LDBStore) Import

func (s *LDBStore) Import(in io.Reader) (int64, error)

of chunks read.

func (*LDBStore) MarkAccessed

func (s *LDBStore) MarkAccessed(addr Address)

MarkAccessed increments the access counter as a best effort for a chunk, so the chunk won't get garbage collected.

func (*LDBStore) Put

func (s *LDBStore) Put(ctx context.Context, chunk Chunk) error

Put adds a chunk to the database, adding indices and incrementing global counters. If it already exists, it merely increments the access count of the existing entry. Is thread safe

func (*LDBStore) PutSchema

func (s *LDBStore) PutSchema(schema string) error

PutSchema is saving a named schema to the LevelDB datastore

func (*LDBStore) SyncIterator

func (s *LDBStore) SyncIterator(since uint64, until uint64, po uint8, f func(Address, uint64) bool) error

SyncIterator(start, stop, po, f) calls f on each hash of a bin po from start to stop

type LDBStoreParams

type LDBStoreParams struct {
	*StoreParams
	Path string
	Po   func(Address) uint8
}

func NewLDBStoreParams

func NewLDBStoreParams(storeparams *StoreParams, path string) *LDBStoreParams

NewLDBStoreParams constructs LDBStoreParams with the specified values.

type LazyChunkReader

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

LazyChunkReader implements LazySectionReader

func TreeJoin

func TreeJoin(ctx context.Context, addr Address, getter Getter, depth int) *LazyChunkReader

Join reconstructs original content based on a root key. When joining, the caller gets returned a Lazy SectionReader, which is seekable and implements on-demand fetching of chunks as and where it is read. New chunks to retrieve are coming from the getter, which the caller provides. If an error is encountered during joining, it appears as a reader error. The SectionReader. As a result, partial reads from a document are possible even if other parts are corrupt or lost. The chunks are not meant to be validated by the chunker when joining. This is because it is left to the DPA to decide which sources are trusted.

func (*LazyChunkReader) Context

func (r *LazyChunkReader) Context() context.Context

func (*LazyChunkReader) Read

func (r *LazyChunkReader) Read(b []byte) (read int, err error)

Read keeps a cursor so cannot be called simulateously, see ReadAt

func (*LazyChunkReader) ReadAt

func (r *LazyChunkReader) ReadAt(b []byte, off int64) (read int, err error)

read at can be called numerous times concurrent reads are allowed Size() needs to be called synchronously on the LazyChunkReader first

func (*LazyChunkReader) Seek

func (r *LazyChunkReader) Seek(offset int64, whence int) (int64, error)

func (*LazyChunkReader) Size

func (r *LazyChunkReader) Size(ctx context.Context, quitC chan bool) (n int64, err error)

Size is meant to be called on the LazySectionReader

type LazySectionReader

type LazySectionReader interface {
	Context() context.Context
	Size(context.Context, chan bool) (int64, error)
	io.Seeker
	io.Reader
	io.ReaderAt
}

Size, Seek, Read, ReadAt

type LazyTestSectionReader

type LazyTestSectionReader struct {
	*io.SectionReader
}

func (*LazyTestSectionReader) Context

func (r *LazyTestSectionReader) Context() context.Context

func (*LazyTestSectionReader) Size

type LocalStore

type LocalStore struct {
	Validators []ChunkValidator

	DbStore *LDBStore
	// contains filtered or unexported fields
}

LocalStore is a combination of inmemory db over a disk persisted db implements a Get/Put with fallback (caching) logic using any 2 ChunkStores

func NewLocalStore

func NewLocalStore(params *LocalStoreParams, mockStore *mock.NodeStore) (*LocalStore, error)

This constructor uses MemStore and DbStore as components

func NewTestLocalStoreForAddr

func NewTestLocalStoreForAddr(params *LocalStoreParams) (*LocalStore, error)

func (*LocalStore) BinIndex

func (ls *LocalStore) BinIndex(po uint8) uint64

func (*LocalStore) Close

func (ls *LocalStore) Close()

Close the local store

func (*LocalStore) FetchFunc

func (ls *LocalStore) FetchFunc(ctx context.Context, addr Address) func(context.Context) error

func (*LocalStore) Get

func (ls *LocalStore) Get(ctx context.Context, addr Address) (chunk Chunk, err error)

Get(chunk *Chunk) looks up a chunk in the local stores This method is blocking until the chunk is retrieved so additional timeout may be needed to wrap this call if ChunkStores are remote and can have long latency

func (*LocalStore) Has

func (ls *LocalStore) Has(ctx context.Context, addr Address) bool

Has queries the underlying DbStore if a chunk with the given address is being stored there. Returns true if it is stored, false if not

func (*LocalStore) Iterator

func (ls *LocalStore) Iterator(from uint64, to uint64, po uint8, f func(Address, uint64) bool) error

func (*LocalStore) Migrate

func (ls *LocalStore) Migrate() error

Migrate checks the datastore schema vs the runtime schema and runs migrations if they don't match

func (*LocalStore) Put

func (ls *LocalStore) Put(ctx context.Context, chunk Chunk) error

Put is responsible for doing validation and storage of the chunk by using configured ChunkValidators, MemStore and LDBStore. If the chunk is not valid, its GetErrored function will return ErrChunkInvalid. This method will check if the chunk is already in the MemStore and it will return it if it is. If there is an error from the MemStore.Get, it will be returned by calling GetErrored on the chunk. This method is responsible for closing Chunk.ReqC channel when the chunk is stored in memstore. After the LDBStore.Put, it is ensured that the MemStore contains the chunk with the same data, but nil ReqC channel.

type LocalStoreParams

type LocalStoreParams struct {
	*StoreParams
	ChunkDbPath string
	Validators  []ChunkValidator `toml:"-"`
}

func NewDefaultLocalStoreParams

func NewDefaultLocalStoreParams() *LocalStoreParams

func (*LocalStoreParams) Init

func (p *LocalStoreParams) Init(path string)

this can only finally be set after all config options (file, cmd line, env vars) have been evaluated

type MemStore

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

func NewMemStore

func NewMemStore(params *StoreParams, _ *LDBStore) (m *MemStore)

NewMemStore is instantiating a MemStore cache keeping all frequently requested chunks in the `cache` LRU cache.

func (*MemStore) Close

func (s *MemStore) Close()

func (*MemStore) Get

func (m *MemStore) Get(_ context.Context, addr Address) (Chunk, error)

func (*MemStore) Has

func (m *MemStore) Has(_ context.Context, addr Address) bool

Has needed to implement SyncChunkStore

func (*MemStore) Put

func (m *MemStore) Put(_ context.Context, c Chunk) error

type NetFetcher

type NetFetcher interface {
	Request(hopCount uint8)
	Offer(source *enode.ID)
}

type NetStore

type NetStore struct {
	NewNetFetcherFunc NewNetFetcherFunc
	// contains filtered or unexported fields
}

NetStore is an extension of local storage it implements the ChunkStore interface on request it initiates remote cloud retrieval using a fetcher fetchers are unique to a chunk and are stored in fetchers LRU memory cache fetchFuncFactory is a factory object to create a fetch function for a specific chunk address

func NewNetStore

func NewNetStore(store SyncChunkStore, nnf NewNetFetcherFunc) (*NetStore, error)

NewNetStore creates a new NetStore object using the given local store. newFetchFunc is a constructor function that can create a fetch function for a specific chunk address.

func (*NetStore) BinIndex

func (n *NetStore) BinIndex(po uint8) uint64

func (*NetStore) Close

func (n *NetStore) Close()

Close chunk store

func (*NetStore) FetchFunc

func (n *NetStore) FetchFunc(ctx context.Context, ref Address) func(context.Context) error

FetchFunc returns nil if the store contains the given address. Otherwise it returns a wait function, which returns after the chunk is available or the context is done

func (*NetStore) Get

func (n *NetStore) Get(rctx context.Context, ref Address) (Chunk, error)

Get retrieves the chunk from the NetStore DPA synchronously. It calls NetStore.get, and if the chunk is not in local Storage it calls fetch with the request, which blocks until the chunk arrived or context is done

func (*NetStore) Has

func (n *NetStore) Has(ctx context.Context, ref Address) bool

Has is the storage layer entry point to query the underlying database to return if it has a chunk or not. Called from the DebugAPI

func (*NetStore) Iterator

func (n *NetStore) Iterator(from uint64, to uint64, po uint8, f func(Address, uint64) bool) error

func (*NetStore) Put

func (n *NetStore) Put(ctx context.Context, ch Chunk) error

Put stores a chunk in localstore, and delivers to all requestor peers using the fetcher stored in the fetchers cache

func (*NetStore) RequestsCacheLen

func (n *NetStore) RequestsCacheLen() int

RequestsCacheLen returns the current number of outgoing requests stored in the cache

type NewNetFetcherFunc

type NewNetFetcherFunc func(ctx context.Context, addr Address, peers *sync.Map) NetFetcher

type Putter

type Putter interface {
	Put(context.Context, ChunkData) (Reference, error)
	// RefSize returns the length of the Reference created by this Putter
	RefSize() int64
	// Close is to indicate that no more chunk data will be Put on this Putter
	Close()
	// Wait returns if all data has been store and the Close() was called.
	Wait(context.Context) error
}

Putter is responsible to store data and create a reference for it

type PyramidChunker

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

func NewPyramidSplitter

func NewPyramidSplitter(params *PyramidSplitterParams) (pc *PyramidChunker)

func (*PyramidChunker) Append

func (pc *PyramidChunker) Append(ctx context.Context) (k Address, wait func(context.Context) error, err error)

func (*PyramidChunker) Join

func (pc *PyramidChunker) Join(addr Address, getter Getter, depth int) LazySectionReader

func (*PyramidChunker) Split

func (pc *PyramidChunker) Split(ctx context.Context) (k Address, wait func(context.Context) error, err error)

type PyramidSplitterParams

type PyramidSplitterParams struct {
	SplitterParams
	// contains filtered or unexported fields
}

func NewPyramidSplitterParams

func NewPyramidSplitterParams(addr Address, reader io.Reader, putter Putter, getter Getter, chunkSize int64) *PyramidSplitterParams

type Reference

type Reference []byte

type SplitterParams

type SplitterParams struct {
	ChunkerParams
	// contains filtered or unexported fields
}

type StoreParams

type StoreParams struct {
	Hash          SwarmHasher `toml:"-"`
	DbCapacity    uint64
	CacheCapacity uint
	BaseKey       []byte
}

func NewDefaultStoreParams

func NewDefaultStoreParams() *StoreParams

func NewStoreParams

func NewStoreParams(ldbCap uint64, cacheCap uint, hash SwarmHasher, basekey []byte) *StoreParams

type SwarmHash

type SwarmHash interface {
	hash.Hash
	ResetWithLength([]byte)
}

type SwarmHasher

type SwarmHasher func() SwarmHash

func MakeHashFunc

func MakeHashFunc(hash string) SwarmHasher

type SyncChunkStore

type SyncChunkStore interface {
	ChunkStore
	BinIndex(po uint8) uint64
	Iterator(from uint64, to uint64, po uint8, f func(Address, uint64) bool) error
	FetchFunc(ctx context.Context, ref Address) func(context.Context) error
}

SyncChunkStore is a ChunkStore which supports syncing

type TreeChunker

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

func NewTreeJoiner

func NewTreeJoiner(params *JoinerParams) *TreeChunker

func NewTreeSplitter

func NewTreeSplitter(params *TreeSplitterParams) *TreeChunker

func (*TreeChunker) Join

func (tc *TreeChunker) Join(ctx context.Context) *LazyChunkReader

func (*TreeChunker) Split

func (tc *TreeChunker) Split(ctx context.Context) (k Address, wait func(context.Context) error, err error)

type TreeEntry

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

Entry to create a tree node

func NewTreeEntry

func NewTreeEntry(pyramid *PyramidChunker) *TreeEntry

type TreeSplitterParams

type TreeSplitterParams struct {
	SplitterParams
	// contains filtered or unexported fields
}

Directories

Path Synopsis
Package feeds defines Swarm Feeds.
Package feeds defines Swarm Feeds.
lookup
Package lookup defines feed lookup algorithms and provides tools to place updates so they can be found
Package lookup defines feed lookup algorithms and provides tools to place updates so they can be found
Package localstore provides disk storage layer for Swarm Chunk persistence.
Package localstore provides disk storage layer for Swarm Chunk persistence.
Package mock defines types that are used by different implementations of mock storages.
Package mock defines types that are used by different implementations of mock storages.
db
Package db implements a mock store that keeps all chunk data in LevelDB database.
Package db implements a mock store that keeps all chunk data in LevelDB database.
mem
Package mem implements a mock store that keeps all chunk data in memory.
Package mem implements a mock store that keeps all chunk data in memory.
rpc
Package rpc implements an RPC client that connect to a centralized mock store.
Package rpc implements an RPC client that connect to a centralized mock store.
test
Package test provides functions that are used for testing GlobalStorer implementations.
Package test provides functions that are used for testing GlobalStorer implementations.

Jump to

Keyboard shortcuts

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