leveldb

package
v0.0.0-...-53d58b0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: BSD-2-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NumLevels int = 7

	// Level-0 compaction is started when we hit this many files.
	L0_CompactionTrigger = 4

	// Soft limit on number of level-0 files.  We slow down writes at this point.
	L0_SlowdownWritesTrigger = 8

	// Maximum number of level-0 files.  We stop writes at this point.
	L0_StopWritesTrigger = 12

	// Maximum level to which a new compacted memtable is pushed if it
	// does not create overlap.  We try to push to level 2 to avoid the
	// relatively expensive level 0=>1 compactions and to avoid some
	// expensive manifest file operations.  We do not push all the way to
	// the largest level since that can generate a lot of wasted disk
	// space if the same key space is being repeatedly overwritten.
	MaxMemCompactLevel = 2

	// Approximate gap in bytes between samples of data read during iteration.
	ReadBytesPeriod = 1048576
)

Grouping of constants. We may want to make some of these parameters set via options.

View Source
const (
	TypeDeletion ValueType = 0
	TypeValue              = 1

	// kValueTypeForSeek defines the ValueType that should be passed when
	// constructing a ParsedInternalKey object for seeking to a particular
	// sequence number (since we sort sequence numbers in decreasing order
	// and the value type is embedded as the low 8 bits in the sequence
	// number in internal keys, we need to use the highest-numbered
	// ValueType, not the lowest).
	TypeValueForSeek = 1
)
View Source
const (
	OK byte = iota
	NotFound
	Corruption
	NotSupported
	InvalidArgument
	IOError
)

Variables

This section is empty.

Functions

func AppendInternalKey

func AppendInternalKey(key ParsedInternalKey) []byte

func BySmallestKey

func BySmallestKey(cmp Comparator) func(f1, f2 *FileMetaData) bool

func CrcValue

func CrcValue(p []byte) levelDbCrc

Return the crc32c of data[0,n-1]

func CurrentFileName

func CurrentFileName(dbname string) string

Return the name of the current file. This file contains the name of the current manifest file. The result will be prefixed with "dbname".

func DecodeFixed32

func DecodeFixed32(buf *bytes.Buffer) (uint32, error)

func DecodeFixed64

func DecodeFixed64(buf *bytes.Buffer) (uint64, error)

func DescriptorFileName

func DescriptorFileName(dbname string, number uint64) string

Return the name of the descriptor file for the db named by "dbname" and the specified incarnation number. The result will be prefixed with "dbname".

func ExtractUserKey

func ExtractUserKey(internalKey []byte) []byte

Returns the user key portion of an internal key. internalKey format: user_key + 8bytes_of_sequence_type

func GetLengthPrefixedSlice

func GetLengthPrefixedSlice(input *bytes.Buffer, result *[]byte) error

func GetVarint32

func GetVarint32(input *bytes.Buffer) (uint32, error)

func GetVarint64

func GetVarint64(input *bytes.Buffer) (uint64, error)

func InfoLogFileName

func InfoLogFileName(dbname string) string

Return the name of the info log file for "dbname".

func LockFileName

func LockFileName(dbname string) string

Return the name of the lock file for the db named by "dbname". The result will be prefixed with "dbname".

func LogFileName

func LogFileName(dbname string, number uint64) string

Return the name of the log file with the specified number in the db named by "dbname". The result will be prefixed with "dbname".

func Open

func Open(opt *Options, dbname string) (*DB, Status)

func PutFixed32

func PutFixed32(dst *bytes.Buffer, value uint32)

func PutFixed64

func PutFixed64(dst *bytes.Buffer, value uint64)

func PutLengthPrefixedSlice

func PutLengthPrefixedSlice(dst *bytes.Buffer, value []byte)

func PutVarint32

func PutVarint32(dst *bytes.Buffer, value uint32)

func PutVarint64

func PutVarint64(dst *bytes.Buffer, value uint64)

func ReadBlock

func ReadBlock(file RandomAccessFile, verifyCrc bool, handle BlockHandle) (BlockContents, Status)

Read the block identified by "handle" from "file". On failure return non-OK. On success return BlockContents and OK.

func SetCurrentFile

func SetCurrentFile(dbname string, descriptorNumber uint64) error

Make the CURRENT file point to the descriptor file with the specified number.

func TableFileName

func TableFileName(dbname string, number uint64) string

Return the name of the sstable with the specified number in the db named by "dbname". The result will be prefixed with "dbname".

func TempFileName

func TempFileName(dbname string, number uint64) string

Return the name of a temporary file owned by the db named "dbname". The result will be prefixed with "dbname".

Types

type BlockBuilder

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

func NewBlockBuilder

func NewBlockBuilder() *BlockBuilder

func (*BlockBuilder) Add

func (bb *BlockBuilder) Add(key, value []byte)

REQUIRES: Finish() has not been called since the last call to Reset(). REQUIRES: key is larger than any previously added key

func (*BlockBuilder) CurrentSizeEstimate

func (bb *BlockBuilder) CurrentSizeEstimate() int

func (*BlockBuilder) Empty

func (bb *BlockBuilder) Empty() bool

func (*BlockBuilder) Finish

func (bb *BlockBuilder) Finish() []byte

func (*BlockBuilder) Reset

func (bb *BlockBuilder) Reset()

type BlockContents

type BlockContents struct {
	Data     []byte // Actual contents of data
	Cachable bool   // True iff data can be cached
}

type BlockHandle

type BlockHandle struct {
	Offset uint64 // The offset of the block in the file.
	Size   uint64 // The size of the stored block
}

