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