c_simple_example.c 3.0 KB

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