📄 bdb_types.hpp
字号:
// if mute == true function do not report overflow condition, but just // truncates the string value enum EOverflowAction { eThrowOnOverflow, eTruncateOnOverflow }; protected: CBDB_FieldStringBase() : CBDB_Field(eVariableLength) {}};////// String field type designed to work with C-strings (ASCIIZ)///class NCBI_BDB_EXPORT CBDB_FieldString : public CBDB_FieldStringBase{public: CBDB_FieldString(); // Class factory for string fields. // Default (zero) value of 'buf_size' uses GetBufferSize(). virtual CBDB_Field* Construct(size_t buf_size) const { CBDB_FieldString* fld = new CBDB_FieldString(); fld->SetBufferSize(buf_size ? buf_size : GetBufferSize()); return fld; } operator const char* () const; const CBDB_FieldString& operator= (const CBDB_FieldString& str); const CBDB_FieldString& operator= (const char* str); const CBDB_FieldString& operator= (const string& str); void Set(const char* str, EOverflowAction if_overflow = eThrowOnOverflow); string Get() const { return string((const char*)GetBuffer()); } virtual string GetString() const { return Get(); } bool IsEmpty() const; bool IsBlank() const; // IField virtual int Compare(const void* p1, const void* p2, bool /* byte_swapped */) const; virtual size_t GetDataLength(const void* buf) const; virtual void SetMinVal(); virtual void SetMaxVal(); virtual void SetString(const char*); virtual void SetStdString(const string& str) { SetString(str.c_str()); } virtual void ToString(string& str) const { str = (const char*) GetBuffer(); } virtual BDB_CompareFunction GetCompareFunction(bool) const { return BDB_StringCompare; } };/// Case-insensitive (but case preserving) string field type ///class NCBI_BDB_EXPORT CBDB_FieldStringCase : public CBDB_FieldString{public: typedef CBDB_FieldString CParent; explicit CBDB_FieldStringCase() : CBDB_FieldString() {} // Accessors operator const char* () const { return (const char*) GetBuffer(); } const CBDB_FieldStringCase& operator= (const CBDB_FieldString& str) { Set(str); return *this; } const CBDB_FieldStringCase& operator= (const CBDB_FieldStringCase& str) { Set(str); return *this; } const CBDB_FieldStringCase& operator= (const char* str) { Set(str); return *this; } const CBDB_FieldStringCase& operator= (const string& str) { Set(str.c_str()); return *this; } virtual int Compare(const void* p1, const void* p2, bool/* byte_swapped */) const { _ASSERT(p1 && p2); return NStr::strcasecmp((const char*) p1, (const char*) p2); } virtual BDB_CompareFunction GetCompareFunction(bool /* byte_swapped */) const { return BDB_StringCaseCompare; } };////// Length prefised string field type///class NCBI_BDB_EXPORT CBDB_FieldLString : public CBDB_FieldStringBase{public: CBDB_FieldLString(); // Class factory for string fields. // Default (zero) value of 'buf_size' uses GetBufferSize(). virtual CBDB_Field* Construct(size_t buf_size) const;// operator const char* () const; operator string() const { return GetString(); } const CBDB_FieldLString& operator= (const CBDB_FieldLString& str); const CBDB_FieldLString& operator= (const char* str); const CBDB_FieldLString& operator= (const string& str); void Set(const char* str, EOverflowAction if_overflow = eThrowOnOverflow); string Get() const; virtual string GetString() const { return Get(); } bool IsEmpty() const; bool IsBlank() const; // IField virtual int Compare(const void* p1, const void* p2, bool /* byte_swapped */) const; virtual size_t GetDataLength(const void* buf) const; virtual void SetMinVal(); virtual void SetMaxVal(); virtual void SetString(const char*); virtual void SetStdString(const string& str); virtual BDB_CompareFunction GetCompareFunction(bool) const { return BDB_LStringCompare; } virtual void ToString(string& str) const;protected: const unsigned char* GetLString(const unsigned char* str, bool check_legacy, int* str_len) const;};/// BDB Data Field Buffer manager class. /// For internal use in BDB library.////// @internalclass NCBI_BDB_EXPORT CBDB_BufferManager{public: /// Return number of fields attached using function Bind unsigned int FieldCount() const; const CBDB_Field& GetField(unsigned int idx) const; CBDB_Field& GetField(unsigned int idx); /// Find the field with the specified name. Name is case insensitive. /// @return -1 if field cannot be found int GetFieldIndex(const string& name) const; /// Return TRUE if buffer is in a non-native byte order bool IsByteSwapped() const { return m_ByteSwapped; } /// Sets maximum number of fields participating in comparison /// Should be less than total number of fields in the buffer void SetFieldCompareLimit(unsigned int n_fields); /// Get number of fields in comparison. /// 0 - means no forced limit unsigned int GetFieldCompareLimit() const; /// Return TRUE if buffer l-strings should check about legacy /// c-str compatibility bool IsLegacyStrings() const { return m_LegacyString; } /// Get DBT.size of the parent file (key or data area) /// (Set by CBDB_File after read) size_t GetDBT_Size() const { return m_DBT_Size; } ~CBDB_BufferManager();protected: CBDB_BufferManager(); /// Create internal data buffer, assign places in this buffer to the fields void Construct(); /// Set minimum possible value to buffer fields from 'idx_from' to 'idx_to' void SetMinVal(unsigned int idx_from, unsigned int idx_to); /// Set maximum possible value to buffer fields from 'idx_from' to 'idx_to' void SetMaxVal(unsigned int idx_from, unsigned int idx_to); /// Attach 'field' to the buffer. /// NOTE: buffer manager will not own the attached object, nor will it /// keep ref counters or do any other automagic memory management. void Bind(CBDB_Field* field, ENullable is_nullable = eNotNullable); /// Duplicate (dynamic allocation is used) all fields from 'buf_mgr' and /// bind them to the this buffer manager. Field values are not copied. /// NOTE: CBDB_BufferManager does not own or deallocate fields, /// caller is responsible for deallocation. void CopyFieldsFrom(const CBDB_BufferManager& buf_mgr); /// Copy all field values from the 'buf_mgr'. void DuplicateStructureFrom(const CBDB_BufferManager& buf_mgr); /// Compare fields of this buffer with those of 'buf_mgr' using /// CBDB_Field::CompareWith(). /// Optional 'n_fields' parameter used when we want to compare only /// several first fields instead of all. int Compare(const CBDB_BufferManager& buf_mgr, unsigned int n_fields = 0) const; /// Return TRUE if any field bound to this buffer manager has variable /// length (i.e. packable) bool IsPackable() const; /// Check if all NOT NULLABLE fields were assigned. /// Throw an exception if not. void CheckNullConstraint() const; void ArrangePtrsUnpacked(); void ArrangePtrsPacked(); void Clear(); unsigned Pack(); unsigned Unpack(); /// Pack the buffer and initialize DBT structure for write operation void PrepareDBT_ForWrite(DBT* dbt); /// Initialize DBT structure for read operation. void PrepareDBT_ForRead(DBT* dbt); /// Calculate buffer size size_t ComputeBufferSize() const; /// Return TRUE if buffer can carry NULL fields bool IsNullable() const; /// Set byte swapping flag for the buffer void SetByteSwapped(bool byte_swapped) { m_ByteSwapped = byte_swapped; } /// Mark buffer as "NULL fields ready". /// NOTE: Should be called before buffer construction. void SetNullable(); void SetNull(unsigned int field_idx, bool value); bool IsNull (unsigned int field_idx) const; size_t ComputeNullSetSize() const; bool TestNullBit(unsigned int idx) const; void SetNullBit (unsigned int idx, bool value); void SetAllNull(); /// Return buffer compare function BDB_CompareFunction GetCompareFunction() const; /// Set C-str detection void SetLegacyStringsCheck(bool value) { m_LegacyString = value; } void SetDBT_Size(size_t size) { m_DBT_Size = size; }private: CBDB_BufferManager(const CBDB_BufferManager&); CBDB_BufferManager& operator= (const CBDB_BufferManager&);private: vector<CBDB_Field*> m_Fields; /// Array of pointers to the fields' data vector<void*> m_Ptrs; char* m_Buffer; size_t m_BufferSize; size_t m_PackedSize; size_t m_DBT_Size; bool m_Packable; /// TRUE if buffer is in a non-native arch. bool m_ByteSwapped; /// TRUE if buffer can carry NULL fields bool m_Nullable; /// size of the 'is NULL' bitset in bytes size_t m_NullSetSize; /// Number of fields in key comparison unsigned int m_CompareLimit; /// Flag to check for legacy string compatibility bool m_LegacyString;private: friend class CBDB_Field; friend class CBDB_BLobFile; friend class CBDB_File; friend class CBDB_FileCursor; friend class CBDB_FC_Condition;};/* @} *//////////////////////////////////////////////////////////////////////////////// IMPLEMENTATION of INLINE functions//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// CBDB_Field:://inline bool CBDB_Field::IsVariableLength() const{ return m_Flags.VariableLength == 1;}inline size_t CBDB_Field::GetBufferSize() const{ return m_BufferSize;}inline bool CBDB_Field::IsBufferAttached() const{ return m_Flags.Attached == 1;}inline bool CBDB_Field::IsNullable() const{ return m_Flags.Nullable == 1;}inline void CBDB_Field::SetNullable(){ m_Flags.Nullable = 1;}inline void CBDB_Field::SetNotNull(){ m_BufferManager->SetNull(m_BufferIdx, false);}inline void CBDB_Field::SetNull(){ _ASSERT(m_BufferManager->IsNullable()); m_BufferManager->SetNull(m_BufferIdx, true);}inline bool CBDB_Field::IsNull() const{ return m_BufferManager->IsNull(m_BufferIdx);}inline void* CBDB_Field::Unpack(){ _ASSERT(m_BufferManager); m_BufferManager->Unpack(); return GetBuffer();}inline void CBDB_Field::SetBuffer(void* buf, size_t buf_size){ m_Buffer = buf; if ( buf_size ) m_BufferSize = buf_size; m_Flags.Attached = 1;}inline void CBDB_Field::SetBufferSize(size_t buf_size){ _ASSERT(buf_size != 0); m_BufferSize = buf_size;}inline void CBDB_Field::SetBufferIdx(unsigned int idx){ m_BufferIdx = idx;}inline const void* CBDB_Field::GetBuffer() const{ return m_Buffer;}inline void* CBDB_Field::GetBuffer() { return m_Buffer;}inline void CBDB_Field::SetBufferManager(CBDB_BufferManager* owner){ _ASSERT(owner); m_BufferManager = owner;}inline size_t CBDB_Field::GetLength() const{ return GetDataLength(m_Buffer);}inline int CBDB_Field::CompareWith(const CBDB_Field& field) const{ bool byte_swapped = m_BufferManager->IsByteSwapped(); return Compare(GetBuffer(), field.GetBuffer(), byte_swapped);}inline bool CBDB_Field::IsSameType(const CBDB_Field& field) const{ try { const type_info& t1 = typeid(*this); const type_info& t2 = typeid(field); return (t1 == t2) != 0; } catch (...) { } return false;}inline void CBDB_Field::CopyFrom(const CBDB_Field& src){ if (this == &src) return; if ( !IsSameType(src) ) { BDB_THROW(eType, "Wrong field type"); } CopyFrom(src.GetBuffer());}inline void CBDB_Field::CopyFrom(const void* src_buf){ _ASSERT(src_buf); void* dst_ptr = Unpack(); _ASSERT(dst_ptr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -