1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ZORBA_TREE_ID_GENERATOR_H
17 #define ZORBA_TREE_ID_GENERATOR_H
18 
19 #include <zorbautils/hashmap_itemh.h>
20 #include <zorbautils/mutex.h>
21 
22 #include "tree_id.h"
23 
24 namespace zorba {
25 
26 namespace simplestore {
27 
28 /*******************************************************************************
29   This class is an abstract class for tree ID generation. It provides
30   a creation method, two comparison methods (= and <) as well as a way
31   to parse a string back to an ID.
32 ********************************************************************************/
33 class TreeIdGenerator
34 {
35 public:
~TreeIdGenerator()36   virtual ~TreeIdGenerator() {}
37 
38   virtual TreeId create() = 0;
39 };
40 
41 
42 /*******************************************************************************
43   Zorba's implementation of the tree ID generator.
44 ********************************************************************************/
45 class SimpleTreeIdGenerator : public TreeIdGenerator
46 {
47 private:
48   ulong theNextId;
SYNC_CODE(Mutex theCounterMutex;)49   SYNC_CODE(Mutex theCounterMutex;)
50 
51 public:
52   SimpleTreeIdGenerator() : theNextId(1) {}
53 
54   virtual TreeId create();
55 };
56 
57 
58 /*******************************************************************************
59   This class allows generation of independent tree ID generators (each of
60   them might have its own counter).
61 ********************************************************************************/
62 class TreeIdGeneratorFactory
63 {
64 public:
~TreeIdGeneratorFactory()65   virtual ~TreeIdGeneratorFactory() {}
66 
67   virtual TreeIdGenerator* createTreeGenerator(ulong aCollectionId) = 0;
68 
69   virtual TreeIdGenerator& getDefaultTreeIdGenerator() = 0;
70 };
71 
72 
73 /*******************************************************************************
74   Zorba's implementation of the tree ID generator factory.
75 ********************************************************************************/
76 class SimpleTreeIdGeneratorFactory : public TreeIdGeneratorFactory
77 {
78 protected:
79   SimpleTreeIdGenerator theDefaultGenerator;
80 
81 public:
82   virtual TreeIdGenerator* createTreeGenerator(ulong aCollectionId);
83 
84   virtual TreeIdGenerator& getDefaultTreeIdGenerator();
85 };
86 
87 } // simplestore
88 } // zorba
89 
90 #endif /* ZORBA_TREE_ID_GENERATOR_H */
91