BlockHandle is a pointer to the extent of a file that stores a data block or a meta block.

func (*BlockHandle) DecodeFrom

func (bh *BlockHandle) DecodeFrom(input []byte) Status

func (*BlockHandle) DecodeFromBuf

func (bh *BlockHandle) DecodeFromBuf(buf *bytes.Buffer) Status

func (*BlockHandle) EncodeTo

func (bh *BlockHandle) EncodeTo() []byte

func (*BlockHandle) EncodeToBuf

func (bh *BlockHandle) EncodeToBuf(buf *bytes.Buffer)

type BytewiseComparatorImpl

type BytewiseComparatorImpl struct{}

func (*BytewiseComparatorImpl) Compare

func (dummy *BytewiseComparatorImpl) Compare(key1, key2 []byte) int

func (*BytewiseComparatorImpl) FindShortSuccessor

func (dummy *BytewiseComparatorImpl) FindShortSuccessor(key *[]byte)

func (*BytewiseComparatorImpl) FindShortestSeparator

func (dummy *BytewiseComparatorImpl) FindShortestSeparator(start *[]byte, limit string)

func (*BytewiseComparatorImpl) Name

func (dummy *BytewiseComparatorImpl) Name() string

type Comparator

type Comparator interface {
	// Three-way comparison.  Returns value:
	//   < 0 iff "a" < "b",
	//   == 0 iff "a" == "b",
	//   > 0 iff "a" > "b"
	Compare(key1, key2 []byte) int

	// The name of the comparator.  Used to check for comparator
	// mismatches (i.e., a DB created with one comparator is
	// accessed using a different comparator.
	//
	// The client of this package should switch to a new name whenever
	// the comparator implementation changes in a way that will cause
	// the relative ordering of any two keys to change.
	//
	// Names starting with "leveldb." are reserved and should not be used
	// by any clients of this package.
	Name() string

	// If *start < limit, changes *start to a short string in [start,limit).
	// Simple comparator implementations may return with *start unchanged,
	// i.e., an implementation of this method that does nothing is correct.
	FindShortestSeparator(start *[]byte, limit string)

	// Changes *key to a short string >= *key.
	// Simple comparator implementations may return with *key unchanged,
	// i.e., an implementation of this method that does nothing is correct.
	FindShortSuccessor(key *[]byte)
}

A Comparator object provides a total order across slices that are used as keys in an sstable or a database. A Comparator implementation must be thread-safe since leveldb may invoke its methods concurrently from multiple threads.

func NewBytewiseComparator

func NewBytewiseComparator() Comparator

Return a builtin comparator that uses lexicographic byte-wise ordering. The result remains the property of this module and must not be deleted.

type DB

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

func NewDB

func NewDB(opt *Options) *DB

func (*DB) BuildBatchGroup

func (db *DB) BuildBatchGroup(lastWriter **list.Element, mustNotBeSync bool) *WriteBatch

REQUIRES: Writer list must be non-empty REQUIRES: First writer must have a non-null batch

func (*DB) Close

func (db *DB) Close()

func (*DB) CompactMemTable

func (db *DB) CompactMemTable()

func (*DB) Delete

func (db *DB) Delete(opt *WriteOptions, key []byte) Status

Remove the database entry (if any) for "key". Returns OK on success, and a non-OK status on error. It is not an error if "key" did not exist in the database. Note: consider setting options.sync = true.

func (*DB) DeleteObsoleteFiles

func (db *DB) DeleteObsoleteFiles()

func (*DB) Get

func (db *DB) Get(opt *ReadOptions, key []byte) ([]byte, Status)

If the database contains an entry for "key" store the corresponding value in *value and return OK.

If there is no entry for "key" leave *value unchanged and return a status for which Status::IsNotFound() returns true.

May return some other Status on an error.

func (*DB) Put

func (db *DB) Put(opt *WriteOptions, key, value []byte) Status

Set the database entry for "key" to "value". Returns OK on success, and a non-OK status on error. Note: consider setting opt.Sync = true.

func (*DB) RecordBackgroundError

func (db *DB) RecordBackgroundError(err Status)

func (*DB) Recover

func (db *DB) Recover() (*VersionEdit, Status)

func (*DB) Write

func (db *DB) Write(opt *WriteOptions, myBatch *WriteBatch) Status

func (*DB) WriteLevel0Table

func (db *DB) WriteLevel0Table(mem *MemTable) (Status, *VersionEdit)

新的ldb文件记录到edit中

type EmptyIterator

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

func (*EmptyIterator) Key

func (_ *EmptyIterator) Key() []byte

func (*EmptyIterator) Next

func (_ *EmptyIterator) Next()

func (*EmptyIterator) Prev

func (_ *EmptyIterator) Prev()

func (*EmptyIterator) Seek

func (_ *EmptyIterator) Seek([]byte)

func (*EmptyIterator) SeekToFirst

func (_ *EmptyIterator) SeekToFirst()

func (*EmptyIterator) SeekToLast

func (_ *EmptyIterator) SeekToLast()

func (*EmptyIterator) Status

func (i *EmptyIterator) Status() Status

func (*EmptyIterator) Valid

func (_ *EmptyIterator) Valid() bool

func (*EmptyIterator) Value

func (_ *EmptyIterator) Value() []byte

type Env

