00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __BankStream_AMOS_HH
00011 #define __BankStream_AMOS_HH 1
00012
00013 #include "Bank_AMOS.hh"
00014 #include <vector>
00015
00016
00017
00018
00019 namespace AMOS {
00020
00021
00033
00034 class BankStream_t : public Bank_t
00035 {
00036
00037 protected:
00038
00039 static const Size_t DEFAULT_BUFFER_SIZE;
00041
00042 static const Size_t MAX_OPEN_PARTITIONS;
00044
00045
00046
00049 void init()
00050 {
00051 oldPartition_m = NULL;
00052 fixed_store_only_m = false;
00053 eof_m = false;
00054 curr_bid_m = 1;
00055 ate_m = false;
00056 triples_m . resize (1);
00057 }
00058
00059
00060
00061 bool inrange()
00062 {
00063 return ( curr_bid_m > 0 && curr_bid_m <= last_bid_m );
00064 }
00065
00066
00067 bool fixed_store_only_m;
00068 bool eof_m;
00069 ID_t curr_bid_m;
00070 bool ate_m;
00071 std::vector<const IDMap_t::HashTriple_t *> triples_m;
00072
00073 BankPartition_t * oldPartition_m;
00074
00075 public:
00076
00077 enum bankseekdir
00078 {
00079 BEGIN,
00080 CURR,
00081 END
00082 };
00083
00084
00085
00101 BankStream_t (NCode_t type)
00102 : Bank_t (type)
00103 {
00104 init();
00105 triples_m [NULL_ID] = NULL;
00106 buffer_size_m = DEFAULT_BUFFER_SIZE;
00107 max_partitions_m = MAX_OPEN_PARTITIONS;
00108 }
00109
00110
00111
00112 BankStream_t (const std::string & type)
00113 : Bank_t (type)
00114 {
00115 init();
00116 triples_m [NULL_ID] = NULL;
00117 buffer_size_m = DEFAULT_BUFFER_SIZE;
00118 max_partitions_m = MAX_OPEN_PARTITIONS;
00119 }
00120
00121
00122
00123 ~BankStream_t()
00124 {
00125 if ( is_open_m )
00126 close();
00127 }
00128
00129
00130
00131 void append (IBankable_t & obj)
00132 {
00133 operator<< (obj);
00134 }
00135
00136
00137
00138 void assignEID (ID_t iid, const std::string & eid);
00139
00140
00141
00142 void assignIID (const std::string & eid, ID_t iid);
00143
00144
00145
00149 void clean();
00150
00151
00152
00153 void clear()
00154 {
00155 init();
00156 Bank_t::clear();
00157 }
00158
00159
00160
00161 void close()
00162 {
00163 init();
00164 Bank_t::close();
00165 }
00166
00167
00168
00171 void concat (BankStream_t & source);
00172
00173
00174
00175 void create (const std::string & dir, BankMode_t mode = B_READ | B_WRITE)
00176 {
00177 init();
00178 Bank_t::create (dir, mode);
00179 }
00180
00181
00182
00183 void destroy()
00184 {
00185 init();
00186 Bank_t::destroy();
00187 }
00188
00189
00190
00200 bool eof() const
00201 {
00202 return ( is_open_m ? eof_m : true );
00203 }
00204
00205
00206
00207 void fetch (ID_t iid, IBankable_t & obj)
00208 {
00209 Bank_t::fetch (iid, obj);
00210 oldPartition_m = NULL;
00211 }
00212
00213
00214 void fetch (const std::string & eid, IBankable_t & obj)
00215 {
00216 Bank_t::fetch (eid, obj);
00217 oldPartition_m = NULL;
00218 }
00219
00220
00221 void fetchFix (ID_t iid, IBankable_t & obj)
00222 {
00223 Bank_t::fetchFix(iid, obj);
00224 oldPartition_m = NULL;
00225 }
00226
00227
00228 void fetchFix (const std::string & eid, IBankable_t & obj)
00229 {
00230 Bank_t::fetchFix(eid, obj);
00231 oldPartition_m = NULL;
00232 }
00233
00234
00235
00236
00247 BankStream_t & ignore (bankstreamoff n);
00248
00249
00250
00251 void open (const std::string & dir, BankMode_t mode = B_READ | B_WRITE);
00252
00253
00254
00257 void remove (ID_t iid)
00258 {
00259 ate_m = false;
00260
00261 ID_t bid = lookupBID (iid);
00262 removeBID (bid);
00263 triples_m [bid] = NULL;
00264 idmap_m . remove (iid);
00265
00266 oldPartition_m = NULL;
00267 }
00268
00269
00270
00271 void remove (const std::string & eid)
00272 {
00273 ate_m = false;
00274
00275 ID_t bid = lookupBID (eid);
00276 removeBID (bid);
00277 triples_m [bid] = NULL;
00278 idmap_m . remove (eid);
00279
00280 oldPartition_m = NULL;
00281 }
00282
00283
00284 void removeByBID(ID_t bid)
00285 {
00286 if (bid < 0 || bid > last_bid_m)
00287 {
00288 AMOS_THROW_IO ("Cannot replaceByBID: outside valid bid range");
00289 }
00290
00291 ate_m = false;
00292 removeBID(bid);
00293
00294 if (triples_m[bid])
00295 {
00296 if (triples_m[bid]->iid)
00297 {
00298 idmap_m.remove(triples_m[bid]->iid);
00299 }
00300 else if (!triples_m[bid]->eid.empty())
00301 {
00302 idmap_m.remove(triples_m[bid]->eid);
00303 }
00304
00305 triples_m[bid] = NULL;
00306 }
00307
00308 oldPartition_m = NULL;
00309 }
00310
00311
00312
00313 void replace (ID_t iid, IBankable_t & obj);
00314
00315
00316
00317 void replace (const std::string & eid, IBankable_t & obj);
00318
00319
00320
00321 void replaceByBID(ID_t bid, IBankable_t & obj);
00322
00323
00324
00325
00333 void setFixedStoreOnly(bool onlyFixed)
00334 {
00335 fixed_store_only_m = onlyFixed;
00336 }
00337
00338
00339
00354 BankStream_t & seekg (bankstreamoff off, bankseekdir dir)
00355 {
00356 if ( ! is_open_m || ! (mode_m & B_READ) )
00357 AMOS_THROW_IO ("Cannot seekg: bank not open for reading");
00358
00359 switch ( dir )
00360 {
00361 case BEGIN: curr_bid_m = 1; break;
00362 case END: curr_bid_m = last_bid_m + 1; break;
00363 case CURR: break;
00364 default: AMOS_THROW ("Cannot seekg: bad bankseekdir value");
00365 }
00366 ignore (off);
00367 oldPartition_m = NULL;
00368
00369 return *this;
00370 }
00371
00372
00373
00385 BankStream_t & seekg (ID_t pos)
00386 {
00387 if ( ! is_open_m || ! (mode_m & B_READ) )
00388 AMOS_THROW_IO ("Cannot seekg: bank not open for reading");
00389
00390 if ( pos != curr_bid_m )
00391 {
00392 curr_bid_m = pos;
00393 eof_m = !inrange();
00394 }
00395
00396 oldPartition_m = NULL;
00397
00398 return *this;
00399 }
00400
00401
00402
00412 ID_t tellg() const
00413 {
00414 return curr_bid_m;
00415 }
00416
00417
00418
00428 ID_t tellp() const
00429 {
00430 return last_bid_m + 1;
00431 }
00432
00433
00434
00439 operator bool() const
00440 {
00441 return !eof();
00442 }
00443
00444
00445
00450 bool operator!() const
00451 {
00452 return eof();
00453 }
00454
00455
00456
00477 BankStream_t & operator>> (IBankable_t & obj);
00478
00479
00480
00489 BankStream_t & operator<< (IBankable_t & obj);
00490 };
00491
00492 }
00493
00494 #endif // #ifndef __BankStream_AMOS_HH