~zorba-coders/zorba/jsoniq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef ZORBA_SIMPLE_STORE
#define ZORBA_SIMPLE_STORE

#include "shared_types.h"
#include "store_defs.h"
#include "hashmap_nodep.h"

#if (defined (WIN32) || defined (WINCE))
#include "node_items.h"
#include "store/api/collection.h"
#include "store/api/index.h"
#include "store/api/ic.h"
#endif

#include "store/api/store.h"

#include "store/util/hashmap_stringbuf.h"

#include "zorbautils/mutex.h"
#include "zorbautils/lock.h"
#include "zorbautils/hashmap_itemp.h"
#include "zorbautils/hashmap_zstring_nonserializable.h"

namespace zorba
{
  class StoreManager;

class XQueryDiagnostics;

namespace store
{
  class IteratorFactory;
  class ValueIndexSpecification;
}

namespace simplestore
{

class StringPool;
class QNamePool;
class XmlLoader;
class FastXmlLoader;
class Index;
class BasicItemFactory;
class NodeFactory;
class PULPrimitiveFactory;

typedef zorba::HashMapZString<XmlNode_t> DocumentSet;
typedef ItemPointerHashMap<store::Index_t> IndexSet;
typedef ItemPointerHashMap<store::IC_t> ICSet;



/*******************************************************************************
  theSchemaTypeNames:
  -------------------
  Maps each enum value from SchemaTypeNames (see store/api/xs_types_enum.h) to 
  its associated QName item.

  theSchemaTypeCodes:
  -------------------

  theCollectionCounter:
  ---------------------
  Incremented every time a new collection is created. The current value of the  
  counter is then assigned as the id of the new collection.

  theNamespacePool:
  -----------------

  theQNamePool:
  -------------

  theItemFactory:
  ---------------
  Factory to create items.

  theIteratorFactory:
  -------------------
  Factory to create iterators.

  theNodeFactory:
  ---------------
  Factory to create node items.

  theDocuments:
  -------------
  A hashmap that for each xml tree that does not belong to any collection, maps
  the URI of the tree to the root node of the tree.

  theCollections:
  ---------------
  Container which contains the collections of the store. It includes a map that
  maps the qname of each collection to the collection's container object.

  theIndices:  
  -----------
  A hashmap that for each index, maps the qname of the index to the index 
  container object.
  
  theICs:
  -------
  A hashmap the for each integrity constraint, maps the qname of the ic to the
  ic's container object.

  theReferencesToNodeMap:
  -----------------------
  A hashmap that maps node references to the referenced nodes

  theNodeToReferencesMap:
  -----------------------
  A hashmap that maps nodes into their references

********************************************************************************/
class SimpleStore : public store::Store
{
  friend class zorba::StoreManager;

  typedef std::map<const zstring, const store::Item*> RefNodeMap;
  typedef NodePointerHashMap<zstring> NodeRefMap;

public:
  static const char* XS_URI;
  static const char* XML_URI;
  static const char* ZXSE_URI;
  static const char* JDM_URI;

  static const ulong XML_URI_LEN;

protected:
  static const ulong NAMESPACE_POOL_SIZE;
  static const ulong DEFAULT_DOCUMENT_SET_SIZE;
  static const ulong DEFAULT_URI_COLLECTION_SET_SIZE;
  static const ulong DEFAULT_INDICES_SET_SIZE;
  static const ulong DEFAULT_INTEGRITY_CONSTRAINT_SET_SIZE;

public:
  zstring                       theEmptyNs;
  zstring                       theXmlSchemaNs;

  std::vector<store::Item_t>    theSchemaTypeNames;
  std::map<store::Item*, store::SchemaTypeCode> theSchemaTypeCodes;
  store::Item_t                 XS_UNTYPED_QNAME;
  store::Item_t                 XS_ANY_QNAME;
  store::Item_t                 XS_ANY_SIMPLE_QNAME;

  store::Item_t                 JDM_OBJECT_QNAME;
  store::Item_t                 JDM_ARRAY_QNAME;
  store::Item_t                 JDM_NULL_QNAME;

protected:
  ulong                         theNumUsers;

  ulong                         theUriCounter;
  SYNC_CODE(Mutex               theUriCounterMutex;)

  ulong                         theCollectionCounter;
  SYNC_CODE(Mutex               theCollectionCounterMutex;)

