@ -6,6 +6,8 @@
# ifndef ROCKSDB_LITE
# ifndef ROCKSDB_LITE
# include "rocksdb/utilities/object_registry.h"
# include "rocksdb/utilities/object_registry.h"
# include "rocksdb/customizable.h"
# include "test_util/testharness.h"
# include "test_util/testharness.h"
namespace ROCKSDB_NAMESPACE {
namespace ROCKSDB_NAMESPACE {
@ -205,6 +207,230 @@ TEST_F(EnvRegistryTest, TestRegistryParents) {
ASSERT_NOK ( child - > NewUniqueObject < Env > ( " cousin " , & guard ) ) ;
ASSERT_NOK ( child - > NewUniqueObject < Env > ( " cousin " , & guard ) ) ;
ASSERT_NOK ( uncle - > NewUniqueObject < Env > ( " cousin " , & guard ) ) ;
ASSERT_NOK ( uncle - > NewUniqueObject < Env > ( " cousin " , & guard ) ) ;
}
}
class MyCustomizable : public Customizable {
public :
static const char * Type ( ) { return " MyCustomizable " ; }
MyCustomizable ( const char * prefix , const std : : string & id ) : id_ ( id ) {
name_ = id_ . substr ( 0 , strlen ( prefix ) - 1 ) ;
}
const char * Name ( ) const override { return name_ . c_str ( ) ; }
std : : string GetId ( ) const override { return id_ ; }
private :
std : : string id_ ;
std : : string name_ ;
} ;
TEST_F ( EnvRegistryTest , TestManagedObjects ) {
auto registry = ObjectRegistry : : NewInstance ( ) ;
auto m_a1 = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
auto m_a2 = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_OK ( registry - > SetManagedObject < MyCustomizable > ( m_a1 ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a1 ) ;
ASSERT_NOK ( registry - > SetManagedObject < MyCustomizable > ( m_a2 ) ) ;
ASSERT_OK ( registry - > SetManagedObject < MyCustomizable > ( m_a1 ) ) ;
m_a1 . reset ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_OK ( registry - > SetManagedObject < MyCustomizable > ( m_a2 ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a2 ) ;
}
TEST_F ( EnvRegistryTest , TestTwoManagedObjects ) {
auto registry = ObjectRegistry : : NewInstance ( ) ;
auto m_a = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
auto m_b = std : : make_shared < MyCustomizable > ( " " , " B " ) ;
std : : vector < std : : shared_ptr < MyCustomizable > > objects ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , nullptr ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 0U ) ;
ASSERT_OK ( registry - > SetManagedObject ( m_a ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 1U ) ;
ASSERT_EQ ( objects . front ( ) , m_a ) ;
ASSERT_OK ( registry - > SetManagedObject ( m_b ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , m_b ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 2U ) ;
ASSERT_OK ( registry - > ListManagedObjects ( " A " , & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 1U ) ;
ASSERT_EQ ( objects . front ( ) , m_a ) ;
ASSERT_OK ( registry - > ListManagedObjects ( " B " , & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 1U ) ;
ASSERT_EQ ( objects . front ( ) , m_b ) ;
ASSERT_OK ( registry - > ListManagedObjects ( " C " , & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 0U ) ;
m_a . reset ( ) ;
objects . clear ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , m_b ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 1U ) ;
ASSERT_EQ ( objects . front ( ) , m_b ) ;
m_b . reset ( ) ;
objects . clear ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , nullptr ) ;
}
TEST_F ( EnvRegistryTest , TestAlternateNames ) {
auto registry = ObjectRegistry : : NewInstance ( ) ;
auto m_a = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
auto m_b = std : : make_shared < MyCustomizable > ( " " , " B " ) ;
std : : vector < std : : shared_ptr < MyCustomizable > > objects ;
// Test no objects exist
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " TheOne " ) , nullptr ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 0U ) ;
// Mark "TheOne" to be A
ASSERT_OK ( registry - > SetManagedObject ( " TheOne " , m_a ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " TheOne " ) , m_a ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 1U ) ;
ASSERT_EQ ( objects . front ( ) , m_a ) ;
// Try to mark "TheOne" again.
ASSERT_NOK ( registry - > SetManagedObject ( " TheOne " , m_b ) ) ;
ASSERT_OK ( registry - > SetManagedObject ( " TheOne " , m_a ) ) ;
// Add "A" as a managed object. Registered 2x
ASSERT_OK ( registry - > SetManagedObject ( m_a ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " B " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " TheOne " ) , m_a ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 2U ) ;
// Delete "A".
m_a . reset ( ) ;
objects . clear ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " TheOne " ) , nullptr ) ;
ASSERT_OK ( registry - > SetManagedObject ( " TheOne " , m_b ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " TheOne " ) , m_b ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 1U ) ;
ASSERT_EQ ( objects . front ( ) , m_b ) ;
m_b . reset ( ) ;
objects . clear ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " TheOne " ) , nullptr ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objects ) ) ;
ASSERT_EQ ( objects . size ( ) , 0U ) ;
}
TEST_F ( EnvRegistryTest , TestTwoManagedClasses ) {
class MyCustomizable2 : public MyCustomizable {
public :
static const char * Type ( ) { return " MyCustomizable2 " ; }
MyCustomizable2 ( const char * prefix , const std : : string & id )
: MyCustomizable ( prefix , id ) { }
} ;
auto registry = ObjectRegistry : : NewInstance ( ) ;
auto m_a1 = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
auto m_a2 = std : : make_shared < MyCustomizable2 > ( " " , " A " ) ;
std : : vector < std : : shared_ptr < MyCustomizable > > obj1s ;
std : : vector < std : : shared_ptr < MyCustomizable2 > > obj2s ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable2 > ( " A " ) , nullptr ) ;
ASSERT_OK ( registry - > SetManagedObject ( m_a1 ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a1 ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable2 > ( " A " ) , nullptr ) ;
ASSERT_OK ( registry - > SetManagedObject ( m_a2 ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable2 > ( " A " ) , m_a2 ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & obj1s ) ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & obj2s ) ) ;
ASSERT_EQ ( obj1s . size ( ) , 1U ) ;
ASSERT_EQ ( obj2s . size ( ) , 1U ) ;
ASSERT_EQ ( obj1s . front ( ) , m_a1 ) ;
ASSERT_EQ ( obj2s . front ( ) , m_a2 ) ;
m_a1 . reset ( ) ;
obj1s . clear ( ) ;
obj2s . clear ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable2 > ( " A " ) , m_a2 ) ;
m_a2 . reset ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable2 > ( " A " ) , nullptr ) ;
}
TEST_F ( EnvRegistryTest , TestManagedObjectsWithParent ) {
auto base = ObjectRegistry : : NewInstance ( ) ;
auto registry = ObjectRegistry : : NewInstance ( base ) ;
auto m_a = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
auto m_b = std : : make_shared < MyCustomizable > ( " " , " A " ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_OK ( base - > SetManagedObject ( m_a ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_a ) ;
ASSERT_NOK ( registry - > SetManagedObject ( m_b ) ) ;
ASSERT_OK ( registry - > SetManagedObject ( m_a ) ) ;
m_a . reset ( ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , nullptr ) ;
ASSERT_OK ( registry - > SetManagedObject ( m_b ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " A " ) , m_b ) ;
}
TEST_F ( EnvRegistryTest , TestGetOrCreateManagedObject ) {
auto registry = ObjectRegistry : : NewInstance ( ) ;
registry - > AddLibrary ( " test " ) - > Register < MyCustomizable > (
" MC(@.*)? " ,
[ ] ( const std : : string & uri , std : : unique_ptr < MyCustomizable > * guard ,
std : : string * /* errmsg */ ) {
guard - > reset ( new MyCustomizable ( " MC " , uri ) ) ;
return guard - > get ( ) ;
} ) ;
std : : shared_ptr < MyCustomizable > m_a , m_b , obj ;
std : : vector < std : : shared_ptr < MyCustomizable > > objs ;
std : : unordered_map < std : : string , std : : string > opt_map ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " MC@A " ) , nullptr ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " MC@B " ) , nullptr ) ;
ASSERT_OK ( registry - > GetOrCreateManagedObject ( " MC@A " , & m_a ) ) ;
ASSERT_OK ( registry - > GetOrCreateManagedObject ( " MC@B " , & m_b ) ) ;
ASSERT_EQ ( registry - > GetManagedObject < MyCustomizable > ( " MC@A " ) , m_a ) ;
ASSERT_OK ( registry - > GetOrCreateManagedObject ( " MC@A " , & obj ) ) ;
ASSERT_EQ ( obj , m_a ) ;
ASSERT_OK ( registry - > GetOrCreateManagedObject ( " MC@B " , & obj ) ) ;
ASSERT_EQ ( obj , m_b ) ;
ASSERT_OK ( registry - > ListManagedObjects ( & objs ) ) ;
ASSERT_EQ ( objs . size ( ) , 2U ) ;
objs . clear ( ) ;
m_a . reset ( ) ;
obj . reset ( ) ;
ASSERT_OK ( registry - > GetOrCreateManagedObject ( " MC@A " , & m_a ) ) ;
ASSERT_EQ ( 1 , m_a . use_count ( ) ) ;
ASSERT_OK ( registry - > GetOrCreateManagedObject ( " MC@B " , & obj ) ) ;
ASSERT_EQ ( 2 , obj . use_count ( ) ) ;
}
} // namespace ROCKSDB_NAMESPACE
} // namespace ROCKSDB_NAMESPACE
int main ( int argc , char * * argv ) {
int main ( int argc , char * * argv ) {