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 85 of file Persistent_Naming_Context.cpp.

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

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 92 of file Persistent_Naming_Context.cpp.

00093 {
00094 }


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 116 of file Persistent_Naming_Context.cpp.

00117 {
00118   return map_->current_size ();
00119 }

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 97 of file Persistent_Naming_Context.cpp.

References ACE_Allocator::free().

Referenced by TAO_Persistent_Naming_Context::~TAO_Persistent_Naming_Context().

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

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 ACE_CHECK_RETURN, ACE_DECLARE_NEW_CORBA_ENV, ACE_ENV_ARG_PARAMETER, 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       ACE_DECLARE_NEW_CORBA_ENV;
00077       obj = orb_->string_to_object (entry.ref_ ACE_ENV_ARG_PARAMETER);
00078       ACE_CHECK_RETURN (-1);
00079       type = entry.type_;
00080 
00081       return 0;
00082     }
00083 }

TAO_Persistent_Bindings_Map::HASH_MAP * TAO_Persistent_Bindings_Map::map void   ) 
 

Get a pointer to the underlying hash map.

Definition at line 104 of file Persistent_Naming_Context.cpp.

00105 {
00106   return this->map_;
00107 }

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 122 of file Persistent_Naming_Context.cpp.

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

Referenced by TAO_Persistent_Naming_Context::init().

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

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 144 of file Persistent_Naming_Context.cpp.

References HASH_MAP.

Referenced by open().

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

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 152 of file Persistent_Naming_Context.cpp.

Referenced by TAO_Persistent_Naming_Context::TAO_Persistent_Naming_Context().

00154 {
00155   allocator_ = alloc;
00156   map_ = map;
00157 }

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 160 of file Persistent_Naming_Context.cpp.

References ACE_CHECK_RETURN, ACE_DECLARE_NEW_CORBA_ENV, ACE_ENV_ARG_PARAMETER, 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().

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

size_t TAO_Persistent_Bindings_Map::total_size void   ) 
 

Return the size of the underlying hash table.

Definition at line 110 of file Persistent_Naming_Context.cpp.

00111 {
00112   return this->map_->total_size ();
00113 }

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 Thu Nov 9 13:57:43 2006 for TAO_CosNaming by doxygen 1.3.6