• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • TDevelop Catalog Library
 

TDevelop Catalog Library

  • lib
  • catalog
catalog.cpp
1 /* This file is part of TDevelop
2  Copyright (C) 2003 Roberto Raggi <roberto@kdevelop.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "catalog.h"
20 #include <tqdir.h>
21 #include <tqfile.h>
22 #include <tqfileinfo.h>
23 #include <tqdatastream.h>
24 
25 #include <krandomsequence.h>
26 #include <kdebug.h>
27 
28 #include <cstring>
29 #include <cstdlib>
30 
31 #include <config.h>
32 
33 #ifdef USE_DB_H_PATH
34 #include USE_DB_H_PATH
35 #else
36 #include <db.h>
37 #endif
38 
39 struct _Catalog_Private
40 {
41  TQString dbName;
42 
43  DB* dbp;
44  TQMap<TQCString, DB*> indexList;
45  KRandomSequence rnd;
46  bool enabled;
47 
48  _Catalog_Private()
49  : dbp( 0 ), enabled( true )
50  {
51  }
52 
53  bool hasIndex( const TQCString& name ) const
54  {
55  return indexList.contains( name );
56  }
57 
58  DB* index( const TQCString& name )
59  {
60  return indexList[ name ];
61  }
62 
63  bool addItem( DB* dbp, const TQCString& id, const Tag& tag )
64  {
65  Q_ASSERT( dbp != 0 );
66 
67  DBT key, data;
68  int ret;
69 
70  std::memset( &key, 0, sizeof(key) );
71  std::memset( &data, 0, sizeof(data) );
72 
73  TQByteArray a1;
74  {
75  TQDataStream stream( a1, IO_WriteOnly );
76  stream << id;
77  key.data = a1.data();
78  key.size = a1.size();
79  }
80 
81  TQByteArray a2;
82  {
83  TQDataStream stream( a2, IO_WriteOnly );
84  tag.store( stream );
85  data.data = a2.data();
86  data.size = a2.size();
87  }
88 
89  ret = dbp->put( dbp, 0, &key, &data, 0 );
90 
91  return ret == 0;
92  }
93 
94  bool addItem( DB* dbp, const TQVariant& id, const TQCString& v )
95  {
96  Q_ASSERT( dbp != 0 );
97 
98  DBT key, data;
99  int ret;
100 
101  std::memset( &key, 0, sizeof(key) );
102  std::memset( &data, 0, sizeof(data) );
103 
104  TQByteArray a1;
105  {
106  TQDataStream stream( a1, IO_WriteOnly );
107  stream << id;
108  key.data = a1.data();
109  key.size = a1.size();
110  }
111 
112  TQByteArray a2;
113  {
114  TQDataStream stream( a2, IO_WriteOnly );
115  stream << v;
116  data.data = a2.data();
117  data.size = a2.size();
118  }
119 
120  ret = dbp->put( dbp, 0, &key, &data, 0 );
121 
122  return ret == 0;
123  }
124 
125 };
126 
127 
131  Catalog::Catalog()
132  : d( new _Catalog_Private() )
133 {
134 }
135 
139  Catalog::~Catalog()
140 {
141  close();
142  delete( d );
143  d = 0;
144 }
145 
149  TQValueList<TQCString> Catalog::indexList() const
150 {
151  TQValueList<TQCString> l;
152  TQMap<TQCString, DB*>::Iterator it = d->indexList.begin();
153  while( it != d->indexList.end() ){
154  l << it.key();
155  ++it;
156  }
157 
158  return l;
159 }
160 
161 bool Catalog::enabled() const
162 {
163  return d->enabled;
164 }
165 
166 void Catalog::setEnabled( bool isEnabled )
167 {
168  d->enabled = isEnabled;
169 }
170 
175  void Catalog::addIndex( const TQCString& name )
176 {
177  Q_ASSERT( d->dbp != 0 );
178 
179  TQMap<TQCString, DB*>::Iterator it = d->indexList.find( name );
180  if( it == d->indexList.end() ){
181  DB* dbp = 0;
182 
183  int ret;
184 
185  if ((ret = db_create(&dbp, 0, 0)) != 0) {
186  kdDebug() << "db_create: " << db_strerror(ret) << endl;
187  return /*false*/;
188  }
189 
190  if ((ret = dbp->set_flags(dbp, DB_DUP | DB_DUPSORT)) != 0) {
191  dbp->err(dbp, ret, "set_flags: DB_DUP | DB_DUPSORT");
192  dbp->close( dbp, 0 );
193  return;
194  }
195 
196  TQFileInfo fileInfo( d->dbName );
197  TQString indexName = fileInfo.dirPath(true) + "/" + fileInfo.baseName(true) + "." + TQString(name) + ".idx";
198 
199  if( (ret = dbp->set_cachesize( dbp, 0, 2 * 1024 * 1024, 0 )) != 0 ){
200  kdDebug() << "set_cachesize: " << db_strerror(ret) << endl;
201  }
202 
203  if ((ret = dbp->open(
204  dbp, NULL, TQFile::encodeName( indexName ).data(), 0, DB_BTREE, DB_CREATE, 0664)) != 0) {
205  kdDebug() << "db_open: " << db_strerror(ret) << endl;
206  dbp->close( dbp, 0 );
207  return;
208  }
209 
210  d->indexList[ name ] = dbp;
211  }
212 }
213 
218  void Catalog::close()
219 {
220  d->dbName = TQString();
221 
222  TQMap<TQCString, DB*>::Iterator it = d->indexList.begin();
223  while( it != d->indexList.end() ){
224  if( it.data() ){
225  it.data()->close( it.data(), 0 );
226  }
227  ++it;
228  }
229  d->indexList.clear();
230 
231  if( d->dbp != 0 ){
232  d->dbp->close( d->dbp, 0 );
233  d->dbp = 0;
234  }
235 }
236 
241  void Catalog::open( const TQString& dbName )
242 {
243  Q_ASSERT( d->dbp == 0 );
244 
245  d->dbName = dbName;
246 
247  int ret;
248 
249  if ((ret = db_create(&d->dbp, 0, 0)) != 0) {
250  kdDebug() << "db_create: " << db_strerror(ret) << endl;
251  return /*false*/;
252  }
253 
254  if ((ret = d->dbp->set_flags(d->dbp, DB_RECNUM)) != 0) {
255  d->dbp->err(d->dbp, ret, "set_flags: DB_RECNUM");
256  close();
257  return;
258  }
259 
260  if( (ret = d->dbp->set_cachesize( d->dbp, 0, 2 * 1024 * 1024, 0 )) != 0 ){
261  kdDebug() << "set_cachesize: " << db_strerror(ret) << endl;
262  }
263 
264  if ((ret = d->dbp->open(
265  d->dbp, NULL, d->dbName.local8Bit(), 0, DB_BTREE, DB_CREATE, 0664)) != 0) {
266  kdDebug() << "db_open: " << db_strerror(ret) << endl;
267  close();
268  return;
269  }
270 }
271 
276  TQString Catalog::dbName() const
277 {
278  return d->dbName;
279 }
280 
285  bool Catalog::isValid() const
286 {
287  return d->dbp != 0;
288 }
289 
294  void Catalog::addItem( Tag& tag )
295 {
296  if( tag.name().isEmpty() )
297  return;
298 
299  TQCString id = generateId();
300 
301  tag.setId( id );
302  if( d->addItem(d->dbp, id, tag) ){
303  TQMap<TQCString, DB*>::Iterator it = d->indexList.begin();
304  while( it != d->indexList.end() ){
305  if( tag.hasAttribute(it.key()) )
306  d->addItem( it.data(), tag.attribute(it.key()), id );
307  ++it;
308  }
309  }
310 }
311 
316  Tag Catalog::getItemById( const TQCString& id )
317 {
318  Q_ASSERT( d->dbp != 0 );
319 
320  DBT key, data;
321  std::memset( &key, 0, sizeof(key) );
322  std::memset( &data, 0, sizeof(data) );
323 
324  TQByteArray a1;
325  {
326  TQDataStream stream( a1, IO_WriteOnly );
327  stream << id;
328  key.data = a1.data();
329  key.size = a1.size();
330  }
331 
332  int ret = d->dbp->get( d->dbp, 0, &key, &data, 0 );
333  Q_ASSERT( ret == 0 );
334 
335  Tag tag;
336 
337  if( ret == 0 ){
338  TQByteArray a;
339  a.setRawData( (const char*) data.data, data.size );
340  TQDataStream stream( a, IO_ReadOnly );
341  tag.load( stream );
342  a.resetRawData( (const char*) data.data, data.size );
343  }
344 
345  return tag;
346 }
347 
352  void Catalog::sync()
353 {
354  Q_ASSERT( d->dbp != 0 );
355  d->dbp->sync( d->dbp, 0 );
356 
357  TQMap<TQCString, DB*>::Iterator it = d->indexList.begin();
358  while( it != d->indexList.end() ){
359  it.data()->sync( it.data(), 0 );
360  ++it;
361  }
362 }
363 
368  TQValueList<Tag> Catalog::query( const TQValueList<QueryArgument>& args )
369 {
370  TQValueList<Tag> tags;
371 
372  DBT key, data;
373 
374  DBC** cursors = new DBC* [ args.size() + 1 ];
375 
376  TQValueList< TQPair<TQCString,TQVariant> >::ConstIterator it = args.begin();
377  int current = 0;
378  while( it != args.end() ){
379  TQCString indexName = (*it).first;
380  TQVariant value = (*it).second;
381 
382  if( d->hasIndex(indexName) ) {
383  DB* dbp = d->index( indexName );
384  Q_ASSERT( dbp != 0 );
385 
386  std::memset( &key, 0, sizeof(key) );
387  std::memset( &data, 0, sizeof(data) );
388 
389  TQByteArray a1;
390  {
391  TQDataStream stream( a1, IO_WriteOnly );
392  stream << value;
393  key.data = a1.data();
394  key.size = a1.size();
395  }
396 
397  DBC* cursor = 0;
398  int rtn = dbp->cursor( dbp, 0, &cursor, 0 );
399 
400  if ( rtn == 0 ) {
401 
402  rtn = cursor->c_get( cursor, &key, &data, DB_SET );
403 
404  if ( rtn == 0 ) {
405  cursors[ current++ ] = cursor;
406  }
407  else if ( rtn != DB_NOTFOUND) {
408  kdDebug() << "fetching cursor failed: " << db_strerror(rtn) << endl;
409  cursor->c_close(cursor);
410  }
411  }
412  else {
413  kdDebug() << "creating cursor failed: " << db_strerror(rtn) << endl;
414  }
415  }
416  ++it;
417  }
418 
419  cursors[ current ] = 0;
420 
421  if( current == args.size() ) {
422 
423  DBC* join_curs = 0;
424  int rtn = d->dbp->join( d->dbp, cursors, &join_curs, 0 );
425 
426  if ( rtn == 0 ) {
427 
428  std::memset( &key, 0, sizeof(key) );
429  std::memset( &data, 0, sizeof(data) );
430 
431  while( join_curs->c_get(join_curs, &key, &data, 0) == 0 ) {
432 
433  TQByteArray a2;
434  a2.setRawData( (const char*) data.data, data.size );
435  TQDataStream s( a2, IO_ReadOnly );
436  Tag tag;
437  tag.load( s );
438  a2.resetRawData( (const char*) data.data, data.size );
439  tags << tag;
440  }
441 
442  join_curs->c_close( join_curs );
443  }
444  else {
445  kdDebug() << "joining results failed: " << db_strerror(rtn) << endl;
446  }
447  }
448 
449  DBC** c = cursors;
450  while( *c != 0 ){
451  (*c)->c_close( *c );
452  ++c;
453  }
454  delete[] cursors;
455 
456  return tags;
457 }
458 
459  TQCString Catalog::generateId()
460 {
461  static int n = 1;
462  TQCString asStr;
463  asStr.sprintf( "%05d", n++ );
464  return asStr;
465 }
466 
Catalog::addIndex
void addIndex(const TQCString &name)
Definition: catalog.cpp:175
catalog.h
Catalog database - the persistent symbol store database.

TDevelop Catalog Library

Skip menu "TDevelop Catalog Library"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

TDevelop Catalog Library

Skip menu "TDevelop Catalog Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for TDevelop Catalog Library by doxygen 1.8.13
This website is maintained by Timothy Pearson.