2015-07-05 13:35:15 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include "rocksdb/write_batch.h"
|
|
|
|
|
|
|
|
namespace py_rocks {
|
|
|
|
|
|
|
|
class RecordItemsHandler: public rocksdb::WriteBatch::Handler {
|
|
|
|
public:
|
|
|
|
enum Optype {PutRecord, MergeRecord, DeleteRecord};
|
|
|
|
|
|
|
|
class BatchItem {
|
|
|
|
public:
|
|
|
|
BatchItem(
|
|
|
|
const Optype& op,
|
2018-11-02 19:27:14 +01:00
|
|
|
uint32_t column_family_id,
|
2015-07-05 13:35:15 +02:00
|
|
|
const rocksdb::Slice& key,
|
|
|
|
const rocksdb::Slice& value):
|
|
|
|
op(op),
|
2018-11-02 19:27:14 +01:00
|
|
|
column_family_id(column_family_id),
|
2015-07-05 13:35:15 +02:00
|
|
|
key(key),
|
|
|
|
value(value)
|
|
|
|
{}
|
|
|
|
|
|
|
|
const Optype op;
|
2018-11-02 19:27:14 +01:00
|
|
|
uint32_t column_family_id;
|
2015-07-05 13:35:15 +02:00
|
|
|
const rocksdb::Slice key;
|
|
|
|
const rocksdb::Slice value;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<BatchItem> BatchItems;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/* Items is filled during iteration. */
|
|
|
|
RecordItemsHandler(BatchItems* items): items(items) {}
|
|
|
|
|
2018-11-02 19:27:14 +01:00
|
|
|
virtual rocksdb::Status PutCF(
|
|
|
|
uint32_t column_family_id, const Slice& key, const Slice& value) {
|
|
|
|
this->items->emplace_back(PutRecord, column_family_id, key, value);
|
|
|
|
return rocksdb::Status::OK();
|
2015-07-05 13:35:15 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 19:27:14 +01:00
|
|
|
virtual rocksdb::Status MergeCF(
|
|
|
|
uint32_t column_family_id, const Slice& key, const Slice& value) {
|
|
|
|
this->items->emplace_back(MergeRecord, column_family_id, key, value);
|
|
|
|
return rocksdb::Status::OK();
|
2015-07-05 13:35:15 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 19:27:14 +01:00
|
|
|
virtual rocksdb::Status DeleteCF(
|
|
|
|
uint32_t column_family_id, const Slice& key) {
|
|
|
|
this->items->emplace_back(DeleteRecord, column_family_id, key, rocksdb::Slice());
|
|
|
|
return rocksdb::Status::OK();
|
|
|
|
}
|
2015-07-05 13:35:15 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
BatchItems* items;
|
|
|
|
};
|
|
|
|
|
|
|
|
rocksdb::Status
|
|
|
|
get_batch_items(const rocksdb::WriteBatch* batch, RecordItemsHandler::BatchItems* items) {
|
|
|
|
RecordItemsHandler handler(items);
|
|
|
|
return batch->Iterate(&handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|