KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVSQLROOTFile.cpp
Go to the documentation of this file.
1 #include "KVSQLROOTFile.h"
2 #include "TSystem.h"
3 
5 
6 
7 
10 KVString KVSQLROOTFile::UUID_for_object(const KVString& name) const
11 {
12  // Return unique identifier used to store object with given name in ROOT file
13 
14  fObjDB->select_data("objTable", "unique_id", Form("name = \"%s\"", name.Data()));
15  KVString uuid;
16  while (fObjDB->get_next_result())
17  uuid = get_objTable()["unique_id"].get_data<KVString>();
18  return uuid;
19 }
20 
21 
22 
25 
27 {
28  // Retrieve object from file using its name
29 
30  auto it = fObjList.find(uuid.Data());// object already retrieved from file ?
31  if (it != fObjList.end()) return it->second;
32  auto t = fObjStore->Get(uuid);
33  fObjList[uuid.Data()] = t;
34  return t;
35 }
36 
37 
38 
44 
46 {
47  // Open or (re)create a combined ROOT object store & SQL database with info on the stored objects.
48  //
49  // \param[in] filepath path to file. any shell variables will be expanded.
50  // \param[in] option see TFile constructors for possible values. default="READ".
51 
52  TString FP = filepath;
53  TString OPT = option;
54  OPT.ToUpper();
55 
57  if (OPT.Contains("CREATE")) {
58  // create directory using path
59  gSystem->mkdir(filepath, true); // recursive: create any missing directories in path
60  }
61  FP.Append("/");
62  TString rpath = FP + "objStore.root";
63  if (OPT == "RECREATE") gSystem->Unlink(rpath);
65  fObjStore.reset(new TFile(rpath, OPT));
67  TString sqlpath = FP + "objInfos.sqlite";
68  if (OPT == "RECREATE") gSystem->Unlink(sqlpath);
69  fObjDB.reset(new KVSQLite::database(sqlpath));
70  if (OPT.Contains("CREATE")) {
71  // set up object table
72  KVSQLite::table tmp("objTable");
73  tmp.add_primary_key("obj_idx");
74  tmp.add_column("name", "TEXT");
75  tmp.add_column("class", "TEXT");
76  tmp.add_column("unique_id", "TEXT");
77  fObjDB->add_table(tmp);
78  }
79 }
80 
81 
82 
85 
86 void KVSQLROOTFile::WriteObject(const TObject* obj, const KVNameValueList& infos)
87 {
88  // Write object in ROOT file, store infos given in list
89 
90  if (!fObjDB->has_table("objInfos")) {
91  // set up object info table if not already done
92  KVSQLite::table tmp("objInfos");
93  tmp.add_primary_key("info_idx");
94  tmp.add_foreign_key("objTable", "obj_idx");
95  fObjDB->add_table(tmp);
96  }
97  // add any missing columns to info table
98  fObjDB->add_missing_columns("objInfos", infos);
99  // get unique ID for object
100  TUUID unid;
101  TString unique_id = unid.AsString();
102  // write object in ROOT file
104  fObjStore->cd();
105  obj->Write(unique_id);
107  // store infos on object
108  // 1 - write name, class & unique id in object table
109  fObjDB->prepare_data_insertion("objTable");
110  get_objTable()["name"] = obj->GetName();
111  get_objTable()["class"] = obj->ClassName();
112  get_objTable()["unique_id"] = unique_id;
113  fObjDB->insert_data_row();
114  fObjDB->end_data_insertion();
115  // 2 - get index of object from object table
116  fObjDB->select_data("objTable", "obj_idx", Form("unique_id=\"%s\"", unique_id.Data()));
117  int obj_idx;
118  while (fObjDB->get_next_result()) obj_idx = get_objTable()["obj_idx"].get_data<int>();
119  // 3 - add infos on object in info table
120  fObjDB->prepare_data_insertion("objInfos");
121  KVNameValueList info_copy(infos);
122  info_copy.SetValue("obj_idx", obj_idx);
123  //get_objInfos()["obj_idx"]=obj_idx;
124  //for(auto& info : infos) get_objInfos()[info.GetName()]=info;
125  get_objInfos().prepare_data(info_copy);
126  fObjDB->insert_data_row();
127  fObjDB->end_data_insertion();
128 }
129 
130 
131 
134 
136 {
137  // Return pointer to object with given name
138 
140 }
141 
142 
143 
146 
148 {
149  // List the contents of the file with associated infos
150  KVString colnames = "name,class,";
151  colnames += get_objInfos().get_column_names("obj_idx info_idx");
152  colnames.Begin(",");
153  TString tabs("\t\t\t\t");
154  while (!colnames.End()) {
155  std::cout << colnames.Next() << tabs;
156  }
157  std::cout << "\n==========================================================================================\n";
158  if (fObjDB->select_data("objTable,objInfos", colnames)) {
159  while (fObjDB->get_next_result()) {
160  colnames.Begin(",");
161  while (!colnames.End()) {
162  auto colname = colnames.Next();
163  if (colname == "name" || colname == "class")
164  std::cout << get_objTable()[colname].get_data<TString>() << tabs;
165  else
166  std::cout << get_objInfos()[colname].get_data<TString>() << tabs;
167  }
168  std::cout << std::endl;
169  }
170  }
171  else {
172  Error("ls", "Problem with KVSQLite::database::select_data");
173  }
174 }
175 
176 
177 
198 
200 {
201  // Fill the list given as argument with pointers to all objects which obey the given selection.
202  //
203  // The 'where' string will be used as the `WHERE` clause of an SQLite selection.
204  //
205  // Examples:
206  //
207  //~~~~
208  //WHERE column_1 = 100;
209  //
210  //WHERE column_2 IN (1,2,3);
211  //
212  //WHERE column_3 LIKE 'An%';
213  //
214  //WHERE column_4 BETWEEN 10 AND 20;
215  //~~~~
216  //
217  // Note that string arguments should be enclosed in single quotes.
218  //
219  // The columns of both the `objTable` (`name`, `class`) and `objInfos` tables can be used in the selection.
220 
221  fObjDB->select_data("objTable,objInfos", "unique_id", where);
222  while (fObjDB->get_next_result()) {
223  auto uuid = get_objTable()["unique_id"].get_data<KVString>();
224  list->Add(get_object_with_UUID(uuid));
225  }
226 }
227 
228 
252 
253 void KVSQLROOTFile::FillListOfObjectsWithSelection(KVSeqCollection* list, const KVString& where, const KVString& numberlist_column, int value)
254 {
255  // Fill the list given as argument with pointers to all objects which obey the given selection.
256  // 'numberlist_column' is the name of a column in the `objInfos` table containing strings which may be
257  // interpreted as KVNumberList objects. Only the entries with a number list containing 'value'
258  // will be selected.
259  //
260  // The 'where' string will be used as the `WHERE` clause of an SQLite selection.
261  //
262  // Examples:
263  //
264  //~~~~
265  //WHERE column_1 = 100;
266  //
267  //WHERE column_2 IN (1,2,3);
268  //
269  //WHERE column_3 LIKE 'An%';
270  //
271  //WHERE column_4 BETWEEN 10 AND 20;
272  //~~~~
273  //
274  // Note that string arguments should be enclosed in single quotes.
275  //
276  // The columns of both the `objTable` (`name`, `class`) and `objInfos` tables can be used in the selection.
277 
278  fObjDB->select_data("objTable,objInfos", Form("unique_id,%s", numberlist_column.Data()), where);
279  while (fObjDB->get_next_result()) {
280  KVNumberList nl(get_objInfos()[numberlist_column].get_data<KVString>());
281  if (nl.Contains(value)) {
282  auto uuid = get_objTable()["unique_id"].get_data<KVString>();
283  list->Add(get_object_with_UUID(uuid));
284  }
285  }
286 }
287 
288 
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define OPT
const char Option_t
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
void SetValue(const Char_t *name, value_type value)
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:83
Bool_t Contains(Int_t val) const
returns kTRUE if the value 'val' is contained in the ranges defined by the number list
Combine ROOT file containing objects with SQLite database with info on the objects.
Definition: KVSQLROOTFile.h:19
void WriteObject(const TObject *, const KVNameValueList &)
Write object in ROOT file, store infos given in list.
void FillListOfObjectsWithSelection(KVSeqCollection *list, const KVString &where)
KVSQLite::table & get_objTable() const
Definition: KVSQLROOTFile.h:33
KVSQLite::table & get_objInfos() const
Definition: KVSQLROOTFile.h:37
TObject * Get(const KVString &name) const
Return pointer to object with given name.
void ls(Option_t *="") const
List the contents of the file with associated infos.
std::unordered_map< std::string, TObject * > fObjList
for quick look-up of objects using unique id
Definition: KVSQLROOTFile.h:42
TObject * get_object_with_UUID(const KVString &name) const
Retrieve object from file using its name.
void restore_working_directory()
Definition: KVSQLROOTFile.h:28
KVString UUID_for_object(const KVString &) const
Return unique identifier used to store object with given name in ROOT file.
std::unique_ptr< TFile > fObjStore
Definition: KVSQLROOTFile.h:20
std::unique_ptr< KVSQLite::database > fObjDB
Definition: KVSQLROOTFile.h:21
KVSQLROOTFile(const KVString &filepath, Option_t *option="READ")
void save_working_directory()
Definition: KVSQLROOTFile.h:24
Interface to ROOT SQLite database backend ,.
Definition: SQLiteDB.h:389
const column & add_primary_key(const TString &name)
Definition: SQLiteDB.cpp:1152
TString get_column_names(const TString &exclude="", const TString &delim=",") const
Definition: SQLiteDB.cpp:1282
column & add_column(const KVSQLite::column &c)
Definition: SQLiteDB.cpp:1115
const column & add_foreign_key(const TString &other_table, const TString &other_column)
Definition: SQLiteDB.cpp:1177
void prepare_data(const KVNameValueList &, const KVNamedParameter *=nullptr)
Definition: SQLiteDB.cpp:1244
KaliVeda extensions to ROOT collection classes.
virtual void Add(TObject *obj)
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Begin(TString delim) const
Definition: KVString.cpp:565
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
virtual const char * GetName() const
virtual const char * ClassName() const
virtual void Error(const char *method, const char *msgfmt,...) const
TString & Append(char c, Ssiz_t rep=1)
const char * Data() const
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
virtual char * ExpandPathName(const char *path)
virtual int Unlink(const char *name)
const char * AsString() const