type Env interface {
	// Create an object that sequentially reads the file with the specified name.
	// On success, return new file and nil status.
	// On failure return nil and non-nil status . If the file does
	// not exist, returns a non-OK status.  Implementations should return a
	// NotFound status when the file does not exist.
	//
	// The returned file will only be accessed by one thread at a time.
	NewSequentialFile(fname string) (SequentialFile, Status)

	// Create an object supporting random-access reads from the file with the
	// specified name. On success, return new file in and nil status.  On failure return nil and
	// non-nil status.  If the file does not exist, returns a non-nil
	// status. Implementations should return a NotFound status when the file does
	// not exist.
	//
	// The returned file may be concurrently accessed by multiple threads.
	NewRandomAccessFile(fname string) (RandomAccessFile, Status)

	// Create an object that writes to a new file with the specified
	// name. Deletes any existing file with the same name and creates a
	// new file. On success, return new file and nil status. On failure return nil and
	// non-nil status.
	//
	// The returned file will only be accessed by one thread at a time.
	NewWritableFile(fname string) (WritableFile, Status)

	// Create an object that either appends to an existing file, or
	// writes to a new file (if the file does not exist to begin with).
	// On success, return new file and nil status. On failure return nil and
	// non-nil status.
	//
	// The returned file will only be accessed by one thread at a time.
	//
	// May return an IsNotSupportedError error if this Env does
	// not allow appending to an existing file. Users of Env (including
	// the leveldb implementation) must be prepared to deal with
	// an Env that does not support appending.
	NewAppendableFile(fname string) (WritableFile, Status)

	// Returns true iff the named file exists.
	FileExists(fname string) bool

	// Return the names of the children of the specified directory.
	// The names are relative to "dir".
	// Original contents of *results are dropped.
	GetChildren(dir string) ([]string, Status)

	// Delete the named file.
	//
	// The default implementation calls DeleteFile, to support legacy Env
	// implementations. Updated Env implementations must override RemoveFile and
	// ignore the existence of DeleteFile. Updated code calling into the Env API
	// must call RemoveFile instead of DeleteFile.
	//
	// A future release will remove DeleteDir and the default implementation of
	// RemoveDir.
	RemoveFile(fname string) Status

	// Create the specified directory.
	CreateDir(dirname string) Status

	// Delete the specified directory.
	//
	// The default implementation calls DeleteDir, to support legacy Env
	// implementations. Updated Env implementations must override RemoveDir and
	// ignore the existence of DeleteDir. Modern code calling into the Env API
	// must call RemoveDir instead of DeleteDir.
	//
	// A future release will remove DeleteDir and the default implementation of
	// RemoveDir.
	RemoveDir(dirname string) Status

	// Store the size of fname in *file_size.
	GetFileSize(fname string) (int64, Status)

	// Rename file src to target.
	RenameFile(src, target string) Status

	// Lock the specified file. Used to prevent concurrent access to
	// the same db by multiple processes.  On failure, return nil and bad status.
	//
	// On success, return the object that represents the
	// acquired lock and OK.  The caller should call
	// UnlockFile(lock) to release the lock.  If the process exits,
	// the lock will be automatically released.
	//
	// If somebody else already holds the lock, finishes immediately
	// with a failure.  I.e., this call does not wait for existing locks
	// to go away.
	//
	// May create the named file if it does not already exist.
	LockFile(fname string) (FileLock, Status)

	// Release the lock acquired by a previous successful call to LockFile.
	// REQUIRES: lock was returned by a successful LockFile() call
	// REQUIRES: lock has not already been unlocked.
	UnlockFile(lock FileLock) Status
}

func DefaultEnv

func DefaultEnv() Env

type FileLock

type FileLock interface {
	io.Closer
}

Identifies a locked file.

type FileMetaData

type FileMetaData struct {
	Level    int
	Number   uint64
	FileSize uint64

	Smallest InternalKey
	Largest  InternalKey
}

func (*FileMetaData) String

func (f *FileMetaData) String() string

type FileType

type FileType int
const (
	LogFile FileType = iota
	DBLockFile
	TableFile
	DescriptorFile
	CurrentFile
	TempFile
	InfoLogFile

	InvalidFile = 1000
)

func ParseFileName

func ParseFileName(filename string) (bool, uint64, FileType)

Owned filenames have the form:

dbname/CURRENT
dbname/LOCK
dbname/LOG
dbname/LOG.old
dbname/MANIFEST-[0-9]+
dbname/[0-9]+.(log|ldb)

If filename is a leveldb file, store the type of the file in *type. The number encoded in the filename is stored in *number. If the filename was successfully parsed, returns true. Else return false.

type FilterBlockBuilder

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

A FilterBlockBuilder is used to construct all of the filters for a particular Table. It generates a single string which is stored as a special block in the Table.

The sequence of calls to FilterBlockBuilder must match the regexp:

(StartBlock AddKey*)* Finish

func (*FilterBlockBuilder) AddKey

func (fbb *FilterBlockBuilder) AddKey(key []byte)

func (*FilterBlockBuilder) Finish

func (fbb *FilterBlockBuilder) Finish() []byte

func (*FilterBlockBuilder) StartBlock

func (fbb *FilterBlockBuilder) StartBlock(blockOffset uint64)

type FilterBlockReader

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

func NewFilterBlockReader

func NewFilterBlockReader(policy FilterPolicy, contents []byte) *FilterBlockReader

func (*FilterBlockReader) KeyMayMatch

func (fbr *FilterBlockReader) KeyMayMatch(blockOffset uint64, key []byte) bool

type FilterPolicy

