ImplicitCollection.cpp 9.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/

#include "ImplicitCollection.h"
#include "ImplicitCollectionIterator.h"
#include "ImplicitCollectionMetadata.h"

#include "PersistencySvc/ISession.h"
#include "PersistencySvc/IDatabase.h"
#include "PersistencySvc/IContainer.h"
#include "PersistencySvc/PersistencySvcException.h"

#include "POOLCore/Exception.h"
#include "CoralBase/MessageStream.h"

#include "StorageSvc/DbType.h"

#include <sstream>
#include <memory>


namespace pool {

   ImplicitCollection::
   ImplicitCollection( ISession* session,
                       std::string connection,
                       std::string name,
                       ICollection::OpenMode mode )
         :
         m_container( 0 ),
         m_description( name,"ImplicitCollection", connection ),
         m_metadata( 0 )
   {
      open( mode, session );
   }


   ImplicitCollection::
   ImplicitCollection( const ICollectionDescription* description,
                       ICollection::OpenMode mode,
                       ISession* session )
         :
         m_container( 0 ),
         m_description( *description ),
         m_metadata( 0 )
   {
      open( mode, session );
   }


   void
   ImplicitCollection::
   open( ICollection::OpenMode mode,
         ISession* session )
   {
      coral::MessageStream log( "ImplicitCollection");

      if ( mode != ICollection::READ ) {
         log << coral::Error << "An implicit collection can be opened only in READ mode" << coral::MessageStream::endmsg;
         throw Exception( "An implicit collection can be opened only in READ mode",
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }

      DatabaseSpecification::NameType dbNameType = DatabaseSpecification::UNDEFINED;

      // parse the connection string
      const std::string& connection = m_description.connection();
      std::string::size_type pos = connection.find( ":" );
      if ( pos == std::string::npos ) {
         log << coral::Error << "Badly formed connection string : \"" << connection << "\"" << coral::MessageStream::endmsg;
         throw Exception( "Badly formed connection string",
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }

      const std::string dbType = connection.substr( 0, pos );

      if ( dbType == "PFN" ) dbNameType = DatabaseSpecification::PFN;
      else if ( dbType == "LFN" ) dbNameType = DatabaseSpecification::LFN;
      else if ( dbType == "FID" ) dbNameType = DatabaseSpecification::FID;
      else {
         log << coral::Error << "Unrecognizable database name type : \"" << dbType << "\"" << coral::MessageStream::endmsg;
         throw Exception( "Unrecognizable database name type : " + dbType,
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }

      std::string dbName = "";
      std::string technologyName = "";
      std::istringstream is( connection.substr( pos + 1 ).c_str() );
      is >> dbName >> technologyName;

      if ( dbName.empty() ) {
         log << coral::Error << "Invalid database name " << coral::MessageStream::endmsg;
         throw Exception( "Invalid database name",
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }
  
      if( !session ) {
         throw Exception( "session object not set",
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }
  
      IDatabase* database = session->databaseHandle( dbName, dbNameType );
      if( !database ) {
         throw Exception( "Could not retrieve a database handle",
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }

      if ( database->openMode() == IDatabase::CLOSED ) {
         // The following fix was added to allow the reading of an implicit 
         // collection in the absence of a POOL file catalog. For now, it assumes 
         // a ROOT persistency storage type if no other type can be found. 
         try {
            database->connectForRead();
         }
         catch ( PersistencySvcException& /* exception */ ) {
	    // use provided tech name or assume ROOT
            DbType theDbType = (technologyName != "") ? DbType::getType( technologyName ) : ROOT_StorageType;
	    // setting tech will make connectForRead work without a catalog
            database->setTechnology( theDbType.type() );
            database->connectForRead();
         }
      }

      const std::string& name = m_description.name();
      std::vector< std::string > containers = database->containers();
      for( std::vector< std::string >::const_iterator iContainer = containers.begin();
           iContainer != containers.end(); ++iContainer ) {
         if( name == *iContainer ) {
            m_container = database->containerHandle( name );
            break;
         }
      }
      delete database;

      if( !m_container ) {
         throw Exception( "Could not open the container " + name,
                          "ImplicitCollection::ImplicitCollection",
                          "ImplicitCollection" );
      }

      log << coral::Info << "Opened the implicit collection with connection string \""
          << connection << "\"" << coral::MessageStream::endmsg
          << "and a name \"" << name << "\"" << coral::MessageStream::endmsg;
   }


   
   ImplicitCollection::~ImplicitCollection()
   {
      delete m_container;
      delete m_metadata;
   }

   

   ICollection::OpenMode 
   ImplicitCollection::openMode() const{
      return ICollection::READ;
   }


   // old method implemented for backward compatibility
   // and maybe also for the ease of use?
   ImplicitCollectionIterator*
   ImplicitCollection::select( std::string primaryQuery,
                               std::string,
                               std::string )
   {
      // iterator object supporting the collection query interface
      std::auto_ptr<ImplicitCollectionIterator>
	 iterquery( new ImplicitCollectionIterator( *m_container, m_description ) );
      iterquery->setCondition( primaryQuery );
      iterquery->execute();
      return iterquery.release();
   }



   void
   ImplicitCollection::commit(bool)
   {
   }


   void
   ImplicitCollection::rollback()
   {
   }


   void
   ImplicitCollection::close()
   {
      // can't be closed
   }


   void
   ImplicitCollection::open()
   {
      // hmm, no-op at the moment  //MN
   }

   bool
   ImplicitCollection::isOpen() const
   {
      return true;
   }

   const ICollectionDescription& ImplicitCollection::description() const
   {
      return m_description;
   }

      
   ICollectionQuery* ImplicitCollection::newQuery() const
   {
      return new ImplicitCollectionIterator( *m_container, m_description ); 
   }

   ICollectionSchemaEditor& ImplicitCollection::schemaEditor()
   {
      std::string errorMsg = "Cannot modify an ImplicitCollection.";
      throw Exception( errorMsg,
                       "ImplicitCollection::schemaEditor",
                       "ImplicitCollection" );
      return *(ICollectionSchemaEditor*)0; 
   }

   ICollectionDataEditor& ImplicitCollection::dataEditor()
   {
      std::string errorMsg = "Cannot modify an ImplicitCollection.";
      throw Exception( errorMsg,
                       "ImplicitCollection::dataEditor",
                       "ImplicitCollection" );
      return *(ICollectionDataEditor*)0;
   }

   ICollectionMetadata& ImplicitCollection::metadata()
   {
      if( !m_metadata ) {
         m_metadata = new ImplicitCollectionMetadata();
      }
      return *m_metadata;
   } 

   void methodNotImplemented(const std::string &method)
   {
      throw Exception( std::string("method not implemented"),
                       std::string("ImplicitCollection::") + method,
                       "ImplicitCollection" );
   }

   bool
   ImplicitCollection::exists( const std::string& /*name*/, bool, bool ) const
   {
      methodNotImplemented("exists");
      return false;
   }
      
   bool
   ImplicitCollection::drop( const std::string&, bool, bool)
   {
      methodNotImplemented("drop");
      return false;
   }
      
   bool
   ImplicitCollection::rename( const std::string&, const std::string&)
   {
      methodNotImplemented("rename");
      return false;
   }
      
   bool
   ImplicitCollection::grantToUser( const std::string&,
                                ICollection::Privilege,
                                const std::string&,
                                bool)
   {
      methodNotImplemented("grantToUser");
      return false;
   }
      
   bool
   ImplicitCollection::revokeFromUser( const std::string&,
                                   ICollection::Privilege,
                                   const std::string&,
                                   bool)
   {
      methodNotImplemented("revokeFromUser");
      return false;
   }
      
   bool
   ImplicitCollection::grantToPublic( const std::string&, bool)
   {
      methodNotImplemented("grantToPublic");
      return false;
   }
            
   bool
   ImplicitCollection::revokeFromPublic( const std::string&, bool )
   {
      methodNotImplemented("revokeFromPublic");
      return false;
   }      
}