|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.core; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.core.meta.MetaData; |
|
25 |
| import org.apache.xindice.util.Configuration; |
|
26 |
| import org.apache.xindice.xml.dom.DOMParser; |
|
27 |
| |
|
28 |
| import org.w3c.dom.Document; |
|
29 |
| |
|
30 |
| import java.util.StringTokenizer; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public final class MetaSystemCollection extends Collection { |
|
40 |
| |
|
41 |
| private static final Log log = LogFactory.getLog(MetaSystemCollection.class); |
|
42 |
| |
|
43 |
| public static final String METACOL = "meta"; |
|
44 |
| public static final String METAS = "Metas"; |
|
45 |
| public static final String COLLECTION_META_DATA = "_META_DATA_"; |
|
46 |
| |
|
47 |
| private static String METACOL_DEFINITION |
|
48 |
| |
|
49 |
| = "<collection name='" + METACOL + "'>" |
|
50 |
| + " <collections>" |
|
51 |
| |
|
52 |
| + " <collection name='" + METAS + "' compressed='true'>" |
|
53 |
| + " </collection>" |
|
54 |
| + " </collections>" |
|
55 |
| + "</collection>"; |
|
56 |
| |
|
57 |
| private String dbCanonicalName; |
|
58 |
| private String sysCanonicalName; |
|
59 |
| |
|
60 |
| |
|
61 |
121
| public MetaSystemCollection(Database db) {
|
|
62 |
121
| super(db);
|
|
63 |
| |
|
64 |
| |
|
65 |
121
| dbCanonicalName = db.getCanonicalName();
|
|
66 |
121
| sysCanonicalName = db.getSystemCollection().getCanonicalName();
|
|
67 |
| } |
|
68 |
| |
|
69 |
3
| public void init() throws DBException {
|
|
70 |
| |
|
71 |
3
| try {
|
|
72 |
3
| Document metaDoc = DOMParser.toDocument(METACOL_DEFINITION);
|
|
73 |
3
| Configuration metaCfg = new Configuration(metaDoc, false);
|
|
74 |
3
| setConfig(metaCfg);
|
|
75 |
| } catch (Exception e) { |
|
76 |
0
| if (log.isFatalEnabled()) {
|
|
77 |
0
| log.fatal("FATAL ERROR: Generating System Collection '" + METACOL + "'", e);
|
|
78 |
| } |
|
79 |
0
| System.exit(1);
|
|
80 |
| } |
|
81 |
| } |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
24802
| public Collection getMetaCollection(Collection collection, boolean create) throws DBException {
|
|
93 |
24802
| String path = collection.getCanonicalName();
|
|
94 |
| |
|
95 |
| |
|
96 |
24802
| if (!path.startsWith(dbCanonicalName)) {
|
|
97 |
0
| return null;
|
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
24802
| if (path.startsWith(getCanonicalName())) {
|
|
102 |
9654
| return null;
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
15148
| if (path.startsWith(sysCanonicalName)) {
|
|
107 |
6823
| return null;
|
|
108 |
| } |
|
109 |
| |
|
110 |
8325
| Collection current = getCollection(METAS);
|
|
111 |
8325
| StringTokenizer st = new StringTokenizer(path.substring(dbCanonicalName.length()), "/");
|
|
112 |
| |
|
113 |
8325
| while (current != null && st.hasMoreTokens()) {
|
|
114 |
15068
| String segment = st.nextToken().trim();
|
|
115 |
15068
| if (segment.length() == 0) {
|
|
116 |
0
| continue;
|
|
117 |
| } |
|
118 |
| |
|
119 |
15068
| Collection childcol = current.getCollection(segment);
|
|
120 |
15068
| if (childcol == null) {
|
|
121 |
410
| if (!create) {
|
|
122 |
21
| return null;
|
|
123 |
| } |
|
124 |
| |
|
125 |
389
| String cfgText
|
|
126 |
| = "<collection name='" + segment + "' compressed='true'>" |
|
127 |
| + " <filer class='org.apache.xindice.core.filer.BTreeFiler' pagesize='1024'/>" |
|
128 |
| + "</collection>"; |
|
129 |
| |
|
130 |
389
| try {
|
|
131 |
389
| Document cfgDoc = DOMParser.toDocument(cfgText);
|
|
132 |
389
| Configuration cfg = new Configuration(cfgDoc, false);
|
|
133 |
389
| childcol = current.createCollection(segment, cfg);
|
|
134 |
| } catch (DBException de) { |
|
135 |
0
| throw de;
|
|
136 |
| } catch (Exception e) { |
|
137 |
0
| throw new DBException(FaultCodes.getFaultCode(e));
|
|
138 |
| } |
|
139 |
| } |
|
140 |
15047
| current = childcol;
|
|
141 |
| } |
|
142 |
| |
|
143 |
8304
| return current;
|
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
779
| public void dropCollectionMeta(Collection collection) throws DBException {
|
|
154 |
779
| Collection mcol = getMetaCollection(collection, false);
|
|
155 |
779
| if (null != mcol) {
|
|
156 |
369
| try {
|
|
157 |
369
| dropCollection(mcol);
|
|
158 |
| } catch (DBException e) { |
|
159 |
0
| if (log.isWarnEnabled()) {
|
|
160 |
0
| log.warn("ignored exception", e);
|
|
161 |
| } |
|
162 |
| } |
|
163 |
| } |
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
3566
| public MetaData getCollectionMeta(Collection collection) throws DBException {
|
|
176 |
3566
| Collection mcol = getMetaCollection(collection, true);
|
|
177 |
| |
|
178 |
3566
| if (null != mcol) {
|
|
179 |
2258
| MetaData meta = (MetaData) mcol.getObject(COLLECTION_META_DATA);
|
|
180 |
2258
| if (meta == null) {
|
|
181 |
389
| meta = new MetaData();
|
|
182 |
| } |
|
183 |
| |
|
184 |
2258
| if (meta.getType() == MetaData.UNKNOWN) {
|
|
185 |
389
| meta.setType(MetaData.COLLECTION);
|
|
186 |
| } |
|
187 |
2258
| meta.setOwner(collection.getCanonicalName());
|
|
188 |
2258
| meta.setDirty(false);
|
|
189 |
2258
| return meta;
|
|
190 |
| } |
|
191 |
1308
| return null;
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
3534
| public void setCollectionMeta(Collection collection, MetaData meta) throws DBException {
|
|
202 |
3534
| if (null == meta) {
|
|
203 |
0
| return;
|
|
204 |
| } |
|
205 |
| |
|
206 |
3534
| Collection mcol = getMetaCollection(collection, true);
|
|
207 |
3534
| if (null != mcol) {
|
|
208 |
2226
| mcol.setObject(COLLECTION_META_DATA, meta);
|
|
209 |
2226
| meta.setDirty(false);
|
|
210 |
| } |
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
1221
| public void dropDocumentMeta(Collection collection, String id) throws DBException {
|
|
222 |
1221
| Collection mcol = getMetaCollection(collection, false);
|
|
223 |
1221
| if (null != mcol) {
|
|
224 |
305
| try {
|
|
225 |
305
| mcol.remove(id);
|
|
226 |
| } catch (DBException e) { |
|
227 |
0
| if (log.isWarnEnabled()) {
|
|
228 |
0
| log.warn("ignored exception", e);
|
|
229 |
| } |
|
230 |
| } |
|
231 |
| } |
|
232 |
| } |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
7864
| public MetaData getDocumentMeta(Collection collection, String id) throws DBException {
|
|
244 |
7864
| Collection mcol = getMetaCollection(collection, true);
|
|
245 |
7864
| if (null != mcol) {
|
|
246 |
1586
| MetaData meta = (MetaData) mcol.getObject(id);
|
|
247 |
| |
|
248 |
1586
| if (meta == null) {
|
|
249 |
1528
| meta = new MetaData();
|
|
250 |
| } |
|
251 |
| |
|
252 |
1586
| if (meta.getType() == MetaData.UNKNOWN) {
|
|
253 |
1528
| meta.setType(MetaData.DOCUMENT);
|
|
254 |
| } |
|
255 |
1586
| meta.setOwner(collection.getCanonicalName() + "/" + id);
|
|
256 |
1586
| meta.setDirty(false);
|
|
257 |
1586
| return meta;
|
|
258 |
| } |
|
259 |
6278
| return null;
|
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
7838
| public void setDocumentMeta(Collection collection, String id, MetaData meta) throws DBException {
|
|
271 |
7838
| if (null == meta) {
|
|
272 |
0
| return;
|
|
273 |
| } |
|
274 |
| |
|
275 |
7838
| Collection mcol = getMetaCollection(collection, true);
|
|
276 |
7838
| if (null != mcol) {
|
|
277 |
1560
| mcol.setObject(id, meta);
|
|
278 |
1560
| meta.setDirty(false);
|
|
279 |
| } |
|
280 |
| } |
|
281 |
| } |