206ac519afc165f5cd7956ea12992e63ca44726f
[platform/upstream/ejdb.git] / src / ejdb / ejdb_private.h
1 /**************************************************************************************************
2  *  EJDB database library http://ejdb.org
3  *  Copyright (C) 2012-2015 Softmotions Ltd <info@softmotions.com>
4  *
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  *************************************************************************************************/
16
17 #ifndef EJDB_PRIVATE_H
18 #define EJDB_PRIVATE_H
19
20 #include "ejdb.h"
21 #include "tcutil.h"
22 #include "tctdb.h"
23 #include "tchdb.h"
24
25 #include <assert.h>
26
27 EJDB_EXTERN_C_START
28
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)
32
33 #define EJDB_MAX_COLLECTIONS 1024
34
35
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 */
42 };
43
44 struct EJDB {
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 */
49 };
50
51 enum { /**> Query field flags */
52     // Comparison flags
53     EJCOMPGT = 1, /**> Comparison GT */
54     EJCOMPGTE = 1 << 1, /**> Comparison GTE */
55     EJCOMPLT = 1 << 2, /**> Comparison LT */
56     EJCOMPLTE = 1 << 3, /**> Comparison LTE */
57     EJCONDSTARTWITH = 1 << 4, /**> Starts with */
58
59     EJFEXCLUDED = 1 << 5, /**> If query field excluded from matching */
60     EJFNOINDEX = 1 << 6, /**> Do not use index for field */
61     EJFORDERUSED = 1 << 7, /**> This ordering field was used */
62     EJFPKMATCHING = 1 << 8, /**> _id PK field matching */
63
64     EJCONDICASE = 1 << 9, /**> Ignore case in matching */
65
66     EJCONDSET = 1 << 10, /**> $set Set field update operation */
67     EJCONDINC = 1 << 11, /**> $inc Inc field update operation */
68     EJCONDADDSET = 1 << 12, /**> $addToSet Adds value to the array only if its not in the array already.  */
69     EJCONDPULL = 1 << 13, /**> $pull Removes all occurrences of value from field, if field is an array */
70     EJCONDUPSERT = 1 << 14, /**> $upsert Upsert $set operation */
71     EJCONDALL = 1 << 15, /**> 'All' modificator for $pull or $addToSet ($addToSetAll or $pullAll) */
72     EJCONDOIT = 1 << 16, /**> $do query field operation */
73     EJCONDUNSET = 1 << 17, /**> $unset Field value */
74     EJCONDRENAME = 1 << 18 /**> $rename Field value */
75 };
76
77 enum { /**> Query flags */
78     EJQINTERNAL = 1, /**> Internal query object used in _ejdbqryexecute */
79     EJQUPDATING = 1 << 1, /**> Query in updating mode */
80     EJQDROPALL = 1 << 2, /**> Drop bson object if matched */
81     EJQONLYCOUNT = 1 << 3, /**> Only count mode */
82     EJQHASUQUERY = 1 << 4 /**> It means the query contains update $(query) fields #91 */
83 };
84
85 typedef struct { /**> $(query) matchin slot used in update $ placeholder processing. #91 */
86     int32_t mpos; /**> array position of matched element */
87     int32_t dpos; /**> $ position in the fieldpath */
88     const void *op; /**> Opaque pointer associated with slot */
89 } USLOT;
90
91 struct EJQF { /**> Matching field and status */
92     bool negate; /**> Negate expression */
93     int fpathsz; /**>JSON field path size */
94     int exprsz; /**> Size of query operand expression */
95     int tcop; /**> Matching operation eg. TDBQCSTREQ */
96     bson_type ftype; /**> BSON field type */
97     uint32_t flags; /**> Various field matching|status flags */
98     uint32_t mflags; /**> Temporary matching flags used during single record matching */
99     int order; /**> 0 no order, 1 ASC, -1 DESC */
100     int orderseq; /**> Seq number for order fields */
101     int elmatchgrp; /**> $elemMatch group id */
102     int elmatchpos; /**> $elemMatch fieldpath position */
103     char *fpath; /**>JSON field path */
104     char *expr; /**> Query operand expression, string or TCLIST data */
105     const TDBIDX *idx; /**> Column index for this field if exists */
106     bson *idxmeta; /**> Index metainfo */
107     bson *updateobj; /**> Update bson object for $set and $inc operations */
108     TCLIST *exprlist; /**> List representation of expression */
109     TCMAP *exprmap; /**> Hash map for expression tokens used in $in matching operation. */
110     void *regex; /**> Regular expression object */
111     EJDB *jb; /**> Reference to the EJDB during query processing */
112     EJQ *q; /**> Query object in which this field embedded */
113     double exprdblval; /**> Double value representation */
114     int64_t exprlongval; /**> Integer value represeintation */
115     TCLIST *ufields; /**> Update $(query) prositional fields #91 */
116     TCLIST *uslots; /**> $(query) matching slots USLOT #91 */
117 };
118 typedef struct EJQF EJQF;
119
120 struct EJQ { /**> Query object. */
121     TCLIST *qflist; /**> List of query field objects *EJQF */
122     TCLIST *orqlist; /**> List of $or joined query objects *EJQ */
123     TCLIST *andqlist; /**> List of $and joined query objects *EJQ */
124     bson *hints; /**> Hints bson object */
125     /**> Include $(projection) fields char* names.
126      *  Mapping EJQF fpath => $(projection) field name
127      *  http://docs.mongodb.org/manual/reference/projection/positional/#proj._S_
128      */
129     TCMAP *ifields;
130     uint32_t skip; /**> Number of records to skip. */
131     uint32_t max; /**> Max number of results */
132     uint32_t flags; /**> Control flags */
133     EJQ *lastmatchedorq; /**> Reference to the last matched $or query */
134     EJQF **allqfields; /**> NULL terminated list of all *EJQF fields including all $and $or QF*/
135
136     //Temporal buffers used during query processing
137     TCXSTR *colbuf; /**> TCTDB current column buffer */
138     TCXSTR *bsbuf; /**> current bson object buff */
139     TCXSTR *tmpbuf; /**> Tmp buffer */
140 };
141
142 #define JDBCOLBSON "$"  /**> TCDB colname with BSON byte data */
143 #define JDBCOLBSONL 1  /**> TCDB colname with BSON byte data columen len */
144
145
146 #define JBINOPTMAPTHRESHOLD 16 /**> If number of tokens in `$in` array exeeds it then TCMAP will be used in fullscan matching of tokens */
147
148
149 EJDB_EXPORT bool ejcollockmethod(EJCOLL *coll, bool wr);
150 EJDB_EXPORT bool ejcollunlockmethod(EJCOLL *coll);
151
152 EJDB_EXTERN_C_END
153
154 #endif        /* EJDB_PRIVATE_H */
155