1 /**************************************************************************************************
2 * EJDB database library http://ejdb.org
3 * Copyright (C) 2012-2015 Softmotions Ltd <info@softmotions.com>
5 * This file is part of EJDB.
6 * EJDB is free software; you can redistribute it and/or modify it under the terms of
7 * the GNU Lesser General Public License as published by the Free Software Foundation; either
8 * version 2.1 of the License or any later version. EJDB is distributed in the hope
9 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 * License for more details.
12 * You should have received a copy of the GNU Lesser General Public License along with EJDB;
13 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
14 * Boston, MA 02111-1307 USA.
15 *************************************************************************************************/
17 #ifndef EJDB_PRIVATE_H
18 #define EJDB_PRIVATE_H
29 #define BSON_IS_IDXSUPPORTED_TYPE(atype) (atype == BSON_STRING || \
30 atype == BSON_INT || atype == BSON_LONG || atype == BSON_DOUBLE || \
31 atype == BSON_ARRAY || atype == BSON_DATE)
33 #define EJDB_MAX_COLLECTIONS 1024
36 struct EJCOLL { /**> EJDB Collection. */
37 char *cname; /**> Collection name. */
38 int cnamesz; /**> Collection name length. */
39 TCTDB *tdb; /**> Collection TCTDB. */
40 EJDB *jb; /**> Database handle. */
41 void *mmtx; /*> Mutex for method */
45 EJCOLL * cdbs[EJDB_MAX_COLLECTIONS]; /*> Collection DBs for JSON collections. */
46 int cdbsnum; /*> Count of collection DB. */
47 TCTDB *metadb; /*> Metadata DB. */
48 void *mmtx; /*> Mutex for method */
51 enum { /**> Query field flags */
53 EJCOMPGT = 1u, /**> Comparison GT */
54 EJCOMPGTE = 1u << 1, /**> Comparison GTE */
55 EJCOMPLT = 1u << 2, /**> Comparison LT */
56 EJCOMPLTE = 1u << 3, /**> Comparison LTE */
57 EJCONDSTARTWITH = 1u << 4, /**> Starts with */
59 EJFEXCLUDED = 1u << 5, /**> If query field excluded from matching */
60 EJFNOINDEX = 1u << 6, /**> Do not use index for field */
61 EJFORDERUSED = 1u << 7, /**> This ordering field was used */
62 EJFPKMATCHING = 1u << 8, /**> _id PK field matching */
64 EJCONDICASE = 1u << 9, /**> Ignore case in matching */
66 EJCONDSET = 1u << 10, /**> $set Set field update operation */
67 EJCONDINC = 1u << 11, /**> $inc Inc field update operation */
68 EJCONDADDSET = 1u << 12, /**> $addToSet, $addToSetAll. Adds a value to the array only if its not in the array already. */
69 EJCONDPULL = 1u << 13, /**> $pull Removes all occurrences of value from field, if field is an array */
70 EJCONDUPSERT = 1u << 14, /**> $upsert Upsert $set operation */
71 EJCONDALL = 1u << 15, /**> 'All' modificator for $pull(All), $addToSet(All), $push(All) */
72 EJCONDOIT = 1u << 16, /**> $do query field operation */
73 EJCONDUNSET = 1u << 17, /**> $unset Field value */
74 EJCONDRENAME = 1u << 18, /**> $rename Field value */
75 EJCONDPUSH = 1u << 19 /**> $push, $pushAll. Adds a value to the array */
78 enum { /**> Query flags */
79 EJQINTERNAL = 1, /**> Internal query object used in _ejdbqryexecute */
80 EJQUPDATING = 1u << 1, /**> Query in updating mode */
81 EJQDROPALL = 1u << 2, /**> Drop bson object if matched */
82 EJQONLYCOUNT = 1u << 3, /**> Only count mode */
83 EJQHASUQUERY = 1u << 4 /**> It means the query contains update $(query) fields #91 */
86 typedef struct { /**> $(query) matchin slot used in update $ placeholder processing. #91 */
87 int32_t mpos; /**> array position of matched element */
88 int32_t dpos; /**> $ position in the fieldpath */
89 const void *op; /**> Opaque pointer associated with slot */
92 struct EJQF { /**> Matching field and status */
93 bool negate; /**> Negate expression */
94 int fpathsz; /**>JSON field path size */
95 int exprsz; /**> Size of query operand expression */
96 int tcop; /**> Matching operation eg. TDBQCSTREQ */
97 bson_type ftype; /**> BSON field type */
98 uint32_t flags; /**> Various field matching|status flags */
99 uint32_t mflags; /**> Temporary matching flags used during single record matching */
100 int order; /**> 0 no order, 1 ASC, -1 DESC */
101 int orderseq; /**> Seq number for order fields */
102 int elmatchgrp; /**> $elemMatch group id */
103 int elmatchpos; /**> $elemMatch fieldpath position */
104 char *fpath; /**>JSON field path */
105 char *expr; /**> Query operand expression, string or TCLIST data */
106 const TDBIDX *idx; /**> Column index for this field if exists */
107 bson *idxmeta; /**> Index metainfo */
108 bson *updateobj; /**> Update bson object for $set and $inc operations */
109 TCLIST *exprlist; /**> List representation of expression */
110 TCMAP *exprmap; /**> Hash map for expression tokens used in $in matching operation. */
111 void *regex; /**> Regular expression object */
112 EJDB *jb; /**> Reference to the EJDB during query processing */
113 EJQ *q; /**> Query object in which this field embedded */
114 double exprdblval; /**> Double value representation */
115 int64_t exprlongval; /**> Integer value represeintation */
116 TCLIST *ufields; /**> Update $(query) prositional fields #91 */
117 TCLIST *uslots; /**> $(query) matching slots USLOT #91 */
119 typedef struct EJQF EJQF;
121 struct EJQ { /**> Query object. */
122 TCLIST *qflist; /**> List of query field objects *EJQF */
123 TCLIST *orqlist; /**> List of $or joined query objects *EJQ */
124 TCLIST *andqlist; /**> List of $and joined query objects *EJQ */
125 bson *hints; /**> Hints bson object */
126 /**> Include $(projection) fields char* names.
127 * Mapping EJQF fpath => $(projection) field name
128 * http://docs.mongodb.org/manual/reference/projection/positional/#proj._S_
131 uint32_t skip; /**> Number of records to skip. */
132 uint32_t max; /**> Max number of results */
133 uint32_t flags; /**> Control flags */
134 EJQ *lastmatchedorq; /**> Reference to the last matched $or query */
135 EJQF **allqfields; /**> NULL terminated list of all *EJQF fields including all $and $or QF*/
137 //Temporal buffers used during query processing
138 TCXSTR *colbuf; /**> TCTDB current column buffer */
139 TCXSTR *bsbuf; /**> current bson object buff */
140 TCXSTR *tmpbuf; /**> Tmp buffer */
143 #define JDBCOLBSON "$" /**> TCDB colname with BSON byte data */
144 #define JDBCOLBSONL 1 /**> TCDB colname with BSON byte data columen len */
147 #define JBINOPTMAPTHRESHOLD 16 /**> If number of tokens in `$in` array exeeds it then TCMAP will be used in fullscan matching of tokens */
150 EJDB_EXPORT bool ejcollockmethod(EJCOLL *coll, bool wr);
151 EJDB_EXPORT bool ejcollunlockmethod(EJCOLL *coll);
155 #endif /* EJDB_PRIVATE_H */