Make examples work on Windows (#7304)

Summary:
Quick fixes to examples to make it easier to get familiar with RocksDB for Windows users:

- Set proper temporary directory path on Windows for all examples (with C++17 we should start using std::filesystem)
- Fixed typo and got rid of warnings treated as errors in multi_processes_example.cc
- Get number of available cores on Windows in c_simple_example.c
- Add command to remove DB directory for Windows in compaction_filter_example.cc (print error, but carry on with example upon error, because error code is returned if there is no such directory on Windows)

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7304

Reviewed By: jay-zhuang

Differential Revision: D23450900

Pulled By: pdillinger

fbshipit-source-id: 4256134deb6ae6bb267ed1bd69f814842b95f60f
main
Tomasz Posłuszny 4 years ago committed by Facebook GitHub Bot
parent 55bf42a80c
commit 8d44d792c3
  1. 25
      examples/c_simple_example.c
  2. 4
      examples/column_families_example.cc
  3. 6
      examples/compact_files_example.cc
  4. 16
      examples/compaction_filter_example.cc
  5. 8
      examples/multi_processes_example.cc
  6. 4
      examples/optimistic_transaction_example.cc
  7. 4
      examples/options_file_example.cc
  8. 4
      examples/simple_example.cc
  9. 4
      examples/transaction_example.cc

@ -10,18 +10,35 @@
#include "rocksdb/c.h" #include "rocksdb/c.h"
#if defined(OS_WIN)
#include <Windows.h>
#else
#include <unistd.h> // sysconf() - get CPU count #include <unistd.h> // sysconf() - get CPU count
#endif
const char DBPath[] = "/tmp/rocksdb_simple_example"; #if defined(OS_WIN)
const char DBBackupPath[] = "/tmp/rocksdb_simple_example_backup"; const char DBPath[] = "C:\\Windows\\TEMP\\rocksdb_c_simple_example";
const char DBBackupPath[] =
"C:\\Windows\\TEMP\\rocksdb_c_simple_example_backup";
#else
const char DBPath[] = "/tmp/rocksdb_c_simple_example";
const char DBBackupPath[] = "/tmp/rocksdb_c_simple_example_backup";
#endif
int main(int argc, char **argv) { int main(int argc, char **argv) {
rocksdb_t *db; rocksdb_t *db;
rocksdb_backup_engine_t *be; rocksdb_backup_engine_t *be;
rocksdb_options_t *options = rocksdb_options_create(); rocksdb_options_t *options = rocksdb_options_create();
// Optimize RocksDB. This is the easiest way to // Optimize RocksDB. This is the easiest way to
// get RocksDB to perform well // get RocksDB to perform well.
long cpus = sysconf(_SC_NPROCESSORS_ONLN); // get # of online cores #if defined(OS_WIN)
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
long cpus = system_info.dwNumberOfProcessors;
#else
long cpus = sysconf(_SC_NPROCESSORS_ONLN);
#endif
// Set # of online cores
rocksdb_options_increase_parallelism(options, (int)(cpus)); rocksdb_options_increase_parallelism(options, (int)(cpus));
rocksdb_options_optimize_level_style_compaction(options, 0); rocksdb_options_optimize_level_style_compaction(options, 0);
// create the DB if it's not already present // create the DB if it's not already present

@ -12,7 +12,11 @@
using namespace ROCKSDB_NAMESPACE; using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_column_families_example";
#else
std::string kDBPath = "/tmp/rocksdb_column_families_example"; std::string kDBPath = "/tmp/rocksdb_column_families_example";
#endif
int main() { int main() {
// open DB // open DB

@ -13,7 +13,13 @@
#include "rocksdb/options.h" #include "rocksdb/options.h"
using namespace ROCKSDB_NAMESPACE; using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_compact_files_example";
#else
std::string kDBPath = "/tmp/rocksdb_compact_files_example"; std::string kDBPath = "/tmp/rocksdb_compact_files_example";
#endif
struct CompactionTask; struct CompactionTask;
// This is an example interface of external-compaction algorithm. // This is an example interface of external-compaction algorithm.

@ -54,22 +54,30 @@ class MyFilter : public ROCKSDB_NAMESPACE::CompactionFilter {
mutable int merge_count_ = 0; mutable int merge_count_ = 0;
}; };
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksmergetest";
std::string kRemoveDirCommand = "rmdir /Q /S ";
#else
std::string kDBPath = "/tmp/rocksmergetest";
std::string kRemoveDirCommand = "rm -rf ";
#endif
int main() { int main() {
ROCKSDB_NAMESPACE::DB* raw_db; ROCKSDB_NAMESPACE::DB* raw_db;
ROCKSDB_NAMESPACE::Status status; ROCKSDB_NAMESPACE::Status status;
MyFilter filter; MyFilter filter;
int ret = system("rm -rf /tmp/rocksmergetest"); std::string rm_cmd = kRemoveDirCommand + kDBPath;
int ret = system(rm_cmd.c_str());
if (ret != 0) { if (ret != 0) {
fprintf(stderr, "Error deleting /tmp/rocksmergetest, code: %d\n", ret); fprintf(stderr, "Error deleting %s, code: %d\n", kDBPath.c_str(), ret);
return ret;
} }
ROCKSDB_NAMESPACE::Options options; ROCKSDB_NAMESPACE::Options options;
options.create_if_missing = true; options.create_if_missing = true;
options.merge_operator.reset(new MyMerge); options.merge_operator.reset(new MyMerge);
options.compaction_filter = &filter; options.compaction_filter = &filter;
status = ROCKSDB_NAMESPACE::DB::Open(options, "/tmp/rocksmergetest", &raw_db); status = ROCKSDB_NAMESPACE::DB::Open(options, kDBPath, &raw_db);
assert(status.ok()); assert(status.ok());
std::unique_ptr<ROCKSDB_NAMESPACE::DB> db(raw_db); std::unique_ptr<ROCKSDB_NAMESPACE::DB> db(raw_db);

@ -23,6 +23,8 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
// TODO: port this example to other systems. It should be straightforward for
// POSIX-compliant systems.
#if defined(OS_LINUX) #if defined(OS_LINUX)
#include <dirent.h> #include <dirent.h>
#include <signal.h> #include <signal.h>
@ -30,7 +32,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#endif // !OS_LINUX
#include "rocksdb/db.h" #include "rocksdb/db.h"
#include "rocksdb/options.h" #include "rocksdb/options.h"
@ -136,9 +137,6 @@ static Slice GenerateRandomValue(const size_t max_length, char scratch[]) {
static bool ShouldCloseDB() { return true; } static bool ShouldCloseDB() { return true; }
// TODO: port this example to other systems. It should be straightforward for
// POSIX-compliant systems.
#if defined(OS_LINUX)
void CreateDB() { void CreateDB() {
long my_pid = static_cast<long>(getpid()); long my_pid = static_cast<long>(getpid());
Options options; Options options;
@ -389,7 +387,7 @@ int main(int argc, char** argv) {
} }
#else // OS_LINUX #else // OS_LINUX
int main() { int main() {
fpritnf(stderr, "Not implemented.\n"); fprintf(stderr, "Not implemented.\n");
return 0; return 0;
} }
#endif // !OS_LINUX #endif // !OS_LINUX

@ -13,7 +13,11 @@
using namespace ROCKSDB_NAMESPACE; using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_transaction_example";
#else
std::string kDBPath = "/tmp/rocksdb_transaction_example"; std::string kDBPath = "/tmp/rocksdb_transaction_example";
#endif
int main() { int main() {
// open DB // open DB

@ -20,7 +20,11 @@
using namespace ROCKSDB_NAMESPACE; using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_options_file_example";
#else
std::string kDBPath = "/tmp/rocksdb_options_file_example"; std::string kDBPath = "/tmp/rocksdb_options_file_example";
#endif
namespace { namespace {
// A dummy compaction filter // A dummy compaction filter

@ -12,7 +12,11 @@
using namespace ROCKSDB_NAMESPACE; using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_simple_example";
#else
std::string kDBPath = "/tmp/rocksdb_simple_example"; std::string kDBPath = "/tmp/rocksdb_simple_example";
#endif
int main() { int main() {
DB* db; DB* db;

@ -13,7 +13,11 @@
using namespace ROCKSDB_NAMESPACE; using namespace ROCKSDB_NAMESPACE;
#if defined(OS_WIN)
std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_transaction_example";
#else
std::string kDBPath = "/tmp/rocksdb_transaction_example"; std::string kDBPath = "/tmp/rocksdb_transaction_example";
#endif
int main() { int main() {
// open DB // open DB

Loading…
Cancel
Save