Struct Index

Source
pub struct Index { /* private fields */ }
Expand description

Main Index Insertion-only mapping from bytes to a list of u64s.

An Index is backed by an append-only file in the filesystem. Internally, it uses base16 radix trees for keys and linked list for u64 values. The file format was designed to be able to support other types of indexes (ex. non-radix-trees). Though none of them are implemented.

Implementations§

Source§

impl Index

Source

pub fn try_clone(&self) -> Result<Self>

Return a cloned Index with pending in-memory changes.

Source

pub fn try_clone_without_dirty(&self) -> Result<Self>

Return a cloned Index without pending in-memory changes.

This is logically equivalent to calling clear_dirty immediately on the result after try_clone, but potentially cheaper.

Source

pub fn get_meta(&self) -> &[u8]

Get metadata attached to the root node. This is what previously set by Index::set_meta.

Source

pub fn get_original_meta(&self) -> &[u8]

Get metadata attached to the root node at file open time. This is what stored on the filesystem at the index open time, not affected by Index::set_meta.

Source

pub fn set_meta<B: AsRef<[u8]>>(&mut self, meta: B)

Set metadata attached to the root node. Will be written at Index::flush time.

Source

pub fn clear_dirty(&mut self)

Remove dirty (in-memory) state. Restore the Index to the state as if it’s just loaded from disk without modifications.

Source

pub fn flush(&mut self) -> Result<u64>

Flush changes to disk.

Take the file lock when writing.

Return 0 if nothing needs to be written. Otherwise return the new file length on success. Return io::ErrorKind::PermissionDenied if the file was marked read-only at open time.

The new file length can be used to obtain the exact same view of the index as it currently is. That means, other changes to the indexes won’t be “combined” during flush. For example, given the following events happened in order:

  • Open. Get Index X.
  • Open using the same arguments. Get Index Y.
  • Write key “p” to X.
  • Write key “q” to Y.
  • Flush X. Get new length LX.
  • Flush Y. Get new length LY.
  • Open using LY as logical_len. Get Index Z.

Then key “p” does not exist in Z. This allows some advanced use cases. On the other hand, if “merging changes” is the desired behavior, the caller needs to take another lock, re-instantiate Index and re-insert keys.

For in-memory-only indexes, this function does nothing and returns 0, unless read-only was set at open time.

Source

pub fn get<K: AsRef<[u8]>>(&self, key: &K) -> Result<LinkOffset>

Lookup by key. Return LinkOffset.

To test if the key exists or not, use Offset::is_null. To obtain all values, use LinkOffset::values.

Source

pub fn scan_prefix_base16( &self, base16: impl Iterator<Item = u8>, ) -> Result<RangeIter<'_>>

Scan entries which match the given prefix in base16 form. Return RangeIter which allows accesses to keys and values.

Source

pub fn scan_prefix<B: AsRef<[u8]>>(&self, prefix: B) -> Result<RangeIter<'_>>

Scan entries which match the given prefix in base256 form. Return RangeIter which allows accesses to keys and values.

Source

pub fn scan_prefix_hex<B: AsRef<[u8]>>( &self, prefix: B, ) -> Result<RangeIter<'_>>

Scan entries which match the given prefix in hex form. Return RangeIter which allows accesses to keys and values.

Source

pub fn range<'a>( &self, range: impl RangeBounds<&'a [u8]>, ) -> Result<RangeIter<'_>>

Scans entries whose keys are within the given range.

Returns a double-ended iterator, which provides accesses to keys and values.

Source

pub fn insert<K: AsRef<[u8]>>(&mut self, key: &K, value: u64) -> Result<()>

Insert a key-value pair. The value will be the head of the linked list. That is, get(key).values().first() will return the newly inserted value.

Source

pub fn remove(&mut self, key: impl AsRef<[u8]>) -> Result<()>

Remove all values associated with the given key.

Source

pub fn remove_prefix(&mut self, prefix: impl AsRef<[u8]>) -> Result<()>

Remove all values associated with all keys with the given prefix.

Source

pub fn insert_advanced( &mut self, key: InsertKey<'_>, value: InsertValue, ) -> Result<()>

Update the linked list for a given key.

If link is None, behave like insert. Otherwise, ignore the existing values key mapped to, create a new link entry that chains to the given LinkOffset.

key could be a reference, or an embedded value. See InsertKey for details.

This is a low-level API.

Source

pub fn slice_to_bytes(&self, slice: &[u8]) -> Bytes

Convert a slice to Bytes. Do not copy the slice if it’s from the on-disk buffer.

Source

pub fn verify(&self) -> Result<()>

Verify checksum for the entire on-disk buffer.

Trait Implementations§

Source§

impl Debug for Index

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Index

§

impl !RefUnwindSafe for Index

§

impl Send for Index

§

impl Sync for Index

§

impl Unpin for Index

§

impl !UnwindSafe for Index

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more