1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
11 #include "rocksdb/c.h"
16 #include <unistd.h> // sysconf() - get CPU count
20 const char DBPath
[] = "C:\\Windows\\TEMP\\rocksdb_c_simple_example";
21 const char DBBackupPath
[] =
22 "C:\\Windows\\TEMP\\rocksdb_c_simple_example_backup";
24 const char DBPath
[] = "/tmp/rocksdb_c_simple_example";
25 const char DBBackupPath
[] = "/tmp/rocksdb_c_simple_example_backup";
28 int main(int argc
, char **argv
) {
30 rocksdb_backup_engine_t
*be
;
31 rocksdb_options_t
*options
= rocksdb_options_create();
32 // Optimize RocksDB. This is the easiest way to
33 // get RocksDB to perform well.
35 SYSTEM_INFO system_info
;
36 GetSystemInfo(&system_info
);
37 long cpus
= system_info
.dwNumberOfProcessors
;
39 long cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
41 // Set # of online cores
42 rocksdb_options_increase_parallelism(options
, (int)(cpus
));
43 rocksdb_options_optimize_level_style_compaction(options
, 0);
44 // create the DB if it's not already present
45 rocksdb_options_set_create_if_missing(options
, 1);
49 db
= rocksdb_open(options
, DBPath
, &err
);
52 // open Backup Engine that we will use for backing up our database
53 be
= rocksdb_backup_engine_open(options
, DBBackupPath
, &err
);
57 rocksdb_writeoptions_t
*writeoptions
= rocksdb_writeoptions_create();
58 const char key
[] = "key";
59 const char *value
= "value";
60 rocksdb_put(db
, writeoptions
, key
, strlen(key
), value
, strlen(value
) + 1,
64 rocksdb_readoptions_t
*readoptions
= rocksdb_readoptions_create();
66 char *returned_value
=
67 rocksdb_get(db
, readoptions
, key
, strlen(key
), &len
, &err
);
69 assert(strcmp(returned_value
, "value") == 0);
72 // create new backup in a directory specified by DBBackupPath
73 rocksdb_backup_engine_create_new_backup(be
, db
, &err
);
78 // If something is wrong, you might want to restore data from last backup
79 rocksdb_restore_options_t
*restore_options
= rocksdb_restore_options_create();
80 rocksdb_backup_engine_restore_db_from_latest_backup(be
, DBPath
, DBPath
,
81 restore_options
, &err
);
83 rocksdb_restore_options_destroy(restore_options
);
85 db
= rocksdb_open(options
, DBPath
, &err
);
89 rocksdb_writeoptions_destroy(writeoptions
);
90 rocksdb_readoptions_destroy(readoptions
);
91 rocksdb_options_destroy(options
);
92 rocksdb_backup_engine_close(be
);