Clover coverage report -
Coverage timestamp: Sun Nov 16 2008 23:05:30 GMT
file stats: LOC: 281   Methods: 9
NCLOC: 157   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MetaSystemCollection.java 77.3% 84.1% 100% 83%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: MetaSystemCollection.java 713235 2008-11-12 01:03:39Z vgritsenko $
 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    * MetaSystemCollection represents the Meta System Collection. Beyond
 34    * standard Collection operations, this class will provide facilities
 35    * for Meta data management.
 36    *
 37    * @version $Revision: 713235 $, $Date: 2008-11-12 01:03:39 +0000 (Wed, 12 Nov 2008) $
 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    // Meta System Collection
 49    = "<collection name='" + METACOL + "'>"
 50    + " <collections>"
 51    // Meta Collections
 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    // Set the canonical names
 65  121 dbCanonicalName = db.getCanonicalName();
 66  121 sysCanonicalName = db.getSystemCollection().getCanonicalName();
 67    }
 68   
 69  3 public void init() throws DBException {
 70    // Bootstrap the Meta Collection
 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    * Returns the corresponding Meta Collection for the given
 85    * collection, optionally creating the hierarchy
 86    *
 87    * @param collection the collection whose meta you want
 88    * @param create whether or not to create the meta if its missing
 89    * @return collection the meta collection for the requested collection
 90    * @throws DBException if unable to get meta collection
 91    */
 92  24802 public Collection getMetaCollection(Collection collection, boolean create) throws DBException {
 93  24802 String path = collection.getCanonicalName();
 94   
 95    // different database
 96  24802 if (!path.startsWith(dbCanonicalName)) {
 97  0 return null;
 98    }
 99   
 100    // this is the meta collection
 101  24802 if (path.startsWith(getCanonicalName())) {
 102  9654 return null;
 103    }
 104   
 105    // this is the system collection
 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    * dropCollectionMeta removes the MetaData for the specified
 148    * Collection.
 149    *
 150    * @param collection The Collection whose meta is required
 151    * @throws DBException if unable to delete collection meta data
 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    * getCollectionMeta retrieves the MetaData for the specified
 168    * Collection. Assumption is that the corresponding collection
 169    * must already exist.
 170    *
 171    * @param collection The Collection whose meta is required
 172    * @return The requested MetaData
 173    * @throws DBException if unable to retrieve collection meta data
 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    * setCollectionMeta save the Meta for the specified Collection.
 196    *
 197    * @param collection The Collection that owns the MetaData
 198    * @param meta The MetaData
 199    * @throws DBException if unable to update collection meta data
 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    * dropDocumentMeta removes the meta data for the specified
 215    * document.
 216    *
 217    * @param collection Document collection
 218    * @param id Document key
 219    * @throws DBException if unable to delete document meta data
 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    * getDocumentMeta retrieves the meta data for the specified
 236    * document.
 237    *
 238    * @param collection Document collection
 239    * @param id Document key
 240    * @return The requested MetaData
 241    * @throws DBException if unable to retrieve document meta data
 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    * setDocumentMeta sets the meta data for the specified document.
 264    *
 265    * @param collection Document collection
 266    * @param id Document key
 267    * @param meta New document meta data
 268    * @throws DBException if unable to update document meta data
 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    }