17c1180603
Summary: The Env registration framework supports registering client Envs and selecting which one to instantiate according to a text field. This enabled things like adding the -env_uri argument to db_bench, so the same binary could be reused with different Envs just by changing CLI config. Now this problem has come up again in a non-Env context, as I want to instantiate a client Statistics implementation from db_bench, which is configured entirely via text parameters. Also, in the future we may wish to use it for deserializing client objects when loading OPTIONS file. This diff generalizes the Env registration logic to work with arbitrary types. - Generalized registration and instantiation code by templating them - The entire implementation is in a header file as that's Google style guide's recommendation for template definitions - Pattern match with std::regex_match rather than checking prefix, which was the previous behavior - Rename functions/files to be non-Env-specific Closes https://github.com/facebook/rocksdb/pull/1776 Differential Revision: D4421933 Pulled By: ajkr fbshipit-source-id: 34647d1
73 lines
2.0 KiB
C++
73 lines
2.0 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/object_registry.h"
|
|
#include "util/testharness.h"
|
|
|
|
namespace rocksdb {
|
|
|
|
class EnvRegistryTest : public testing::Test {
|
|
public:
|
|
static int num_a, num_b;
|
|
};
|
|
|
|
int EnvRegistryTest::num_a = 0;
|
|
int EnvRegistryTest::num_b = 0;
|
|
|
|
static Registrar<Env> test_reg_a("a://.*", [](const std::string& uri,
|
|
std::unique_ptr<Env>* env_guard) {
|
|
++EnvRegistryTest::num_a;
|
|
return Env::Default();
|
|
});
|
|
|
|
static Registrar<Env> test_reg_b("b://.*", [](const std::string& uri,
|
|
std::unique_ptr<Env>* env_guard) {
|
|
++EnvRegistryTest::num_b;
|
|
// Env::Default() is a singleton so we can't grant ownership directly to the
|
|
// caller - we must wrap it first.
|
|
env_guard->reset(new EnvWrapper(Env::Default()));
|
|
return env_guard->get();
|
|
});
|
|
|
|
TEST_F(EnvRegistryTest, Basics) {
|
|
std::unique_ptr<Env> env_guard;
|
|
auto res = NewCustomObject<Env>("a://test", &env_guard);
|
|
ASSERT_NE(res, nullptr);
|
|
ASSERT_EQ(env_guard, nullptr);
|
|
ASSERT_EQ(1, num_a);
|
|
ASSERT_EQ(0, num_b);
|
|
|
|
res = NewCustomObject<Env>("b://test", &env_guard);
|
|
ASSERT_NE(res, nullptr);
|
|
ASSERT_NE(env_guard, nullptr);
|
|
ASSERT_EQ(1, num_a);
|
|
ASSERT_EQ(1, num_b);
|
|
|
|
res = NewCustomObject<Env>("c://test", &env_guard);
|
|
ASSERT_EQ(res, nullptr);
|
|
ASSERT_EQ(env_guard, nullptr);
|
|
ASSERT_EQ(1, num_a);
|
|
ASSERT_EQ(1, num_b);
|
|
}
|
|
|
|
} // namespace rocksdb
|
|
|
|
int main(int argc, char** argv) {
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|
|
|
|
#else // ROCKSDB_LITE
|
|
#include <stdio.h>
|
|
|
|
int main(int argc, char** argv) {
|
|
fprintf(stderr, "SKIPPED as EnvRegistry is not supported in ROCKSDB_LITE\n");
|
|
return 0;
|
|
}
|
|
|
|
#endif // ROCKSDB_LITE
|