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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
//$$FILE$$
//$$VERSION$$
//$$DATE$$
//$$LICENSE$$
/*!
** \file TableFile.h
**
** \brief Header file for Block and TableFile classes.
*/
#ifndef TABLEFILE_H
#define TABLEFILE_H
#include <vector>
#include <set>
#include "rcsb_types.h"
#include "mapped_ptr_vector.h"
#include "mapped_ptr_vector.C"
#include "GenString.h"
#include "ISTable.h"
#include "Serializer.h"
/**
** \class Block
**
** \brief Public class that represents a data block, that contains tables.
**
** This class represents a data block, that can come from DDL,
** dictionary or CIF files. Data block is a container of tables.
** This class provides methods for construction and destruction, tables
** manipulation (addition, retrieval, deleting, writing), data blocks
** comparison.
*/
class Block
{
public:
mapped_ptr_vector<ISTable, StringLess> _tables;
/**
** Utility method, not part of users public API, and will soon be removed.
**
** Constructs a data block.
**
** \param[in] name - the name of the data block
** \param[in] serP - pointer to the File Navigator object
** \param[in] fileMode - optional parameter that indicates data block
** mode. Possible values are read-only, create, update and virtual.
** \param[in] caseSense - optional parameter that indicates case
** sensitivity of table names. Possible values are case sensitive
** and case in-sensitive. If not specified, case sensitive table
** names are assumed.
**
** \return Not applicable
**
** \pre None
**
** \post None
**
** \exception: None
*/
Block(const string& name, Serializer* serP,
const eFileMode fileMode = READ_MODE, const Char::eCompareType
caseSense = Char::eCASE_SENSITIVE);
/**
** Utility method, not part of users public API, and will soon be removed.
**
** Destructs a data block.
**
** \param: Not applicable
**
** \return Not applicable
**
** \pre None
**
** \post None
**
** \exception: None
*/
~Block();
/**
** Compares a data block to another data block.
**
** \param[in] inBlock - reference to input data block
**
** \return vector of pairs, where the first value in a pair is a
** table name and the second value in a pair is one of the following
** indicators of table differences: \n \n
** eMISSING - table exists only in the input block and not in this
** block \n
** eEXTRA - table exists only in this block and not in the input block \n
** eCASE_SENSE - table exists in both blocks, but with different column
** name case sensitivity \n
** eMORE_COLS - table exists in both blocks, but the table in this block
** has more columns than the table in the input block \n
** eLESS_COLS - table exists in both blocks, but the table in this block
** has less columns than the table in the input block \n
** eCOL_NAMES - table exists in both blocks, but tables have different
** column names \n
** eMORE_ROWS - table exists in both blocks, but the table in this block
** has more rows than the table in the input block \n
** eLESS_ROWS - table exists in both blocks, but the table in this block
** has less rows than the table in the input block \n
** eCELLS - table exists in both blocks, but tables have different
** content \n
**
** \pre None
**
** \post None
**
** \exception: None
*/
vector<pair<string, ISTable::eTableDiff> > operator==(Block& inBlock);
/**
** Utility method, not part of users public API, and will soon be removed.
**
** Sets the name of a data block.
**
** \param[in] name - the name of the data block
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline void SetName(const string& name);
/**
** Retrieves data block name.
**
** \param: None
**
** \return Constant reference to a string that contains data block name.
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline const string& GetName() const;
/**
** Adds a table to the block. If a table with the specified name
** already exists, it will be overwritten.
**
** \param[in] name - optional parameter that indicates the name of the
** table to be added
** \param[in] colCaseSense - optional parameter that indicates case
** sensitivity of column names. Possible values are case sensitive and
** case in-sensitive. If not specified, a table with case sensitive
** column names is constructed.
**
** \return Reference to the table
**
** \pre None
**
** \post None
**
** \exception: None
*/
ISTable& AddTable(const std::string& name = string(),
const Char::eCompareType colCaseSense = Char::eCASE_SENSITIVE);
/**
** Utility method, not part of users public API, and will soon be removed.
*/
void AddTable(const string& name, const int indexInFile = 0,
ISTable* isTableP = NULL);
/**
** Changes the name of a table in the data block.
**
** \param[in] oldName - the name of the table which is to be renamed
** \param[in] newName - the new table name
**
** \return None
**
** \pre \e oldName must be non-empty
** \pre Table with name \e oldName must be present
** \pre \e newName must be non-empty
** \pre Table with name \e newName must not be present
** \pre Block must be in create or update mode
**
**
** \post None
**
** \exception EmptyValueException - if \e oldName is empty
** \exception NotFoundException - if table with name \e oldName does
** not exist
** \exception EmptyValueException - if \e newName is empty
** \exception AlreadyExistsException - if table with name \e newName
** already exists
** \exception FileModeException - if block is not in create or
** update mode
*/
void RenameTable(const string& oldName, const string& newName);
/**
** Retrieves names of all tables in a data block.
**
** \param[out] tableNames - retrieved table names
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void GetTableNames(vector<string>& tableNames);
/**
** Checks for table presence in the data block.
**
** \param[in] tableName - table name
**
** \return true - if table exists
** \return false - if table does not exist
**
** \pre None
**
** \post None
**
** \exception: None
*/
bool IsTablePresent(const string& tableName);
/**
** Retrieves a table reference.
**
** \param[in] tableName - table name
**
** \return Reference to the table, if table was found
**
** \pre None
**
** \post None
**
** \exception NotFoundException - if table with name \e tableName
** does not exist
*/
ISTable& GetTable(const string& tableName);
/**
** Retrieves a pointer to the table.
**
** \param[in] tableName - table name
**
** \return Pointer to the table, if table was found
** \return NULL, if table was not found
**
** \pre None
**
** \post None
**
** \exception: None
*/
ISTable* GetTablePtr(const string& tableName);
/**
** Deletes a table from a data block.
**
** \param[in] tableName - table name
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void DeleteTable(const string& tableName);
/**
** Writes a table to the data block. In this context, writing means
** adding it (if it does not already exist) or updating it (if it
** already exists).
**
** \param[in] isTable - reference to the table
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void WriteTable(ISTable& isTable);
/**
** Writes a table to the data block. In this context, writing means
** adding it (if it does not already exist) or updating it (if it
** already exists).
**
** \param[in] isTableP - pointer to the table
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void WriteTable(ISTable* isTableP);
/**
** Utility method, not part of users public API, and will soon be removed.
*/
void Print();
private:
string _name;
eFileMode _fileMode;
Serializer* _ser;
Block(const Block& t);
Block& operator=(const Block& inBlock);
ISTable* _GetTablePtr(const unsigned int tableIndex);
};
/**
** \class TableFile
**
** \brief Public class that represents a file composed of blocks with tables.
**
** This class represents an ordered container of data blocks. Data blocks can
** come from DDL, dictionary or CIF files, where each data block is a
** container of tables. This class provides methods for construction and
** destruction, data blocks manipulation (addition, retrieval, renaming.).
** The class does in-memory management of data blocks, as well as
** serialization and de-serialization to and from a file. The class supports
** the following file modes: read-only, create, update and virtual. In
** read-only mode, blocks and tables can only be read (from an existing table
** file that has been previously serialized to a file) and cannot be
** modified. Create mode is used to create a table file from scratch and
** add/update blocks and tables in it and serialize it to a file. Update mode
** is used to update an existing table file (that has been previously
** serialized to a file). Virtual mode only provides in-memory management of
** data blocks and is used when object persistency is not needed. Hence, all
** modes except virtual mode provide association between in-memory data
** blocks and persistent data blocks stored in a file.
*/
class TableFile
{
public:
enum eStatusInd
{
eCLEAR_STATUS = 0x0000,
eDUPLICATE_BLOCKS = 0x0001,
eUNNAMED_BLOCKS = 0x0002
};
/**
** Constructs a table file.
**
** \param[in] caseSense - optional parameter that indicates case
** sensitivity of table names in blocks. Possible values are case
** sensitive and case in-sensitive. If not specified, case sensitive
** table names are assumed.
**
** \return Not applicable
**
** \pre None
**
** \post Constructed table file has virtual file mode.
**
** \exception: None
*/
TableFile(const Char::eCompareType caseSense = Char::eCASE_SENSITIVE);
/**
** Constructs a table file.
**
** \param[in] fileMode - table file mode. Possible values are
** read-only, create, update and virtual.
** \param[in] fileName - relative or absolute name of the file
** where object persistency is maintained. If \e fileMode specifies
** virtual mode, this parameter is ignored.
** \param[in] caseSense - optional parameter that indicates case
** sensitivity of table names in blocks. Possible values are case
** sensitive and case in-sensitive. If not specified, case sensitive
** table names are assumed.
**
** \return Not applicable
**
** \pre None
**
** \post None
**
** \exception: None
*/
TableFile(const eFileMode fileMode, const string& fileName,
const Char::eCompareType caseSense = Char::eCASE_SENSITIVE);
/**
** Destructs a table file, by first flushing all the modified tables in
** data blocks (for create mode or update mode) and then releasing all
** in-memory objects.
**
** \param: Not applicable
**
** \return Not applicable
**
** \pre None
**
** \post None
**
** \exception: None
*/
virtual ~TableFile();
/**
** Retrieves the name of the file that persistently holds data blocks
** and their tables.
**
** \param: None
**
** \return String that contains the file name.
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline string GetFileName(void);
/**
** Retrieves table file mode.
**
** \param: None
**
** \return READ_MODE - if read-only mode
** \return CREATE_MODE - if create mode
** \return UPDATE_MODE - if update mode
** \return VIRTUAL_MODE - if virtual mode
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline eFileMode GetFileMode(void);
/**
** Retrieves case sensitivity of table names in blocks.
**
** \param: None
**
** \return eCASE_SENSITIVE - if case sensitive
** \return eCASE_INSENSITIVE - if case in-sensitive
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline Char::eCompareType GetCaseSensitivity(void);
/**
** Retrieves table file status in form of one or more flags.
**
** \param: None
**
** \return One or more of these flags: \n
** eCLEAR_STATUS - no flag value indicates that there are no flags set \n
** eDUPLICATE_BLOCKS - flag that indicates existence of blocks with
** the same name, which are internally stored with different names \n
** eUNNAMED_BLOCKS - flag that indicates existence of blocks with
** empty names
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline unsigned int GetStatusInd(void);
/**
** Retrieves the number of data blocks in the table file.
**
** \param: None
**
** \return The number of data blocks in the table file.
**
** \pre None
**
** \post None
**
** \exception: None
*/
inline unsigned int GetNumBlocks();
/**
** Retrieves data block names.
**
** \param[out] blockNames - retrieved data block names
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void GetBlockNames(vector<string>& blockNames);
/**
** Retrieves the name of the first data block.
**
** \param: None
**
** \return String that contains the name of the first data block.
**
** \pre None
**
** \post None
**
** \exception: None
*/
string GetFirstBlockName();
/**
** Checks for data block existence.
**
** \param[in] blockName - the name of the data block
**
** \return true - if data block exists
** \return false - if data block does not exist
**
** \pre None
**
** \post None
**
** \exception: None
*/
bool IsBlockPresent(const string& blockName);
/**
** Adds a block to the table file. If a block with the specified name
** already exists, table file stores it under different internal name,
** that is obtained by appending a "#" symbol and the current block
** count. After writing blocks, with these kinds of block names,
** to an ASCII file, "#" symbol becomes a comment and the text after
** it is ignored. This enables the preservation of all duplicate blocks
** in the written file.
**
** \param[in] blockName - the name of the data block
**
** \return String that contains the internally assigned data block name.
** This value is different from \e blockName, if data block with
** the name \e blockName, already exists when this method is invoked.
**
** \pre None
**
** \post None
**
** \exception: None
*/
string AddBlock(const string& blockName);
/**
** Retrieves a reference to the data block in the table file.
**
** \param[in] blockName - the name of the data block
**
** \return Reference to the data block in the table file.
**
** \pre Data block with name \e blockName must be present
**
** \post None
**
** \exception NotFoundException - if data block with name \e blockName
** does not exist
*/
Block& GetBlock(const string& blockName);
/**
** Changes the data block name.
**
** \param[in] oldBlockName - the name of the data block which is to
** be renamed
** \param[in] newBlockName - the new data block name
**
** \return String that contains the internally assigned data block name.
** This value is different from \e newBlockName, if data block with
** the name \e newBlockName, already exists when this method is invoked.
**
** \pre Table file must have at least one data block.
** \pre Data block with name \e oldBlockName must be present
**
** \post None
**
** \exception EmptyContainerException - if table file has no data blocks
** \exception NotFoundException - if data block with name \e oldBlockName
** does not exist
*/
string RenameBlock(const string& oldBlockName, const string& newBlockName);
/**
** Changes the name of the first data block in table file.
**
** \param[in] newBlockName - the new data block name
**
** \return String that contains the internally assigned data block name.
** This value is different from \e newBlockName, if data block with
** the name \e newBlockName, already exists when this method is invoked.
**
** \pre Table file must have at least one data block.
**
** \post None
**
** \exception EmptyContainerException - if table file has no data blocks
*/
inline string RenameFirstBlock(const string& newBlockName);
/**
** Writes only the new or modified tables in data blocks to the
** associated persistent storage file (specified at table file
** construction time).
**
** \param: None
**
** \return None
**
** \pre Table file must be in create or update mode
**
** \post None
**
** \exception FileModeException - if table file is not in create or
** update mode
*/
void Flush();
/**
** Writes all the data blocks and their tables in the specified file.
** The inteded purpose of this method is to write to a file different
** than the one specified at construction time.
**
** \param[in] fileName - relative or absolute name of the file
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void Serialize(const string& fileName);
/**
** Flushes the table file (if in create or update mode) and closes
** the associated persistent storage file.
**
** \param: None
**
** \return None
**
** \pre None
**
** \post None
**
** \exception: None
*/
void Close();
protected:
string _fileName;
eFileMode _fileMode;
// Indicates case sensitivity of identifiers
Char::eCompareType _caseSense;
// Indicates the current status of the object
unsigned int _statusInd;
mapped_ptr_vector<Block, StringLess> _blocks;
Serializer* _f;
void _SetStatusInd(const string& blockName);
void _AddBlock(const string& blockName, Serializer* serP);
void _GetNumTablesInBlocks(vector<UInt32>& numTablesInBlocks);
ISTable* _GetTablePtr(const unsigned int blockIndex,
const unsigned int tableIndex);
void _GetAllTables();
unsigned int GetTotalNumTables();
void GetTableNames(vector<string>& tableNames);
void GetTablesIndices(vector<unsigned int>& tablesIndices);
void GetSortedTablesIndices(vector<unsigned int>& tablesIndices);
void _ReadFileIndex();
void _ReadFileIndexVersion0();
void _ReadFileIndexVersion1();
void _WriteFileIndex(Serializer* serP,
const vector<unsigned int>& tableLocs);
private:
static const string _version;
void Init();
void Open(const string& fileName, const eFileMode fileMode);
unsigned int GetBlockIndexFromTableId(const string& tableId);
string GetTableNameFromTableId(const string& tableId);
string MakeInternalBlockName(const string& blockName,
const unsigned int blockIndex);
void PrintHeaderInfo();
};
inline void Block::SetName(const string& name)
{
_name = name;
}
inline const string& Block::GetName() const
{
return _name;
}
inline string TableFile::GetFileName(void)
{
return _fileName;
}
inline eFileMode TableFile::GetFileMode(void)
{
return _fileMode;
}
inline Char::eCompareType TableFile::GetCaseSensitivity(void)
{
return(_caseSense);
}
inline unsigned int TableFile::GetStatusInd(void)
{
return _statusInd;
}
inline unsigned int TableFile::GetNumBlocks()
{
return _blocks.size();
}
inline string TableFile::RenameFirstBlock(const string& newBlockName)
{
return(RenameBlock(GetFirstBlockName(), newBlockName));
}
#endif // TABLEFILE_H
|