]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | #include <stdio.h> | |
7 | #include <string.h> | |
8 | #include <stdlib.h> | |
9 | #include <assert.h> | |
10 | ||
11 | #include "rocksdb/c.h" | |
12 | ||
20effc67 TL |
13 | #if defined(OS_WIN) |
14 | #include <Windows.h> | |
15 | #else | |
7c673cae | 16 | #include <unistd.h> // sysconf() - get CPU count |
20effc67 | 17 | #endif |
7c673cae | 18 | |
20effc67 TL |
19 | #if defined(OS_WIN) |
20 | const char DBPath[] = "C:\\Windows\\TEMP\\rocksdb_c_simple_example"; | |
21 | const char DBBackupPath[] = | |
22 | "C:\\Windows\\TEMP\\rocksdb_c_simple_example_backup"; | |
23 | #else | |
24 | const char DBPath[] = "/tmp/rocksdb_c_simple_example"; | |
25 | const char DBBackupPath[] = "/tmp/rocksdb_c_simple_example_backup"; | |
26 | #endif | |
7c673cae FG |
27 | |
28 | int main(int argc, char **argv) { | |
29 | rocksdb_t *db; | |
30 | rocksdb_backup_engine_t *be; | |
31 | rocksdb_options_t *options = rocksdb_options_create(); | |
32 | // Optimize RocksDB. This is the easiest way to | |
20effc67 TL |
33 | // get RocksDB to perform well. |
34 | #if defined(OS_WIN) | |
35 | SYSTEM_INFO system_info; | |
36 | GetSystemInfo(&system_info); | |
37 | long cpus = system_info.dwNumberOfProcessors; | |
38 | #else | |
39 | long cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
40 | #endif | |
41 | // Set # of online cores | |
7c673cae FG |
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); | |
46 | ||
47 | // open DB | |
48 | char *err = NULL; | |
49 | db = rocksdb_open(options, DBPath, &err); | |
50 | assert(!err); | |
51 | ||
52 | // open Backup Engine that we will use for backing up our database | |
53 | be = rocksdb_backup_engine_open(options, DBBackupPath, &err); | |
54 | assert(!err); | |
55 | ||
56 | // Put key-value | |
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, | |
61 | &err); | |
62 | assert(!err); | |
63 | // Get value | |
64 | rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create(); | |
65 | size_t len; | |
66 | char *returned_value = | |
67 | rocksdb_get(db, readoptions, key, strlen(key), &len, &err); | |
68 | assert(!err); | |
69 | assert(strcmp(returned_value, "value") == 0); | |
70 | free(returned_value); | |
71 | ||
72 | // create new backup in a directory specified by DBBackupPath | |
73 | rocksdb_backup_engine_create_new_backup(be, db, &err); | |
74 | assert(!err); | |
75 | ||
76 | rocksdb_close(db); | |
77 | ||
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); | |
82 | assert(!err); | |
83 | rocksdb_restore_options_destroy(restore_options); | |
84 | ||
85 | db = rocksdb_open(options, DBPath, &err); | |
86 | assert(!err); | |
87 | ||
88 | // cleanup | |
89 | rocksdb_writeoptions_destroy(writeoptions); | |
90 | rocksdb_readoptions_destroy(readoptions); | |
91 | rocksdb_options_destroy(options); | |
92 | rocksdb_backup_engine_close(be); | |
93 | rocksdb_close(db); | |
94 | ||
95 | return 0; | |
96 | } |