type FilterPolicy interface {
	// Return the name of this policy.  Note that if the filter encoding
	// changes in an incompatible way, the name returned by this method
	// must be changed.  Otherwise, old incompatible filters may be
	// passed to methods of this type.
	Name() string

	// keys contains a list of keys (potentially with duplicates)
	// that are ordered according to the user supplied comparator.
	// Append a filter that summarizes keys to *dst.
	//
	// Warning: do not change the initial contents of *dst.  Instead,
	// append the newly constructed filter to *dst.
	CreateFilter(keys [][]byte, dst *[]byte)

	// "filter" contains the data appended by a preceding call to
	// CreateFilter() on this class.  This method must return true if
	// the key was in the list of keys passed to CreateFilter().
	// This method may return true or false if the key was not on the
	// list, but it should aim to return false with a high probability.
	KeyMayMatch(key, filter []byte) bool
}

func NewBloomFilterPolicy

func NewBloomFilterPolicy(bitsPerKey int) FilterPolicy

Return a new filter policy that uses a bloom filter with approximately the specified number of bits per key. A good value for bits_per_key is 10, which yields a filter with ~ 1% false positive rate.

Callers must delete the result after any database that is using the result has been closed.

Note: if you are using a custom comparator that ignores some parts of the keys being compared, you must not use NewBloomFilterPolicy() and must provide your own FilterPolicy that also ignores the corresponding parts of the keys. For example, if the comparator ignores trailing spaces, it would be incorrect to use a FilterPolicy (like NewBloomFilterPolicy) that does not ignore trailing spaces in keys.

type Footer struct {
	MetaIndex BlockHandle // The block handle for the metaindex block of the table
	DataIndex BlockHandle // The block handle for the index block of the table
}

Footer encapsulates the fixed information stored at the tail end of every table file.

func (*Footer) DecodeFrom

func (f *Footer) DecodeFrom(input []byte) Status

func (*Footer) DecodeFromBuf

func (f *Footer) DecodeFromBuf(input *bytes.Buffer) Status

func (*Footer) EncodeTo

func (f *Footer) EncodeTo() []byte

type InternalKey

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

Modules in this directory should keep internal keys wrapped inside the following class instead of plain strings so that we do not incorrectly use string comparisons instead of an InternalKeyComparator.

func NewInternalKey

func NewInternalKey(userKey []byte, s SequenceNumber, t ValueType) *InternalKey

func (*InternalKey) Clear

func (ikey *InternalKey) Clear()

func (*InternalKey) DecodeFrom

func (ikey *InternalKey) DecodeFrom(s []byte)

func (*InternalKey) Encode

func (ikey *InternalKey) Encode() []byte

func (*InternalKey) UserKey

func (ikey *InternalKey) UserKey() []byte

type InternalKeyComparator

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

Implements Comparator. A comparator for internal keys that uses a specified comparator for the user key portion and breaks ties by decreasing sequence number.

func (*InternalKeyComparator) Compare

func (icmp *InternalKeyComparator) Compare(key1, key2 []byte) int

key1, key2 is InternalKey

func (*InternalKeyComparator) FindShortSuccessor

func (icmp *InternalKeyComparator) FindShortSuccessor(key *[]byte)

func (*InternalKeyComparator) FindShortestSeparator

func (icmp *InternalKeyComparator) FindShortestSeparator(start *[]byte, limit string)

func (*InternalKeyComparator) Name

func (icmp *InternalKeyComparator) Name() string

type Iterator

type Iterator interface {
	// An iterator is either positioned at a key/value pair, or
	// not valid.  This method returns true iff the iterator is valid.
	Valid() bool

	// Position at the first key in the source.  The iterator is Valid()
	// after this call iff the source is not empty.
	SeekToFirst()

	// Position at the last key in the source.  The iterator is
	// Valid() after this call iff the source is not empty.
	SeekToLast()

	// Position at the first key in the source that at or past target
	// The iterator is Valid() after this call iff the source contains
	// an entry that comes at or past target.
	Seek(target []byte)

	// Moves to the next entry in the source.  After this call, Valid() is
	// true iff the iterator was not positioned at the last entry in the source.
	// REQUIRES: Valid()
	Next()

	// Moves to the previous entry in the source.  After this call, Valid() is
	// true iff the iterator was not positioned at the first entry in source.
	// REQUIRES: Valid()
	Prev()

	// Return the key for the current entry.  The underlying storage for
	// the returned slice is valid only until the next modification of
	// the iterator.
	// REQUIRES: Valid()
	Key() []byte

	// Return the value for the current entry.  The underlying storage for
	// the returned slice is valid only until the next modification of
	// the iterator.
	// REQUIRES: Valid()
	Value() []byte

	// If an error has occurred, return it.  Else return an ok status.
	Status() Status
}

func NewEmptyIterator

func NewEmptyIterator(s Status) Iterator

Return an empty iterator with the specified status.

func NewMemTableIterator

func NewMemTableIterator(mtable *MemTable) Iterator

func NewSkiplistIterator

func NewSkiplistIterator(sk *SkipList) Iterator

type LevelDBStatus

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

func (*LevelDBStatus) Error

func (s *LevelDBStatus) Error() string

func (*LevelDBStatus) IsCorruption

func (s *LevelDBStatus) IsCorruption() bool

func (*LevelDBStatus) IsIOError

func (s *LevelDBStatus) IsIOError() bool

func (*LevelDBStatus) IsInvalidArgument

func (s *LevelDBStatus) IsInvalidArgument() bool

func (*LevelDBStatus) IsNotFound

func (s *LevelDBStatus) IsNotFound() bool

func (*LevelDBStatus) IsNotSupported

func (s *LevelDBStatus) IsNotSupported() bool

func (*LevelDBStatus) IsOK

