forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcloud_dump.cc
More file actions
106 lines (88 loc) · 3.58 KB
/
Copy pathcloud_dump.cc
File metadata and controls
106 lines (88 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2017-present, Rockset, Inc. All rights reserved.
#include <cstdio>
#include <iostream>
#include <string>
#include "rocksdb/cloud/db_cloud.h"
#include "rocksdb/options.h"
using namespace ROCKSDB_NAMESPACE;
// This is the local directory where the db is stored.
std::string kDBPath = "/tmp/rocksdb_cloud_durable";
// This is the name of the cloud storage bucket where the db
// is made durable. if you are using AWS, you have to manually
// ensure that this bucket name is unique to you and does not
// conflict with any other S3 users who might have already created
// this bucket name.
std::string kBucketSuffix = "cloud.durable.example.";
std::string kRegion = "us-west-2";
int main() {
// cloud environment config options here
CloudFileSystemOptions cloud_fs_options;
// Store a reference to a cloud file system. A new cloud file system object
// should be associated with every new cloud-db.
std::shared_ptr<FileSystem> cloud_fs;
cloud_fs_options.credentials.InitializeSimple(
getenv("AWS_ACCESS_KEY_ID"), getenv("AWS_SECRET_ACCESS_KEY"));
if (!cloud_fs_options.credentials.HasValid().ok()) {
fprintf(
stderr,
"Please set env variables "
"AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY with cloud credentials");
return -1;
}
// Append the user name to the bucket name in an attempt to make it
// globally unique. S3 bucket-names need to be globally unique.
// If you want to rerun this example, then unique user-name suffix here.
char* user = getenv("USER");
kBucketSuffix.append(user);
// "rockset." is the default bucket prefix
const std::string bucketPrefix = "rockset.";
cloud_fs_options.src_bucket.SetBucketName(kBucketSuffix, bucketPrefix);
cloud_fs_options.dest_bucket.SetBucketName(kBucketSuffix, bucketPrefix);
// create a bucket name for debugging purposes
const std::string bucketName = bucketPrefix + kBucketSuffix;
// Create a new AWS cloud env Status
CloudFileSystem* cfs;
Status s = CloudFileSystemEnv::NewAwsFileSystem(
FileSystem::Default(), kBucketSuffix, kDBPath, kRegion, kBucketSuffix,
kDBPath, kRegion, cloud_fs_options, nullptr, &cfs);
if (!s.ok()) {
fprintf(stderr, "Unable to create cloud env in bucket %s. %s\n",
bucketName.c_str(), s.ToString().c_str());
return -1;
}
cloud_fs.reset(cfs);
// Create options and use the AWS file system that we created earlier
auto cloud_env = NewCompositeEnv(cloud_fs);
Options options;
options.env = cloud_env.get();
options.create_if_missing = true;
// No persistent read-cache
std::string persistent_cache = "";
// open DB
DBCloud* db;
s = DBCloud::Open(options, kDBPath, persistent_cache, 0, &db);
if (!s.ok()) {
fprintf(stderr, "Unable to open db at path %s with bucket %s. %s\n",
kDBPath.c_str(), bucketName.c_str(), s.ToString().c_str());
return -1;
}
// print all values in the database
ROCKSDB_NAMESPACE::Iterator* it =
db->NewIterator(ROCKSDB_NAMESPACE::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
std::cout << it->key().ToString() << ": " << it->value().ToString()
<< std::endl;
}
delete it;
delete db;
// verify that the data is somewhat sane by manaully scanning for cfs
std::vector<std::string> cf_names;
s = ROCKSDB_NAMESPACE::DB::ListColumnFamilies(options, kDBPath, &cf_names);
for (const std::string& cf : cf_names) {
std::cout << " Found Column Family " << cf;
}
std::cout << " \n";
fprintf(stdout, "Successfully read db at path %s in bucket %s.\n",
kDBPath.c_str(), bucketName.c_str());
return 0;
}