Skip to main content

Overview

The WriteBatch class holds a collection of updates to apply atomically to a database. All updates are applied in the order they are added to the WriteBatch. WriteBatch supports Put, Delete, Merge, and DeleteRange operations.

Constructor

size_t
default:"0"
Number of bytes to pre-allocate for the batch. Useful for avoiding reallocations when batch size is known.
size_t
default:"0"
Maximum size of the batch in bytes. If 0, no limit is enforced.
size_t
default:"0"
Number of bytes used to store protection information for each key entry. Currently supported values are zero (disabled) and eight.

Write Operations

Put

Store the mapping “key->value” in the database.
ColumnFamilyHandle*
Target column family. If nullptr, uses the default column family.
const Slice&
The key to store. When user-defined timestamp is enabled, key should point to a contiguous buffer with timestamp appended after user key.
const Slice&
User-defined timestamp for the key.
const Slice&
The value to associate with the key.

TimedPut (Experimental)

Stores the mapping “key->value” with a specified write time.
uint64_t
Unix timestamp indicating when the write occurred.

PutEntity

Store wide column entities.

Delete

Erase a key from the database.

SingleDelete

See DB::SingleDelete() for semantics and constraints.

DeleteRange

Delete a range of keys [begin_key, end_key).
const Slice&
Start of the range (inclusive).
const Slice&
End of the range (exclusive).

Merge

Merge “value” with the existing value of “key” using the merge operator.

Utility Methods

Clear

Clear all updates buffered in this batch. Internally calls resize() on the string buffer, so allocated memory capacity may not be freed.

Count

Returns the number of updates in the batch.

GetDataSize

Returns the size of the serialized batch in bytes.

Data

Retrieve the serialized version of this batch.

SetMaxBytes

Set the maximum size limit for the batch.

Save Points

Save points allow you to rollback portions of a WriteBatch.

SetSavePoint

Records the state of the batch for future calls to RollbackToSavePoint(). May be called multiple times.

RollbackToSavePoint

Remove all entries added since the most recent SetSavePoint() and removes that save point. Returns Status::NotFound() if no save point exists.

PopSavePoint

Pop the most recent save point without rolling back. Returns Status::NotFound() if no save point exists.

Content Queries

Query what types of operations are in the batch:

Iteration

Handler

Implement a WriteBatch::Handler to iterate over the contents of a batch:

Iterate

Iterate over the batch contents, calling the appropriate handler methods.

Examples

Thread Safety

Multiple threads can invoke const methods on a WriteBatch without external synchronization. However, if any thread may call a non-const method, all threads accessing the same WriteBatch must use external synchronization.

See Also