rocksdb/utilities/env_registry.cc
Andrew Kryczka af0c9ac01d Env registry for URI-based Env selection [pluggable Env part 1]
Summary:
This enables configurable Envs without recompiling. For example, my
next diff will make env_test test an Env created by NewEnvFromUri(). Then,
users can determine which Env is tested simply by providing the URI for
NewEnvFromUri() (e.g., through a CLI argument or environment variable).

The registration process allows us to register any Env that is linked with the
RocksDB library, so we can register our internal Envs as well.

The registration code is inspired by our internal InitRegistry.

Test Plan: new unit test

Reviewers: IslamAbdelRahman, lightmark, ldemailly, sdong

Reviewed By: sdong

Subscribers: leveldb, dhruba, andrewkr

Differential Revision: https://reviews.facebook.net/D58449
2016-06-03 08:15:16 -07:00

48 lines
1.2 KiB
C++

// Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#ifndef ROCKSDB_LITE
#include "rocksdb/utilities/env_registry.h"
#include <map>
#include <string>
namespace rocksdb {
struct EnvRegistryEntry {
std::string prefix;
EnvFactoryFunc env_factory;
};
struct EnvRegistry {
static EnvRegistry* Get() {
static EnvRegistry instance;
return &instance;
}
std::vector<EnvRegistryEntry> entries;
private:
EnvRegistry() = default;
};
Env* NewEnvFromUri(const std::string& uri, std::unique_ptr<Env>* env_guard) {
env_guard->reset();
for (const auto& entry : EnvRegistry::Get()->entries) {
if (uri.compare(0, entry.prefix.size(), entry.prefix) == 0) {
return entry.env_factory(uri, env_guard);
}
}
return nullptr;
}
EnvRegistrar::EnvRegistrar(std::string uri_prefix, EnvFactoryFunc env_factory) {
EnvRegistry::Get()->entries.emplace_back(
EnvRegistryEntry{std::move(uri_prefix), std::move(env_factory)});
}
} // namespace rocksdb
#endif // ROCKSDB_LITE