TAO_Persistent_Bindings_Map Class Reference

Provides hash-table-based persistent storage for name to object bindings in a Naming Context. More...

#include <Persistent_Naming_Context.h>

Inheritance diagram for TAO_Persistent_Bindings_Map:

Inheritance graph
[legend]
Collaboration diagram for TAO_Persistent_Bindings_Map:

Collaboration graph
[legend]
List of all members.

Public Types

typedef ACE_Hash_Map_With_Allocator<
TAO_Persistent_ExtId, TAO_Persistent_IntId
HASH_MAP
 Underlying data structure - typedef for ease of use.


Public Member Functions

 TAO_Persistent_Bindings_Map (CORBA::ORB_ptr orb)
 Constructor.

int open (size_t hash_map_size, ACE_Allocator *alloc)
void set (HASH_MAP *map, ACE_Allocator *alloc)
virtual ~TAO_Persistent_Bindings_Map (void)
void destroy (void)
HASH_MAPmap (void)
 Get a pointer to the underlying hash map.

size_t total_size (void)
 Return the size of the underlying hash table.

virtual size_t current_size (void)
 Return the size of the underlying hash table.

virtual int bind (const char *id, const char *kind, CORBA::Object_ptr obj, CosNaming::BindingType type)
virtual int rebind (const char *id, const char *kind, CORBA::Object_ptr obj, CosNaming::BindingType type)
virtual int unbind (const char *id, const char *kind)
virtual int find (const char *id, const char *kind, CORBA::Object_ptr &obj, CosNaming::BindingType &type)

Protected Member Functions

int open_helper (size_t hash_table_size, void *buffer)
int shared_bind (const char *id, const char *kind, CORBA::Object_ptr obj, CosNaming::BindingType type, int rebind)
 Helper: factors common code from and .


Protected Attributes

ACE_Allocatorallocator_
 Pointer to the allocator we use to make bindings persistent.

HASH_MAPmap_
 Pointer to the underlying hash map.

CORBA::ORB_var orb_
 Pointer to the orb. We need it to do string/object conversions.


Detailed Description

Provides hash-table-based persistent storage for name to object bindings in a Naming Context.

Wrapper on top of ACE_Hash_Map_With_Allocator (which is a wrapper around ACE_Hash_Map_Manager). Uses ACE_Allocator (allocating from persistent storage) to make bindings persistent and supports TAO_Bindings_Map interface. Used by TAO_Persistent_Naming_Context.

Definition at line 35 of file Persistent_Naming_Context.h.


Member Typedef Documentation

typedef ACE_Hash_Map_With_Allocator<TAO_Persistent_ExtId, TAO_Persistent_IntId> TAO_Persistent_Bindings_Map::HASH_MAP
 

Underlying data structure - typedef for ease of use.

Definition at line 41 of file Persistent_Naming_Context.h.

Referenced by open_helper().


Constructor & Destructor Documentation

TAO_Persistent_Bindings_Map::TAO_Persistent_Bindings_Map CORBA::ORB_ptr  orb  ) 
 

Constructor.

Definition at line 83 of file Persistent_Naming_Context.cpp.

00084   : allocator_ (0),
00085     map_ (0),
00086     orb_ (CORBA::ORB::_duplicate (orb))
00087 {
00088 }

TAO_Persistent_Bindings_Map::~TAO_Persistent_Bindings_Map void   )  [virtual]
 

Destructor. Does not deallocate the hash map: if an instance of this class goes out of scope, its hash_map remains in persistent storage.

Definition at line 90 of file Persistent_Naming_Context.cpp.

00091 {
00092 }


Member Function Documentation

int TAO_Persistent_Bindings_Map::bind const char *  id,
const char *  kind,
CORBA::Object_ptr  obj,
CosNaming::BindingType  type
[virtual]
 

Add a binding with the specified parameters to the table. Return 0 on success and -1 on failure, 1 if there already is a binding with and .

Implements TAO_Bindings_Map.

Definition at line 44 of file Persistent_Naming_Context.cpp.

References shared_bind().

00048 {
00049   return this->shared_bind (id, kind, obj, type, 0);
00050 }

size_t TAO_Persistent_Bindings_Map::current_size void   )  [virtual]
 

Return the size of the underlying hash table.

Implements TAO_Bindings_Map.

Definition at line 114 of file Persistent_Naming_Context.cpp.

00115 {
00116   return map_->current_size ();
00117 }

void TAO_Persistent_Bindings_Map::destroy void   ) 
 

