2017-07-16 01:03:42 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2016-12-14 19:21:54 +01:00
|
|
|
|
2014-11-27 22:49:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-11-30 06:42:42 +01:00
|
|
|
#include <stdlib.h>
|
2014-11-27 22:49:19 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "rocksdb/c.h"
|
|
|
|
|
2020-09-02 03:02:25 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
2015-01-13 03:17:30 +01:00
|
|
|
#include <unistd.h> // sysconf() - get CPU count
|
2020-09-02 03:02:25 +02:00
|
|
|
#endif
|
2014-11-27 22:49:19 +01:00
|
|
|
|
2020-09-02 03:02:25 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
const char DBPath[] = "C:\\Windows\\TEMP\\rocksdb_c_simple_example";
|
|
|
|
const char DBBackupPath[] =
|
|
|
|
"C:\\Windows\\TEMP\\rocksdb_c_simple_example_backup";
|
|
|
|
#else
|
|
|
|
const char DBPath[] = "/tmp/rocksdb_c_simple_example";
|
|
|
|
const char DBBackupPath[] = "/tmp/rocksdb_c_simple_example_backup";
|
|
|
|
#endif
|
2014-11-27 22:49:19 +01:00
|
|
|
|
2014-12-18 15:48:46 +01:00
|
|
|
int main(int argc, char **argv) {
|
2015-01-13 03:17:30 +01:00
|
|
|
rocksdb_t *db;
|
2015-01-31 13:47:49 +01:00
|
|
|
rocksdb_backup_engine_t *be;
|
2015-01-13 03:17:30 +01:00
|
|
|
rocksdb_options_t *options = rocksdb_options_create();
|
|
|
|
// Optimize RocksDB. This is the easiest way to
|
2020-09-02 03:02:25 +02:00
|
|
|
// get RocksDB to perform well.
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
GetSystemInfo(&system_info);
|
|
|
|
long cpus = system_info.dwNumberOfProcessors;
|
|
|
|
#else
|
|
|
|
long cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
#endif
|
|
|
|
// Set # of online cores
|
2015-01-13 03:17:30 +01:00
|
|
|
rocksdb_options_increase_parallelism(options, (int)(cpus));
|
|
|
|
rocksdb_options_optimize_level_style_compaction(options, 0);
|
|
|
|
// create the DB if it's not already present
|
|
|
|
rocksdb_options_set_create_if_missing(options, 1);
|
|
|
|
|
|
|
|
// open DB
|
|
|
|
char *err = NULL;
|
|
|
|
db = rocksdb_open(options, DBPath, &err);
|
|
|
|
assert(!err);
|
|
|
|
|
2015-05-23 01:13:11 +02:00
|
|
|
// open Backup Engine that we will use for backing up our database
|
2015-02-09 10:16:04 +01:00
|
|
|
be = rocksdb_backup_engine_open(options, DBBackupPath, &err);
|
2015-01-31 13:47:49 +01:00
|
|
|
assert(!err);
|
|
|
|
|
2015-01-13 03:17:30 +01:00
|
|
|
// Put key-value
|
|
|
|
rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
|
|
|
|
const char key[] = "key";
|
|
|
|
const char *value = "value";
|
|
|
|
rocksdb_put(db, writeoptions, key, strlen(key), value, strlen(value) + 1,
|
|
|
|
&err);
|
|
|
|
assert(!err);
|
|
|
|
// Get value
|
|
|
|
rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create();
|
|
|
|
size_t len;
|
|
|
|
char *returned_value =
|
|
|
|
rocksdb_get(db, readoptions, key, strlen(key), &len, &err);
|
|
|
|
assert(!err);
|
|
|
|
assert(strcmp(returned_value, "value") == 0);
|
|
|
|
free(returned_value);
|
|
|
|
|
2015-02-09 10:16:04 +01:00
|
|
|
// create new backup in a directory specified by DBBackupPath
|
2015-01-31 13:47:49 +01:00
|
|
|
rocksdb_backup_engine_create_new_backup(be, db, &err);
|
|
|
|
assert(!err);
|
|
|
|
|
2015-02-09 17:34:50 +01:00
|
|
|
rocksdb_close(db);
|
|
|
|
|
|
|
|
// If something is wrong, you might want to restore data from last backup
|
|
|
|
rocksdb_restore_options_t *restore_options = rocksdb_restore_options_create();
|
2015-02-09 18:53:30 +01:00
|
|
|
rocksdb_backup_engine_restore_db_from_latest_backup(be, DBPath, DBPath,
|
|
|
|
restore_options, &err);
|
2015-02-09 17:34:50 +01:00
|
|
|
assert(!err);
|
|
|
|
rocksdb_restore_options_destroy(restore_options);
|
|
|
|
|
|
|
|
db = rocksdb_open(options, DBPath, &err);
|
|
|
|
assert(!err);
|
|
|
|
|
2015-01-13 03:17:30 +01:00
|
|
|
// cleanup
|
|
|
|
rocksdb_writeoptions_destroy(writeoptions);
|
|
|
|
rocksdb_readoptions_destroy(readoptions);
|
|
|
|
rocksdb_options_destroy(options);
|
2015-01-31 13:47:49 +01:00
|
|
|
rocksdb_backup_engine_close(be);
|
2015-01-13 03:17:30 +01:00
|
|
|
rocksdb_close(db);
|
|
|
|
|
|
|
|
return 0;
|
2014-11-27 22:49:19 +01:00
|
|
|
}
|