1 /*************** Xindex H Declares Source Code File (.H) ***************/
2 /*  Name: XINDEX.H    Version 3.5                                      */
3 /*                                                                     */
4 /*  (C) Copyright to the author Olivier BERTRAND          2004 - 2015  */
5 /*                                                                     */
6 /*  This file contains the XINDEX class declares.                      */
7 /***********************************************************************/
8 #ifndef __XINDEX_H__
9 #define  __XINDEX_H__
10 #include "block.h"
11 #include "csort.h"            /* Base class declares                   */
12 #include "xtable.h"
13 #include "valblk.h"
14 #if defined(XMAP)
15 #include "maputil.h"
16 #endif   // XMAP
17 
18 enum IDT {TYPE_IDX_ERROR = 0,         /* Type not defined              */
19           TYPE_IDX_INDX  = 4,         /* Permanent standard index      */
20           TYPE_IDX_XROW  = 5};        /* Permanent row index           */
21 
22 #if defined(XMAP)
23 typedef        MEMMAP   *MMP;
24 #endif   // XMAP
25 typedef class  INDEXDEF *PIXDEF;
26 typedef class  KPARTDEF *PKPDEF;
27 typedef class  XINDEX   *PXINDEX;
28 typedef class  XLOAD    *PXLOAD;
29 typedef class  KXYCOL   *PXCOL;
30 
31 /***********************************************************************/
32 /*  Structures used when checking for possible indexing                */
33 /***********************************************************************/
34 typedef struct index_col *PICOL;
35 typedef struct index_val *PIVAL;
36 typedef struct index_def *PINDX;
37 typedef struct indx_used *PXUSED;
38 
39 typedef struct index_val : public BLOCK {
index_valindex_val40   index_val(PXOB xp) {Next = NULL; Xval = xp; Kp = NULL;}
41   PIVAL  Next;                    // Next value
42   PXOB   Xval;                    // To value or array
43   int   *Kp;                      // The coordonates in a LSTBLK
44   } IVAL;
45 
46 typedef struct index_col : public BLOCK {
index_colindex_col47   index_col(PCOL cp)
48     {Next = Nxtgrp = NULL; Colp = cp; Ngrp = N = 0; Vals = NULL;}
49   PICOL  Next;                    // Next column
50   PICOL  Nxtgrp;                  // Next group
51   PCOL   Colp;                    // The column
52   PIVAL  Vals;                    // To column values
53   int    Ngrp;                    // Group  number of values
54   int    N;                       // Column number of values
55   } ICOL;
56 
57 typedef struct index_def : public BLOCK {
index_defindex_def58   index_def(PIXDEF xdp)
59     {Next = NULL; Pxdf = xdp; Cols = NULL; Alloc = false;}
60   PINDX  Next;
61   PIXDEF Pxdf;
62   PICOL  Cols;
63   bool   Alloc;                   // Must allocate values
64   } INDX;
65 
66 typedef struct index_off {
67   union {
68 #if defined(WORDS_BIGENDIAN)
69     struct {int High; int Low;} v;
70 #else   // !WORDS_BIGENDIAN
71     struct {int Low; int High;} v;
72 #endif   //!WORDS_BIGENDIAN
73     longlong Val;                 // File position
74     }; // end of union
75   } IOFF;
76 
77 /***********************************************************************/
78 /*  Index definition block.                                            */
79 /***********************************************************************/
80 class DllExport INDEXDEF : public BLOCK { /* Index description block   */
81   friend class PLUGCAT;
82   friend class DOSDEF;
83   friend class ha_connect;
84   friend int PlgMakeIndex(PGLOBAL g, PSZ name, PIXDEF pxdf, bool add);
85  public:
86   // Constructor
87   INDEXDEF(char *name, bool uniq = false, int n = 0);
88 
89   // Implementation
GetNext(void)90   PIXDEF  GetNext(void) {return Next;}
SetNext(PIXDEF pxdf)91   void    SetNext(PIXDEF pxdf) {Next = pxdf;}
GetName(void)92   PSZ     GetName(void) {return (PSZ)Name;}
IsUnique(void)93   bool    IsUnique(void) {return Unique;}
IsDynamic(void)94   bool    IsDynamic(void) {return Dynamic;}
IsAuto(void)95   bool    IsAuto(void) {return AutoInc;}
IsValid(void)96   bool    IsValid(void) {return !Invalid;}
SetAuto(bool b)97   void    SetAuto(bool b) {AutoInc = b;}
SetInvalid(bool b)98   void    SetInvalid(bool b) {Invalid = b;}
GetNparts(void)99   int     GetNparts(void) {return Nparts;}
GetID(void)100   int     GetID(void) {return ID;}
SetID(int n)101   void    SetID(int n) {ID = n;}
GetToKeyParts(void)102   PKPDEF  GetToKeyParts(void) {return ToKeyParts;}
SetToKeyParts(PKPDEF kp)103   void    SetToKeyParts(PKPDEF kp) {ToKeyParts = kp;}
SetNParts(uint np)104   void    SetNParts(uint np) {Nparts = (signed)np;}
SetMaxSame(int mxs)105   void    SetMaxSame(int mxs) {MaxSame = mxs;}
106   void    SetMxsame(PXINDEX x);
GetMaxSame(void)107   int     GetMaxSame(void) {return MaxSame;}
108   bool    Define(PGLOBAL g, void *memp, PTABDEF dfp, LPCSTR p);
109   PIXDEF  GetIndexOf(PCOL colp, bool hd = false);
110   int     IsIndexOf(PCOL colp);
111   PKXBASE CheckIndexing(PGLOBAL g, PTDBDOS tdbp);
112   PINDX   CheckAND(PGLOBAL g, PINDX pix1, PINDX pix2);
113   PINDX   CheckOR(PGLOBAL g, PINDX pix1, PINDX pix2);
114   PINDX   CheckEQ(PGLOBAL g, PTDB tdbp, PXOB *arg, int op, int *kp = NULL);
115   bool    TestEQ(PGLOBAL g, PTDB tdbp, PXOB *arg, int op, bool b = false);
116 
117  protected:
118   PIXDEF  Next;               /* To next block                         */
119   PKPDEF  ToKeyParts;         /* To the key part definitions           */
120   char   *Name;               /* Index name                            */
121   bool    Unique;             /* true if defined as unique             */
122   bool    Invalid;            /* true if marked as Invalid             */
123   bool    AutoInc;            /* true if unique key in auto increment  */
124   bool    Dynamic;            /* KINDEX style                          */
125   bool    Mapped;             /* Use file mapping                      */
126   int     Nparts;             /* Number of key parts                   */
127   int     ID;                 /* Index ID number                       */
128   int     MaxSame;            /* Max number of same values             */
129   }; // end of INDEXDEF
130 
131 typedef struct indx_used : public BLOCK {
indx_usedindx_used132   indx_used(PTDB tp, PIXDEF xdp, PCOL *cp, int k)
133   {Tname = (char*)tp->GetName(); Xname = xdp->GetName(); Cp = cp; K = k;}
134   PXUSED Next;
135   char  *Tname;
136   PSZ    Xname;
137   PCOL  *Cp;
138   int    K;
139   } XUSED;
140 
141 /***********************************************************************/
142 /*  Index Key Part definition block.                                   */
143 /***********************************************************************/
144 class DllExport KPARTDEF : public BLOCK { /* Index Key Part desc block */
145   friend class INDEXDEF;
146   friend class XINDEX;
147   friend class PLUGCAT;
148   friend class DOSDEF;
149   friend class ha_connect;
150   friend int PlgMakeIndex(PGLOBAL g, PSZ name, PIXDEF pxdf, bool add);
151  public:
152   KPARTDEF(PSZ name, int n);       // Constructor
153 
154   // Implementation
GetNext(void)155   PKPDEF GetNext(void) {return Next;}
GetName(void)156   PSZ    GetName(void) {return (PSZ)Name;}
GetNcol(void)157   int    GetNcol(void) {return Ncol;}
SetNext(PKPDEF pkdf)158   void   SetNext(PKPDEF pkdf) {Next = pkdf;}
SetKlen(int len)159   void   SetKlen(int len) {Klen = len;}
SetMxsame(int mxs)160   void   SetMxsame(int mxs) {Mxsame = mxs;}
161 
162  protected:
163   PKPDEF Next;                /* To next block                         */
164   PSZ    Name;                /* Field name                            */
165   int    Mxsame;              /* Field max same values                 */
166   int    Ncol;                /* Field number                          */
167   int    Klen;                /* Key length                            */
168   }; // end of KPARTDEF
169 
170 /***********************************************************************/
171 /*  This is the XDB Index virtual base class declaration.              */
172 /***********************************************************************/
173 class DllExport XXBASE : public CSORT, public BLOCK {
174   friend class INDEXDEF;
175   friend class KXYCOL;
176  public:
177   // Constructor
178   XXBASE(PTDBDOS tbxp, bool b);
179 
180   // Implementation
181   virtual IDT  GetType(void) = 0;
182   virtual void Reset(void) = 0;
IsMul(void)183   virtual bool IsMul(void) {return false;}
IsRandom(void)184   virtual bool IsRandom(void) {return true;}
IsDynamic(void)185   virtual bool IsDynamic(void) {return Dynamic;}
SetDynamic(bool dyn)186   virtual void SetDynamic(bool dyn) {Dynamic = dyn;}
187 //virtual bool HaveSame(void) {return false;}
GetCurPos(void)188   virtual int  GetCurPos(void) {return Cur_K;}
SetNval(int n)189   virtual void SetNval(int n) {assert(n == 1);}
SetOp(OPVAL op)190   virtual void SetOp(OPVAL op) {Op = op;}
GetNdif(void)191           int  GetNdif(void) {return Ndif;}
GetNum_K(void)192           int  GetNum_K(void) {return Num_K;}
GetCur_K(void)193           int  GetCur_K(void) {return Cur_K;}
GetID(void)194           int  GetID(void) {return ID;}
SetID(int id)195           void SetID(int id) {ID = id;}
SetNth(int n)196           void SetNth(int n) {Nth = n;}
GetPof(void)197           int *GetPof(void) {return Pof;}
GetPex(void)198           int *GetPex(void) {return Pex;}
IsSorted(void)199           bool IsSorted(void) {return Srtd;}
FreeIndex(void)200           void FreeIndex(void) {PlgDBfree(Index);}
201 
202   // Methods
203   virtual void Printf(PGLOBAL g, FILE *f, uint n);
204   virtual void Prints(PGLOBAL g, char *ps, uint z);
205   virtual bool Init(PGLOBAL g) = 0;
206   virtual bool Make(PGLOBAL g, PIXDEF sxp) = 0;
207 #if defined(XMAP)
208   virtual bool MapInit(PGLOBAL g) = 0;
209 #endif   // XMAP
MaxRange(void)210   virtual int  MaxRange(void) {return 1;}
211   virtual int  Fetch(PGLOBAL g) = 0;
NextVal(bool)212   virtual bool NextVal(bool) {return true;}
PrevVal(void)213   virtual bool PrevVal(void) {return true;}
214   virtual int  FastFind(void) = 0;
Reorder(PGLOBAL)215   virtual bool Reorder(PGLOBAL) {return true;}
216   virtual int  Range(PGLOBAL, int = 0, bool = true) {return -1;} // Means error
217   virtual int  Qcompare(int *, int *) = 0;
GroupSize(void)218   virtual int  GroupSize(void) {return 1;}
219   virtual void Close(void) = 0;
220 
221  protected:
222   // Members
223   PTDBASE Tbxp;             // Points to calling table TDB
224   PXCOL   To_KeyCol;        // To KeyCol class list
225   MBLOCK  Record;           // Record allocation block
226   int*   &To_Rec;           // We are using ftell, fseek
227   int     Cur_K;            // Index of current record
228   int     Old_K;            // Index of last record
229   int     Num_K;            // Size of Rec_K pointer array
230   int     Ndif;             // Number of distinct values
231   int     Bot;              // Bottom of research index
232   int     Top;              // Top    of research index
233   int     Inf, Sup;         // Used for block optimization
234   OPVAL   Op;               // Search operator
235   bool    Mul;              // true if multiple
236   bool    Srtd;             // true for sorted column
237   bool    Dynamic;          // true when dynamically made
238   int     Val_K;            // Index of current value
239   int     Nblk;             // Number of blocks
240   int     Sblk;             // Block size
241   int     Thresh;           // Thresh for sorting join indexes
242   int     ID;               // Index ID number
243   int     Nth;              // Nth constant to fetch
244   }; // end of class XXBASE
245 
246 /***********************************************************************/
247 /*  This is the standard (multicolumn) Index class declaration.        */
248 /***********************************************************************/
249 class DllExport XINDEX : public XXBASE {
250   friend class KXYCOL;
251  public:
252   // Constructor
253   XINDEX(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp,
254                        PCOL *cp, PXOB *xp = NULL, int k = 0);
255 
256   // Implementation
GetType(void)257   virtual IDT  GetType(void) {return TYPE_IDX_INDX;}
IsMul(void)258   virtual bool IsMul(void) {return (Nval < Nk) ? true : Mul;}
259 //virtual bool HaveSame(void) {return Op == OP_SAME;}
GetCurPos(void)260   virtual int  GetCurPos(void) {return (Pex) ? Pex[Cur_K] : Cur_K;}
SetNval(int n)261   virtual void SetNval(int n) {Nval = n;}
GetMaxSame(void)262           int  GetMaxSame(void) {return MaxSame;}
263 
264   // Methods
265   virtual void Reset(void);
266   virtual bool Init(PGLOBAL g);
267 #if defined(XMAP)
268   virtual bool MapInit(PGLOBAL g);
269 #endif   // XMAP
270   virtual int  Qcompare(int *, int *);
271   virtual int  Fetch(PGLOBAL g);
272   virtual int  FastFind(void);
273   virtual int  GroupSize(void);
274   virtual int  Range(PGLOBAL g, int limit = 0, bool incl = true);
MaxRange(void)275   virtual int  MaxRange(void) {return MaxSame;}
276   virtual int  ColMaxSame(PXCOL kp);
277   virtual void Close(void);
278   virtual bool NextVal(bool eq);
279   virtual bool PrevVal(void);
280   virtual bool Make(PGLOBAL g, PIXDEF sxp);
281   virtual bool SaveIndex(PGLOBAL g, PIXDEF sxp);
282   virtual bool Reorder(PGLOBAL g);
283           bool GetAllSizes(PGLOBAL g,/* int &ndif,*/ int &numk);
284 
285  protected:
286           bool AddColumns(void);
287           bool NextValDif(void);
288 
289   // Members
290   PIXDEF  Xdp;              // To index definition
291   PTDBDOS Tdbp;             // Points to calling table TDB
292   PXLOAD  X;                // To XLOAD class
293   PXCOL   To_LastCol;       // To the last key part block
294   PXCOL   To_LastVal;       // To the last used key part block
295   PCOL   *To_Cols;          // To array of indexed columns
296   PXOB   *To_Vals;          // To array of column values
297   int     Nk;               // The number of indexed columns
298   int     Nval;             // The number of used columns
299   int     Incr;             // Increment of record position
300   int     MaxSame;          // Max number of same values
301   }; // end of class XINDEX
302 
303 /***********************************************************************/
304 /*  This is the fast single column index class declaration.            */
305 /***********************************************************************/
306 class DllExport XINDXS : public XINDEX {
307   friend class KXYCOL;
308  public:
309   // Constructor
310   XINDXS(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp = NULL);
311 
312   // Implementation
SetNval(int n)313   virtual void SetNval(int n) {assert(n == 1);}
314 
315   // Methods
316   virtual int  Qcompare(int *, int *);
317   virtual int  Fetch(PGLOBAL g);
318   virtual int  FastFind(void);
319   virtual bool NextVal(bool eq);
320   virtual bool PrevVal(void);
321   virtual int  Range(PGLOBAL g, int limit = 0, bool incl = true);
322   virtual int  GroupSize(void);
323 
324  protected:
325   // Members
326   }; // end of class XINDXS
327 
328 /***********************************************************************/
329 /*  This is the saving/loading index utility base class.               */
330 /***********************************************************************/
331 class DllExport XLOAD : public BLOCK {
332   friend class XINDEX;
333   friend class XBIGEX;
334   friend class XBIGXS;
335  public:
336   // Constructor
337   XLOAD(void);
338 
339   // Methods
340   virtual bool  Open(PGLOBAL g, char *filename, int id, MODE mode) = 0;
341   virtual bool  Seek(PGLOBAL g, int low, int high, int origin) = 0;
342   virtual bool  Read(PGLOBAL g, void *buf, int n, int size) = 0;
343   virtual int   Write(PGLOBAL g, void *buf, int n,
344                                             int size, bool& rc) = 0;
345   virtual void  Close(char *fn, int id) = 0;
346   virtual void  Close(void);
347 #if defined(XMAP)
348   virtual void *FileView(PGLOBAL g, char *fn) = 0;
349 #endif   // XMAP
350 
351  protected:
352   // Members
353 #if defined(_WIN32)
354   HANDLE  Hfile;                // Handle to file or map
355 #else    // UNIX
356   int     Hfile;                // Descriptor to file or map
357 #endif   // UNIX
358   IOFF    NewOff;               // New offset
359   }; // end of class XLOAD
360 
361 /***********************************************************************/
362 /*  This is the saving/loading indexes utility class.                  */
363 /***********************************************************************/
364 class DllExport XFILE : public XLOAD {
365  public:
366   // Constructor
367   XFILE(void);
368 
369   // Methods
370   virtual bool  Open(PGLOBAL g, char *filename, int id, MODE mode);
371   virtual bool  Seek(PGLOBAL g, int low, int high, int origin);
372   virtual bool  Read(PGLOBAL g, void *buf, int n, int size);
373   virtual int   Write(PGLOBAL g, void *buf, int n, int size, bool& rc);
374   virtual void  Close(char *fn, int id);
375   virtual void  Close(void);
376 #if defined(XMAP)
377   virtual void *FileView(PGLOBAL g, char *fn);
378 #endif   // XMAP
379 
380  protected:
381   // Members
382   FILE   *Xfile;                // Index stream file
383 #if defined(XMAP)
384   MMP     Mmp;                  // Mapped view base address and length
385 #endif   // XMAP
386   }; // end of class XFILE
387 
388 /***********************************************************************/
389 /*  This is the saving/loading huge indexes utility class.             */
390 /***********************************************************************/
391 class DllExport XHUGE : public XLOAD {
392  public:
393   // Constructor
XHUGE(void)394   XHUGE(void) : XLOAD() {}
395 
396   // Methods
397   using XLOAD::Close;
398   virtual bool  Open(PGLOBAL g, char *filename, int id, MODE mode);
399   virtual bool  Seek(PGLOBAL g, int low, int high, int origin);
400   virtual bool  Read(PGLOBAL g, void *buf, int n, int size);
401   virtual int   Write(PGLOBAL g, void *buf, int n, int size, bool& rc);
402   virtual void  Close(char *fn, int id);
403 #if defined(XMAP)
404   virtual void *FileView(PGLOBAL g, char *fn);
405 #endif   // XMAP
406 
407  protected:
408   // Members
409   }; // end of class XHUGE
410 
411 /***********************************************************************/
412 /*  This is the XDB index for columns containing ROWID values.         */
413 /***********************************************************************/
414 class DllExport XXROW : public XXBASE {
415   friend class KXYCOL;
416  public:
417   // Constructor
418   XXROW(PTDBDOS tbxp);
419 
420   // Implementation
GetType(void)421   virtual IDT  GetType(void) {return TYPE_IDX_XROW;}
422   virtual void Reset(void);
423 
424   // Methods
425   virtual bool Init(PGLOBAL g);
426 #if defined(XMAP)
MapInit(PGLOBAL)427   virtual bool MapInit(PGLOBAL) {return true;}
428 #endif   // XMAP
429   virtual int  Fetch(PGLOBAL g);
430   virtual int  FastFind(void);
MaxRange(void)431   virtual int  MaxRange(void) {return 1;}
432   virtual int  Range(PGLOBAL g, int limit = 0, bool incl = true);
Qcompare(int *,int *)433   virtual int  Qcompare(int *, int *) {assert(false); return 0;}
Make(PGLOBAL,PIXDEF)434   virtual bool Make(PGLOBAL, PIXDEF) {return false;}
Close(void)435   virtual void Close(void) {}
436 
437  protected:
438   // Members
439   PTDBDOS Tdbp;             // Points to calling table TDB
440   PVAL    Valp;             // The value to match in index
441   }; // end of class XXROW
442 
443 /***********************************************************************/
444 /*  Definition of class KXYCOL used to store values of indexed columns */
445 /***********************************************************************/
446 class KXYCOL: public BLOCK {
447   friend class INDEXDEF;
448   friend class XINDEX;
449   friend class XINDXS;
450   friend class XBIGEX;
451   friend class XBIGXS;
452   friend class TDBDOS;
453  public:
454   // Constructors
455   KXYCOL(PKXBASE kp);
456 
457   // Implementation
GetType(void)458   int  GetType(void) {return Type;}
459   void SetValue(PCOL colp, int i);
460 
461  public:
462   // Methods
463   virtual bool Init(PGLOBAL g, PCOL colp, int n, bool sm, int kln);
464   virtual bool InitFind(PGLOBAL g, PXOB xp);
465   virtual void ReAlloc(PGLOBAL g, int n);
466   virtual void FreeData(void);
467   virtual void FillValue(PVAL valp);
468   virtual int  CompVal(int i);
469 //        void InitBinFind(void *vp);
470           bool MakeBlockArray(PGLOBAL g, int nb, int size);
471           int  Compare(int i1, int i2);
472           int  CompBval(int i);
Save(int i)473           void Save(int i) {Valp->SetBinValue(Kblp->GetValPtr(i));}
Restore(int j)474           void Restore(int j) {Kblp->SetValue(Valp, j);}
Move(int j,int k)475           void Move(int j, int k) {Kblp->Move(k, j);}
476 
477   // Specific functions
478 #if defined(XMAP)
479           BYTE *MapInit(PGLOBAL g, PCOL colp, int *n, BYTE *m);
480 #endif   // XMAP
481           int *MakeOffset(PGLOBAL g, int n);
482 
483  protected:
484   // Members
485   PXCOL   Next;            // To next in the key part list
486   PXCOL   Previous;        // To previous in the key part list
487   PKXBASE Kxp;             // To the INDEX class block
488   PCOL    Colp;            // To matching object if a column
489   bool    IsSorted;        // true if column is already sorted
490   bool    Asc;             // true for ascending sort, false for Desc
491   MBLOCK  Keys;            // Data array allocation block
492   void*  &To_Keys;         // To data array
493   PVBLK   Kblp;            // To Valblock of the data array
494   MBLOCK  Bkeys;           // Block array allocation block
495   void*  &To_Bkeys;        // To block array
496   PVBLK   Blkp;            // To Valblock of the block array
497   PVAL    Valp;            // Value use by Find
498   int     Klen;            // Length of character string or num value
499   int     Kprec;           // The Value(s) precision or CI
500   int     Type;            // The Value(s) type
501   bool    Prefix;          // Key on CHAR column prefix
502   MBLOCK  Koff;            // Offset allocation block
503   CPINT  &Kof;             // Reference to offset array
504   int     Val_K;           // Index of current column value
505   int     Ndf;             // Number of stored values
506   int     Mxs;             // Max same for this column
507   }; // end of class KXYCOL
508 
509 #endif // __XINDEX_H__
510