Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
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  122 public MetaSystemCollection(Database db) {
 62  122 super(db);
 63   
 64    // Set the canonical names
 65  122 dbCanonicalName = db.getCanonicalName();
 66  122 sysCanonicalName = db.getSystemCollection().getCanonicalName();
 67    }
 68   
 69  4 public void init() throws DBException {
 70    // Bootstrap the Meta Collection
 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    * 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  32701 public Collection getMetaCollection(Collection collection, boolean create) throws DBException {
 93  32701 String path = collection.getCanonicalName();
 94   
 95    // different database
 96  32701 if (!path.startsWith(dbCanonicalName)) {
 97  0 return null;
 98    }
 99   
 100    // this is the meta collection
 101  32701 if (path.startsWith(getCanonicalName())) {
 102  12790 return null;
 103    }
 104   
 105    // this is the system collection
 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    * 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  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    * 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  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    * 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  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    * 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  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    * 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  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    * 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  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    }