func (s *LevelDBStatus) IsOK() bool

func (*LevelDBStatus) String

func (s *LevelDBStatus) String() string

Return a string representation of this status suitable for printing. Returns the string "OK" for success.

type LogReader

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

func NewLogReader

func NewLogReader(src SequentialFile) *LogReader

Create a reader that will return log records from "*file". "*file" must remain live while this Reader is in use.

If "reporter" is non-NULL, it is notified whenever some data is dropped due to a detected corruption. "*reporter" must remain live while this Reader is in use.

If "checksum" is true, verify checksums if available.

The Reader will start reading at the first record located at physical position >= initial_offset within the file.

func (*LogReader) Close

func (lr *LogReader) Close() Status

func (*LogReader) EnableCheckCrc

func (lr *LogReader) EnableCheckCrc(enable bool)

func (*LogReader) ReadRecord

func (lr *LogReader) ReadRecord(record *[]byte) bool

Read the next record into *record. Returns true if read successfully, false if we hit end of the input. May use "*scratch" as temporary storage. The contents filled in *record will only be valid until the next mutating operation on this reader or the next mutation to *scratch.

type LogWriter

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

func NewLogWriter

func NewLogWriter(dest WritableFile) *LogWriter

Create a writer that will append data to "*dest". "*dest" must be initially empty. "*dest" must remain live while this Writer is in use.

func (*LogWriter) AddRecord

func (lw *LogWriter) AddRecord(slice []byte) Status

func (*LogWriter) Close

func (lw *LogWriter) Close() Status

func (*LogWriter) SetCrash

func (lw *LogWriter) SetCrash(crash bool)

func (*LogWriter) Sync

func (lw *LogWriter) Sync() Status

type LookupKey

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

A helper class useful for DB::Get()

func NewLookupKey

func NewLookupKey(userKey []byte, seq SequenceNumber) *LookupKey

Initialize *this for looking up user_key at a snapshot with the specified sequence number.

func (*LookupKey) InternalKey

func (lk *LookupKey) InternalKey() []byte

Return an internal key (suitable for passing to an internal iterator)

func (*LookupKey) MemtableKey

func (lk *LookupKey) MemtableKey() []byte

Return a key suitable for lookup in a MemTable.

func (*LookupKey) UserKey

func (lk *LookupKey) UserKey() []byte

Return the user key

type MemTable

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

func NewMemtable

func NewMemtable(cmp *InternalKeyComparator) *MemTable

func (*MemTable) Add

func (mtbl *MemTable) Add(seq SequenceNumber, t ValueType, key, value []byte)

Add an entry into memtable that maps key to value at the specified sequence number and with the specified type. Typically value will be empty if type==kTypeDeletion.

func (*MemTable) ApproximateMemoryUsage

func (mtbl *MemTable) ApproximateMemoryUsage() uint64

Returns an estimate of the number of bytes of data in use by this data structure.

REQUIRES: external synchronization to prevent simultaneous operations on the same MemTable.

func (*MemTable) Get

func (mtbl *MemTable) Get(key *LookupKey, value *[]byte) (bool, Status)

If memtable contains a value for key, store it in *value and return true. If memtable contains a deletion for key, store a NotFound() error in *status and return true. Else, return false.

func (*MemTable) NewIterator

func (mtbl *MemTable) NewIterator() Iterator

Return an iterator that yields the contents of the memtable.

The caller must ensure that the underlying MemTable remains live while the returned iterator is live. The keys returned by this iterator are internal keys encoded by AppendInternalKey in the db/format.{h,cc} module.

type MemTableIterator

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

func (*MemTableIterator) Key

func (mit *MemTableIterator) Key() []byte

func (*MemTableIterator) Next

func (mit *MemTableIterator) Next()

func (*MemTableIterator) Prev

func (mit *MemTableIterator) Prev()

func (*MemTableIterator) Seek

func (mit *MemTableIterator) Seek(target []byte)

func (*MemTableIterator) SeekToFirst

func (mit *MemTableIterator) SeekToFirst()

func (*MemTableIterator) SeekToLast

func (mit *MemTableIterator) SeekToLast()

func (*MemTableIterator) Status

func (mit *MemTableIterator) Status() Status

func (*MemTableIterator) Valid

func (mit *MemTableIterator) Valid() bool

func (*MemTableIterator) Value

func (mit *MemTableIterator) Value() []byte

type MemtableKeyComparator

type MemtableKeyComparator struct {
	*InternalKeyComparator
}

func NewMemtableKeyComparator

func NewMemtableKeyComparator(icmp *InternalKeyComparator) *MemtableKeyComparator

func (*MemtableKeyComparator) Compare

func (mcmp *MemtableKeyComparator) Compare(key1, key2 []byte) int

func (*MemtableKeyComparator) Name

func (mcmp *MemtableKeyComparator) Name() string

type Options

