libdballe  6.8
results.h
1 /*
2  * memdb/query - Infrastructure used to query memdb data
3  *
4  * Copyright (C) 2013 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_MEMDB_RESULTS_H
23 #define DBA_MEMDB_RESULTS_H
24 
25 #include <dballe/core/stlutils.h>
26 #include <dballe/memdb/valuestorage.h>
27 #include <dballe/memdb/index.h>
28 #include <dballe/memdb/match.h>
29 #include <vector>
30 #include <memory>
31 #include <cstddef>
32 
33 // #define TRACE_QUERY
34 
35 namespace dballe {
36 
37 namespace memdb {
38 template<typename T> struct ValueStorage;
39 
40 #ifdef TRACE_QUERY
41 #define IF_TRACE_QUERY if (1)
42 void trace_query(const char* fmt, ...);
43 #else
44 #define IF_TRACE_QUERY if (0)
45 #define trace_query(...) do {} while(0)
46 #endif
47 
48 namespace results {
49 
51 class Base
52 {
53 protected:
55  std::vector<std::set<size_t>*> transient_sets;
56 
59 
62 
64  bool all;
65 
67  bool empty;
68 
69  Base();
70 
71 public:
72  ~Base();
73 
75  bool is_select_all() const { return all; }
76 
78  bool is_empty() const { return empty; }
79 
81  void set_to_empty() { all = false; empty = true; }
82 
83  void add_union(std::auto_ptr< stl::Sequences<size_t> > seq);
84 
86  void add_singleton(size_t singleton);
87 
88  void add_set(const std::set<size_t>& p);
89 
91  void add_set(std::auto_ptr< std::set<size_t> > p);
92 };
93 
94 }
95 
96 template<typename T>
97 class Results : public results::Base
98 {
99 public:
100  const ValueStorage<T>& values;
101 
102 protected:
105 
106 public:
107  Results(const ValueStorage<T>& values) : values(values) {}
108 
109  template<typename K>
110  bool add(const Index<K>& index, const K& val);
111 
112  template<typename K>
113  bool add(const Index<K>& index, const K& min, const K& max);
114 
115  template<typename K>
116  bool add_since(const Index<K>& index, const typename Index<K>::const_iterator begin)
117  {
118  bool res = false;
119  for (typename Index<K>::const_iterator i = begin; i != index.end(); ++i)
120  {
121  this->add(i->second);
122  res = true;
123  }
124  all = false;
125  return res;
126  }
127 
128  template<typename K>
129  bool add_until(const Index<K>& index, const typename Index<K>::const_iterator end)
130  {
131  bool res = false;
132  for (typename Index<K>::const_iterator i = index.begin(); i != end; ++i)
133  {
134  this->add(i->second);
135  res = true;
136  }
137  all = false;
138  return res;
139  }
140 
141  void add(Match<T>* f)
142  {
143  filter.add(f);
144  all = false;
145  }
146 
152  template<typename OUTITER>
153  void copy_valptrs_to(OUTITER res);
154 
160  template<typename OUTITER>
161  void copy_indices_to(OUTITER res);
162 
163 private:
164  Results(const Results<T>&);
165  Results<T> operator=(const Results<T>&);
166 };
167 
168 namespace match {
169 
170 template<typename T>
172 {
173 protected:
174  const Index<const T*>& index;
175  stl::Sequences<size_t>* sequences;
176  bool found;
177 
178 public:
179  SequenceBuilder(const Index<const T*>& index)
180  : index(index), sequences(new stl::Sequences<size_t>), found(false) {}
181  ~SequenceBuilder()
182  {
183  if (sequences) delete sequences;
184  }
185 
186  bool found_items_in_index() const { return found; }
187 
188  std::auto_ptr< stl::Sequences<size_t> > release_sequences()
189  {
190  std::auto_ptr< stl::Sequences<size_t> > res(sequences);
191  sequences = 0;
192  return res;
193  }
194 
195  void insert(const T* val)
196  {
197  const std::set<size_t>* s = index.search(val);
198  if (!s || s->empty()) return;
199  sequences->add(*s);
200  found = true;
201  }
202 
203 private:
205  SequenceBuilder& operator=(const SequenceBuilder&);
206 };
207 
208 }
209 
210 
211 }
212 }
213 
214 #endif
215 
216 
void copy_valptrs_to(OUTITER res)
Send results to res.
bool empty
True if it has been determined that there are no results.
Definition: results.h:67
bool is_empty() const
Check if we just select all elements.
Definition: results.h:78
void set_to_empty()
Disregard everything and just return no items.
Definition: results.h:81
match::FilterBuilder< T > filter
Filters to apply to each candidate result.
Definition: results.h:104
void copy_indices_to(OUTITER res)
Send results to res.
bool all
True if all elements are selected.
Definition: results.h:64
std::vector< std::set< size_t > * > transient_sets
Keep track of transient sets here, for memory management purpose.
Definition: results.h:55
bool is_select_all() const
Check if we just select all elements.
Definition: results.h:75
Definition: cmdline.h:34
const std::set< size_t > * search(const T &el) const
Lookup all positions for a value.
Build an And of filters step by step.
Definition: match.h:68
stl::SetIntersection< size_t > * indices
Sets of possible results, to be intersected.
Definition: results.h:61
void add_singleton(size_t singleton)
Add a set of one single element to intersect with the rest.
Definition: results.h:171
Non-template part of Results, split here for faster compilation.
Definition: results.h:51
Index element positions based by one value.
Definition: index.h:41
Definition: mem/cursor.h:35
stl::Sequences< size_t > * others_to_intersect
Sequences of possible results to be intersected.
Definition: results.h:58
Definition: levtr.h:33