KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVDBTable.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 $Id: KVDBTable.cpp,v 1.18 2007/04/27 14:46:29 franklan Exp $
3  KVTable.cpp - description
4  -------------------
5  begin : jeu fév 6 2003
6  copyright : (C) 2003 by Alexis Mignon
7  email : mignon@ganil.fr
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 #include "KVDBTable.h"
19 #include "KVDBRecord.h"
20 #include "Riostream.h"
21 #include "TROOT.h"
22 
23 #include <KVUniqueNameList.h>
24 
25 
26 using namespace std;
27 
29 
30 
31 
34 {
35  fIsUnique = kFALSE;
36 }
37 
38 
39 
40 
42 
43 KVDBTable::KVDBTable(const Char_t* name, const Char_t* type,
44  Bool_t unique)
45  : TFolder() // do not call TFolder(name,type) ctor as it creates a default TList for fFolders
46 {
47  SetName(name);
48  SetTitle(type);
49  fIsUnique = unique;
50  if (unique) fFolders = new KVUniqueNameList;
51  else fFolders = new KVHashList;
52  SetOwner();
53 }
54 
55 
56 
57 
59 
60 KVDBTable::~KVDBTable()
61 {
62  gROOT->GetListOfCleanups()->Remove(this);
63 }
64 
65 
66 
67 
73 
75 {
76  // Add a KVDBRecord to the list of available records and return kTRUE
77  // If a default formatting string for names of numbered records has been
78  // set (see SetDefaultFormat(const TString&)) the name of the record
79  // will be automatically set here according to the format and its number.
80 
82  Add(rec);
83  rec->SetTable(this);
84  return kTRUE;
85 }
86 
87 
88 
89 
92 
94 {
95  //Remove a KVDBRecord from the list of available records
96 
97  Remove(rec);
98  rec->SetTable(0);
99 }
100 
101 
102 
103 
105 
107 {
108  cout << ClassName() << " : " << GetName() << " <---> " << GetTitle() << " ["
109  << GetRecords()->GetSize() << " records]" << endl;
110 }
111 
112 
113 
121 
123 {
124  // Set a default formatting string for names of numbered records.
125  // e.g. if fmt="Rec#%d", each record which is added to the table will have
126  // its name automatically set to "Rec#1", "Rec#2", etc., according to the
127  // record number.
128  // This allows to replace a slow sequential scan for a record based on number
129  // (GetRecord(Int_t)) with a fast hash-based look-up
130 
131  fDefFormatNumRec = fmt.Data();
132 }
133 
134 
135 
136 
145 
147 {
148  // Sequential search for record using its number.
149  // To keep things simple, we only search in the top level of the folder structure.
150  //
151  // NOTE: if there are many records, this sequential search can be long.
152  // If a default format for the record name using its number has been defined
153  // (see SetDefaultFormat(const TString&)) the search will be performed using
154  // the resulting formatted object name, which is fast (hash list).
155 
156  if (HasDefaultFormat()) return GetRecord(Form(fDefFormatNumRec.Data(), num));
157 
158  TIter next(GetListOfFolders());
159  KVDBRecord* obj = 0;
160  while ((obj = (KVDBRecord*) next()) && (obj->GetNumber() != num));
161  return (obj ? (obj->GetNumber() == num ? obj : 0) : 0);
162 }
163 
164 
165 
166 
171 
173 {
174  // The list of records is a KVHashList or KVUniqueNameList
175  // Call this method if the names of the records change after having been
176  // added to the table, otherwise GetRecord(const char*) will not work.
177 
178  ((KVHashList*)fFolders)->Rehash();
179 }
180 
181 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
const char Option_t
int type
#define gROOT
char * Form(const char *fmt,...)
Record folder for the database.
Definition: KVDBRecord.h:42
virtual void SetTable(const KVDBTable *table)
Definition: KVDBRecord.cpp:280
virtual Int_t GetNumber() const
Definition: KVDBRecord.h:72
Table in a database.
Definition: KVDBTable.h:33
TString fDefFormatNumRec
default formatting for names of numbered records
Definition: KVDBTable.h:49
virtual Bool_t AddRecord(KVDBRecord *add)
Definition: KVDBTable.cpp:74
void Rehash(void)
Definition: KVDBTable.cpp:172
virtual void RemoveRecord(KVDBRecord *add)
Remove a KVDBRecord from the list of available records.
Definition: KVDBTable.cpp:93
Bool_t HasDefaultFormat() const
Definition: KVDBTable.h:80
void SetDefaultFormat(const TString &)
Definition: KVDBTable.cpp:122
Bool_t fIsUnique
Must each record name be unique ?
Definition: KVDBTable.h:47
virtual KVDBRecord * GetRecord(const Char_t *rec_name) const
Definition: KVDBTable.h:57
virtual KVSeqCollection * GetRecords() const
Definition: KVDBTable.h:62
virtual void ls(Option_t *option="*") const
Definition: KVDBTable.cpp:106
Extended version of ROOT THashList.
Definition: KVHashList.h:28
virtual Int_t GetSize() const
Optimised list in which named objects can only be placed once.
virtual void Add(TObject *obj)
TCollection * GetListOfFolders() const
virtual void SetOwner(Bool_t owner=kTRUE)
TCollection * fFolders
virtual void Remove(TObject *obj)
virtual const char * GetName() const
virtual void SetTitle(const char *title="")
virtual const char * GetTitle() const
virtual void SetName(const char *name)
virtual const char * ClassName() const
const char * Data() const