type Options struct {

	// Comparator used to define the order of keys in the table.
	// Default: a comparator that uses lexicographic byte-wise ordering
	//
	// REQUIRES: The client must ensure that the comparator supplied
	// here has the same name and orders keys *exactly* the same as the
	// comparator provided to previous open calls on the same DB.
	Comp Comparator

	// If true, the database will be created if it is missing.
	// Default: false
	CreateIfMissing bool

	// If true, an error is raised if the database already exists.
	// Default: false
	ErrorIfExists bool

	// Use the specified object to interact with the environment,
	// e.g. to read/write files, schedule background work, etc.
	// Default: DefaultEnv()
	Env Env

	// Amount of data to build up in memory (backed by an unsorted log
	// on disk) before converting to a sorted on-disk file.
	//
	// Larger values increase performance, especially during bulk loads.
	// Up to two write buffers may be held in memory at the same time,
	// so you may wish to adjust this parameter to control memory usage.
	// Also, a larger write buffer will result in a longer recovery time
	// the next time the database is opened.
	//
	// Default: 4MB
	WriteBufferSize uint64

	// Approximate size of user data packed per block.  Note that the
	// block size specified here corresponds to uncompressed data.  The
	// actual size of the unit read from disk may be smaller if
	// compression is enabled.  This parameter can be changed dynamically.
	//
	// Default: 4K
	BlockSize int64

	// Number of keys between restart points for delta encoding of keys.
	// This parameter can be changed dynamically.  Most clients should
	// leave this parameter alone.
	//
	// Default: 16
	BlockRestartInterval int

	// Compress blocks using the specified compression algorithm.  This
	// parameter can be changed dynamically.
	//
	// Default: kSnappyCompression, which gives lightweight but fast
	// compression.
	//
	// Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
	//    ~200-500MB/s compression
	//    ~400-800MB/s decompression
	// Note that these speeds are significantly faster than most
	// persistent storage speeds, and therefore it is typically never
	// worth switching to kNoCompression.  Even if the input data is
	// incompressible, the kSnappyCompression implementation will
	// efficiently detect that and will switch to uncompressed mode.
	CompressionType byte

	// If non-NULL, use the specified filter policy to reduce disk reads.
	// Many applications will benefit from passing the result of
	// NewBloomFilterPolicy() here.
	//
	// Default: NULL
	Policy FilterPolicy
}

Options to control the behavior of a database (passed to DB::Open)

func NewCreateIfMissingOptions

func NewCreateIfMissingOptions() *Options

func NewOptions

func NewOptions() *Options

type ParsedInternalKey

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

func NewParsedInternalKey

func NewParsedInternalKey(userKey []byte, seq SequenceNumber, t ValueType) ParsedInternalKey

type PosixEnv

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

func NewPosixEnv

func NewPosixEnv() *PosixEnv

func (*PosixEnv) CreateDir

func (env *PosixEnv) CreateDir(dir string) Status

func (*PosixEnv) FileExists

func (env *PosixEnv) FileExists(fname string) bool

func (*PosixEnv) GetChildren

func (env *PosixEnv) GetChildren(dir string) ([]string, Status)

func (*PosixEnv) GetFileSize

func (env *PosixEnv) GetFileSize(fname string) (int64, Status)

func (*PosixEnv) LockFile

func (env *PosixEnv) LockFile(fname string) (FileLock, Status)

func (*PosixEnv) NewAppendableFile

func (env *PosixEnv) NewAppendableFile(fname string) (WritableFile, Status)

func (*PosixEnv) NewRandomAccessFile

func (env *PosixEnv) NewRandomAccessFile(fname string) (RandomAccessFile, Status)

func (*PosixEnv) NewSequentialFile

func (env *PosixEnv) NewSequentialFile(fname string) (SequentialFile, Status)

func (*PosixEnv) NewWritableFile

func (env *PosixEnv) NewWritableFile(fname string) (WritableFile, Status)

func (*PosixEnv) RemoveDir

func (env *PosixEnv) RemoveDir(dir string) Status

func (*PosixEnv) RemoveFile

func (env *PosixEnv) RemoveFile(fname string) Status

func (*PosixEnv) RenameFile

func (env *PosixEnv) RenameFile(src, target string) Status

func (*PosixEnv) UnlockFile

func (env *PosixEnv) UnlockFile(lock FileLock) Status

type PosixFileLock

type PosixFileLock struct {
	*os.File
}

func NewPosixFileLock

func NewPosixFileLock(fname string) (*PosixFileLock, error)

func (*PosixFileLock) Flock

func (file *PosixFileLock) Flock() Status

func (*PosixFileLock) Funlock

func (file *PosixFileLock) Funlock() Status

type PosixRandomAccessFile

type PosixRandomAccessFile struct {
	*os.File
}

Implements random read access in a file using pread().

func NewPosixRandomAccessFile

func NewPosixRandomAccessFile(fname string) (*PosixRandomAccessFile, error)

func (*PosixRandomAccessFile) Read

func (file *PosixRandomAccessFile) Read(offset int64, n int, scratch []byte) ([]byte, Status)

type PosixSequentialFile

type PosixSequentialFile struct {
	*os.File
}

Implements sequential read access in a file using read().

func NewPosixSequentialFile

func NewPosixSequentialFile(fname string) (*PosixSequentialFile, error)

func (*PosixSequentialFile) Read

func (file *PosixSequentialFile) Read(n int, scratch []byte) ([]byte, Status)

func (*PosixSequentialFile) Skip

func (file *PosixSequentialFile) Skip(n int64) Status

type PosixWritableFile

type PosixWritableFile struct {
	*os.File
	*bufio.Writer
}

func NewPosixWritableFile

func NewPosixWritableFile(fname string, appendable bool) (*PosixWritableFile, error)

func (*PosixWritableFile) Append

func (file *PosixWritableFile) Append(data []byte) Status

func (*PosixWritableFile) Close

func (file *PosixWritableFile) Close() error

func (*PosixWritableFile) Flush

func (file *PosixWritableFile) Flush() Status

func (*PosixWritableFile) Sync

func (file *PosixWritableFile) Sync() Status

type RandomAccessFile

