Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

column.h

Go to the documentation of this file.
00001 // column.h --
00002 // $Id: column.h,v 1.13 2003/11/23 01:42:50 wcvs Exp $
00003 // This is part of Metakit, see https://www.equi4.com/metakit/
00004 
00009 #ifndef __COLUMN_H__
00010 #define __COLUMN_H__
00011 
00013 // Declarations in this file
00014 
00015   class c4_Column;          // a column in a table
00016   class c4_ColIter;         // an iterator over column data
00017   class c4_ColCache;          // manages a cache for columns
00018 
00019   class c4_Persist;         // not defined here
00020   class c4_Strategy;          // not defined here
00021 
00023 
00024 class c4_Column
00025 {
00026   c4_PtrArray _segments;
00027   t4_i32 _position;
00028   t4_i32 _size;
00029   c4_Persist* _persist;
00030   t4_i32 _gap;
00031   int _slack;
00032   bool _dirty;
00033   
00034 public:
00035   c4_Column (c4_Persist* persist_);
00036     //: Constructs a column using the specified persistence manager.
00037   ~c4_Column ();
00038 
00039   void SetBuffer(t4_i32);
00040     //: Allocate a new buffer of the specified size.
00041 
00042   c4_Persist* Persist() const;
00043     //: Returns persistence manager for this column, or zero.
00044   c4_Strategy& Strategy() const;
00045     //: Returns the associated strategy pointer.
00046   t4_i32 Position() const;
00047     //: Special access for the DUMP program.
00048   t4_i32 ColSize() const;
00049     //: Returns the number of bytes as stored on disk.
00050   bool IsDirty() const;
00051     //: Returns true if contents needs to be saved.
00052 
00053   void SetLocation(t4_i32, t4_i32);
00054     //: Sets the position and size of this column on file.
00055   void PullLocation(const t4_byte*& ptr_);
00056     //: Extract position and size of this column.
00057 
00058   int AvailAt(t4_i32 offset_) const;
00059     //: Returns number of bytes we can access at once.
00060   const t4_byte* LoadNow(t4_i32);
00061     //: Makes sure the data is loaded into memory.
00062   t4_byte* CopyNow(t4_i32);
00063     //: Makes sure a copy of the data is in memory.
00064   void Grow(t4_i32, t4_i32);
00065     //: Grows the buffer by inserting space.
00066   void Shrink(t4_i32, t4_i32);
00067     //: Shrinks the buffer by removing space.
00068   void SaveNow(c4_Strategy&, t4_i32 pos_);
00069     //: Save the buffer to file.
00070 
00071   const t4_byte* FetchBytes(t4_i32 pos_, int len_, c4_Bytes& buffer_, bool forceCopy_);
00072     //: Returns pointer to data, use buffer only if non-contiguous.
00073   void StoreBytes(t4_i32 pos_, const c4_Bytes& buffer_);
00074     //: Stores a copy of the buffer in the column.
00075 
00076   bool RequiresMap() const;
00077   void ReleaseAllSegments();
00078 
00079   static t4_i32 PullValue(const t4_byte*& ptr_);
00080   static void PushValue(t4_byte*& ptr_, t4_i32 v_);
00081 
00082   void InsertData(t4_i32 index_, t4_i32 count_, bool clear_);
00083   void RemoveData(t4_i32 index_, t4_i32 count_);
00084   void RemoveGap();
00085 
00086   enum { kSegBits = 12, kSegMax = 1 << kSegBits, kSegMask = kSegMax - 1 };
00087 
00088 private:
00089   static int fSegIndex(t4_i32 offset_);
00090   static t4_i32 fSegOffset(int index_);
00091   static int fSegRest(t4_i32 offset_);
00092 
00093   bool UsesMap(const t4_byte*) const;
00094   bool IsMapped() const;
00095 
00096   void ReleaseSegment(int);
00097   void SetupSegments();
00098   void Validate() const;
00099   void FinishSlack();
00100 
00101   void MoveGapUp(t4_i32 pos_);
00102   void MoveGapDown(t4_i32 pos_);
00103   void MoveGapTo(t4_i32 pos_);
00104 
00105   t4_byte* CopyData(t4_i32, t4_i32, int);
00106 };
00107 
00109 
00110 class c4_ColOfInts : public c4_Column
00111 {
00112 public:
00113   c4_ColOfInts (c4_Persist* persist_, int width_ =sizeof (t4_i32));
00114 
00115   int RowCount() const;
00116   void SetRowCount(int numRows_);
00117 
00118   void FlipBytes();
00119   
00120   int ItemSize(int index_);
00121   const void* Get(int index_, int& length_);
00122   void Set(int index_, const c4_Bytes& buf_);
00123   
00124   t4_i32 GetInt(int index_);
00125   void SetInt(int index_, t4_i32 value_);
00126 
00127   void Insert(int index_, const c4_Bytes& buf_, int count_);
00128   void Remove(int index_, int count_);
00129 
00130   static int CalcAccessWidth(int numRows_, t4_i32 colSize_);
00131 
00132   void SetAccessWidth(int bits_);
00133   void FixSize(bool fudge_);
00134   void ForceFlip();
00135 
00136   static int DoCompare(const c4_Bytes& b1_, const c4_Bytes& b2_);
00137 
00138 private:
00139   typedef void (c4_ColOfInts::*tGetter) (int);
00140   typedef bool (c4_ColOfInts::*tSetter) (int, const t4_byte*);
00141 
00142   void Get_0b(int index_);
00143   void Get_1b(int index_);
00144   void Get_2b(int index_);
00145   void Get_4b(int index_);
00146   void Get_8i(int index_);
00147   void Get_16i(int index_);
00148   void Get_16r(int index_);
00149   void Get_32i(int index_);
00150   void Get_32r(int index_);
00151   void Get_64i(int index_);
00152   void Get_64r(int index_);
00153 
00154   bool Set_0b(int index_, const t4_byte* item_);
00155   bool Set_1b(int index_, const t4_byte* item_);
00156   bool Set_2b(int index_, const t4_byte* item_);
00157   bool Set_4b(int index_, const t4_byte* item_);
00158   bool Set_8i(int index_, const t4_byte* item_);
00159   bool Set_16i(int index_, const t4_byte* item_);
00160   bool Set_16r(int index_, const t4_byte* item_);
00161   bool Set_32i(int index_, const t4_byte* item_);
00162   bool Set_32r(int index_, const t4_byte* item_);
00163   bool Set_64i(int index_, const t4_byte* item_);
00164   bool Set_64r(int index_, const t4_byte* item_);
00165 
00166   void ResizeData(int index_, int count_, bool clear_ =false);
00167 
00168   tGetter _getter;
00169   tSetter _setter;
00170 
00171   union {
00172   t4_byte _item[8]; // holds temp result (careful with alignment!)
00173   double _aligner;  // needed for SPARC
00174   };
00175 
00176   int _currWidth;   // number of bits used for one entry (0..64)
00177   int _dataWidth;   // number of bytes used for passing a value along
00178   int _numRows;
00179   bool _mustFlip;
00180 };
00181 
00183 
00184 class c4_ColIter
00185 {
00186   c4_Column& _column;
00187   t4_i32 _limit;
00188   t4_i32 _pos;
00189   int _len;
00190   const t4_byte* _ptr;
00191 
00192 public:
00193   c4_ColIter (c4_Column& col_, t4_i32 offset_, t4_i32 limit_);
00194 //  ~c4_ColIter ();
00195 
00196   bool Next();
00197   bool Next(int max_);
00198 
00199   const t4_byte* BufLoad() const;
00200   t4_byte* BufSave();
00201   int BufLen() const;
00202 };
00203 
00205 
00206 #if q4_INLINE
00207 #include "column.inl"
00208 #endif
00209 
00211 
00212 #endif


Metakit C++ API Reference - https://www.equi4.com/metakit.html - extracted with Doxygen