00001
00002
00003 #include "PSDL_Datastore.h"
00004 #include "PSDL_OctetSeq.h"
00005 #include "PSDL_String.h"
00006 #include "ace/SString.h"
00007 #include "ace/OS_NS_unistd.h"
00008
00009 #include "tao/debug.h"
00010 #include "tao/OctetSeqC.h"
00011
00012 #include "ace/Auto_Ptr.h"
00013
00014 ACE_RCSID (PSS, PSDL_Datastore, "PSDL_Datastore.cpp,v 1.9 2005/10/04 11:38:19 jwillemsen Exp")
00015
00016 TAO_PSDL_Datastore::TAO_PSDL_Datastore ()
00017 : allocator_ (0),
00018 obj_ref_map_ (0),
00019 index_file_ ("Data_Store"),
00020 base_address_ (ACE_DEFAULT_BASE_ADDR)
00021 {
00022 this->open (this->index_file_,
00023 this->base_address_);
00024 }
00025
00026 TAO_PSDL_Datastore::~TAO_PSDL_Datastore (void)
00027 {
00028
00029
00030 }
00031
00032 int
00033 TAO_PSDL_Datastore::unbind (const char *name)
00034 {
00035 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, -1);
00036
00037 TAO_PSDL_String psdl_string (this->allocator_);
00038
00039 psdl_string = name;
00040
00041 if (this->obj_ref_map_->unbind (psdl_string, this->allocator_) != 0)
00042 return -1;
00043
00044 return 0;
00045 }
00046
00047 int
00048 TAO_PSDL_Datastore::bind (const char *name,
00049 const CORBA::OctetSeq &octet_seq)
00050 {
00051 int result = -1;
00052
00053 TAO_PSDL_OctetSeq psdl_octet_seq (this->allocator_);
00054
00055 psdl_octet_seq = octet_seq;
00056
00057 TAO_PSDL_String psdl_string (this->allocator_);
00058
00059 psdl_string = name;
00060
00061 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, -1);
00062
00063
00064
00065 result = this->obj_ref_map_->bind (psdl_string,
00066 psdl_octet_seq,
00067 this->allocator_);
00068
00069
00070
00071
00072 return result;
00073 }
00074
00075 int
00076 TAO_PSDL_Datastore::find (const char *name,
00077 CORBA::OctetSeq &octet_seq)
00078 {
00079 TAO_PSDL_OctetSeq psdl_seq;
00080
00081 TAO_PSDL_String psdl_string (this->allocator_);
00082
00083 psdl_string = CORBA::string_dup (name);
00084
00085 int result = this->obj_ref_map_->find (psdl_string,
00086 psdl_seq,
00087 this->allocator_);
00088
00089 if (result == 0)
00090 {
00091 octet_seq.replace (psdl_seq.length_,
00092 psdl_seq.length_,
00093 psdl_seq.buffer_);
00094 }
00095 else
00096 {
00097 ACE_DEBUG ((LM_ERROR,
00098 "Couldnt find the entry in the Database. stopping",
00099 -1));
00100 }
00101
00102 return result;
00103 }
00104
00105 ACE_Allocator*
00106 TAO_PSDL_Datastore::allocator (void)
00107 {
00108 return allocator_;
00109 }
00110
00111 int
00112 TAO_PSDL_Datastore::open (const ACE_TCHAR *file_name,
00113 void *base_address)
00114 {
00115 this->base_address_ = base_address;
00116
00117 this->index_file_ = ACE_OS::strdup (file_name);
00118 if (this->index_file_ == 0)
00119 return -1;
00120
00121 return create_index ();
00122 }
00123
00124 int
00125 TAO_PSDL_Datastore::create_index (void)
00126 {
00127
00128 if (ACE_OS::strlen (this->index_file_) >= MAXNAMELEN + MAXPATHLEN)
00129 {
00130 errno = ENAMETOOLONG;
00131 return -1;
00132 }
00133
00134 #if !defined (CHORUS)
00135 ACE_MMAP_Memory_Pool::OPTIONS options (this->base_address_);
00136 #else
00137
00138 ACE_MMAP_Memory_Pool::OPTIONS options (0,
00139 0,
00140 0,
00141 ACE_CHORUS_LOCAL_NAME_SPACE_T_SIZE);
00142 #endif
00143
00144
00145
00146 ACE_NEW_RETURN (this->allocator_,
00147 ALLOCATOR (this->index_file_,
00148 this->index_file_,
00149 &options),
00150 -1);
00151
00152 #if !defined (ACE_LACKS_ACCESS)
00153
00154 if (ACE_OS::access (this->index_file_, F_OK) != 0)
00155 ACE_ERROR_RETURN ((LM_ERROR,
00156 "create_index\n"),
00157 -1);
00158 #endif
00159
00160 void *name_obj_map = 0;
00161
00162
00163
00164 if (this->allocator_->find (TAO_PERSISTENT_NAME_OBJ_MAP, name_obj_map) == 0)
00165 {
00166 this->obj_ref_map_ = static_cast<NAME_OBJ_REF_MAP *> (name_obj_map);
00167 }
00168 else
00169 {
00170
00171
00172
00173 size_t index_size = sizeof (NAME_OBJ_REF_MAP);
00174 name_obj_map = this->allocator_->malloc (index_size);
00175
00176 if (name_obj_map == 0
00177 || create_index_helper (name_obj_map) == -1
00178 || this->allocator_->bind (TAO_PERSISTENT_NAME_OBJ_MAP,
00179 name_obj_map) == -1)
00180 {
00181
00182 ACE_ERROR ((LM_ERROR,
00183 "create_index\n"));
00184 this->allocator_->remove ();
00185 return -1;
00186 }
00187 }
00188 return 0;
00189 }
00190
00191 int
00192 TAO_PSDL_Datastore::create_index_helper (void *buffer)
00193 {
00194 this->obj_ref_map_ = new(buffer) NAME_OBJ_REF_MAP(this->allocator_) ;
00195 return 0;
00196 }