type RandomAccessFile interface {
	// Read up to "n" bytes from the file starting at "offset".
	// "scratch[0..n-1]" may be written by this routine.  Return
	// the data that was read (including if fewer than "n" bytes were
	// successfully read).
	// If an error was encountered, returns a non-OK status.
	//
	// Safe for concurrent use by multiple threads.
	Read(offset int64, n int, scratch []byte) ([]byte, Status)

	io.Closer
}

A file abstraction for randomly reading the contents of a file.

type ReadOptions

type ReadOptions struct {
	// If true, all data read from underlying storage will be
	// verified against corresponding checksums.
	// Default: false
	VerifyChecksums bool

	// Should the data read for this iteration be cached in memory?
	// Callers may wish to set this field to false for bulk scans.
	// Default: true
	FillCache bool

	// If "snapshot" is <= kMaxSequenceNumber, read as of the supplied snapshot
	// (which must belong to the DB that is being read and which must
	// not have been released).  If "snapshot" is beyond kMaxSequenceNumber, use an implicit
	// snapshot of the state at the beginning of this read operation.
	// Default: kMaxSequenceNumber+1
	Snapshot SequenceNumber
}

Options that control read operations

func NewReadOptions

func NewReadOptions() *ReadOptions

type RecordType

type RecordType byte

type SequenceNumber

type SequenceNumber uint64

type SequentialFile

type SequentialFile interface {
	// Read up to "n" bytes from the file.  "scratch[0..n-1]" may be
	// written by this routine.  Return the data that was
	// read (including if fewer than "n" bytes were successfully read).
	// If an error was encountered, returns a non-OK status.
	//
	// REQUIRES: External synchronization
	Read(n int, scratch []byte) ([]byte, Status)

	// Skip "n" bytes from the file. This is guaranteed to be no
	// slower that reading the same data, but may be faster.
	//
	// If end of file is reached, skipping will stop at the end of the
	// file, and Skip will return OK.
	//
	// REQUIRES: External synchronization
	Skip(n int64) Status

	io.Closer
}

A file abstraction for reading sequentially through a file

type SkipList

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

func NewSkipList

func NewSkipList(cmp Comparator) *SkipList

func (*SkipList) ByteSize

func (sl *SkipList) ByteSize() uint64

func (*SkipList) Contains

func (sl *SkipList) Contains(key []byte) bool

func (*SkipList) Height

func (sl *SkipList) Height() int32

func (*SkipList) Insert

func (sl *SkipList) Insert(key, value []byte) error

single thread insert

func (*SkipList) MaxHeight

func (sl *SkipList) MaxHeight() int32

func (*SkipList) NumOfNode

func (sl *SkipList) NumOfNode() int

func (*SkipList) String

func (sl *SkipList) String() string

type SkiplistIterator

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

func (*SkiplistIterator) Key

func (it *SkiplistIterator) Key() []byte

func (*SkiplistIterator) Next

func (it *SkiplistIterator) Next()

func (*SkiplistIterator) Prev

func (it *SkiplistIterator) Prev()

func (*SkiplistIterator) Seek

func (it *SkiplistIterator) Seek(target []byte)

func (*SkiplistIterator) SeekToFirst

func (it *SkiplistIterator) SeekToFirst()

func (*SkiplistIterator) SeekToLast

func (it *SkiplistIterator) SeekToLast()

func (*SkiplistIterator) Status

func (it *SkiplistIterator) Status() Status

func (*SkiplistIterator) Valid

func (it *SkiplistIterator) Valid() bool

func (*SkiplistIterator) Value

func (it *SkiplistIterator) Value() []byte

type Status

type Status interface {
	fmt.Stringer

	// !!! nil status is OK
	IsOK() bool
	IsNotFound() bool
	IsCorruption() bool
	IsNotSupported() bool
	IsInvalidArgument() bool
	IsIOError() bool

	// builtin error interface
	error
}

func BuildTable

func BuildTable(mem *MemTable, builder *TableBuilder, meta *FileMetaData) Status

No lock for this func call

func NewStatus

func NewStatus(s byte, msg ...string) Status

type TableBuilder

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

func NewTableBuilder

func NewTableBuilder(opts *Options, file WritableFile) *TableBuilder

func (*TableBuilder) Abandon

func (tb *TableBuilder) Abandon()

func (*TableBuilder) Add

func (tb *TableBuilder) Add(key, value []byte)

func (*TableBuilder) FileSize

func (tb *TableBuilder) FileSize() uint64

func (*TableBuilder) Finish

func (tb *TableBuilder) Finish() Status

func (*TableBuilder) Flush

func (tb *TableBuilder) Flush()

func (*TableBuilder) NumEntries

func (tb *TableBuilder) NumEntries() uint64

type ValueType

type ValueType byte

type Version

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

func (*Version) Get

func (ver *Version) Get(opt *ReadOptions, lkey *LookupKey) ([]byte, Status)

func (*Version) NumFiles

func (ver *Version) NumFiles(level int) int

func (*Version) String

func (ver *Version) String() string

type VersionBuilder

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

func (*VersionBuilder) Apply

func (vb *VersionBuilder) Apply(edit *VersionEdit)

Apply all of the edits in *edit to the current state.

func (*VersionBuilder) MaybeAddFile

func (vb *VersionBuilder) MaybeAddFile(v *Version, level int, f *FileMetaData)

func (*VersionBuilder) SaveTo

func (vb *VersionBuilder) SaveTo() *Version

Save the current state in *v.

type VersionEdit

type VersionEdit struct {
	Comparator    string
	HasComparator bool

	LogNumber    uint64
	HasLogNumber bool

	NextFileNumber    uint64
	HasNextFileNumber bool

	LastSequence    SequenceNumber
	HasLastSequence bool

	DeletedFiles []FileMetaData
	NewFiles     []FileMetaData
}

