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 |
122
| public MetaSystemCollection(Database db) {
|
62 |
122
| super(db);
|
63 |
| |
64 |
| |
65 |
122
| dbCanonicalName = db.getCanonicalName();
|
66 |
122
| sysCanonicalName = db.getSystemCollection().getCanonicalName();
|
67 |
| } |
68 |
| |
69 |
4
| public void init() throws DBException {
|
70 |
| |
71 |
4
| try {
|
72 |
4
| Document metaDoc = DOMParser.toDocument(METACOL_DEFINITION);
|
73 |
4
| Configuration metaCfg = new Configuration(metaDoc, false);
|
74 |
4
| 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 |
32701
| public Collection getMetaCollection(Collection collection, boolean create) throws DBException {
|
93 |
32701
| String path = collection.getCanonicalName();
|
94 |
| |
95 |
| |
96 |
32701
| if (!path.startsWith(dbCanonicalName)) {
|
97 |
0
| return null;
|
98 |
| } |
99 |
| |
100 |
| |
101 |
32701
| if (path.startsWith(getCanonicalName())) {
|
102 |
12790
| return null;
|
103 |
| } |
104 |
| |
105 |
| |
106 |
19911
| if (path.startsWith(sysCanonicalName)) {
|
107 |
9000
| return null;
|
108 |
| } |
109 |
| |
110 |
10911
| Collection current = getCollection(METAS);
|
111 |
10911
| StringTokenizer st = new StringTokenizer(path.substring(dbCanonicalName.length()), "/");
|
112 |
| |
113 |
10911
| while (current != null && st.hasMoreTokens()) {
|
114 |
20275
| String segment = st.nextToken().trim();
|
115 |
20275
| if (segment.length() == 0) {
|
116 |
0
| continue;
|
117 |
| } |
118 |
| |
119 |
20275
| Collection childcol = current.getCollection(segment);
|
120 |
20275
| if (childcol == null) {
|
121 |
539
| if (!create) {
|
122 |
22
| return null;
|
123 |
| } |
124 |
| |
125 |
517
| String cfgText
|
126 |
| = "<collection name='" + segment + "' compressed='true'>" |
127 |
| + " <filer class='org.apache.xindice.core.filer.BTreeFiler' pagesize='1024'/>" |
128 |
| + "</collection>"; |
129 |
| |
130 |
517
| try {
|
131 |
517
| Document cfgDoc = DOMParser.toDocument(cfgText);
|
132 |
517
| Configuration cfg = new Configuration(cfgDoc, false);
|
133 |
517
| 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 |
20253
| current = childcol;
|
141 |
| } |
142 |
| |
143 |
10889
| return current;
|
144 |
| } |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
1035
| public void dropCollectionMeta(Collection collection) throws DBException {
|
154 |
1035
| Collection mcol = getMetaCollection(collection, false);
|
155 |
1035
| if (null != mcol) {
|
156 |
496
| try {
|
157 |
496
| 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 |
4756
| public MetaData getCollectionMeta(Collection collection) throws DBException {
|
176 |
4756
| Collection mcol = getMetaCollection(collection, true);
|
177 |
| |
178 |
4756
| if (null != mcol) {
|
179 |
2972
| MetaData meta = (MetaData) mcol.getObject(COLLECTION_META_DATA);
|
180 |
2972
| if (meta == null) {
|
181 |
517
| meta = new MetaData();
|
182 |
| } |
183 |
| |
184 |
2972
| if (meta.getType() == MetaData.UNKNOWN) {
|
185 |
517
| meta.setType(MetaData.COLLECTION);
|
186 |
| } |
187 |
2972
| meta.setOwner(collection.getCanonicalName());
|
188 |
2972
| meta.setDirty(false);
|
189 |
2972
| return meta;
|
190 |
| } |
191 |
1784
| return null;
|
192 |
| } |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
4717
| public void setCollectionMeta(Collection collection, MetaData meta) throws DBException {
|
202 |
4717
| if (null == meta) {
|
203 |
0
| return;
|
204 |
| } |
205 |
| |
206 |
4717
| Collection mcol = getMetaCollection(collection, true);
|
207 |
4717
| if (null != mcol) {
|
208 |
2933
| mcol.setObject(COLLECTION_META_DATA, meta);
|
209 |
2933
| meta.setDirty(false);
|
210 |
| } |
211 |
| } |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
1724
| public void dropDocumentMeta(Collection collection, String id) throws DBException {
|
222 |
1724
| Collection mcol = getMetaCollection(collection, false);
|
223 |
1724
| if (null != mcol) {
|
224 |
461
| try {
|
225 |
461
| 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 |
10253
| public MetaData getDocumentMeta(Collection collection, String id) throws DBException {
|
244 |
10253
| Collection mcol = getMetaCollection(collection, true);
|
245 |
10253
| if (null != mcol) {
|
246 |
2032
| MetaData meta = (MetaData) mcol.getObject(id);
|
247 |
| |
248 |
2032
| if (meta == null) {
|
249 |
1948
| meta = new MetaData();
|
250 |
| } |
251 |
| |
252 |
2032
| if (meta.getType() == MetaData.UNKNOWN) {
|
253 |
1948
| meta.setType(MetaData.DOCUMENT);
|
254 |
| } |
255 |
2032
| meta.setOwner(collection.getCanonicalName() + "/" + id);
|
256 |
2032
| meta.setDirty(false);
|
257 |
2032
| return meta;
|
258 |
| } |
259 |
8221
| return null;
|
260 |
| } |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
10216
| public void setDocumentMeta(Collection collection, String id, MetaData meta) throws DBException {
|
271 |
10216
| if (null == meta) {
|
272 |
0
| return;
|
273 |
| } |
274 |
| |
275 |
10216
| Collection mcol = getMetaCollection(collection, true);
|
276 |
10216
| if (null != mcol) {
|
277 |
1995
| mcol.setObject(id, meta);
|
278 |
1995
| meta.setDirty(false);
|
279 |
| } |
280 |
| } |
281 |
| } |