  ulong                         theTreeCounter;
  SYNC_CODE(Mutex               theTreeCounterMutex;)

  StringPool                  * theNamespacePool;
  QNamePool                   * theQNamePool;

  BasicItemFactory            * theItemFactory;
  store::IteratorFactory      * theIteratorFactory;
  NodeFactory                 * theNodeFactory;
  PULPrimitiveFactory         * thePULFactory;

  DocumentSet                   theDocuments;
  CollectionSet               * theCollections;
  IndexSet                      theIndices;
  ICSet                         theICs;
  IndexSet                      theHashMaps;

  SYNC_CODE(Lock                theGlobalLock;)

  long                          theTraceLevel;

  RefNodeMap                    theReferencesToNodeMap;
  NodeRefMap                    theNodeToReferencesMap;

#ifndef ZORBA_NO_FULL_TEXT
  internal::StemmerProvider const * theStemmerProvider;
  TokenizerProvider const     * theTokenizerProvider;
#endif /* ZORBA_NO_FULL_TEXT */

public:
  void shutdown(bool soft = true);

  store::ItemFactory* getItemFactory() const;

  store::IteratorFactory* getIteratorFactory() const { return theIteratorFactory; }

  NodeFactory& getNodeFactory() const { return *theNodeFactory; }

  PULPrimitiveFactory& getPULFactory() const { return *thePULFactory; }

  StringPool& getNamespacePool() const { return *theNamespacePool; }

  QNamePool& getQNamePool() const { return *theQNamePool; }

  SYNC_CODE(Lock& getGlobalLock() { return theGlobalLock; })

  long getTraceLevel() const { return theTraceLevel; }

  XmlLoader* getXmlLoader(
      XQueryDiagnostics*,
      const store::LoadProperties& loadProperties);

  ulong createCollectionId();

  ulong createTreeId();

  store::Collection_t createCollection(
      const store::Item_t& aName,
      const std::vector<store::Annotation_t>& annotations,
      const store::Item_t& aNodeType,
      bool isDynamic,
      bool isJSONIQ);

  void deleteCollection(
      const store::Item* aName,
      bool isDynamic,
      bool isJSONIQ);

  store::Collection_t getCollection(
      const store::Item* aName,
      bool isDynamic,
      bool isJSONIQ);

  void addCollection(store::Collection_t& collection);

  store::Iterator_t listCollectionNames(bool dynamic, bool jsoniq);

  store::Index_t createIndex(
      const store::Item_t& qname,
      const store::IndexSpecification& spec,
      store::Iterator* sourceIter);
  
  // needs to be virtual to allow implementation of additional stores
  virtual void populateValueIndex(
      const store::Index_t& index,
      store::Iterator* sourceIter,
      ulong numColumns);

  // needs to be virtual to allow implementation of additional stores
  virtual void populateGeneralIndex(
      const store::Index_t& index,
      store::Iterator* sourceIter,
      ulong numColumns);

  // needs to be virtual to allow implementation of additional stores
  virtual store::Index_t refreshIndex(
      const store::Item_t& qname,
      const store::IndexSpecification& spec,
      store::Iterator* sourceIter);

  // needs to be virtual to allow implementation of additional stores
  virtual const IndexSet& getIndices() const { return theIndices; }

  void addIndex(store::Index_t& index);

  store::Index* getIndex(const store::Item* qname);

  void deleteIndex(const store::Item* qname);

  store::Iterator_t listIndexNames();

  store::IC_t activateIC(
      const store::Item_t& icQName,
      const store::Item_t& collectionQName,
      bool& isApplied);
  
  store::IC_t activateForeignKeyIC(
      const store::Item_t& icQName,
      const store::Item_t& fromCollectionQName,
      const store::Item_t& toCollectionQName,
      bool& isApplied);

  store::IC_t deactivateIC(
      const store::Item_t& icQName,
      bool& isApplied);

  store::Iterator_t listActiveICNames();

  store::IC* getIC(const store::Item* icQName);

  /*****************************************************************************
  
  *****************************************************************************/
  virtual store::Index* getMap(const store::Item* aQName) const;

  virtual store::Iterator_t listMapNames();


  /*****************************************************************************
  
  *****************************************************************************/
  store::Item_t loadDocument(
        const zstring& baseUri,
        const zstring& docUri,
        std::istream& stream,
        const store::LoadProperties& loadProperties);