func (*VersionEdit) AddFile

func (ve *VersionEdit) AddFile(level int, fileNumber, fileSize uint64, smallest, largest InternalKey)

Add the specified file at the specified number. REQUIRES: This version has not been saved (see VersionSet::SaveTo) REQUIRES: "smallest" and "largest" are smallest and largest keys in file

func (*VersionEdit) Clear

func (ve *VersionEdit) Clear()

func (*VersionEdit) Decode

func (ve *VersionEdit) Decode(input []byte) Status

func (*VersionEdit) DeleteFile

func (ve *VersionEdit) DeleteFile(level int, fileNumber uint64)

Delete the specified "file" from the specified "level".

func (*VersionEdit) Encode

func (ve *VersionEdit) Encode() []byte

func (*VersionEdit) SetComparatorName

func (ve *VersionEdit) SetComparatorName(name string)

func (*VersionEdit) SetLastSequence

func (ve *VersionEdit) SetLastSequence(seq SequenceNumber)

func (*VersionEdit) SetLogNumber

func (ve *VersionEdit) SetLogNumber(num uint64)

func (*VersionEdit) SetNextFile

func (ve *VersionEdit) SetNextFile(num uint64)

type VersionLevelState

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

type VersionSet

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

func NewVersionSet

func NewVersionSet(dbname string, opts *Options, icmp *InternalKeyComparator) *VersionSet

func (*VersionSet) AppendVersion

func (vs *VersionSet) AppendVersion(v *Version)

func (*VersionSet) Current

func (vs *VersionSet) Current() *Version

func (*VersionSet) LastSequence

func (vs *VersionSet) LastSequence() SequenceNumber

func (*VersionSet) LiveFiles

func (vs *VersionSet) LiveFiles() []uint64

func (*VersionSet) LogAndApply

func (vs *VersionSet) LogAndApply(edit *VersionEdit, mu *sync.Mutex) Status

Apply *edit to the current version to form a new descriptor that is both saved to persistent state and installed as the new current version. Will release *mu while actually writing to the file. REQUIRES: *mu is held on entry. REQUIRES: no other thread concurrently calls LogAndApply()

func (*VersionSet) LogNumber

func (vs *VersionSet) LogNumber() uint64

func (*VersionSet) ManifestFileNumber

func (vs *VersionSet) ManifestFileNumber() uint64

func (*VersionSet) MarkFileNumberUsed

func (vs *VersionSet) MarkFileNumberUsed(number uint64)

func (*VersionSet) NewFileNumber

func (vs *VersionSet) NewFileNumber() uint64

func (*VersionSet) Recover

func (vs *VersionSet) Recover() Status

Recover the last saved descriptor from persistent storage.

func (*VersionSet) ReuseFileNumber

func (vs *VersionSet) ReuseFileNumber(number uint64)

func (*VersionSet) SetLastSequence

func (vs *VersionSet) SetLastSequence(s SequenceNumber)

func (*VersionSet) WriteSnapshot

func (vs *VersionSet) WriteSnapshot() Status

type WritableFile

type WritableFile interface {
	Append(data []byte) Status
	Flush() Status
	Sync() Status
	io.Closer
}

A file abstraction for sequential writing. The implementation must provide buffering since callers may append small fragments at a time to the file.

type WriteBatch

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

func NewWriteBatch

func NewWriteBatch() *WriteBatch

func (*WriteBatch) Append

func (wb *WriteBatch) Append(src *WriteBatch)

func (*WriteBatch) ByteSize

func (wb *WriteBatch) ByteSize() int

func (*WriteBatch) Clear

func (wb *WriteBatch) Clear()

func (*WriteBatch) Contents

func (wb *WriteBatch) Contents() []byte

func (*WriteBatch) Count

func (wb *WriteBatch) Count() int

func (*WriteBatch) Delete

func (wb *WriteBatch) Delete(key []byte)

func (*WriteBatch) ForEach

func (wb *WriteBatch) ForEach(wbp WriteBatchProcessor) Status

func (*WriteBatch) Put

func (wb *WriteBatch) Put(key, value []byte)

func (*WriteBatch) Sequence

func (wb *WriteBatch) Sequence() SequenceNumber

func (*WriteBatch) SetContents

func (wb *WriteBatch) SetContents(contents []byte)

func (*WriteBatch) SetCount

func (wb *WriteBatch) SetCount(c int)

func (*WriteBatch) SetSequence

func (wb *WriteBatch) SetSequence(s SequenceNumber)

type WriteBatchProcessor

type WriteBatchProcessor interface {
	ProcessWriteBatch(seq SequenceNumber, tp ValueType, key, value []byte)
}

For iterate writebatch and process each write request

type WriteOptions

type WriteOptions struct {
	// If true, the write will be flushed from the operating system
	// buffer cache (by calling WritableFile.Sync()) before the write
	// is considered complete.  If this flag is true, writes will be
	// slower.
	//
	// If this flag is false, and the machine crashes, some recent
	// writes may be lost.  Note that if it is just the process that
	// crashes (i.e., the machine does not reboot), no writes will be
	// lost even if sync==false.
	//
	// In other words, a DB write with sync==false has similar
	// crash semantics as the "write()" system call.  A DB write
	// with sync==true has similar crash semantics to a "write()"
	// system call followed by "fsync()".
	//
	// Default: false
	Sync bool
}

Options that control write operations

Jump to

Keyboard shortcuts

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