This method removes the hash map from persistent storage/frees up the memory. The hash map better be empty, since we are not cleaning up the insides. (We could add to clean entries, but not the data inside the entries.

Definition at line 95 of file Persistent_Naming_Context.cpp.

References ACE_Allocator::free().

Referenced by TAO_Persistent_Naming_Context::~TAO_Persistent_Naming_Context().

00096 {
00097   this->map_->ACE_Hash_Map_With_Allocator<TAO_Persistent_ExtId, TAO_Persistent_IntId>::~ACE_Hash_Map_With_Allocator ();
00098   this->allocator_->free (map_);
00099 }

int TAO_Persistent_Bindings_Map::find const char *  id,
const char *  kind,
CORBA::Object_ptr obj,
CosNaming::BindingType type
[virtual]
 

Find the binding containing and in the table, and pass binding's type and object back to the caller by reference. Return 0 on success and -1 on failure. Note: a 'duplicated' object reference is assigned to , so the caller is responsible for its deallocation.

Implements TAO_Bindings_Map.

Definition at line 62 of file Persistent_Naming_Context.cpp.

References TAO_Persistent_IntId::ref_, and TAO_Persistent_IntId::type_.

00066 {
00067   TAO_Persistent_ExtId name (id, kind);
00068   TAO_Persistent_IntId entry;
00069 
00070   if (this->map_->find (name,
00071                         entry,
00072                         this->allocator_) != 0)
00073     return -1;
00074   else
00075     {
00076       obj = orb_->string_to_object (entry.ref_);
00077       type = entry.type_;
00078 
00079       return 0;
00080     }
00081 }

TAO_Persistent_Bindings_Map::HASH_MAP * TAO_Persistent_Bindings_Map::map void   ) 
 

Get a pointer to the underlying hash map.

Definition at line 102 of file Persistent_Naming_Context.cpp.

Referenced by TAO_Persistent_Naming_Context::list(), and TAO_Persistent_Naming_Context::make_new_context().

00103 {
00104   return this->map_;
00105 }

int TAO_Persistent_Bindings_Map::open size_t  hash_map_size,
ACE_Allocator alloc
 

Allocate hash map of size from persistent storage using the .

Definition at line 120 of file Persistent_Naming_Context.cpp.

References ACE_Allocator::free(), ACE_Allocator::malloc(), and open_helper().

Referenced by TAO_Persistent_Naming_Context::init().

00122 {
00123   allocator_ = alloc;
00124 
00125   // Use allocator to allocate space for the hash map.
00126   void *hash_map = 0;
00127   size_t map_size = sizeof (HASH_MAP);
00128   hash_map = this->allocator_->malloc (map_size);
00129 
00130   // If allocation failed ...
00131   if (hash_map == 0)
00132     return -1;
00133 
00134   // Initialize allocated hash map through placement new.
00135   if (open_helper (hash_table_size, hash_map) == -1)
00136     this->allocator_->free (hash_map);
00137 
00138   return 0;
00139 }

int TAO_Persistent_Bindings_Map::open_helper size_t  hash_table_size,
void *  buffer
[protected]
 

Helper to the method. By isolating placement new into a separate method, we can deal with memory allocation failures more efficiently. If there is a problem in HASH_MAP constructor, we can clean up preallocated space.

Definition at line 142 of file Persistent_Naming_Context.cpp.

References HASH_MAP.

Referenced by open().

00144 {
00145   this->map_ = new (buffer) HASH_MAP (hash_table_size, this->allocator_);
00146   return 0;
00147 }

int TAO_Persistent_Bindings_Map::rebind const char *  id,
const char *  kind,
CORBA::Object_ptr  obj,
CosNaming::BindingType  type
[virtual]
 

