Skip to main content

DefragContext

Struct DefragContext 

Source
pub struct DefragContext { /* private fields */ }

Implementations§

Source§

impl DefragContext

Source

pub unsafe fn new(defrag_ctx: *mut RedisModuleDefragCtx) -> DefragContext

Creates a new DefragContext from a poiter to raw::RedisModuleDefragCtx. The function is exposed for users that wants to implement the defrag function on their module datatype, they can use this function to create DefragContext that can be used in a safely manner.

§Safety

The function is considered unsafe because the provided pointer must be a valid pointer to raw::RedisModuleDefragCtx, and the Redis GIL must be held. Notice that the returned DefragContext borrows the pointer to raw::RedisModuleDefragCtx so it can not outlive it (this means that it should not be used once the defrag callback ends).

Source

pub fn should_stop(&self) -> bool

When the data type defrag callback iterates complex structures, this function should be called periodically. A false return indicates the callback may continue its work. A true indicates it should stop.

When stopped, the callback may use Self::set_cursor to store its position so it can later use Self::get_cursor to resume defragging.

When stopped and more work is left to be done, the callback should return 1. Otherwise, it should return 0.

NOTE: Modules should consider the frequency in which this function is called, so it generally makes sense to do small batches of work in between calls.

Source

pub fn set_cursor(&self, cursor: u64) -> Status

Store an arbitrary cursor value for future re-use.

This should only be called if Self::should_stop has returned a non-zero value and the defrag callback is about to exit without fully iterating its data type.

This behavior is reserved to cases where late defrag is performed. Late defrag is selected for keys that implement the free_effort callback and return a free_effort value that is larger than the defrag ‘active-defrag-max-scan-fields’ configuration directive.

Smaller keys, keys that do not implement free_effort or the global defrag callback are not called in late-defrag mode. In those cases, a call to this function will return Status::Err.

The cursor may be used by the module to represent some progress into the module’s data type. Modules may also store additional cursor-related information locally and use the cursor as a flag that indicates when traversal of a new key begins. This is possible because the API makes a guarantee that concurrent defragmentation of multiple keys will not be performed.

Source

pub fn get_cursor(&self) -> Result<u64, RedisError>

Fetch a cursor value that has been previously stored using Self::set_cursor. If not called for a late defrag operation, Err will be returned.

Source

pub unsafe fn defrag_realloc<T>(&self, ptr: *mut T) -> *mut T

Defrag a memory allocation previously allocated by RM_Alloc, RM_Calloc, etc. The defragmentation process involves allocating a new memory block and copying the contents to it, like realloc().

If defragmentation was not necessary, NULL is returned and the operation has no other effect.

§Safety

The function is unsafe because it is assumed that the pointer is valid and previusly allocated. It is considered undefined if this is not the case.

If a non-NULL value is returned, the caller should use the new pointer instead of the old one and update any reference to the old pointer, which must not be used again.

Source

pub fn defrag_alloc<T>(&self, layout: Layout) -> *mut T

Allocate memory using defrag allocator if supported by the current Redis server, fallback to regular allocation otherwise.

Source

pub fn defrag_dealloc<T>(&self, ptr: *mut T, layout: Layout)

Deallocate memory using defrag deallocator if supported by the current Redis server, fallback to regular deallocation otherwise.

Source

pub fn defrag_redis_string(&self, s: RedisString) -> RedisString

Defrag a RedisString

NOTE: It is only possible to defrag strings that have a single reference. Typically this means strings that was copy/cloned using RedisString::safe_clone or created using RedisString::new will not be defrag and will be returned as is.

Trait Implementations§

Source§

impl Debug for DefragContext

Source§

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

Formats the value using the given formatter. Read more
Source§

impl RedisLockIndicator for DefragContext

Having a DefragContext is indication that we are currently holding the Redis GIL, this is why it is safe to implement a RedisLockIndicator for DefragContext.

Auto Trait Implementations§

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, 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.