pub struct DefragContext { /* private fields */ }Implementations§
Source§impl DefragContext
impl DefragContext
Sourcepub unsafe fn new(defrag_ctx: *mut RedisModuleDefragCtx) -> DefragContext
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).
Sourcepub fn should_stop(&self) -> bool
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.
Sourcepub fn set_cursor(&self, cursor: u64) -> Status
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.
Sourcepub fn get_cursor(&self) -> Result<u64, RedisError>
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.
Sourcepub unsafe fn defrag_realloc<T>(&self, ptr: *mut T) -> *mut T
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.
Sourcepub fn defrag_alloc<T>(&self, layout: Layout) -> *mut T
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.
Sourcepub fn defrag_dealloc<T>(&self, ptr: *mut T, layout: Layout)
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.
Sourcepub fn defrag_redis_string(&self, s: RedisString) -> RedisString
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
impl Debug for DefragContext
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.