ddc
NavHint.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 //
3 // DDC originally by Alexey Sokirko
4 // Changes and modifications 2018 by Bryan Jurish
5 //
6 // This file is part of DDC.
7 //
8 // DDC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // DDC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
20 //
21 #ifndef DDC_NAV_HINT_H
22 #define DDC_NAV_HINT_H
23 
24 #include "../ConcordLib/ConcCommon.h"
25 #include "../ConcordLib/LRUCache.h"
26 #include "../CommonLib/ddcThread.h"
27 #include <numeric> //-- for std::accumulate
28 
29 //======================================================================
30 
32 #define DDC_DEFAULT_NAV_HINT_CACHE_CAPACITY 1024
33 
35 #define DDC_DEFAULT_NAV_HINT_CACHE_STEP 1000
36 
37 //------------------------------------------------------
38 //#define DEBUG_NAVCACHE 1
39 #undef DEBUG_NAVCACHE
40 #ifdef DEBUG_NAVCACHE
41 # define NAVCACHE_DEBUG(x) x
42 #else
43 # define NAVCACHE_DEBUG(x)
44 #endif
45 
46 //------------------------------------------------------
47 //#define TRACE_NAVCACHE 1
48 #undef TRACE_NAVCACHE
49 #ifdef TRACE_NAVCACHE
50 # define NAVCACHE_TRACE(x) x
51 #else
52 # define NAVCACHE_TRACE(x)
53 #endif
54 
55 //======================================================================
57 struct NavHintKey {
58  string m_QueryStr;
59  size_t m_HitNo;
60 
62  NavHintKey(const string& qstr="", const size_t hitno=0)
63  : m_QueryStr(qstr), m_HitNo(hitno)
64  {};
65 
67  inline void clear()
68  {
69  ClearString(m_QueryStr);
70  m_HitNo = 0;
71  };
72 
76  inline bool operator< (const NavHintKey &X) const
77  {
78  return (m_QueryStr < X.m_QueryStr ? true //-- QueryStr is primary sort-key
79  : (m_QueryStr==X.m_QueryStr ? (m_HitNo > X.m_HitNo) //-- ... only compare HitNo if QueryStr matches (otherwise map<> chokes)
80  : false)); //-- ... QueryStr>X.QueryStr
81  };
82 
84  inline bool operator== (const NavHintKey &X) const
85  { return m_HitNo == X.m_HitNo && m_QueryStr == X.m_QueryStr; };
86 
88  inline void assign(const string& qstr, const size_t hitno=0)
89  {
90  m_QueryStr = qstr;
91  m_HitNo = hitno;
92  };
93 
95  inline NavHintKey& operator= (const NavHintKey &X)
96  {
98  return *this;
99  };
100 
102  inline string toString() const
103  { return Format("%zd %s", m_HitNo, m_QueryStr.c_str()); };
104 
106  void fromString(const char *s);
107 
109  string toJson() const;
110 };
111 
112 
113 //======================================================================
115 struct NavHint {
116  string m_SortKey;
117  size_t m_Offset;
118  vector<string> m_DtrHints;
119 
122  NavHint(size_t nDtrs=0)
123  : m_SortKey(""), m_Offset(0), m_DtrHints(nDtrs,"")
124  {};
125 
127  NavHint(size_t Offset, size_t nDtrs)
128  : m_SortKey(""), m_Offset(Offset), m_DtrHints(nDtrs,"")
129  {};
130 
132  NavHint(const string& SortKey, size_t Offset, size_t nDtrs)
133  : m_SortKey(SortKey), m_Offset(Offset), m_DtrHints(nDtrs,"")
134  {};
135 
137  NavHint(const string& SortKey, size_t Offset, const vector<string> DtrHints)
138  : m_SortKey(SortKey), m_Offset(Offset), m_DtrHints(DtrHints)
139  {};
140 
142  NavHint(const string& HintStr)
143  { fromString(HintStr); };
144 
146  inline void clear(size_t nDtrs=0)
147  {
148  m_SortKey.clear();
149  m_Offset = 0;
150  m_DtrHints.assign(nDtrs,"");
151  };
152 
154  inline bool empty() const
155  { return m_SortKey.empty() && m_Offset==0; };
156 
158  size_t OffsetTotal() const;
159 
161  inline void UpdateOffset()
162  { m_Offset = OffsetTotal(); };
163 
165  inline const char* DtrHint(size_t DtrNo) const
166  { return (DtrNo < m_DtrHints.size() ? m_DtrHints[DtrNo].c_str() : ""); };
167 
169  inline size_t DtrOffset(size_t DtrNo) const
170  { return (DtrNo < m_DtrHints.size() ? strtoul(m_DtrHints[DtrNo].c_str(),NULL,0) : 0); };
171 
173  inline NavHint& operator= (const NavHint &X)
174  {
175  m_SortKey = X.m_SortKey;
176  m_Offset = X.m_Offset;
177  m_DtrHints = X.m_DtrHints;
178  return *this;
179  };
180 
182  inline NavHint& operator= (const string& S)
183  {
184  fromString(S);
185  return *this;
186  };
187 
189  inline void update(const string& SortKey, size_t Offset, size_t nDtrs)
190  {
191  m_SortKey = SortKey;
192  m_Offset = Offset;
193  m_DtrHints.resize(nDtrs);
194  };
195 
204  string toString(bool includeSortKey=true) const;
205 
207  void fromString(const string& Str);
208 
210  string toJson() const;
211 };
212 
213 //======================================================================
215 class NavHintCache : public ddcLockable
216 {
217 public:
218  //------------------------------------------------------------
219  // public typedefs
220  typedef NavHintKey KeyT;
221  typedef NavHint ValT;
222 
223  //----------------------------------------------------
226 
227 public:
228  //------------------------------------------------------------
229  // public data
230  CacheT m_Cache;
231 
232 public:
233  //------------------------------------------------------------
234  // constructors etc.
235 
239  : m_Cache(capacity, &m_Mutex)
240  {};
241 
244  {};
245 
246  //------------------------------------------------------------
247  // API
248 
250  inline void reserve(size_t capacity_)
251  {
252  lock();
253  try {
254  m_Cache.reserve(capacity_);
255  } catch (...) {
256  unlock();
257  throw;
258  }
259  unlock();
260  };
261 
262  //----------------------------------------------------
264  inline size_t size() const
265  {
266  size_t sz=0;
267  lock();
268  try {
269  sz = m_Cache.size();
270  } catch (...) {
271  unlock();
272  throw;
273  }
274  unlock();
275  return sz;
276  };
277 
278  //----------------------------------------------------
280  inline void clear()
281  {
282  lock();
283  try {
284  m_Cache.clear();
285  } catch (...) {
286  unlock();
287  throw;
288  }
289  unlock();
290  };
291 
292  //----------------------------------------------------
301  bool lower_bound(ValT& val, const KeyT& key, bool doPromote=true);
302 
303  //----------------------------------------------------
306  inline ValT lower_bound(const KeyT& key, bool doPromote=true)
307  {
308  ValT val;
309  lower_bound(val, key, doPromote);
310  return val;
311  };
312 
313  //----------------------------------------------------
317  inline void insert(const KeyT& key, const ValT& val)
318  {
319  lock();
320  try {
321  NAVCACHE_TRACE(ddcLogTrace(Format("NavCache::insert(\"%s\",\"%s\")", escapeCString(key.toString()).c_str(), escapeCString(val.toString()).c_str())));
322  m_Cache.insert(key,val);
323  } catch (...) {
324  unlock();
325  throw;
326  }
327  unlock();
328  };
329 
330  //------------------------------------------------------------
331  // convenience API
332 
334  inline ValT lower_bound(const string& QueryStr, size_t HitNo)
335  { return lower_bound(KeyT(QueryStr,HitNo)); };
336 
338  inline void insert(const string& QueryStr, size_t HitNo, const string& SortKey, const size_t Offset, const vector<string>& DtrHints)
339  { insert(KeyT(QueryStr,HitNo), ValT(SortKey,Offset,DtrHints)); };
340 
341  //------------------------------------------------------------
342  // debugging
344  string toJson() const;
345 };
346 
347 
348 #endif /* DDC_NAV_HINT_H */
349 
350 /*--- emacs style variables ---
351  * Local Variables:
352  * mode: C++
353  * c-file-style: "ellemtel"
354  * c-basic-offset: 4
355  * tab-width: 8
356  * indent-tabs-mode: nil
357  * End:
358  */
generic lockable object wrapper class
Definition: ddcThread.h:30
void insert(const string &QueryStr, size_t HitNo, const string &SortKey, const size_t Offset, const vector< string > &DtrHints)
wrapper for insert(KeyT(QueryStr, HitNo), ValT(SortKey,Offset,DtrHints))
Definition: NavHint.h:338
NavHint(size_t Offset, size_t nDtrs)
constructor given local offset and number of daughters
Definition: NavHint.h:127
const char * DtrHint(size_t DtrNo) const
get hint-string for daughter number DtrNo
Definition: NavHint.h:165
ValT lower_bound(const string &QueryStr, size_t HitNo)
wrapper for lower_bound(KeyT(QueryStr, HitNo))
Definition: NavHint.h:334
navigation hint-key for branch server get_first_hits() "paging"
Definition: NavHint.h:57
string toString(bool includeSortKey=true) const
Definition: NavHint.cpp:62
string Format(const char *format,...)
Definition: ddcString.cpp:393
void UpdateOffset()
convenience wrapper: set local offset to sum of daughter offsets
Definition: NavHint.h:161
CacheT m_Cache
underlying LRU cache
Definition: NavHint.h:230
size_t m_HitNo
maximum logical hit number covered w.r.t. local corpus
Definition: NavHint.h:59
ddcLRUCache< KeyT, ValT > CacheT
typedef for underlying LRU cache object
Definition: NavHint.h:225
string toString() const
debug: native stringification
Definition: NavHint.h:102
size_t DtrOffset(size_t DtrNo) const
get offset for daughter number DtrNo
Definition: NavHint.h:169
void fromString(const char *s)
debug: native decoding from string
Definition: NavHint.cpp:28
NavHintKey KeyT
navigation cache key type
Definition: NavHint.h:220
void insert(const KeyT &key, const ValT &val)
Definition: LRUCache.h:226
void reserve(size_t capacity_)
wraps lock(); m_Cache.reserve(capacity_); unlock()
Definition: NavHint.h:250
void clear()
wraps lock(); m_Cache.clear(); unlock()
Definition: NavHint.h:280
void clear(size_t nDtrs=0)
clear the hint
Definition: NavHint.h:146
string m_QueryStr
original query string
Definition: NavHint.h:58
#define ddcLogTrace(Msg)
Definition: ddcLog.h:115
bool empty() const
returns true iff this is an empty hint
Definition: NavHint.h:154
void clear()
clear the object
Definition: NavHint.h:67
void ClearString(string &S)
Definition: utilit.h:497
vector< string > m_DtrHints
offset hints for immediate daughter subcorpora
Definition: NavHint.h:118
NavHint(const string &SortKey, size_t Offset, size_t nDtrs)
constructor given sort key, offset, and number of subcorpora
Definition: NavHint.h:132
~NavHintCache()
default destructor (empty)
Definition: NavHint.h:243
bool operator<(const NavHintKey &X) const
Definition: NavHint.h:76
NavHintCache(size_t capacity=1024)
Definition: NavHint.h:238
void clear()
clear all cached items (no implicit locks)
Definition: LRUCache.h:113
size_t reserve(size_t cap)
update cache capacity; implicitly calls clean()
Definition: LRUCache.h:152
navigation hint with respect to local subcorpus tree, for get_first_hits "paging" ...
Definition: NavHint.h:115
NavHint(const string &SortKey, size_t Offset, const vector< string > DtrHints)
full constructor
Definition: NavHint.h:137
void update(const string &SortKey, size_t Offset, size_t nDtrs)
assigns SortKey and Offset and resize DtrHints
Definition: NavHint.h:189
size_t size() const
get number of currently cached items
Definition: LRUCache.h:144
LRU cache for faster "paging" through daughter hits for a branch server.
Definition: NavHint.h:215
size_t size() const
wraps lock(); m_Cache.size(); unlock()
Definition: NavHint.h:264
NavHintKey & operator=(const NavHintKey &X)
assignment operator
Definition: NavHint.h:95
NavHint ValT
navigation cache value type
Definition: NavHint.h:221
void insert(const KeyT &key, const ValT &val)
Definition: NavHint.h:317
string escapeCString(const std::string &s)
Definition: ddcString.cpp:30
string toJson() const
debug: JSON stringification
Definition: NavHint.cpp:40
void assign(const string &qstr, const size_t hitno=0)
verbose assignment
Definition: NavHint.h:88
string m_SortKey
minimum sort key from this node or any subcorpus
Definition: NavHint.h:116
NavHintKey(const string &qstr="", const size_t hitno=0)
default constructor
Definition: NavHint.h:62
size_t m_Offset
minimum offset for local corpus node
Definition: NavHint.h:117
bool operator==(const NavHintKey &X) const
equality operator (for debugging)
Definition: NavHint.h:84
NavHint(size_t nDtrs=0)
Definition: NavHint.h:122
NavHint(const string &HintStr)
constructor from string
Definition: NavHint.h:142
ValT lower_bound(const KeyT &key, bool doPromote=true)
Definition: NavHint.h:306