  store::Item_t loadDocument(
        const zstring& baseUri,
        const zstring& docUri,
        std::istream* stream,
        const store::LoadProperties& loadProperties);

  /*****************************************************************************
  
  *****************************************************************************/
  void addNode(const zstring& uri, const store::Item_t& node);

  store::Iterator_t getDocumentNames() const;

  store::Item_t getDocument(const zstring& uri);

  void deleteDocument(const zstring& uri);

  void deleteAllDocuments();

  /*****************************************************************************
  
  *****************************************************************************/
  store::Index_t createHashMap(
      const store::Item_t& aQName,
      const store::IndexSpecification& aSpec);

  store::Index_t destroyHashMap(const store::Item_t& aQName);

  store::Index_t getHashMap(const store::Item_t& aQName) const;

  void addHashMap(const store::Index_t& aMap);

  /*****************************************************************************
  
  *****************************************************************************/
  short compareNodes(store::Item* node1, store::Item* node2) const;

  store::Iterator_t sortNodes(
        store::Iterator* iterator,
        bool ascendent,
        bool duplicateElemination,
        bool aAllowAtomics = false);

  store::Iterator_t distinctNodes(store::Iterator* input, bool aAllowAtomics);

  store::Iterator_t checkDistinctNodes(store::Iterator* input);

  bool getStructuralInformation(store::Item_t& result, const store::Item* node);

  bool getPathInfo(
        const store::Item* docUri,
        std::vector<const store::Item*>& contextPath,
        std::vector<const store::Item*>& relativePath,
        bool isAttrPath,
        bool& found,
        bool& unique);

  /* ------------------------ Node Reference Management ---------------------------*/

  bool getNodeReference(store::Item_t& result, store::Item* node);

  bool hasReference(const store::Item* node);

  bool getNodeByReference(store::Item_t& result, const zstring& reference);

  bool unregisterNode(XmlNode* node);

  /* ------------------------ Temp Sequence Management ---------------------------*/

  store::TempSeq_t createTempSeq(bool lazy);

  store::TempSeq_t createTempSeq(const store::Iterator_t& iterator, bool lazy);

  store::TempSeq_t createTempSeq(std::vector<store::Item_t>& item_v);

  store::TempSeq_t createTempSeq(store::Item_t& item);

#ifdef ZORBA_WITH_JSON
  /* ----------------------- JSON Array  Management --------------------------*/

  void populateJSONArray(
      store::Item* array,
      store::Iterator* iter,
      const store::CopyMode& copymode);
#endif

#ifndef ZORBA_NO_FULL_TEXT
  internal::StemmerProvider const* getStemmerProvider() const;

  void setStemmerProvider(internal::StemmerProvider const*);
#endif /* ZORBA_NO_FULL_TEXT */

protected:
  SimpleStore();

  virtual ~SimpleStore();

  void init();

  void initTypeNames();

  // Functions to create/destory the node and item factories. These functions
  // are called from init and shutdown, respectively. Having this functionality
  // factorized allows others to derive from the SimpleStore and provide their
  // own factories.

  virtual NodeFactory* createNodeFactory() const;

  virtual void destroyNodeFactory(NodeFactory*) const;

  virtual BasicItemFactory* createItemFactory() const;

  virtual void destroyItemFactory(BasicItemFactory*) const;

  virtual PULPrimitiveFactory* createPULPrimitiveFactory() const;

  virtual void destroyPULPrimitiveFactory(PULPrimitiveFactory*) const;

  virtual CollectionSet* createCollectionSet() const;

  virtual void destroyCollectionSet(CollectionSet*) const;

#ifndef ZORBA_NO_FULL_TEXT
  TokenizerProvider const* getTokenizerProvider() const;

  void setTokenizerProvider(TokenizerProvider const*);
#endif /* ZORBA_NO_FULL_TEXT */

#ifdef ZORBA_WITH_JSON
  store::Item_t parseJSON(
      std::istream& stream,
      internal::diagnostic::location* relative_error_loc
    );
#endif
};

} // namespace store
} // namespace zorba

#endif /* ZORBA_STORE_SIMPLE_STORE_H */
/*
 * Local variables:
 * mode: c++
 * End:
 */
/* vim:set et sw=2 ts=2: */