include/db/bdb/bdb_file.hpp

Go to the documentation of this file.
00001 #ifndef BDB_FILE_HPP__
00002 #define BDB_FILE_HPP__
00003 
00004 /* $Id: bdb_file.hpp 163327 2009-06-15 15:40:12Z ivanovp $
00005  * ===========================================================================
00006  *
00007  *                            PUBLIC DOMAIN NOTICE
00008  *               National Center for Biotechnology Information
00009  *
00010  *  This software/database is a "United States Government Work" under the
00011  *  terms of the United States Copyright Act.  It was written as part of
00012  *  the author's official duties as a United States Government employee and
00013  *  thus cannot be copyrighted.  This software/database is freely available
00014  *  to the public for use. The National Library of Medicine and the U.S.
00015  *  Government have not placed any restriction on its use or reproduction.
00016  *
00017  *  Although all reasonable efforts have been taken to ensure the accuracy
00018  *  and reliability of the software and data, the NLM and the U.S.
00019  *  Government do not and cannot warrant the performance or results that
00020  *  may be obtained by using this software or data. The NLM and the U.S.
00021  *  Government disclaim all warranties, express or implied, including
00022  *  warranties of performance, merchantability or fitness for any particular
00023  *  purpose.
00024  *
00025  *  Please cite the author in any work or product based on this material.
00026  *
00027  * ===========================================================================
00028  *
00029  * Author:  Anatoliy Kuznetsov
00030  *
00031  * File Description: Berkeley DB File classes.
00032  *
00033  */
00034 
00035 /// @file bdb_file.hpp
00036 /// BDB File management.
00037 
00038 #include <corelib/ncbistre.hpp>
00039 #include <util/itransaction.hpp>
00040 #include <util/compress/compress.hpp>
00041 #include <db/bdb/bdb_types.hpp>
00042 #include <stdio.h>
00043 
00044 
00045 BEGIN_NCBI_SCOPE
00046 
00047 /** @addtogroup BDB_Files
00048  *
00049  * @{
00050  */
00051 
00052 
00053 /// BDB Return codes
00054 ///
00055 
00056 
00057 enum EBDB_ErrCode {
00058     eBDB_Ok,
00059     eBDB_NotFound,
00060     eBDB_KeyDup,
00061     eBDB_KeyEmpty,
00062     eBDB_MultiRowEnd
00063 };
00064 
00065 
00066 class CBDB_Env;
00067 class CBDB_Transaction;
00068 
00069 
00070 /// Raw file class wraps up basic Berkeley DB operations.
00071 ///
00072 class  CBDB_RawFile : public ITransactional
00073 {
00074 public:
00075     static const char kDefaultDatabase[];  // = "_table"
00076 
00077     /// BDB file open mode
00078     enum EOpenMode {
00079         eReadWrite,
00080         eReadOnly,
00081         eCreate,         //!< implies 'eReadWrite' too
00082         eReadWriteCreate //!< read-write, create if it doesn't exist
00083     };
00084 
00085     /// Berkeley DB database type
00086     enum EDBType {
00087         eBtree,
00088         eQueue,
00089         eHash
00090     };
00091 
00092     /// BLOB read mode, controld data buffer reallocation when
00093     /// there is not enough space in buffer
00094     ///
00095     enum EReallocMode {
00096         eReallocAllowed,
00097         eReallocForbidden
00098     };
00099 
00100     /// Control key duplicates in Btree
00101     enum EDuplicateKeys {
00102         eDuplicatesDisable,
00103         eDuplicatesEnable
00104     };
00105 
00106     enum EIgnoreError {
00107         eIgnoreError,
00108         eThrowOnError
00109     };
00110 
00111     /// BerkeleyDB compaction methods and flags
00112     enum ECompact {
00113 
00114         //< Do not attempt to free any pages from the underlying file
00115         eCompactNoFree,
00116 
00117         //< Free only pages that already exist as free pages in the file
00118         eCompactFreeExisting,
00119 
00120         //< Return all free pages to the file system after compaction
00121         eCompactFreeAll
00122     };
00123 
00124     /// typedef for raw buffer operations
00125     typedef CSimpleBuffer TBuffer;
00126 
00127 public:
00128     CBDB_RawFile(EDuplicateKeys dup_keys = eDuplicatesDisable,
00129                  EDBType        db_type  = eBtree);
00130     virtual ~CBDB_RawFile();
00131 
00132     /// Associate file with environment. Should be called before
00133     /// file opening.
00134     void SetEnv(CBDB_Env& env);
00135 
00136     /// Get pointer on file environment
00137     /// Return NULL if no environment has been set
00138     CBDB_Env* GetEnv() { return m_Env; }
00139 
00140     /// Open file with specified access mode
00141     void Open(const string& filename,
00142               EOpenMode     open_mode,
00143               bool          support_dirty_read = false,
00144               unsigned      rec_len = 0);
00145     /// Open file with specified filename and database name.
00146     /// (Berkeley DB supports having several database tables in one file.)
00147     void Open(const string& filename,
00148               const string& database,
00149               EOpenMode   open_mode,
00150               bool        support_dirty_read = false,
00151               unsigned    rec_len = 0);
00152     /// Attach class to external BerkeleyDB file instance.
00153     /// Note: Should be already open.
00154     void Attach(CBDB_RawFile& bdb_file);
00155     /// Close file
00156     void Close();
00157     /// Reopen database file. (Should be already open).
00158     void Reopen(EOpenMode open_mode,
00159                 bool      support_dirty_read = false,
00160                 unsigned  rec_len = 0);
00161 
00162     /// Remove the database specified by the filename and database arguments
00163     void Remove(const string& filename, const string& database = kEmptyStr);
00164     /// Empty the database. Return number of records removed.
00165     unsigned int Truncate();
00166     /// Workaround for truncate of large databases. Executes out of transaction,
00167     /// and as such cannot be rolled back/
00168     unsigned int SafeTruncate();
00169     /// Rename a database.  NOTE: This cannot be called on an opened file.
00170     void Rename(const string& fname,
00171                 const string& old_name,
00172                 const string& new_name);
00173 
00174     /// Compact the database.  The target fill percent per page can be
00175     /// supplied, to allow for known expansion
00176     void Compact(ECompact compact_type = eCompactNoFree,
00177                  int target_fill_pct = 0);
00178 
00179     /// Extended version of compact
00180     /// This version performs iterative compacting and uses a callback
00181     /// to request an exit
00182     typedef bool (*FContinueCompact)(void);
00183     void CompactEx(FContinueCompact compact_callback,
00184                    ECompact compact_type = eCompactNoFree,
00185                    int target_fill_pct = 0);
00186 
00187     // Set Berkeley DB page size value. By default OS default is used.
00188     void SetPageSize(unsigned int page_size);
00189 
00190     // Get Berkeley DB page size value. By default OS default is used.
00191     unsigned int GetPageSize();
00192 
00193     /// Set Berkeley DB memory cache size for the file (default is 256K).
00194     void SetCacheSize(unsigned int cache_size);
00195 
00196     /// Turn OFF reverse splitting
00197     void RevSplitOff();
00198 
00199     /// Disable BTREE comparison override
00200     void DisableCmpOverride() { m_CmpOverride = false; }
00201 
00202     /// Set the priority for this database's pages in the buffer cache
00203     /// This is generally a temporary advisement, and works only if an
00204     /// environment is used.
00205     enum ECachePriority {
00206         eCache_Lowest,
00207         eCache_Low,
00208         eCache_Default,
00209         eCache_High,
00210         eCache_Highest
00211     };
00212     void SetCachePriority(ECachePriority priority);
00213 
00214     const string& FileName() const;
00215     const string& Database() const;
00216 
00217     /// Set comparison function. Default implementation installs bdb_types based
00218     /// function. Can be overloaded for some specific cases.
00219     virtual void SetCmp(DB*) = 0;
00220 
00221     /// Set hash function. Default implementation installs bdb_types based
00222     /// function. Can be overloaded for some specific cases.
00223     virtual void SetHash(DB*);
00224 
00225     /// Return TRUE if the file is open
00226     bool IsOpen() const;
00227 
00228     // Return TRUE if the file is attached to some other BDB file
00229     bool IsAttached() const;
00230 
00231     /// Return TRUE if the if the underlying database files were created
00232     /// on an architecture of the different byte order
00233     bool IsByteSwapped() const { return m_ByteSwapped; }
00234 
00235     /// Return TRUE if file can contain duplicate keys
00236     bool DuplicatesAllowed() const { return m_DuplicateKeys == eDuplicatesEnable; }
00237 
00238     /// Return the key duplicate mode value
00239     EDuplicateKeys GetDupKeysMode() const { return m_DuplicateKeys; }
00240 
00241     /// Return file name
00242     const string& GetFileName() const { return m_FileName; }
00243 
00244     /// Return the file open mode
00245     EOpenMode GetOpenMode() const { return m_OpenMode; }
00246 
00247     /// Flush any cached information to disk
00248     void Sync();
00249 
00250     /// Compute database statistic, return number of records.
00251     /// (Can be time consuming)
00252     unsigned CountRecs(bool bFast = false);
00253 
00254     /// Print database statistics
00255     void PrintStat(CNcbiOstream & out);
00256 
00257     // ITransactional:
00258 
00259     virtual void SetTransaction(ITransaction* trans);
00260     virtual void RemoveTransaction(ITransaction* trans);
00261     virtual ITransaction* GetTransaction();
00262 
00263     /// Get current transaction
00264     CBDB_Transaction* GetBDBTransaction() { return m_Trans; }
00265 
00266     /// Get record length
00267     /// Works for fixed length record DBs only (Queue)
00268     unsigned GetRecLen() const;
00269 
00270     /// Set hash table density (fill factor)
00271     void SetHashFillFactor(unsigned h_ffactor);
00272 
00273     /// Set an estimate of hash table final size
00274     void SetHashNelem(unsigned h_nelem);
00275 
00276     /// Disable hash method override
00277     /// (Berkeley DB will use it's own default hashing method)
00278     void DisableHashOverride() { m_CmpOverride = false; }
00279 
00280     /// Set the minimum number of keys per page (BTREE access methods only)
00281     void SetBtreeMinKeysPerPage(unsigned int keys_per_page);
00282     unsigned int GetBtreeMinKeysPerPage();
00283 
00284 
00285     /// Set record compressor
00286     ///
00287     /// Record compression should only be used if we do not
00288     /// use partial record storage and retrieval.
00289     ///
00290     void SetCompressor(ICompression* compressor,
00291                        EOwnership    own = eTakeOwnership);
00292 
00293 private:
00294     /// forbidden
00295     CBDB_RawFile(const CBDB_RawFile&);
00296     CBDB_RawFile& operator= (const CBDB_RawFile&);
00297 
00298 protected:
00299     void x_Open(const char* filename, const char* database,
00300                 EOpenMode open_mode,
00301                 bool      support_dirty_read,
00302                 unsigned  rec_len);
00303     void x_Create(const char* filename, const char* database);
00304 
00305     void x_Close(EIgnoreError close_mode);
00306 
00307     /// Create m_DB member, set page, cache parameters
00308     ///
00309     /// @param rec_len
00310     ///    record length (must be non zero for Queue type)
00311     void x_CreateDB(unsigned rec_len);
00312 
00313 
00314     /// Set current transaction
00315     void x_SetTransaction(CBDB_Transaction* trans);
00316 
00317     void x_RemoveTransaction(CBDB_Transaction* trans);
00318 
00319     /// Get transaction handler.
00320     ///
00321     /// Function returns NULL if no transaction has been set.
00322     ///
00323     /// @sa SetTransaction
00324     DB_TXN* GetTxn();
00325 
00326     /// Create DB cursor
00327     DBC* CreateCursor(CBDB_Transaction* trans = 0,
00328                       unsigned int      flags = 0) const;
00329 
00330 
00331     /// Internal override for DB->get(...)
00332     /// This method overrides destination buffer and uses compressor:
00333     /// Should only be used with DB_DBT_USERMEM flag.
00334     ///
00335     int x_DB_Fetch(DBT *key,
00336                    DBT *data,
00337                    unsigned flags);
00338 
00339 
00340     /// Internal override for DBC->c_get(...)
00341     /// This method overrides destination buffer and uses compressor:
00342     /// Should only be used with DB_DBT_USERMEM flag.
00343     ///
00344     int x_DBC_Fetch(DBC* dbc,
00345                     DBT *key,
00346                     DBT *data,
00347                     unsigned flags);
00348 
00349     /// Override for DB->put(...)
00350     /// Handles compression.
00351     ///
00352     int x_DB_Put(DBT *key,
00353                  DBT *data,
00354                  unsigned flags);
00355 
00356     /// Override for DBC->c_put(...)
00357     /// Handles compression.
00358     ///
00359     int x_DB_CPut(DBC* dbc,
00360                   DBT *key,
00361                   DBT *data,
00362                   unsigned flags);
00363 
00364 
00365     int x_FetchBufferDecompress(DBT *data, void* usr_data);
00366 
00367     /// Set byte order swapping. Can be overloaded on derived classes
00368     /// @note When overloading DO call parent::x_SetByteSwapped
00369     virtual void x_SetByteSwapped(bool bswp);
00370 
00371 
00372 protected:
00373     EDBType           m_DB_Type;
00374     DB*               m_DB;
00375     DBT*              m_DBT_Key;
00376     DBT*              m_DBT_Data;
00377     CBDB_Env*         m_Env;
00378     CBDB_Transaction* m_Trans;
00379     int               m_TransAssociation;
00380     unsigned          m_RecLen;
00381     unsigned          m_H_ffactor;
00382     unsigned          m_H_nelem;
00383     unsigned          m_BT_minkey;
00384 
00385     AutoPtr<ICompression> m_Compressor;    ///< Record compressor
00386     TBuffer           m_CompressBuffer;
00387 
00388 private:
00389     bool             m_DB_Attached;    //!< TRUE if m_DB doesn't belong here
00390     bool             m_ByteSwapped;    //!< TRUE if file created on a diff.arch.
00391     bool             m_RevSplitOff;    //!< TRUE if reverse splitting is off
00392     bool             m_CmpOverride;    //!< TRUE - NCBI BDB sets its own cmp
00393     string           m_FileName;       //!< filename
00394     string           m_Database;       //!< db name in file (optional)
00395     unsigned         m_PageSize;
00396     unsigned         m_CacheSize;
00397     EDuplicateKeys   m_DuplicateKeys;
00398     EOpenMode        m_OpenMode;
00399 
00400     static const int kOpenFileMask;
00401 
00402     friend class CBDB_FileCursor;
00403 };
00404 
00405 
00406 /// Multirow buffer for reading many rows in one call
00407 ///
00408 
00409 class  CBDB_MultiRowBuffer
00410 {
00411 public:
00412     CBDB_MultiRowBuffer(size_t buf_size);
00413     ~CBDB_MultiRowBuffer();
00414 
00415     /// Get data buffer pointer from last cursor read
00416     const void* GetLastDataPtr() const { return m_LastData; }
00417     /// Get BLOB length from last cursor read
00418     size_t      GetLastDataLen() const { return m_LastDataLen; }
00419 protected:
00420     void  InitDBT();
00421     void  MultipleInit();
00422 private:
00423     CBDB_MultiRowBuffer(const CBDB_MultiRowBuffer&);
00424     CBDB_MultiRowBuffer& operator=(const CBDB_MultiRowBuffer&);
00425 protected:
00426     DBT*     m_Data_DBT;  ///< Temp DBT for multiple fetch
00427     void*    m_Buf;       ///< Multiple row buffer
00428     size_t   m_BufSize;   ///< buffer size
00429     void*    m_BufPtr;    ///< current buffer position
00430     void*    m_LastKey;   ///< Last key pointer returned by DB_MULTIPLE_KEY_NEXT
00431     void*    m_LastData;  ///< Last data pointer returned by DB_MULTIPLE_KEY_NEXT
00432     size_t   m_LastKeyLen;
00433     size_t   m_LastDataLen;
00434 
00435 friend class CBDB_File;
00436 };
00437 
00438 
00439 
00440 /// Berkeley DB file class.
00441 /// Implements primary key and fields functionality.
00442 ///
00443 
00444 class  CBDB_File : public CBDB_RawFile
00445 {
00446 public:
00447     CBDB_File(EDuplicateKeys dup_keys = eDuplicatesDisable,
00448               EDBType        db_type  = eBtree);
00449 
00450     /// Open file with specified access mode
00451     void Open(const string& filename,
00452               EOpenMode     open_mode,
00453               bool          support_dirty_read = false,
00454               unsigned      rec_len = 0);
00455 
00456     /// Open file with specified filename and database name.
00457     /// (Berkeley DB supports having several database tables in one file.)
00458     void Open(const string& filename,
00459               const string& database,
00460               EOpenMode     open_mode,
00461               bool          support_dirty_read = false,
00462               unsigned      rec_len = 0);
00463     /// Reopen the db file
00464     void Reopen(EOpenMode open_mode, bool support_dirty_read = false);
00465 
00466     /// Attach external Berkeley DB file.
00467     /// Note: Should be already open.
00468     void Attach(CBDB_File& db_file);
00469 
00470     /// Fetches the record corresponding to the current key value.
00471     EBDB_ErrCode Fetch() { return x_Fetch(0); }
00472 
00473     /// Fetche the record corresponding to the current key value.
00474     /// Acquire write lock instead of read lock when doing the retrieval.
00475     /// Meaningful only in the presence of transactions.
00476     EBDB_ErrCode FetchForUpdate();
00477 
00478     enum EAfterWrite {
00479         eKeepData,    //!< Keep the inserted data for a while
00480         eDiscardData  //!< Invalidate the inserted data immediately after write
00481     };
00482 
00483     /// Insert new record
00484     EBDB_ErrCode Insert(EAfterWrite write_flag = eDiscardData);
00485 
00486     /// Append record to the queue
00487     /// (works only for DB_QUEUE database type)
00488     ///
00489     /// @return record number (auto increment)
00490     unsigned Append(EAfterWrite write_flag = eDiscardData);
00491 
00492     /// Delete record corresponding to the current key value.
00493     EBDB_ErrCode Delete(EIgnoreError on_error=eThrowOnError);
00494 
00495     /// Update record corresponding to the current key value. If record does not exist
00496     /// it will be inserted.
00497     EBDB_ErrCode UpdateInsert(EAfterWrite write_flag = eDiscardData);
00498 
00499 
00500     void BindKey (const char* field_name,
00501                   CBDB_Field* key_field,
00502                   size_t buf_size = 0);
00503 
00504     void BindData(const char* field_name,
00505                   CBDB_Field* data_field,
00506                   size_t buf_size = 0,
00507                   ENullable is_null = eNullable);
00508 
00509     /// Create the same fieldset as in dbf and bind them to the current file
00510     void DuplicateStructure(const CBDB_File& dbf);
00511 
00512     /// Get Buffer manager for key section of the file
00513     const CBDB_BufferManager* GetKeyBuffer() const { return m_KeyBuf.get(); }
00514 
00515     /// Get Buffer manager for data section of the file
00516     const CBDB_BufferManager* GetDataBuffer() const { return m_DataBuf.get(); }
00517 
00518     /// Get Buffer manager for key section of the file
00519     CBDB_BufferManager* GetKeyBuffer() { return m_KeyBuf.get(); }
00520 
00521     /// Get Buffer manager for data section of the file
00522     CBDB_BufferManager* GetDataBuffer() { return m_DataBuf.get(); }
00523 
00524     /// Sets maximum number of key fields participating in comparison
00525     /// Should be less than total number of key fields
00526     void SetFieldCompareLimit(unsigned int n_fields);
00527 
00528     /// Create new copy of m_DBT_Key.
00529     /// Caller is responsible for proper deletion. See also: DestroyDBT_Clone
00530     DBT* CloneDBT_Key();
00531 
00532     /// Free the DBT structure created by CloneDBT_Key.
00533     static void DestroyDBT_Clone(DBT* dbt);
00534 
00535     /// Set C-str detection
00536     void SetLegacyStringsCheck(bool value);
00537 
00538     /// CBDB_File keeps data in two buffers (key buffer and data buffer).
00539     /// TUnifiedFieldIndex is used to address fields in a non-ambigiuos manner.
00540     /// Negative index addresses fields in the key buffer, positive - data buffer
00541     /// Numbers are 1 based, 0 - means non-existing field
00542     typedef int TUnifiedFieldIndex;
00543 
00544     /// Get field index by name.
00545     /// @param
00546     ///   name field name to find (case insensitive)
00547     /// @return
00548     ///   Field index (0 if not found)
00549     /// @sa TUnifiedFieldIndex
00550     TUnifiedFieldIndex GetFieldIdx(const string& name) const;
00551 
00552     /// Return field by field index
00553     /// @param
00554     ///    idx field index
00555     /// @return field reference
00556     const CBDB_Field& GetField(TUnifiedFieldIndex idx) const;
00557     CBDB_Field& GetField(TUnifiedFieldIndex idx);
00558 
00559     /// Fields deletion is managed by the class when own_fields is TRUE
00560     void SetFieldOwnership(bool own_fields);
00561 
00562     /// Return fields ownership flag
00563     bool IsOwnFields() const { return m_OwnFields; }
00564 
00565     /// Copy record (fields) from another BDB file
00566     /// (MUST have the same structure)
00567     void CopyFrom(const CBDB_File& dbf);
00568 
00569     /// Run database verification (DB->verify)
00570     void Verify(const char* filename, const char* database, FILE* backup);
00571 
00572     /// Turn ON prefix compression.
00573     /// Should be turned on for string based keys. (not LString)
00574     void EnablePrefixCompression() { m_PrefixCompress = true; }
00575 
00576 protected:
00577     /// Unpack internal record buffers
00578     void Discard();
00579 
00580     /// Set comparison function. Default implementation installs bdb_types based
00581     /// function. Can be overloaded for some specific cases.
00582     virtual void SetCmp(DB*);
00583 
00584     /// Read DB cursor
00585     EBDB_ErrCode ReadCursor(DBC* dbc, unsigned int bdb_flag);
00586 
00587     /// Read DB cursor (BLOB)
00588     EBDB_ErrCode ReadCursor(DBC* dbc, unsigned int bdb_flag,
00589                             void**       buf,
00590                             size_t       buf_size,
00591                             EReallocMode allow_realloc);
00592     /// Read DB cursor (BLOB)
00593     EBDB_ErrCode ReadCursor(DBC* dbc, unsigned int bdb_flag,
00594                             TBuffer* buf);
00595 
00596 
00597     /// Multiple-row read into a buffer
00598     /// Buffer is to be traversed using DB_MULTIPLE_KEY_NEXT (BerkeleyDB)
00599     ///
00600     /// @param multirow_only
00601     ///     Fetch only in multirow buffer, method returns eBDB_MultiRowEnd
00602     ///     when the buffer is over
00603     EBDB_ErrCode ReadCursor(DBC*                  dbc,
00604                             unsigned int          bdb_flag,
00605                             CBDB_MultiRowBuffer*  multirow_buf,
00606                             bool                  multirow_only);
00607 
00608 
00609     /// Write DB cursor
00610     EBDB_ErrCode WriteCursor(DBC* dbc, unsigned int bdb_flag,
00611                              EAfterWrite write_flag);
00612 
00613     /// Write BLOB to DB cursor
00614     EBDB_ErrCode WriteCursor(const void* data,
00615                              size_t      size,
00616                              DBC* dbc, unsigned int bdb_flag,
00617                              EAfterWrite write_flag);
00618 
00619     /// Delete DB cursor
00620     EBDB_ErrCode DeleteCursor(DBC* dbc, EIgnoreError);
00621 
00622     /// Check if all NOT NULL fields are assigned.
00623     /// Throw an exception if constraint check failed.
00624     void CheckNullDataConstraint() const;
00625 
00626     /// Function disables processing of m_DBT_data.
00627     /// This function can be used when creating custom BDB file
00628     /// data structures (BLOB storage, etc.) Caller takes full
00629     /// responsibility for filling m_DBT_Data with correct values.
00630     void DisableDataBufProcessing() { m_DataBufDisabled = true; }
00631 
00632     /// Disable NULL/not NULL in data fields
00633     /// (Performance tweak, call before BindData)
00634     void DisableNull() { m_DisabledNull = true; }
00635 
00636     /// Disable packing of variable length fields in the data buffer
00637     /// (Call after BindData)
00638     void DisableDataPacking();
00639 
00640     /// Wrapper around get operation.
00641     EBDB_ErrCode x_Fetch(unsigned int flags);
00642 
00643     virtual void x_SetByteSwapped(bool bswp);
00644 
00645 private:
00646     /// forbidden
00647     CBDB_File(const CBDB_File&);
00648     CBDB_File& operator= (const CBDB_File&);
00649 
00650     /// Record reading prolog function
00651     void x_StartRead();
00652     /// Record reading epilog function
00653     void x_EndRead();
00654 
00655     EBDB_ErrCode x_Write(unsigned int flags, EAfterWrite write_flag,
00656                          DBC * dbc = 0);
00657 
00658     void x_CheckConstructBuffers();
00659 
00660     void x_ConstructKeyBuf();
00661     void x_ConstructDataBuf();
00662 
00663 private:
00664     auto_ptr<CBDB_BufferManager>   m_KeyBuf;
00665     auto_ptr<CBDB_BufferManager>   m_DataBuf;
00666     bool                           m_BufsAttached;
00667     bool                           m_BufsCreated;
00668     bool                           m_DataBufDisabled;
00669     bool                           m_LegacyString;
00670     bool                           m_OwnFields;
00671     bool                           m_DisabledNull;
00672     bool                           m_PrefixCompress; //!< TRUE if prefix compression ON
00673 
00674     friend class CBDB_FileCursor;
00675 };
00676 
00677 
00678 
00679 /// Berkeley DB file class optimized to work with
00680 /// tables having int as the primary key.
00681 ///
00682 
00683 class  CBDB_IdFile : public CBDB_File
00684 {
00685 public:
00686     CBDB_FieldInt4  IdKey;
00687 
00688 public:
00689     CBDB_IdFile();
00690     virtual void SetCmp(DB* db);
00691 };
00692 
00693 
00694 
00695 /* @} */
00696 
00697 
00698 /////////////////////////////////////////////////////////////////////////////
00699 //  IMPLEMENTATION of INLINE functions
00700 /////////////////////////////////////////////////////////////////////////////
00701 
00702 
00703 /// Make field index in CBDB_File format
00704 ///
00705 /// @internal
00706 inline
00707 CBDB_File::TUnifiedFieldIndex BDB_GetUFieldIdx(int fidx, bool key)
00708 {
00709     _ASSERT(fidx >= 0);
00710     ++fidx;
00711     return key ? (-fidx) : (fidx);
00712 }
00713 
00714 
00715 /////////////////////////////////////////////////////////////////////////////
00716 //  CBDB_RawFile::
00717 //
00718 
00719 
00720 inline
00721 void CBDB_RawFile::Open(
00722         const string& filename, EOpenMode open_mode,
00723         bool support_dirty_read,
00724         unsigned rec_len)
00725 {
00726     Open(filename, kEmptyStr, open_mode, support_dirty_read, rec_len);
00727 }
00728 
00729 inline
00730 unsigned CBDB_RawFile::GetRecLen() const
00731 {
00732     _ASSERT(m_DB_Type == eQueue);
00733     return m_RecLen;
00734 }
00735 
00736 inline
00737 const string& CBDB_RawFile::FileName() const
00738 {
00739     return m_FileName;
00740 }
00741 
00742 
00743 inline
00744 const string& CBDB_RawFile::Database() const
00745 {
00746     return m_Database;
00747 }
00748 
00749 
00750 inline
00751 bool CBDB_RawFile::IsOpen() const
00752 {
00753     return !m_FileName.empty();
00754 }
00755 
00756 inline
00757 bool CBDB_RawFile::IsAttached() const
00758 {
00759     return m_DB_Attached;
00760 }
00761 
00762 
00763 /////////////////////////////////////////////////////////////////////////////
00764 //
00765 //  CBDB_File::
00766 //
00767 
00768 
00769 
00770 inline
00771 void CBDB_File::Open(
00772   const string& filename, EOpenMode open_mode,
00773   bool support_dirty_read, unsigned rec_len)
00774 {
00775     Open(filename, "", open_mode, support_dirty_read, rec_len);
00776 }
00777 
00778 
00779 inline
00780 void CBDB_File::CheckNullDataConstraint() const
00781 {
00782     if ( !m_DisabledNull && m_DataBuf.get() )
00783         m_DataBuf->CheckNullConstraint();
00784 }
00785 
00786 inline
00787 void CBDB_File::SetFieldCompareLimit(unsigned int n_fields)
00788 {
00789     m_KeyBuf->SetFieldCompareLimit(n_fields);
00790 }
00791 
00792 
00793 END_NCBI_SCOPE
00794 
00795 #endif
00796 
00797 

Generated on Wed Dec 9 03:00:22 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Wed Dec 09 08:17:27 2009 by modify_doxy.py rev. 173732