User-Defined Function API  8.0-8
 All Classes Functions Enumerations Enumerator
MarkLogic.h
1 /* -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * Copyright 2002-2018 MarkLogic Corporation. All Rights Reserved.
4  *
5  */
29 #ifndef _MARKLOGIC_MARKLOGIC_H_
30 #define _MARKLOGIC_MARKLOGIC_H_
31 
32 #include <stddef.h>
33 
34 #ifdef _MSC_VER
35 typedef __int32 int32_t;
36 typedef unsigned __int32 uint32_t;
37 typedef __int64 int64_t;
38 typedef unsigned __int64 uint64_t;
39 #ifndef MLDLL
40 #define MLDLL __declspec(dllimport)
41 #endif
42 #else // !_MSC_VER
43 #include <stdint.h>
44 #define MLDLL
45 #endif
46 
47 #define MARKLOGIC_API_VERSION 2
48 
49 namespace marklogic
50 {
51 
52 class Map;
53 class MapImpl;
54 class SequenceImpl;
55 class StringImpl;
56 class LangStringImpl;
57 
59 class MLDLL RangeIndex
60 {
61 public:
70  enum Type {
71  INT,
73  LONG,
79  TIME,
80  DATE,
89  POINT
90  };
91 
93  enum Order {
94  ASCENDING,
95  DESCENDING
96  };
97 };
98 
99 template<class T, typename PT>
100 class MLDLL CTypeWrapperBase
101 {
102 protected:
103  PT val;
104 
105 public:
106  CTypeWrapperBase(): val(0) {}
107  CTypeWrapperBase(PT v): val(v) {}
108 
109  operator PT&() { return val; }
110  operator const PT&() const { return val; }
111 
112  bool operator<(T o) const { return val < o.val; }
113  bool operator<=(T o) const { return val <= o.val; }
114  bool operator>(T o) const { return val > o.val; }
115  bool operator>=(T o) const { return val >= o.val; }
116  bool operator==(T o) const { return val == o.val; }
117  bool operator!=(T o) const { return val != o.val; }
118 
119  T operator-(T o) const { return val - o.val; }
120  T operator+(T o) const { return val + o.val; }
121  T operator*(T o) const { return val * o.val; }
122  T operator/(T o) const { return val / o.val; }
123 
124  T operator--(int) { return val--; }
125  T operator++(int) { return val++; }
126  T& operator--() { --val; return (T&)*this; }
127  T& operator++() { ++val; return (T&)*this; }
128  T& operator-=(T o) { val -= o.val; return (T&)*this; }
129  T& operator+=(T o) { val += o.val; return (T&)*this; }
130  T& operator*=(T o) { val *= o.val; return (T&)*this; }
131  T& operator/=(T o) { val /= o.val; return (T&)*this; }
132 };
133 
134 template<class T, typename PT>
135 class MLDLL CTypeWrapperF : public CTypeWrapperBase<T,PT>
136 {
137 protected:
138  typedef CTypeWrapperF<T,PT> Base;
139 
140 public:
141  CTypeWrapperF() {}
143 
144  T operator-() const { return -this->val; }
145  T operator+() const { return +this->val; }
146 };
147 
148 template<class T, typename PT>
149 class MLDLL CTypeWrapperU : public CTypeWrapperBase<T,PT>
150 {
151 protected:
152  typedef CTypeWrapperU<T,PT> Base;
153 
154 public:
155  CTypeWrapperU() {}
157 
158  T operator%(T o) const { return this->val % o.val; }
159  T& operator%=(T o) { this->val %= o.val; return (T&)*this; }
160 };
161 
162 template<class T, typename PT>
163 class MLDLL CTypeWrapper : public CTypeWrapperU<T,PT>
164 {
165 protected:
166  typedef CTypeWrapper<T,PT> Base;
167 
168 public:
169  CTypeWrapper() {}
170  CTypeWrapper(PT v): CTypeWrapperU<T,PT>(v) {}
171 
172  T operator-() const { return -this->val; }
173  T operator+() const { return +this->val; }
174 };
175 
176 
177 #ifdef _MSC_VER
178 typedef double DecimalType;
179 #else
180 typedef long double DecimalType;
181 #endif
182 
183 class MLDLL Decimal : public CTypeWrapperF<Decimal,DecimalType>
184 {
185 public:
186  Decimal(): Base() {}
187  Decimal(DecimalType v): Base(v) {}
188 };
189 
191 class MLDLL DateTime : public CTypeWrapper<DateTime,int64_t>
192 {
193 public:
194  DateTime(): Base() {}
195  DateTime(int64_t v): Base(v) {}
196 };
197 
199 class MLDLL Date : public CTypeWrapper<Date,int64_t>
200 {
201 public:
202  Date(): Base() {}
203  Date(int64_t v): Base(v) {}
204 };
205 
207 class MLDLL Time : public CTypeWrapper<Time,int64_t>
208 {
209 public:
210  Time(): Base() {}
211  Time(int64_t v): Base(v) {}
212 };
213 
215 class MLDLL GYearMonth : public CTypeWrapper<GYearMonth,int>
216 {
217 public:
218  GYearMonth(): Base() {}
219  GYearMonth(int v): Base(v) {}
220 };
221 
223 class MLDLL GYear : public CTypeWrapper<GYear,int>
224 {
225 public:
226  GYear(): Base() {}
227  GYear(int v): Base(v) {}
228 };
229 
231 class MLDLL GMonth : public CTypeWrapperU<GMonth,unsigned>
232 {
233 public:
234  GMonth(): Base() {}
235  GMonth(unsigned v): Base(v) {}
236 };
237 
239 class MLDLL GDay : public CTypeWrapperU<GDay,unsigned>
240 {
241 public:
242  GDay(): Base() {}
243  GDay(unsigned v): Base(v) {}
244 };
245 
247 class MLDLL YearMonthDuration : public CTypeWrapper<YearMonthDuration,int>
248 {
249 public:
250  YearMonthDuration(): Base() {}
251  YearMonthDuration(int v): Base(v) {}
252 };
253 
255 class MLDLL DayTimeDuration : public CTypeWrapper<DayTimeDuration,int64_t>
256 {
257 public:
258  DayTimeDuration(): Base() {}
259  DayTimeDuration(int64_t v): Base(v) {}
260 };
261 
264 class MLDLL String
265 {
266 public:
267  String(StringImpl* =0);
268  String(const char* _str, const char* _collation = 0);
269  String(const String& o);
270  String& operator=(const String& o);
271  ~String();
272 
273  operator bool() const { return str!=0; }
274 
275  operator const char*() const;
276  const char* get() const;
277  size_t length() const;
278  const char* collation() const;
279 
280  bool operator<(const String& o) const;
281  bool operator<=(const String& o) const;
282  bool operator>(const String& o) const;
283  bool operator>=(const String& o) const;
284  bool operator==(const String& o) const;
285  bool operator!=(const String& o) const;
286 
287 protected:
288  StringImpl* str;
289 };
290 
293 class MLDLL LangString
294 {
295 public:
296  LangString(LangStringImpl* =0);
297  LangString(const char* _str, const char* _language = 0);
298  LangString(const LangString& o);
299  LangString& operator=(const LangString& o);
300  ~LangString();
301 
302  operator bool () const { return str!=0; }
303 
304  operator const char*() const;
305  const char* get() const;
306  size_t length() const;
307  const char* language() const;
308 
309  bool operator<(const LangString& o) const;
310  bool operator<=(const LangString& o) const;
311  bool operator>(const LangString& o) const;
312  bool operator>=(const LangString& o) const;
313  bool operator==(const LangString& o) const;
314  bool operator!=(const LangString& o) const;
315 
316 protected:
317  LangStringImpl* str;
318 };
319 
321 class MLDLL Point
322 {
323 protected:
324  float lat;
325  float lng;
326  const void* coord;
327 
328 public:
329  Point();
330  Point(float _lat, float _lng, const char* _coordinateSystem = "wgs84");
331 
332  float latitude() const;
333  float longitude() const;
334  const char* coordinateSystem() const;
335 
336  bool operator<(const Point& o) const;
337  bool operator<=(const Point& o) const;
338  bool operator>(const Point& o) const;
339  bool operator>=(const Point& o) const;
340  bool operator==(const Point& o) const;
341  bool operator!=(const Point& o) const;
342 };
343 
369 class MLDLL TupleIterator
370 {
371 public:
373  virtual void next() =0;
375  virtual bool done() const =0;
376 
377  // Information about this tuple iterator
379  virtual bool descending() const =0;
381  virtual size_t width() const =0;
383  virtual RangeIndex::Type type(size_t i) const =0;
387  virtual const char* collation(size_t i) const =0;
391  virtual const char* coordinateSystem(size_t i) const =0;
392 
393  // Information about the current tuple
400  virtual uint64_t frequency() const =0;
404  virtual bool null(size_t i) const =0;
405 
418  virtual void value(size_t i, int&) const =0;
419  virtual void value(size_t i, unsigned&) const =0;
420  virtual void value(size_t i, int64_t&) const =0;
421  virtual void value(size_t i, uint64_t&) const =0;
422  virtual void value(size_t i, float&) const =0;
423  virtual void value(size_t i, double&) const =0;
424  virtual void value(size_t i, Decimal&) const =0;
425  virtual void value(size_t i, DateTime&) const =0;
426  virtual void value(size_t i, Date&) const =0;
427  virtual void value(size_t i, Time&) const =0;
428  virtual void value(size_t i, GYearMonth&) const =0;
429  virtual void value(size_t i, GYear&) const =0;
430  virtual void value(size_t i, GMonth&) const =0;
431  virtual void value(size_t i, GDay&) const =0;
432  virtual void value(size_t i, YearMonthDuration&) const =0;
433  virtual void value(size_t i, DayTimeDuration&) const =0;
434  virtual void value(size_t i, String&) const =0;
435  virtual void value(size_t i, Point&) const =0;
436  virtual void value(size_t i, LangString&) const =0;
439 protected:
440  TupleIterator();
441  virtual ~TupleIterator();
442 private:
444  TupleIterator &operator=(const TupleIterator&);
445 };
446 
460 class MLDLL OutputSequence
461 {
462 public:
466  virtual void writeValue(int) =0;
467  virtual void writeValue(unsigned) =0;
468  virtual void writeValue(int64_t) =0;
469  virtual void writeValue(uint64_t) =0;
470  virtual void writeValue(float) =0;
471  virtual void writeValue(double) =0;
472  virtual void writeValue(Decimal) =0;
473  virtual void writeValue(DateTime) =0;
474  virtual void writeValue(Date) =0;
475  virtual void writeValue(Time) =0;
476  virtual void writeValue(GYearMonth) =0;
477  virtual void writeValue(GYear) =0;
478  virtual void writeValue(GMonth) =0;
479  virtual void writeValue(GDay) =0;
480  virtual void writeValue(YearMonthDuration) =0;
481  virtual void writeValue(DayTimeDuration) =0;
482  virtual void writeValue(const String&) =0;
483  virtual void writeValue(Point) =0;
484  virtual void writeValue(bool) =0;
485  virtual void writeValue(const LangString&) =0;
513  virtual void startMap() =0;
517  virtual void writeMapKey(const char*) =0;
519  virtual void endMap() =0;
522 protected:
523  OutputSequence();
524  virtual ~OutputSequence();
525 private:
527  OutputSequence &operator=(const OutputSequence&);
528 };
529 
536 class MLDLL Encoder
537 {
538 public:
539  virtual void encode(const void*, size_t) =0;
540 
541  virtual void encode(int) =0;
542  virtual void encode(unsigned) =0;
543  virtual void encode(int64_t) =0;
544  virtual void encode(uint64_t) =0;
545  virtual void encode(float) =0;
546  virtual void encode(double) =0;
547  virtual void encode(Decimal) =0;
548  virtual void encode(DateTime) =0;
549  virtual void encode(Date) =0;
550  virtual void encode(Time) =0;
551  virtual void encode(GYearMonth) =0;
552  virtual void encode(GYear) =0;
553  virtual void encode(GMonth) =0;
554  virtual void encode(GDay) =0;
555  virtual void encode(YearMonthDuration) =0;
556  virtual void encode(DayTimeDuration) =0;
557  virtual void encode(const String&) =0;
558  virtual void encode(Point) =0;
559  virtual void encode(bool) =0;
560  virtual void encode(const LangString&) =0;
561 
562 protected:
563  Encoder();
564  virtual ~Encoder();
565 private:
566  Encoder(const Encoder&);
567  Encoder &operator=(const Encoder&);
568 };
569 
576 class MLDLL Decoder
577 {
578 public:
579  virtual void decode(const void*&, size_t&) =0;
580  virtual void decode(void*, size_t) =0;
581 
582  virtual void decode(int&) =0;
583  virtual void decode(unsigned&) =0;
584  virtual void decode(int64_t&) =0;
585  virtual void decode(uint64_t&) =0;
586  virtual void decode(float&) =0;
587  virtual void decode(double&) =0;
588  virtual void decode(Decimal&) =0;
589  virtual void decode(DateTime&) =0;
590  virtual void decode(Date&) =0;
591  virtual void decode(Time&) =0;
592  virtual void decode(GYearMonth&) =0;
593  virtual void decode(GYear&) =0;
594  virtual void decode(GMonth&) =0;
595  virtual void decode(GDay&) =0;
596  virtual void decode(YearMonthDuration&) =0;
597  virtual void decode(DayTimeDuration&) =0;
598  virtual void decode(String&) =0;
599  virtual void decode(Point&) =0;
600  virtual void decode(bool&) =0;
601  virtual void decode(LangString&) =0;
602 
603 protected:
604  Decoder();
605  virtual ~Decoder();
606 private:
607  Decoder(const Decoder&);
608  Decoder &operator=(const Decoder&);
609 };
610 
622 class MLDLL Reporter
623 {
624 public:
626  enum LogLevel {
627  None,
628  Finest,
629  Finer,
630  Fine,
631  Debug,
632  Config,
633  Info,
634  Notice,
635  Warning,
636  Error,
637  Critical,
638  Alert,
639  Emergency
640  };
641 
646  virtual LogLevel logLevel() const =0;
648  virtual void log(LogLevel level, const char* message) =0;
655  virtual void error(const char* message) =0;
656 
657 protected:
658  Reporter();
659  virtual ~Reporter();
660 private:
661  Reporter(const Reporter&);
662  Reporter &operator=(const Reporter&);
663 };
664 
666 class MLDLL Item
667 {
668 public:
676  enum Type {
677  // Atomic types
678  INT,
697 
698  // QNAME,
699  // NOTATION,
700  // DURATION,
701  // G_MONTH_DAY,
702  // BASE_64_BINARY,
703  // HEX_BINARY,
704  // UNTYPED_ATOMIC,
705 
706  // MarkLogic types
708  MAP,
709 
710  // RDF types
712 
713  // BOX,
714  // CIRCLE,
715  // POLYGON,
716  // LINESTRING,
717  // COMPLEX_POLYGON,
718  // ARRAY,
719 
720  // // Nodes
721  // DOCUMENT,
722  // ELEMENT,
723  // ATTRIBUTE,
724  // TEXT,
725  // PROCESSING_INSTRUCTION,
726  // COMMENT,
727  // NAMESPACE,
728  // BINARY,
729 
730  // FUNCTION,
731 
732  OTHER
733  };
734 };
735 
746 class MLDLL Sequence
747 {
748 public:
749  Sequence(SequenceImpl* =0);
750  Sequence(const Sequence&);
751  Sequence& operator=(const Sequence&);
752  ~Sequence();
753 
755  operator bool() const { return seq!=0; }
756 
760  void next();
762  bool done() const;
763 
765  Item::Type type() const;
769  uint64_t frequency() const;
770 
774  void value(int&) const;
775  void value(unsigned&) const;
776  void value(int64_t&) const;
777  void value(uint64_t&) const;
778  void value(float&) const;
779  void value(double&) const;
780  void value(Decimal&) const;
781  void value(DateTime&) const;
782  void value(Date&) const;
783  void value(Time&) const;
784  void value(GYearMonth&) const;
785  void value(GYear&) const;
786  void value(GMonth&) const;
787  void value(GDay&) const;
788  void value(YearMonthDuration&) const;
789  void value(DayTimeDuration&) const;
790  void value(String&) const;
791  void value(bool&) const;
792 
793  void value(Point&) const;
794  void value(Map&) const;
795  void value(LangString&) const;
798 private:
799  SequenceImpl* seq;
800 };
801 
803 class MLDLL Map
804 {
805 public:
806  Map(MapImpl* =0);
807  Map(const Map&);
808  Map& operator=(const Map&);
809  ~Map();
810 
812  operator bool() const { return map!=0; }
813 
815  Sequence get(const char* key) const;
816 
817 private:
818  MapImpl* map;
819 };
820 
852 class MLDLL AggregateUDF
853 {
854 public:
855  // Management methods
868  virtual AggregateUDF* clone() const =0;
874  virtual void close() =0;
875 
888  virtual void start(Sequence& arg, Reporter& r) =0;
900  virtual void finish(OutputSequence& os, Reporter& r) =0;
901 
902  // Map/reduce methods
914  virtual void map(TupleIterator& values, Reporter& r) =0;
930  virtual void reduce(const AggregateUDF* o, Reporter& r) =0;
931 
932  // Serialize/deserialize
944  virtual void encode(Encoder& e, Reporter& r) =0;
956  virtual void decode(Decoder& d, Reporter& r) =0;
957 
966  virtual RangeIndex::Order getOrder() const;
967 
968 protected:
969  // Detect DLLs compiled against a different API version
983  AggregateUDF(unsigned version = MARKLOGIC_API_VERSION);
984  virtual ~AggregateUDF();
985 };
986 
998 typedef AggregateUDF*(*AggregateFunction)();
999 
1023 class MLDLL Registry
1024 {
1025 public:
1042  void version(unsigned pluginVersion = makeVersion(__DATE__,__TIME__),
1043  unsigned marklogicVersion = MARKLOGIC_API_VERSION);
1044 
1061  template<class T> void registerAggregate(const char* name);
1081  virtual void registerAggregate(const char* name, AggregateFunction f) =0;
1082 
1083 protected:
1084  Registry();
1085  virtual ~Registry();
1086 private:
1087  Registry(const Registry&);
1088  Registry &operator=(const Registry&);
1089  static unsigned makeVersion(const char*,const char*);
1090 protected:
1091  unsigned pluginVersion;
1092 };
1093 
1094 template<class T>
1095 inline AggregateUDF*
1096 construct()
1097 {
1098  return new T();
1099 }
1100 
1101 template<class T>
1102 inline void Registry::
1103 registerAggregate(const char* name)
1104 {
1105  registerAggregate(name, marklogic::construct<T>);
1106 }
1107 
1108 }
1109 
1110 #endif
Encapsulation of XQuery sequence item value type.
Definition: MarkLogic.h:666
xs:gYearMonth
Definition: MarkLogic.h:688
C++ representation of the XQuery type xs:decimal.
Definition: MarkLogic.h:183
Encapsulation of a User Defined Function for performing aggregate analysis across co-occurrences in r...
Definition: MarkLogic.h:852
xs:gDay
Definition: MarkLogic.h:691
xs:gMonth
Definition: MarkLogic.h:690
xs:unsignedLong
Definition: MarkLogic.h:681
C++ representation of the XQuery type xs:gYear.
Definition: MarkLogic.h:223
xs:date
Definition: MarkLogic.h:687
Order
Whether the index values are in ascending or descending order.
Definition: MarkLogic.h:93
Serialize values.
Definition: MarkLogic.h:536
C++ representation of the MarkLogic XQuery cts:point.
Definition: MarkLogic.h:321
Type
The type of the values in a range index.
Definition: MarkLogic.h:70
Definition: MarkLogic.h:135
C++ representation of the RDF type rdf:langString, encapsulating a string value with its language...
Definition: MarkLogic.h:293
C++ representation of the XQuery type xs:time.
Definition: MarkLogic.h:207
A sequence of values produced by your AggregateUDF::finish implementation.
Definition: MarkLogic.h:460
xs:string
Definition: MarkLogic.h:87
xs:gYearMonth
Definition: MarkLogic.h:81
Type
The types of item values that can occur in a Sequence.
Definition: MarkLogic.h:676
xs:int
Definition: MarkLogic.h:678
C++ representation of the XQuery type xs:dateTime.
Definition: MarkLogic.h:191
cts:point (MarkLogic XQuery dialect)
Definition: MarkLogic.h:707
xs:time
Definition: MarkLogic.h:686
xs:long
Definition: MarkLogic.h:680
void registerAggregate(const char *name)
Register an AggregateUDF implementation with MarkLogic Server.
Definition: MarkLogic.h:1103
C++ representation of the XQuery type xs:gYearMonth.
Definition: MarkLogic.h:215
xs:time
Definition: MarkLogic.h:79
xs:float
Definition: MarkLogic.h:75
LogLevel
Available log levels.
Definition: MarkLogic.h:626
A key-value map, equivalent to the MarkLogic XQuery type map:map.
Definition: MarkLogic.h:803
xs:float
Definition: MarkLogic.h:682
xs:yearMonthDuration
Definition: MarkLogic.h:85
Log messages and report errors to MarkLogic Server. You do not need to subclass this class...
Definition: MarkLogic.h:622
xs:date
Definition: MarkLogic.h:80
xs:unsignedInt
Definition: MarkLogic.h:679
Encapsulation of the configuration of a range index.
Definition: MarkLogic.h:59
xs:boolean
Definition: MarkLogic.h:696
xs:yearMonthDuration
Definition: MarkLogic.h:692
xs:int
Definition: MarkLogic.h:71
rdf:langString (SPARQL)
Definition: MarkLogic.h:711
xs:gMonth
Definition: MarkLogic.h:83
C++ representation of the XQuery type xs:dayTimeDuration.
Definition: MarkLogic.h:255
Definition: MarkLogic.h:100
C++ representation of the XQuery type xs:string, encapsulating a string value with its collation...
Definition: MarkLogic.h:264
xs:anyURI
Definition: MarkLogic.h:695
xs:anyURI
Definition: MarkLogic.h:88
xs:double
Definition: MarkLogic.h:683
The MarkLogic Server native plugin registry.
Definition: MarkLogic.h:1023
xs:decimal
Definition: MarkLogic.h:77
A sequence of Item values.
Definition: MarkLogic.h:746
Iterator over a sequence of N-way co-occurrences of range index values from a single stand...
Definition: MarkLogic.h:369
xs:dayTimeDuration
Definition: MarkLogic.h:86
xs:gYear
Definition: MarkLogic.h:689
C++ representation of the XQuery type xs:gMonth.
Definition: MarkLogic.h:231
map:map (MarkLogic XQuery dialect)
Definition: MarkLogic.h:708
xs:decimal
Definition: MarkLogic.h:684
xs:double
Definition: MarkLogic.h:76
C++ representation of the XQuery type xs:date.
Definition: MarkLogic.h:199
C++ representation of the XQuery type xs:gDay.
Definition: MarkLogic.h:239
xs:gYear
Definition: MarkLogic.h:82
xs:dateTime
Definition: MarkLogic.h:78
xs:gDay
Definition: MarkLogic.h:84
xs:unsignedInt
Definition: MarkLogic.h:72
xs:string
Definition: MarkLogic.h:694
xs:long
Definition: MarkLogic.h:73
Definition: MarkLogic.h:149
xs:dateTime
Definition: MarkLogic.h:685
C++ representation of the XQuery type xs:yearMonthDuration.
Definition: MarkLogic.h:247
xs:unsignedLong
Definition: MarkLogic.h:74
De-serialize values.
Definition: MarkLogic.h:576
Definition: MarkLogic.h:163
xs:dayTimeDuration
Definition: MarkLogic.h:693