Xbase64 Class Library  3.1.2
xbndx.h
Go to the documentation of this file.
1 /* xbndx.h
2 
3  Xbase64 project source code
4 
5  This file contains a header file for the xbNdx object, which is used
6  for handling NDX type indices.
7 
8  Copyright (C) 1997,2003 Gary A Kunkel
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 
24  Contact:
25 
26  Email:
27 
28  xdb-devel@lists.sourceforge.net
29  xdb-users@lists.sourceforge.net
30 
31 
32  Regular Mail:
33 
34  XBase Support
35  149C South Main St
36  Keller Texas, 76248
37  USA
38 
39 */
40 
41 #ifndef __XB_NDX_H__
42 #define __XB_NDX_H__
43 
44 #ifdef __GNU_LesserG__
45 #pragma interface
46 #endif
47 
48 #include <xbase64/xbase64.h>
49 #include <string.h>
50 
54 //
55 // Define the following to use inline versions of the respective methods.
56 //
57 #define XB_INLINE_GETDBFNO
58 
59 #define XB_NDX_NODE_BASESIZE 24 // size of base header data
60 
61 #define XB_VAR_NODESIZE // define to enable variable node sizes
62 
63 #ifndef XB_VAR_NODESIZE
64 #define XB_NDX_NODE_SIZE 2048
65 //#define XB_NDX_NODE_SIZE 512 // standard dbase node size
66 #else
67 #define XB_DEFAULT_NDX_NODE_SIZE 512
68 #define XB_MAX_NDX_NODE_SIZE 4096
69 #define XB_NDX_NODE_SIZE NodeSize
70 #define XB_NDX_NODE_MULTIPLE 512
71 #endif // XB_VAR_NODESIZE
72 
74 
77 struct XBDLLEXPORT xbNdxHeadNode { /* ndx header on disk */
78  xbLong StartNode; /* header node is node 0 */
79  xbLong TotalNodes; /* includes header node */
80  xbLong NoOfKeys; /* actual count + 1 */
81  /* not updated by borland dbe? */
82  xbUShort KeyLen; /* length of key data */
84  xbUShort KeyType; /* 00 = Char, 01 = Numeric */
85  xbLong KeySize; /* key len + 8 bytes */
86  char Unknown2;
87  char Unique;
88 // char KeyExpression[488];
89 #ifndef XB_VAR_NODESIZE
90  char KeyExpression[XB_NDX_NODE_SIZE - 24];
91 #else
92  char KeyExpression[XB_MAX_NDX_NODE_SIZE - 24];
93 #endif // XB_VAR_NODESIZE
94 };
95 
97 
100 struct XBDLLEXPORT xbNdxLeafNode { /* ndx node on disk */
102 #ifndef XB_VAR_NODESIZE
103  char KeyRecs[XB_NDX_NODE_SIZE-4];
104 #else
105  char KeyRecs[XB_MAX_NDX_NODE_SIZE - 4];
106 #endif // XB_VAR_NODESIZE
107 };
108 
110 
113 struct XBDLLEXPORT xbNdxNodeLink { /* ndx node memory */
116  xbLong CurKeyNo; /* 0 - KeysPerNode-1 */
117  xbLong NodeNo;
118  struct xbNdxLeafNode Leaf;
119 };
120 
122 
125 class XBDLLEXPORT xbNdx : public xbIndex
126 {
127  public:
128  xbNdx();
130  virtual ~xbNdx();
131 
132 /* don't uncomment next line - it causes seg faults for some undiagnosed reason*/
133 // ~NDX() { if( NdxStatus ) CloseIndex(); }
134 
135  xbShort CreateIndex( const char *IxName, const char *Exp,
136  xbShort Unique, xbShort OverLay );
138  xbULong GetCurDbfRec() { return CurDbfRec; }
140  xbShort GetCurrentKey(char *key);
142  xbShort UniqueIndex() { return HeadNode.Unique; }
145  xbShort FindKey( const char *Key );
148 #ifdef XBASE_DEBUG
149  void DumpHdrNode( xbShort Option );
150  void DumpNodeRec( xbLong NodeNo );
151  void DumpNodeChain();
152  xbShort CheckIndexIntegrity( xbShort Option );
153 #endif
155 
157  xbShort GetNextKey() { return GetNextKey( 1 ); }
159 
161  xbShort GetLastKey() { return GetLastKey( 0, 1 ); }
163 
165  xbShort GetFirstKey() { return GetFirstKey( 1 ); }
167 
169  xbShort GetPrevKey() { return GetPrevKey( 1 ); }
170  xbShort ReIndex(void (*statusFunc)(xbLong itemNum, xbLong numItems) = 0);
171  xbShort KeyExists( const char * Key ) { return FindKey( Key, strlen( Key ), 0 ); }
173 
174  virtual void SetNodeSize(xbShort size);
175 
176  virtual void GetExpression(char *buf, int len);
177  virtual const char* GetExtWithDot(bool lower);
178 
179  protected:
180  virtual xbUShort GetKeyLen();
181  virtual const char* GetKeyExpression();
182  virtual void FreeNodesMemory();
183 
184  protected:
189 
190 #ifndef XB_VAR_NODESIZE
191  char Node[XB_NDX_NODE_SIZE];
192 #else
194 #endif // XB_VAR_NODESIZE
195 
196  xbNdxNodeLink * NodeChain; /* pointer to node chain of index nodes */
197  xbNdxNodeLink * FreeNodeChain; /* pointer to chain of free index nodes */
198  xbNdxNodeLink * CurNode; /* pointer to current node */
199  xbNdxNodeLink * DeleteChain; /* pointer to chain to delete */
200 // xbNdxNodeLink * CloneChain; /* pointer to node chain copy (add dup) */
201 
202 /* private functions */
204 
205 
206  // in line functions for performance reasons
208 
210  inline xbShort CompareKey( const char *Key1, const char *Key2, xbShort Klen )
211  {
212  xbDouble d1, d2;
213  int c;
214 
215  if(!( Key1 && Key2 )) return -1;
216 
217  if( Klen > HeadNode.KeyLen ) Klen = HeadNode.KeyLen;
218 
219  if( HeadNode.KeyType == 0 )
220  {
221  c = memcmp(Key1, Key2, Klen);
222  if(c < 0)
223  return 2;
224  else if(c > 0)
225  return 1;
226  return 0;
227  }
228  else /* key is numeric */
229  {
230  d1 = dbf->xbase->GetDouble( Key1 );
231  d2 = dbf->xbase->GetDouble( Key2 );
232  if( d1 == d2 ) return 0;
233  else if( d1 > d2 ) return 1;
234  else return 2;
235  }
236  }
237 
238 #ifndef XB_INLINE_GETDBFNO
239  xbLong GetDbfNo( xbShort, xbNdxNodeLink * );
240 #else
242 
244  inline xbLong GetDbfNo( xbShort RecNo, xbNdxNodeLink *n )
245  {
246  xbNdxLeafNode *temp;
247  char *p;
248  if( !n ) return 0L;
249  temp = &n->Leaf;
250  if( RecNo < 0 || RecNo > ( temp->NoOfKeysThisNode - 1 )) return 0L;
251  p = temp->KeyRecs + 4;
252  p += RecNo * ( 8 + HeadNode.KeyLen );
253  return( dbf->xbase->GetLong( p ));
254  }
255 #endif
258  virtual xbShort GetHeadNode();
261  void ReleaseNodeMemory(xbNdxNodeLink *n, xbBool doFree = false);
262  xbShort BSearchNode(const char *key, xbShort klen,
263  const xbNdxNodeLink *node, xbShort *comp);
264  xbLong GetLeafFromInteriorNode( const char *Tkey, xbShort Klen );
277  xbShort FindKey( const char *Tkey, xbShort Klen, xbShort RetrieveSw );
290  xbShort FindKey( const char *Tkey, xbLong DbfRec ); /* for a specific dbf no */
291 };
292 #endif /* __XB_NDX_H__ */
xbMH struct
Definition: xbdbf.h:190
xbIndex class
Definition: xbindex.h:63
xbNdx class
Definition: xbndx.h:126
virtual void GetExpression(char *buf, int len)
xbShort UpdateParentKey(xbNdxNodeLink *)
xbShort SplitLeafNode(xbNdxNodeLink *, xbNdxNodeLink *, xbShort, xbLong)
virtual xbShort GetHeadNode()
xbShort GetFirstKey()
Short description.
Definition: xbndx.h:165
xbShort PutLeafNode(xbLong, xbNdxNodeLink *)
xbLong ReusedxbNodeLinks
Definition: xbndx.h:188
xbShort RemoveFromIxList()
virtual const char * GetExtWithDot(bool lower)
xbLong GetTotalNodes()
char * GetKeyData(xbShort, xbNdxNodeLink *)
xbShort PutHeadNode(xbNdxHeadNode *, FILE *, xbShort)
xbShort KeyExists(const char *Key)
Definition: xbndx.h:171
xbShort FindKey(const char *Tkey, xbLong DbfRec)
xbShort GetLeafNode(xbLong, xbShort)
xbShort BSearchNode(const char *key, xbShort klen, const xbNdxNodeLink *node, xbShort *comp)
xbULong GetCurDbfRec()
Definition: xbndx.h:138
xbLong GetLeftNodeNo(xbShort, xbNdxNodeLink *)
xbShort CalcKeyLen()
void ReleaseNodeMemory(xbNdxNodeLink *n, xbBool doFree=false)
virtual const char * GetKeyExpression()
xbNdxNodeLink * NodeChain
Definition: xbndx.h:196
xbShort FindKey()
xbShort CreateKey(xbShort, xbShort)
xbShort FindKey(const char *Tkey, xbShort Klen, xbShort RetrieveSw)
xbLong GetDbfNo(xbShort RecNo, xbNdxNodeLink *n)
Short description.
Definition: xbndx.h:244
xbShort PutKeyData(xbShort, xbNdxNodeLink *)
xbUShort GetKeysPerNode()
xbShort KeyExists(xbDouble)
xbShort GetNextKey(xbShort)
xbShort GetPrevKey()
Short description.
Definition: xbndx.h:169
virtual void SetNodeSize(xbShort size)
xbNdxNodeLink * FreeNodeChain
Definition: xbndx.h:197
xbShort MoveToRightNode(xbNdxNodeLink *, xbNdxNodeLink *)
virtual void FreeNodesMemory()
xbLong GetLeafFromInteriorNode(const char *Tkey, xbShort Klen)
xbShort UniqueIndex()
Definition: xbndx.h:142
xbNdxNodeLink * CurNode
Definition: xbndx.h:198
void UpdateDeleteList(xbNdxNodeLink *)
xbShort FindKey(const char *Key)
xbShort GetNextKey()
Short description.
Definition: xbndx.h:157
xbNdxNodeLink * DeleteChain
Definition: xbndx.h:199
xbShort PutKeyInNode(xbNdxNodeLink *, xbShort, xbLong, xbLong, xbShort)
xbNdxNodeLink * RightSiblingHasSpace(xbNdxNodeLink *)
xbShort CreateIndex(const char *IxName, const char *Exp, xbShort Unique, xbShort OverLay)
xbShort MoveToLeftNode(xbNdxNodeLink *, xbNdxNodeLink *)
xbShort DeleteKey(xbLong)
xbShort SplitINode(xbNdxNodeLink *, xbNdxNodeLink *, xbLong)
xbLong xbNodeLinkCtr
Definition: xbndx.h:187
xbNdxLeafNode LeafNode
Definition: xbndx.h:186
xbShort PutLeftNodeNo(xbShort, xbNdxNodeLink *, xbLong)
xbShort GetFirstKey(xbShort)
xbShort FindKey(xbDouble)
xbShort GetCurrentKey(char *key)
xbShort GetPrevKey(xbShort)
xbNdx(xbDbf *)
xbNdxNodeLink * GetNodeMemory()
xbShort DeleteSibling(xbNdxNodeLink *)
xbShort AddToIxList()
virtual xbUShort GetKeyLen()
void ProcessDeleteList()
xbShort AddKey(xbLong)
xbShort GetLastKey(xbLong, xbShort)
xbNdxNodeLink * LeftSiblingHasSpace(xbNdxNodeLink *)
xbShort CompareKey(const char *Key1, const char *Key2, xbShort Klen)
Short description.
Definition: xbndx.h:210
xbShort RemoveKeyFromNode(xbShort, xbNdxNodeLink *)
xbShort KeyWasChanged()
xbShort PutDbfNo(xbShort, xbNdxNodeLink *, xbLong)
xbNdxHeadNode HeadNode
Definition: xbndx.h:185
xbShort GetLastKey()
Short description.
Definition: xbndx.h:161
virtual ~xbNdx()
xbShort ReIndex(void(*statusFunc)(xbLong itemNum, xbLong numItems)=0)
xbNdxHeadnode struct
Definition: xbndx.h:77
xbLong TotalNodes
Definition: xbndx.h:79
xbUShort KeysPerNode
Definition: xbndx.h:83
xbLong NoOfKeys
Definition: xbndx.h:80
xbLong KeySize
Definition: xbndx.h:85
xbLong StartNode
Definition: xbndx.h:78
xbUShort KeyLen
Definition: xbndx.h:82
char Unknown2
Definition: xbndx.h:86
char Unique
Definition: xbndx.h:87
xbUShort KeyType
Definition: xbndx.h:84
xbNdxLeafNode struct
Definition: xbndx.h:100
xbLong NoOfKeysThisNode
Definition: xbndx.h:101
char KeyRecs[XB_MAX_NDX_NODE_SIZE - 4]
Definition: xbndx.h:105
#define XBDLLEXPORT
Definition: xbase64.h:86
#define XB_MAX_NDX_NODE_SIZE
Definition: xbndx.h:68
#define XB_NDX_NODE_SIZE
Definition: xbndx.h:69
long xbLong
Definition: xbtypes.h:61
short int xbBool
xbBool type
Definition: xbtypes.h:78
short int xbShort
xbShort type
Definition: xbtypes.h:60
unsigned short int xbUShort
xbUShort type
Definition: xbtypes.h:55
double xbDouble
xbDouble type
Definition: xbtypes.h:73
unsigned long int xbULong
xbULong type
Definition: xbtypes.h:50