consolidate
This commit is contained in:
@@ -1,22 +1,167 @@
|
||||
// TODO: In order to use RocksDB's WAL replication helpers, we need to import the C++ library so we use this shim
|
||||
/**
|
||||
C++ shim implementation notes (the important bits)
|
||||
#include "rocksdb_shim.h"
|
||||
#include <rocksdb/db.h>
|
||||
#include <rocksdb/options.h>
|
||||
#include <rocksdb/slice.h>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
In this rocksdb_shim.cc we'll need to use:
|
||||
// Internal structure wrapping rocksdb::DB
|
||||
struct jormun_db {
|
||||
rocksdb::DB* db;
|
||||
};
|
||||
|
||||
rocksdb::DB::Open(...)
|
||||
// Placeholder for WAL iterator (not implemented yet)
|
||||
struct jormun_wal_iter {
|
||||
// TODO: Implement with TransactionLogIterator when needed
|
||||
void* placeholder;
|
||||
};
|
||||
|
||||
db->GetLatestSequenceNumber()
|
||||
// Open database
|
||||
jormun_db* jormun_db_open(const char* path, int create_if_missing, char** err) {
|
||||
rocksdb::Options options;
|
||||
options.create_if_missing = create_if_missing != 0;
|
||||
|
||||
rocksdb::DB* db_ptr = nullptr;
|
||||
rocksdb::Status status = rocksdb::DB::Open(options, path, &db_ptr);
|
||||
|
||||
if (!status.ok()) {
|
||||
if (err) {
|
||||
std::string error_msg = status.ToString();
|
||||
*err = strdup(error_msg.c_str());
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
jormun_db* jdb = new jormun_db;
|
||||
jdb->db = db_ptr;
|
||||
return jdb;
|
||||
}
|
||||
|
||||
db->GetUpdatesSince(seq, &iter)
|
||||
// Close database
|
||||
void jormun_db_close(jormun_db* db) {
|
||||
if (db) {
|
||||
delete db->db;
|
||||
delete db;
|
||||
}
|
||||
}
|
||||
|
||||
from each TransactionLogIterator entry:
|
||||
// Put key-value pair
|
||||
void jormun_db_put(jormun_db* db,
|
||||
const void* key, size_t keylen,
|
||||
const void* val, size_t vallen,
|
||||
char** err) {
|
||||
if (!db || !db->db) {
|
||||
if (err) *err = strdup("Database is null");
|
||||
return;
|
||||
}
|
||||
|
||||
rocksdb::WriteOptions write_options;
|
||||
rocksdb::Slice key_slice(static_cast<const char*>(key), keylen);
|
||||
rocksdb::Slice val_slice(static_cast<const char*>(val), vallen);
|
||||
|
||||
rocksdb::Status status = db->db->Put(write_options, key_slice, val_slice);
|
||||
|
||||
if (!status.ok() && err) {
|
||||
std::string error_msg = status.ToString();
|
||||
*err = strdup(error_msg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
get WriteBatch and serialize via WriteBatch::Data()
|
||||
// Get value for key
|
||||
unsigned char* jormun_db_get(jormun_db* db,
|
||||
const void* key, size_t keylen,
|
||||
size_t* vallen,
|
||||
char** err) {
|
||||
if (!db || !db->db) {
|
||||
if (err) *err = strdup("Database is null");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
rocksdb::ReadOptions read_options;
|
||||
rocksdb::Slice key_slice(static_cast<const char*>(key), keylen);
|
||||
std::string value;
|
||||
|
||||
rocksdb::Status status = db->db->Get(read_options, key_slice, &value);
|
||||
|
||||
if (status.IsNotFound()) {
|
||||
*vallen = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!status.ok()) {
|
||||
if (err) {
|
||||
std::string error_msg = status.ToString();
|
||||
*err = strdup(error_msg.c_str());
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Allocate and copy value
|
||||
*vallen = value.size();
|
||||
unsigned char* result = static_cast<unsigned char*>(malloc(value.size()));
|
||||
if (result) {
|
||||
memcpy(result, value.data(), value.size());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
apply via rocksdb::WriteBatch wb(data); db->Write(write_options, &wb);
|
||||
// Free memory allocated by the shim
|
||||
void jormun_free(void* p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
Also we must configure WAL retention so the followers don’t fall off the end. RocksDB warns the iterator can become invalid if WAL is cleared aggressively; typical controls are WAL TTL / size limit.
|
||||
// ============================================================================
|
||||
// WAL Replication Functions (Stubs for now - to be implemented)
|
||||
// ============================================================================
|
||||
|
||||
https://github.com/facebook/rocksdb/issues/1565
|
||||
*/
|
||||
// Get latest sequence number
|
||||
uint64_t jormun_latest_sequence(jormun_db* db) {
|
||||
if (!db || !db->db) return 0;
|
||||
return db->db->GetLatestSequenceNumber();
|
||||
}
|
||||
|
||||
// Create WAL iterator (stub)
|
||||
jormun_wal_iter* jormun_wal_iter_create(jormun_db* db, uint64_t seq, char** err) {
|
||||
(void)db;
|
||||
(void)seq;
|
||||
if (err) {
|
||||
*err = strdup("WAL iteration not yet implemented");
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Destroy WAL iterator (stub)
|
||||
void jormun_wal_iter_destroy(jormun_wal_iter* it) {
|
||||
if (it) {
|
||||
delete it;
|
||||
}
|
||||
}
|
||||
|
||||
// Get next batch from WAL (stub)
|
||||
int jormun_wal_iter_next(jormun_wal_iter* it,
|
||||
uint64_t* batch_start_seq,
|
||||
unsigned char** out_data,
|
||||
size_t* out_len,
|
||||
char** err) {
|
||||
(void)it;
|
||||
(void)batch_start_seq;
|
||||
(void)out_data;
|
||||
(void)out_len;
|
||||
if (err) {
|
||||
*err = strdup("WAL iteration not yet implemented");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Apply write batch (stub)
|
||||
void jormun_apply_writebatch(jormun_db* db,
|
||||
const unsigned char* data, size_t len,
|
||||
char** err) {
|
||||
(void)db;
|
||||
(void)data;
|
||||
(void)len;
|
||||
if (err) {
|
||||
*err = strdup("WAL apply not yet implemented");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user