summaryrefslogtreecommitdiffstats
path: root/kbabel/kbabeldict/modules/dbsearchengine/database.h
blob: 6697683775280def4f5eb2a8439a98c87827fd1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***************************************************************************
                          database.h  - 
                             -------------------
    begin                : Fri Sep 8 2000
    copyright            : (C) 2000 by Andrea Rizzi
    email                : rizzi@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *                                                                         *
 *   In addition, as a special exception, the copyright holders give       *
 *   permission to link the code of this program with any edition of       *
 *   the TQt library by Trolltech AS, Norway (or with modified versions     *
 *   of TQt that use the same license as TQt), and distribute linked         *
 *   combinations including the two.  You must obey the GNU General        *
 *   Public License in all respects for all of the code used other than    *
 *   TQt. If you modify this file, you may extend this exception to         *
 *   your version of the file, but you are not obligated to do so.  If     *
 *   you do not wish to do so, delete this exception statement from        *
 *   your version.                                                         *
 ***************************************************************************/
/*
 Translation search engine  
 
 
  Copyright  2000
  Andrea Rizzi rizzi@kde.org
 
*/


#ifndef _DATABASE_H_
#define _DATABASE_H_

#include <config.h>

#ifdef USE_DB_H_PATH
#include USE_DB_H_PATH
#else
#ifdef HAVE_DB4_DB_H
#include <db4/db.h>
#else
#include <db.h>
#endif
#endif

#define uint32 u_int32_t

#include <tqvaluelist.h>
#include <tqptrlist.h>
#include <tqstring.h>
#include <tqobject.h>
#include <tqdatetime.h>
#include <tqcstring.h>  //bytearray


class WordItem   // Returned by getWordLocations
{
 public:
  
  WordItem(char *data,TQString w);
  WordItem(TQString w);
  //WordItem(const WordItem &wi);
  //WordItem& operator=(const WordItem & wi );
       	 
  //The word (key in database)
  TQString word;
  //Sorted locations
  //TQByteArray locations;  //too many problems with this..
  //NOTE:
  //This is allocated only if you call WordItem(char *data,TQString w);
  //YOU MUST FREE IT when you do not need it anymore
  //No destructor will do it !!!

  uint32 *locations; 
  
  
  uint32 count;
  //Is this word common ? 
  int score;
  
  bool notFound();
};


class InfoItem
{
 public:

  //Create the NO INFO item 
  InfoItem();
  
  // Create an info item from raw data
  InfoItem(const char *rawData,TQString lang);

  TQString catalogName;  
  TQString lastFullPath;   

  TQString lastTranslator;
  TQDateTime revisionDate;
  TQString charset;
  TQString language;

  
  int size();
  void rawData(char *);
  
};

class TranslationItem
{
 public:
  TQString translation;
  TQValueList<int> infoRef;
  uint32 numRef;
 
};

class DataBaseItem 
{
 public:
  /*
    Create empty class;
  */
  DataBaseItem();
 
  /*
    Create a DataBaseItem from raw data
  */
  DataBaseItem(char *_key,char *_data);

  /*
    return the size (in raw data) of this item.
  */
  uint32 sizeData();
  uint32 sizeKey();
 
  bool isNull() { return (numTra==0); }
 
  /*
    You MUST allocate data of sizeData() byte.
  */
  void toRawData(char *_data);
  void toRawKey(char *_key);

  TQString key;
  TQValueList<TranslationItem> translations;
  uint32 numTra;
  uint32 location; 
};

class DataBaseManager : public TQObject
{

  TQ_OBJECT
  

    public:
  /*
    Main constructor.
    directory is the name of the directory where the database structre
    is stored.
   
    DIRECTORY/translations.$LANG.db       The trasnaltion archive 
    DIRECTORY/catalogsinfo.$LANG.db      Info about catalogs

    //Not yet implemented
    DIRECTORY/wordsindex.$LANG.db      Index of words
     
   
  */
 
  DataBaseManager(TQString directory,TQString lang,TQObject *parent=0,const char *name=0);
  ~DataBaseManager();
  /*
    Create a new databse structure.
   
  */
  int createDataBase(TQString directory,TQString language,int mode=0664);




  /*
    Put a DataBaseItem into the database.
  
    if ow==false enter the new DataBaseItem only
    if the key of DataBaseItem does not previously exist  

    It also update the wordIndex.

  */
 
  int putItem(DataBaseItem *item,bool ow=false);

  DataBaseItem getItem(TQString key);

  /*
    @return the first entry in the database.
  */
 
  DataBaseItem firstItem(); 
 
  /*
    @return the current entry of the database.
  */
 
  DataBaseItem currentItem(); 

  /*
    @return the next entry in the database.
  */

  DataBaseItem nextItem(); 
 

  /*
   * Add a new translation to the database
   * catalog is a valid catalog refnum (use catalogRef to get one)
   * if ow is true a translation of a key coming from catalog is
   * overwritten if you provide a new translation
   */

  int putNewTranslation(TQString key,TQString tran,int catalog,bool ow=true);
  
  /*
    @return info about catalog n
  */
 
  InfoItem getCatalogInfo(int n);
 
  /*
    Add an entry to catalogsinfo database and
    @return a refnum for the new added item 
  */
 
  int addCatalogInfo(InfoItem *catInfo,int);
 
  /*
    Search an Item with the same "location" and 
    @return its refnum.
  */

  int searchCatalogInfo(TQString location);
 
  /* Get a catalog info for location,
   * if it doesn't exist it will create one.
   * @return the refnum
   */

  int catalogRef(TQString location,TQString author,TQString path);
   
  /*
    Put at refnum the catInfo
    @return true if everything is OK
  */

  bool putCatalogInfo(int refnum, InfoItem *catInfo); 

  /*
    Get word info 
  */
  WordItem getWordLocations(TQString word);
 
  /*
    Add a location for word
  */
  bool addLocation(TQString word, unsigned int location);

  /*
    Remove location for word
  */
  bool removeLocation(TQString word, int location);


  /*
   * Rebuild location and word indexes
   */
  void rebuildIndexes();
  
  uint32 appendKey(class TQString);
 
  TQString getKey(uint32 n);

  /*
    Load the catalogs info.
  */
  void loadInfo();


  void sync();

  bool isOk();
  int count();
  int current();

  void openDataBase();
  void closeDataBase();

  static  TQValueList<TQString> wordsIn(TQString string);

 signals:

  void cannotOpenDB(int);


 protected:
  DataBaseItem cursorGet(uint32 flags); 
 
  TQString language;
  TQString basedir;
  TQValueList<InfoItem> info; 
 
  DB *db,*infoDb,*wordDb,*indexDb;
  DBC *cursor;
  bool iAmOk; 
  bool indexOk;  //Database could word without word index
};





#endif