Xpace
data.h
Go to the documentation of this file.
1 
2 /**************************************************************
3  **
4  ** @file data/data.h
5  **
6  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
7  **
8  ** www.xpace.net
9  **
10  **************************************************************/
11 
12 #ifndef XPACE_DATA_H
13 #define XPACE_DATA_H
14 
15 #include "base/types.h"
16 #include "base/exception.h"
17 #include "base/config.h"
18 #include "base/decimalfloat.h"
19 #include "base/datetime.h"
20 
21 namespace Xpace
22 {
23  // ================================ DATA TYPES ==============
24 
26  {
27  public:
28  enum type
29  {
30  btNone = 0,
31  btInt, // int
32  btUint, // uint
33  btFloat, // DecimalFloat
34  btString8, // String8
35  btString16, // String16
36  btBytes, // BytesRef
37  btEnd
38  };
39 
41  (type t,
42  size_t s = 0); // size for int amd string types only
43 
44  explicit
46  (const String& str,
47  size_t s = 0); // size for int and string types only
48 
49  #ifdef XPACE_CONFIG_H
50  explicit
52  (const Configuration& config);
53  #endif
54 
55  type getBaseType
56  ()
57  const;
58 
59  size_t getSize
60  ()
61  const;
62 
63  virtual String getName
64  ()
65  const;
66 
67  template <typename STR>
68  static
69  BaseDataType stringType
70  ();
71  // { return BaseDataType(); }
72 
73  protected:
75  size_t size;
76 
78  () :
79  ty(btNone),
80  size(0)
81  {
82  }
83  };
84 
86  {
87  public:
88  enum type
89  {
90  dtNone = 0,
91  dtDateTime = BaseDataType::btEnd,
93  dtEnd
94  };
95 
97  (type,
98  size_t size = 0);
100  (BaseDataType::type = btNone,
101  size_t size = 0);
102 
103  explicit
105  (const String& str,
106  size_t size = 0);
107 
108  #ifdef XPACE_CONFIG_H
109  explicit
111  (const Configuration& config);
112  #endif
113 
114  type getDerivedType
115  ()
116  const;
117 
118  virtual String getName
119  ()
120  const;
121 
122  bool operator==
124  const;
125 
126  private:
127  type ty;
128  static BaseDataType base_type
129  (type,
130  size_t);
131  };
132 
133  // ================================ ACCESS ==================
134 
135  // pass this when opening a table or store
137  {
141  };
142 
143 // ================================ SOURCE ====================
144 
145  /// A Source is an interface to an object that provides access to each field or column of
146  /// a data record or row. A database or table that supports this access
147  /// pattern will have a method or methods that returns a Source object or
148  /// reference. Note that this class is stateful in the sense that there is a
149  /// current record or row. Also note that the lifetime of a Source is bounded by the
150  /// lifetime of its creating object, or possibly earlier if an action on the
151  /// creator invalidates it.
152 
153  /// The non-LOC part of a Source
154  /// @param RET type of the return value of moveTo()
155  template<typename RET = bool>
157  {
158  public:
159  /// destructor
160  virtual ~SourceBase
161  ()
162  {
163  }
164 
165  /// Move to another document/row/record
166  /// @param targ the new document/row/record number
167  /// @return true iff move was successful
168  virtual RET moveTo
169  (uint64 targ)
170  = 0;
171  };
172 
173  /// @param LOC type of the field or column identifier
174  /// @param RET the return value of the getters
175  template<typename LOC, typename RET = bool>
176  class XPACE_EXPORT Source : public virtual SourceBase<RET>
177  {
178  public:
179  /// Get the value at a location as an int64
180  /// @param location field or column identifier
181  /// @retval value recipient of the data
182  /// @return true iff the value exists. If false, the contents of value are undefined.
183  virtual RET get
184  (LOC location,
185  int64* value)
186  const
187  = 0;
188 
189  /// Get the value at a location as a DecimalFloat
190  /// @param location field or column identifier
191  /// @retval value recipient of the data
192  /// @return true iff the value exists; if false, the contents of value
193  /// are undefined.
194  virtual RET get
195  (LOC location,
197  const
198  = 0;
199 
200  /// Get the value at a location as a String8
201  /// @param location field or column identifier
202  /// @retval value recipient of the data
203  /// @return true iff the value exists; if false, the contents of value
204  /// are undefined.
205  virtual RET get
206  (LOC location,
207  String8* value)
208  const
209  = 0;
210 
211  /// Get the value at a location as a String16
212  /// @param location field or column identifier
213  /// @retval value recipient of the data
214  /// @return true iff the value exists; if false, the contents of value
215  /// are undefined.
216  virtual RET get
217  (LOC location,
218  String16* value)
219  const
220  = 0;
221 
222  /// Get the value at a location as a String
223  /// @param location field or column identifier
224  /// @retval value recipient of the data
225  /// @returns true iff the value exists; if false, the contents of value
226  /// are undefined.
227  virtual RET get
228  (LOC location,
229  String* value)
230  const
231  = 0;
232 
233  /// Get the value at a location as a byte array
234  /// @param location field or column identifier
235  /// @retval value recipient of the data
236  /// @return true iff the value exists; if false, the contents of value
237  /// are undefined.
238  virtual RET get
239  (LOC location,
240  BytesRef* value)
241  const
242  = 0;
243  };
244 
245  // ================================ SINK ====================
246 
247  /// Sink is an interface to an object that receives data.
248  /// Each data object is allowed to have only one Sink.
249 
250  /// Request notification of document/row/record/count (via SinkBase::setCount())
252  {
253  SNC_Never, ///< no count (content comes immediately, retrieval stops when Sink returns false)
254  SNC_Continue, ///<< no count (content comes immediately, retrieval stops when Sink returns false) but keep state for future calls
255  SNC_After, ///< get count after content (content comes immediately; search resolution continues after Sink returns false)
256  SNC_Before ///< get count before content (retrieval is completed before Sink is called)
257  };
258 
259  /// The non-LOC part of a Sink
261  {
262  public:
263  // destructor
264  virtual ~SinkBase
265  ()
266  {
267  }
268 
269  /// Inform the Sink how many documents/records/rows to expect
270  /// @param docs the number of documents/records/rows
271  /// @return true to receive, false to stop
272  virtual bool setCount
273  (uint64 docs)
274  {
275  return true;
276  }
277 
278  /// Begin a new document/record/row, committing current one (if any)
279  /// @param docNum a suggested document
280  /// @return -1 to stop, otherwise a document number
281  virtual int64 start
282  (uint64 docNum)
283  {
284  return docNum;
285  }
286 
287  /// We're finshed with this Sink
288  virtual void close
289  ()
290  {
291  }
292 
293  protected:
294  SinkBase
295  ()
296  {
297  }
298 
299  private:
300  SinkBase
301  (const SinkBase&);
302  SinkBase& operator=
303  (const SinkBase&);
304  };
305 
306  /// @param LOC the location (absolute or relative) of a datum (e.g. table cell or field)
307  /// @param RET type of the return value of the functions; usually a bool, true to continue, false to stop
308  template<typename LOC, typename RET = bool>
309  class XPACE_EXPORT Sink : public virtual SinkBase
310  {
311  public:
312  /// Add a field by name
313  /// @param name the name of the field
314  /// @param type the type of the field
315  /// @param location a proposed location; may be null
316  /// @retval added true iff the field or column was added
317  /// @returns the actual location, NULL to stop
318  virtual LOC add
319  (const String& name,
321  LOC location,
322  bool* added = 0)
323  {
324  if (added)
325  *added = false;
326  return location;
327  }
328 
329  /// Add a field from a Configuration
330  /// @param config the configuration of the field
331  /// @param location a proposed location; may be null
332  /// @retval added true iff the column was added
333  /// @returns the actual location, NULL to stop
334  virtual LOC add
335  (const Configuration& config,
336  LOC location,
337  bool* added = 0)
338  {
339  return add(config.getTag(), DerivedDataType(config), location, added);
340  }
341 
342  /// Move without setting data
343  /// @param location
344  /// @returns a RET, usually a bool
345  virtual RET move
346  (LOC location)
347  {
348  // optional
349  return RET(1);
350  }
351 
352  /// Write an int64
353  /// @param location
354  /// @param value
355  /// @returns a RET, usually a bool
356  virtual RET set
357  (LOC location,
358  int64 value)
359  {
360  throw Unimplemented_Error(__FUNCTION__);
361  }
362 
363  /// Add a DecimalFloat
364  /// @param location
365  /// @param value
366  /// @returns a RET, usually a bool
367  virtual RET set
368  (LOC location,
369  const DecimalFloat& value)
370  {
371  throw Unimplemented_Error(__FUNCTION__);
372  }
373 
374  /// Add a String8
375  /// @param location
376  /// @param value
377  /// @returns a RET, usually a bool
378  virtual RET set
379  (LOC location,
380  const String8& value)
381  {
382  throw Unimplemented_Error(__FUNCTION__);
383  }
384 
385  /// Add a String16
386  /// @param location
387  /// @param value
388  /// @returns a RET, usually a bool
389  virtual RET set
390  (LOC location,
391  const String16& value)
392  {
393  throw Unimplemented_Error(__FUNCTION__);
394  }
395 
396  /// Add a DateTime
397  /// @param location
398  /// @param value
399  /// @returns a RET, usually a bool
400  virtual RET set
401  (LOC location,
402  const DateTime& value)
403  {
404  throw Unimplemented_Error(__FUNCTION__);
405  }
406 
407  /// Add a BytesRef
408  /// @param location
409  /// @param value
410  /// @returns a RET, usually a bool
411  virtual RET set
412  (LOC location,
413  const BytesRef& value)
414  {
415  throw Unimplemented_Error(__FUNCTION__);
416  }
417  };
418 
419  // ================================ EXCEPTIONS ==============
420 
421  /// Thrown when you ask for a type that cannot be converted
422  /// E.g., asking for string data as an int64
423  /// @param T the to-type
424  template <typename T = void>
426  {
427  public:
428  /// @param fromType the actual type in the Table
429  /// @param toType the requested type
431  (BaseDataType fromType,
432  BaseDataType toType);
433 
434  /// @param fromType the actual type in the Table
435  /// NB takes toType from the template param
437  (BaseDataType fromType);
438  };
439 
440  template <>
442  (BaseDataType fromType)
443  {
445  }
446 
447  template <>
449  (BaseDataType fromType)
450  {
452  }
453 
455  {
456  static const char* INPUT_TAG(); // input section
457 
458  static const char* MAP_TAG; // how to map the data file (optional) (cf. DT)
459  static const char* REFS_TAG; // high-level structure (optional) (cf. DI)
460  static const char* STORE_TAG; // the actual data store (cf. DD)
461 
462  static const char* OVERWRITE_TAG; // overwrite on re-opening for write
463  };
464 
465  // ================================ TAGS ====================
466 
468  {
469  static const char* TYPE_TAG;
470  static const char* NONE_TAG;
471  static const char* SIZE_TAG;
472  static const char* INT_TAG;
473  static const char* UINT_TAG;
474  static const char* FLOAT_TAG;
475  static const char* DOUBLE_TAG;
476  static const char* STRING_TAG;
477  static const char* BYTES_TAG;
478  };
479 
481  {
482  static const char* DATETIME_TAG;
483  static const char* IPv4_TAG;
484  };
485 
486  // ==========================================================
487  // ==========================================================
488  // ==========================================================
489 
490  inline
492  (type t,
493  size_t s) :
494  ty(t),
495  size(s)
496  {
497  }
498 
499  template <>
500  inline
501  BaseDataType BaseDataType::stringType<String8>
502  ()
503  {
504  return BaseDataType(btString8);
505  }
506 
507  template <>
508  inline
509  BaseDataType BaseDataType::stringType<String16>
510  ()
511  {
512  return BaseDataType(btString16);
513  }
514 
515  inline
517  ()
518  const
519  {
520  return ty;
521  }
522 
523  inline
524  size_t BaseDataType::getSize
525  ()
526  const
527  {
528  return size;
529  }
530 
531  inline
533  (type t,
534  size_t s) :
535  BaseDataType(base_type(t, s)),
536  ty(t)
537  {
538  }
539 
540  inline
543  size_t s) :
544  BaseDataType(t, s),
545  ty(static_cast<type>(t))
546  {
547  }
548 
549  inline
551  ()
552  const
553  {
554  return ty;
555  }
556 
557  template<typename T>
558  inline
560  (BaseDataType fromType,
561  BaseDataType toType)
562  {
563  addParam(fromType.getName());
564  addParam(toType.getName());
565  }
566 }
567 
568 #endif
static const char * STORE_TAG
Definition: data.h:460
const Xpace_Char16 Xpace_Data_Type type
Definition: table_c.h:141
Copyright (C) 2012 Xpace, LLC.
static const char * DATETIME_TAG
Definition: data.h:482
static const char * OVERWRITE_TAG
Definition: data.h:462
A low-level data holder.
Definition: types_c.h:82
type getBaseType() const
Definition: data.h:517
Each high-level Xpace object has a Configuration.
Definition: config.h:29
const Xpace_Char16 Xpace_Data_Type uint bool * added
Definition: table_c.h:141
A Source is an interface to an object that provides access to each field or column of a data record o...
Definition: data.h:156
A string, Unicode UTF-16 and reference-counted.
Definition: types.h:269
A low-level const data holder.
Definition: types.h:165
A floatimg-point number with explicit mantissa and decimals TODO: normalize.
Definition: decimalfloat.h:33
get count before content (retrieval is completed before Sink is called)
Definition: data.h:256
static const char * REFS_TAG
Definition: data.h:459
Copyright (C) 2012 Xpace, LLC.
static const char * MAP_TAG
Definition: data.h:458
type getDerivedType() const
Definition: data.h:551
unsigned long long uint64
Definition: types.h:87
no count (content comes immediately, retrieval stops when Sink returns false)
Definition: data.h:253
static const char * INT_TAG
Definition: data.h:472
IncompatibleType(BaseDataType fromType, BaseDataType toType)
Definition: data.h:560
virtual String getName() const
const Xpace_Char16 * name
Sink callbacks for table data.
Definition: table_c.h:141
The non-LOC part of a Sink.
Definition: data.h:260
StoreAccess
Definition: data.h:136
static const char * UINT_TAG
Definition: data.h:473
Thrown when you ask for a type that cannot be converted E.g., asking for string data as an int64...
Definition: data.h:425
static const char * BYTES_TAG
Definition: data.h:477
uint int64 value
Definition: table_c.h:159
static const char * TYPE_TAG
Definition: data.h:469
General exceptions.
Definition: exception.h:96
static const char * SIZE_TAG
Definition: data.h:471
long long int64
Definition: types.h:86
static const char * NONE_TAG
Definition: data.h:470
static const char * FLOAT_TAG
Definition: data.h:474
String getTag() const
Xpace project main namespace
Definition: datetime.h:18
static const char * IPv4_TAG
Definition: data.h:483
size_t getSize() const
Definition: data.h:525
get count after content (content comes immediately; search resolution continues after Sink returns fa...
Definition: data.h:255
size_t size
Definition: data.h:75
static const char * DOUBLE_TAG
Definition: data.h:475
static const char * STRING_TAG
Definition: data.h:476
< no count (content comes immediately, retrieval stops when Sink returns false) but keep state for fu...
Definition: data.h:254
SinkNotifyCount
Sink is an interface to an object that receives data.
Definition: data.h:251
DerivedDataType(type, size_t size=0)
Definition: data.h:533

current as of Wed Jun 10 2026 12:00:05