Allow creating backups from C

main
Marko Kevac 10 years ago
parent db9ed5fdb4
commit 86e2a1eeea
  1. 30
      db/c.cc
  2. 9
      examples/c_simple_example.c
  3. 13
      include/rocksdb/c.h

@ -29,6 +29,7 @@
#include "rocksdb/statistics.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/table.h"
#include "rocksdb/utilities/backupable_db.h"
using rocksdb::Cache;
using rocksdb::ColumnFamilyDescriptor;
@ -69,12 +70,15 @@ using rocksdb::WritableFile;
using rocksdb::WriteBatch;
using rocksdb::WriteOptions;
using rocksdb::LiveFileMetaData;
using rocksdb::BackupEngine;
using rocksdb::BackupableDBOptions;
using std::shared_ptr;
extern "C" {
struct rocksdb_t { DB* rep; };
struct rocksdb_backup_engine_t { BackupEngine* rep; };
struct rocksdb_iterator_t { Iterator* rep; };
struct rocksdb_writebatch_t { WriteBatch rep; };
struct rocksdb_snapshot_t { const Snapshot* rep; };
@ -527,6 +531,32 @@ rocksdb_t* rocksdb_open_for_read_only(
return result;
}
rocksdb_backup_engine_t* rocksdb_backup_engine_open(
const char* path,
char** errptr) {
BackupEngine* be;
if (SaveError(errptr, BackupEngine::Open(Env::Default(), BackupableDBOptions(path), &be))) {
return nullptr;
}
rocksdb_backup_engine_t* result = new rocksdb_backup_engine_t;
result->rep = be;
return result;
}
void rocksdb_backup_engine_create_new_backup(
rocksdb_backup_engine_t *be,
rocksdb_t *db,
char** errptr) {
SaveError(errptr, be->rep->CreateNewBackup(db->rep));
}
void rocksdb_backup_engine_close(
rocksdb_backup_engine_t *be) {
delete be->rep;
delete be;
}
void rocksdb_close(rocksdb_t* db) {
delete db->rep;
delete db;

@ -8,9 +8,11 @@
#include <unistd.h> // sysconf() - get CPU count
const char DBPath[] = "/tmp/rocksdb_simple_example";
const char DBBackupPath[] = "/tmp/rocksdb_simple_example_backup";
int main(int argc, char **argv) {
rocksdb_t *db;
rocksdb_backup_engine_t *be;
rocksdb_options_t *options = rocksdb_options_create();
// Optimize RocksDB. This is the easiest way to
// get RocksDB to perform well
@ -25,6 +27,9 @@ int main(int argc, char **argv) {
db = rocksdb_open(options, DBPath, &err);
assert(!err);
be = rocksdb_backup_engine_open(DBBackupPath, &err);
assert(!err);
// Put key-value
rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
const char key[] = "key";
@ -41,10 +46,14 @@ int main(int argc, char **argv) {
assert(strcmp(returned_value, "value") == 0);
free(returned_value);
rocksdb_backup_engine_create_new_backup(be, db, &err);
assert(!err);
// cleanup
rocksdb_writeoptions_destroy(writeoptions);
rocksdb_readoptions_destroy(readoptions);
rocksdb_options_destroy(options);
rocksdb_backup_engine_close(be);
rocksdb_close(db);
return 0;

@ -55,6 +55,7 @@ extern "C" {
/* Exported types */
typedef struct rocksdb_t rocksdb_t;
typedef struct rocksdb_backup_engine_t rocksdb_backup_engine_t;
typedef struct rocksdb_cache_t rocksdb_cache_t;
typedef struct rocksdb_compactionfilter_t rocksdb_compactionfilter_t;
typedef struct rocksdb_compactionfiltercontext_t
@ -104,6 +105,18 @@ extern rocksdb_t* rocksdb_open_for_read_only(
unsigned char error_if_log_file_exist,
char** errptr);
extern rocksdb_backup_engine_t* rocksdb_backup_engine_open(
const char* path,
char** errptr);
extern void rocksdb_backup_engine_create_new_backup(
rocksdb_backup_engine_t *be,
rocksdb_t* db,
char** errptr);
extern void rocksdb_backup_engine_close(
rocksdb_backup_engine_t *be);
extern rocksdb_t* rocksdb_open_column_families(
const rocksdb_options_t* options,
const char* name,

Loading…
Cancel
Save