直接从GitHub上clone下来RocksDB的源码。
RocksDB的put操作的声明代码在头文件include/rocksdb/db.h中:
// Set the database entry for "key" to "value".
// If "key" already exists, it will be overwritten.
// Returns OK on success, and a non-OK status on error.
// Note: consider setting options.sync = true.
virtual Status Put(const WriteOptions& options,
ColumnFamilyHandle* column_family, const Slice& key,
const Slice& value) = 0;
virtual Status Put(const WriteOptions& options, const Slice& key,
const Slice& value) {
return Put(options, DefaultColumnFamily(), key, value);
}
注释说明如果put已经存在的key,那么会对value进行覆盖。
如果不指定column_family,那么会用默认的column_family。
put函数的实现代码在db/db_impl_write.cc文件中:
// Default implementations of convenience methods that subclasses of DB
// can call if they wish
Status DB::Put(const WriteOptions& opt, ColumnFamilyHandle* column_family,
const Slice& key, const Slice& value) {
// Pre-allocate size of write batch conservatively.

本文详细探讨了RocksDB中的Put操作和WriteBatch操作。通过分析源码,揭示了Put如何覆盖已有key的value,以及WriteBatch的内部结构,包括sequence number和record count。在数据写入过程中,RocksDB使用write group合并批量写入,并通过WriteThread确保数据安全写入WAL文件和Memtable,避免数据丢失。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



