c_simple_example.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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).
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include "rocksdb/c.h"
  10. #include <unistd.h> // sysconf() - get CPU count
  11. const char DBPath[] = "/tmp/rocksdb_simple_example";
  12. const char DBBackupPath[] = "/tmp/rocksdb_simple_example_backup";
  13. int main(int argc, char **argv) {
  14. rocksdb_t *db;
  15. rocksdb_backup_engine_t *be;
  16. rocksdb_options_t *options = rocksdb_options_create();
  17. // Optimize RocksDB. This is the easiest way to
  18. // get RocksDB to perform well
  19. long cpus = sysconf(_SC_NPROCESSORS_ONLN); // get # of online cores
  20. rocksdb_options_increase_parallelism(options, (int)(cpus));
  21. rocksdb_options_optimize_level_style_compaction(options, 0);
  22. // create the DB if it's not already present
  23. rocksdb_options_set_create_if_missing(options, 1);
  24. // open DB
  25. char *err = NULL;
  26. db = rocksdb_open(options, DBPath, &err);
  27. assert(!err);
  28. // open Backup Engine that we will use for backing up our database
  29. be = rocksdb_backup_engine_open(options, DBBackupPath, &err);
  30. assert(!err);
  31. // Put key-value
  32. rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
  33. const char key[] = "key";
  34. const char *value = "value";
  35. rocksdb_put(db, writeoptions, key, strlen(key), value, strlen(value) + 1,
  36. &err);
  37. assert(!err);
  38. // Get value
  39. rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create();
  40. size_t len;
  41. char *returned_value =
  42. rocksdb_get(db, readoptions, key, strlen(key), &len, &err);
  43. assert(!err);
  44. assert(strcmp(returned_value, "value") == 0);
  45. free(returned_value);
  46. // create new backup in a directory specified by DBBackupPath
  47. rocksdb_backup_engine_create_new_backup(be, db, &err);
  48. assert(!err);
  49. rocksdb_close(db);
  50. // If something is wrong, you might want to restore data from last backup
  51. rocksdb_restore_options_t *restore_options = rocksdb_restore_options_create();
  52. rocksdb_backup_engine_restore_db_from_latest_backup(be, DBPath, DBPath,
  53. restore_options, &err);
  54. assert(!err);
  55. rocksdb_restore_options_destroy(restore_options);
  56. db = rocksdb_open(options, DBPath, &err);
  57. assert(!err);
  58. // cleanup
  59. rocksdb_writeoptions_destroy(writeoptions);
  60. rocksdb_readoptions_destroy(readoptions);
  61. rocksdb_options_destroy(options);
  62. rocksdb_backup_engine_close(be);
  63. rocksdb_close(db);
  64. return 0;
  65. }