simple_example.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <cstdio>
  6. #include <string>
  7. #include "rocksdb/db.h"
  8. #include "rocksdb/options.h"
  9. #include "rocksdb/slice.h"
  10. using ROCKSDB_NAMESPACE::DB;
  11. using ROCKSDB_NAMESPACE::Options;
  12. using ROCKSDB_NAMESPACE::PinnableSlice;
  13. using ROCKSDB_NAMESPACE::ReadOptions;
  14. using ROCKSDB_NAMESPACE::Status;
  15. using ROCKSDB_NAMESPACE::WriteBatch;
  16. using ROCKSDB_NAMESPACE::WriteOptions;
  17. #if defined(OS_WIN)
  18. std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_simple_example";
  19. #else
  20. std::string kDBPath = "/tmp/rocksdb_simple_example";
  21. #endif
  22. int main() {
  23. DB* db;
  24. Options options;
  25. // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
  26. options.IncreaseParallelism();
  27. options.OptimizeLevelStyleCompaction();
  28. // create the DB if it's not already present
  29. options.create_if_missing = true;
  30. // open DB
  31. Status s = DB::Open(options, kDBPath, &db);
  32. assert(s.ok());
  33. // Put key-value
  34. s = db->Put(WriteOptions(), "key1", "value");
  35. assert(s.ok());
  36. std::string value;
  37. // get value
  38. s = db->Get(ReadOptions(), "key1", &value);
  39. assert(s.ok());
  40. assert(value == "value");
  41. // atomically apply a set of updates
  42. {
  43. WriteBatch batch;
  44. batch.Delete("key1");
  45. batch.Put("key2", value);
  46. s = db->Write(WriteOptions(), &batch);
  47. }
  48. s = db->Get(ReadOptions(), "key1", &value);
  49. assert(s.IsNotFound());
  50. db->Get(ReadOptions(), "key2", &value);
  51. assert(value == "value");
  52. {
  53. PinnableSlice pinnable_val;
  54. db->Get(ReadOptions(), db->DefaultColumnFamily(), "key2", &pinnable_val);
  55. assert(pinnable_val == "value");
  56. }
  57. {
  58. std::string string_val;
  59. // If it cannot pin the value, it copies the value to its internal buffer.
  60. // The intenral buffer could be set during construction.
  61. PinnableSlice pinnable_val(&string_val);
  62. db->Get(ReadOptions(), db->DefaultColumnFamily(), "key2", &pinnable_val);
  63. assert(pinnable_val == "value");
  64. // If the value is not pinned, the internal buffer must have the value.
  65. assert(pinnable_val.IsPinned() || string_val == "value");
  66. }
  67. PinnableSlice pinnable_val;
  68. s = db->Get(ReadOptions(), db->DefaultColumnFamily(), "key1", &pinnable_val);
  69. assert(s.IsNotFound());
  70. // Reset PinnableSlice after each use and before each reuse
  71. pinnable_val.Reset();
  72. db->Get(ReadOptions(), db->DefaultColumnFamily(), "key2", &pinnable_val);
  73. assert(pinnable_val == "value");
  74. pinnable_val.Reset();
  75. // The Slice pointed by pinnable_val is not valid after this point
  76. delete db;
  77. return 0;
  78. }