Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 170   Methods: 11
NCLOC: 66   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
DatabaseChangeObserver.java 100% 94.4% 100% 97%
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: DatabaseChangeObserver.java 564833 2007-08-11 04:25:23Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.core;
 21   
 22    import java.util.Hashtable;
 23    import java.util.Iterator;
 24    import java.util.Map;
 25    import java.util.Timer;
 26    import java.util.TimerTask;
 27   
 28    import org.apache.commons.logging.Log;
 29    import org.apache.commons.logging.LogFactory;
 30    import org.apache.xindice.core.data.Key;
 31    import org.apache.xindice.core.data.Record;
 32    import org.apache.xindice.util.Configuration;
 33    import org.w3c.dom.Document;
 34   
 35    /**
 36    * DatabaseChangeObserver is going to handle changes to collections by
 37    * flushing the apporate collection Filer.
 38    *
 39    * This current version we are only going to worry about document changes and
 40    * their Filer storage are kept updated.
 41    *
 42    * @author <a href="mailto:toddbyrne@gmail.com">Todd Byrne</a>
 43    * @version $Revision: 564833 $, $Date: 2007-08-10 21:25:23 -0700 (Fri, 10 Aug 2007) $
 44    */
 45    public class DatabaseChangeObserver extends DBObserver {
 46   
 47    private static final Log log = LogFactory.getLog(DatabaseChangeObserver.class);
 48   
 49    /** One minute flush period **/
 50    private static final long FLUSH_PERIOD = 1000 * 60;
 51   
 52    // by using a hashtable instead of a queue collections only get added once.
 53    private final Hashtable collectionDirtyFlags;
 54    private Timer flushTimer;
 55   
 56  19 public DatabaseChangeObserver() {
 57  19 collectionDirtyFlags = new Hashtable();
 58  19 flushTimer = new Timer(true);
 59  19 flushTimer.schedule(new FlushTask(), FLUSH_PERIOD, FLUSH_PERIOD);
 60    }
 61   
 62    /**
 63    * Queues the collection to be flushed by the FlushTask synchronized
 64    * by the hashtable
 65    * @param col Collection
 66    * @param key Key
 67    * @throws DBException
 68    */
 69  1725 public void dropDocument(Collection col, Key key) throws DBException {
 70  1725 collectionDirtyFlags.put(col,Boolean.TRUE);
 71    }
 72   
 73    /**
 74    *
 75    * @param col Collection
 76    * @param key Key
 77    * @param document Document
 78    * @param create boolean
 79    * @throws DBException
 80    */
 81  10196 public void putDocument(Collection col, Key key, Document document, boolean create) throws DBException {
 82  10196 collectionDirtyFlags.put(col,Boolean.TRUE);
 83    }
 84   
 85    /**
 86    *
 87    * @param col Collection
 88    * @throws DBException
 89    */
 90  1033 public void createCollection(Collection col) throws DBException {
 91    }
 92   
 93    /**
 94    *
 95    * @param col Collection
 96    * @param record Record
 97    * @param document Document
 98    * @throws DBException
 99    */
 100  35233 public void loadDocument(Collection col, Record record, Document document) throws DBException {
 101    }
 102   
 103    /**
 104    *
 105    * @param col Collection
 106    * @throws DBException
 107    */
 108  1036 public void dropCollection(Collection col) throws DBException {
 109    }
 110   
 111    /**
 112    *
 113    * @param db Database
 114    * @param cfg Configuration
 115    */
 116  1321 public void flushDatabaseConfig(Database db, Configuration cfg) {
 117    }
 118   
 119    /**
 120    *
 121    * @param col Collection
 122    * @param cfg Configuration
 123    */
 124  1894 public void setCollectionConfig(Collection col, Configuration cfg) {
 125    }
 126   
 127    /**
 128    *
 129    * @param db Database
 130    * @param collections Map
 131    * @param cfg Configuration
 132    */
 133  123 public void setDatabaseConfig(Database db, Map collections, Configuration cfg) {
 134    }
 135   
 136    /**
 137    * FlushTask handles the work of
 138    */
 139    private class FlushTask extends TimerTask {
 140  19 public FlushTask() {
 141    }
 142   
 143  9 public void run() {
 144  9 synchronized (collectionDirtyFlags) {
 145  9 int flushCount = 0;
 146  9 Iterator collections = collectionDirtyFlags.entrySet().iterator();
 147  9 while (collections.hasNext()) {
 148  526 Map.Entry e = (Map.Entry) collections.next();
 149  526 Collection c = (Collection) e.getKey();
 150  526 try {
 151    // don't need to flush indexes because everytime a document
 152    // gets added the indexes get flushed.
 153  526 c.getFiler().flush();
 154  526 flushCount ++;
 155    } catch (DBException ex) {
 156  0 log.error("Couldn't flush collection: " + c.getName(), ex);
 157    }
 158    }
 159   
 160  9 if (flushCount > 0) {
 161  4 log.info("Successfully flushed " + flushCount + " collections out of " +
 162    collectionDirtyFlags.size());
 163    }
 164   
 165  9 collectionDirtyFlags.clear();
 166    }
 167    }
 168    }
 169   
 170    }