Overwrite a binding containing and (or create a new one if one doesn't exist) with the specified parameters. Return 0 or 1 on success. Return -1 or -2 on failure. (-2 is returned if the new and old bindings differ in type).

Implements TAO_Bindings_Map.

Definition at line 53 of file Persistent_Naming_Context.cpp.

References shared_bind().

00057 {
00058   return this->shared_bind (id, kind, obj, type, 1);
00059 }

void TAO_Persistent_Bindings_Map::set HASH_MAP map,
ACE_Allocator alloc
 

The hash map has already been preallocated for us. We just need to set our data members take ownership of it.

Definition at line 150 of file Persistent_Naming_Context.cpp.

Referenced by TAO_Persistent_Naming_Context::TAO_Persistent_Naming_Context().

00152 {
00153   allocator_ = alloc;
00154   map_ = map;
00155 }

int TAO_Persistent_Bindings_Map::shared_bind const char *  id,
const char *  kind,
CORBA::Object_ptr  obj,
CosNaming::BindingType  type,
int  rebind
[protected]
 

Helper: factors common code from and .

Definition at line 158 of file Persistent_Naming_Context.cpp.

References ACE_Allocator::free(), ACE_Allocator::malloc(), TAO_Persistent_IntId::ref_, ACE_OS::strcpy(), ACE_OS::strlen(), ACE_Allocator::sync(), and TAO_Persistent_IntId::type_.

Referenced by bind(), and rebind().

00163 {
00164   // Obtain a stringified ior of <obj> (i.e., the representation we can store).
00165   CORBA::String_var ref = orb_->object_to_string (obj);
00166 
00167   // Calculate and allocate the memory we need to store this name to
00168   // object binding.
00169   size_t id_len = ACE_OS::strlen (id) + 1;
00170   size_t kind_len = ACE_OS::strlen (kind) + 1;
00171   size_t ref_len = ACE_OS::strlen (ref.in ()) + 1;
00172   size_t total_len = id_len + kind_len + ref_len;
00173   char *ptr = (char *) this->allocator_->malloc (total_len);
00174 
00175   // Allocation failed - bail out.
00176   if (ptr == 0)
00177     return -1;
00178 
00179   // Allocation succeded - place the data into the allocated memory
00180   // and procceed.
00181   else
00182     {
00183       // Note that the <ref> *must* come first to make sure we can
00184       // retrieve this pointer later on in unbind().
00185       char * ref_ptr = ptr;
00186       char * id_ptr =  ptr + ref_len;
00187       char * kind_ptr = ptr + ref_len + id_len;
00188       ACE_OS::strcpy (ref_ptr, ref.in ());
00189       ACE_OS::strcpy (id_ptr, id);
00190       ACE_OS::strcpy (kind_ptr, kind);
00191 
00192       TAO_Persistent_ExtId new_name (id_ptr, kind_ptr);
00193       TAO_Persistent_IntId new_entry (ref_ptr, type);
00194       int result = -1;
00195 
00196       if (rebind == 0)
00197         {
00198           // Do a normal bind.  This will fail if there's already an
00199           // <new_internal> with the same name.
00200           result = this->map_->bind (new_name, new_entry, this->allocator_);
00201 
00202           if (result == 1)
00203             {
00204               // Entry already existed so bind failed. Free our
00205               // dynamically allocated  memory.
00206               this->allocator_->free ((void *) ptr);
00207               return result;
00208             }
00209         }
00210       else
00211         // Rebind.
00212         {
00213           TAO_Persistent_ExtId old_name;
00214           TAO_Persistent_IntId old_entry;
00215 
00216           // Check that the types of old and new entries match.
00217           if (this->map_->find (new_name,
00218                                 old_entry,
00219                                 this->allocator_) == 0
00220               && type != old_entry.type_)
00221             result = -2;
00222 
00223           // If types match, perform rebind.
00224           else
00225             result = this->map_->rebind (new_name, new_entry,
00226                                          old_name, old_entry,
00227                                          this->allocator_);
00228           if (result == 1)
00229             {
00230               // Free up the old binding's memory, if it was replaced.
00231               // Note, this assumes that the "ref" pointer comes
00232               // first, and that the id, kind, and ref are contiguously
00233               // allocated (see beginning of this method for details).
00234               this->allocator_->free ((void *) old_entry.ref_);
00235             }
00236         }
00237 
00238       // Check for failures, and clean up dynamically allocated memory
00239       // if necessary.
00240       if (result < 0)
00241         this->allocator_->free ((void *) ptr);
00242 
00243       else
00244         // If bind() or rebind() succeeded, they will automatically sync
00245         // up the map manager entry.  However, we must sync up our
00246         // name/value memory.
00247         this->allocator_->sync (ptr, total_len);
00248 
00249       return result;
00250     }
00251 }

size_t TAO_Persistent_Bindings_Map::total_size void   ) 
 

Return the size of the underlying hash table.

Definition at line 108 of file Persistent_Naming_Context.cpp.

Referenced by TAO_Persistent_Naming_Context::new_context().

00109 {
00110   return this->map_->total_size ();
00111 }

TAO_BEGIN_VERSIONED_NAMESPACE_DECL int TAO_Persistent_Bindings_Map::unbind const char *  id,
const char *  kind
[virtual]
 

Remove a binding containing and from the table. Return 0 on success and -1 on failure.

Implements TAO_Bindings_Map.

Definition at line 25 of file Persistent_Naming_Context.cpp.

References ACE_Allocator::free(), and TAO_Persistent_IntId::ref_.

00027 {
00028   TAO_Persistent_ExtId name (id, kind);
00029   TAO_Persistent_IntId entry;
00030   if (this->map_->unbind (name, entry, this->allocator_) != 0)
00031     return -1;
00032   else
00033     {
00034       // Free up the memory we allocated in shared_bind().  Note that
00035       // this assumes that the "ref" pointer comes first and that
00036       // the ref, id and kind are contiguously allocated (see
00037       // shared_bind() for details).
00038       this->allocator_->free ((void *) (entry.ref_));
00039       return 0;
00040     }
00041 }


Member Data Documentation

ACE_Allocator* TAO_Persistent_Bindings_Map::allocator_ [protected]
 

Pointer to the allocator we use to make bindings persistent.

Definition at line 142 of file Persistent_Naming_Context.h.

HASH_MAP* TAO_Persistent_Bindings_Map::map_ [protected]
 

Pointer to the underlying hash map.

Definition at line 145 of file Persistent_Naming_Context.h.

CORBA::ORB_var TAO_Persistent_Bindings_Map::orb_ [protected]
 

Pointer to the orb. We need it to do string/object conversions.

Definition at line 148 of file Persistent_Naming_Context.h.


The documentation for this class was generated from the following files:
Generated on Sun Jan 27 16:16:18 2008 for TAO_CosNaming by doxygen 1.3.6