1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
11 #include "rocksdb/c.h"
13 #include <unistd.h> // sysconf() - get CPU count
15 const char DBPath
[] = "/tmp/rocksdb_simple_example";
16 const char DBBackupPath
[] = "/tmp/rocksdb_simple_example_backup";
18 int main(int argc
, char **argv
) {
20 rocksdb_backup_engine_t
*be
;
21 rocksdb_options_t
*options
= rocksdb_options_create();
22 // Optimize RocksDB. This is the easiest way to
23 // get RocksDB to perform well
24 long cpus
= sysconf(_SC_NPROCESSORS_ONLN
); // get # of online cores
25 rocksdb_options_increase_parallelism(options
, (int)(cpus
));
26 rocksdb_options_optimize_level_style_compaction(options
, 0);
27 // create the DB if it's not already present
28 rocksdb_options_set_create_if_missing(options
, 1);
32 db
= rocksdb_open(options
, DBPath
, &err
);
35 // open Backup Engine that we will use for backing up our database
36 be
= rocksdb_backup_engine_open(options
, DBBackupPath
, &err
);
40 rocksdb_writeoptions_t
*writeoptions
= rocksdb_writeoptions_create();
41 const char key
[] = "key";
42 const char *value
= "value";
43 rocksdb_put(db
, writeoptions
, key
, strlen(key
), value
, strlen(value
) + 1,
47 rocksdb_readoptions_t
*readoptions
= rocksdb_readoptions_create();
49 char *returned_value
=
50 rocksdb_get(db
, readoptions
, key
, strlen(key
), &len
, &err
);
52 assert(strcmp(returned_value
, "value") == 0);
55 // create new backup in a directory specified by DBBackupPath
56 rocksdb_backup_engine_create_new_backup(be
, db
, &err
);
61 // If something is wrong, you might want to restore data from last backup
62 rocksdb_restore_options_t
*restore_options
= rocksdb_restore_options_create();
63 rocksdb_backup_engine_restore_db_from_latest_backup(be
, DBPath
, DBPath
,
64 restore_options
, &err
);
66 rocksdb_restore_options_destroy(restore_options
);
68 db
= rocksdb_open(options
, DBPath
, &err
);
72 rocksdb_writeoptions_destroy(writeoptions
);
73 rocksdb_readoptions_destroy(readoptions
);
74 rocksdb_options_destroy(options
);
75 rocksdb_backup_engine_close(be
);