All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.treetank.io.LRULog Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2011, University of Konstanz, Distributed Systems Group
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the University of Konstanz nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL  BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.treetank.io;

import static com.google.common.base.Objects.toStringHelper;

import java.io.File;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.treetank.access.conf.ResourceConfiguration;
import org.treetank.api.IDataFactory;
import org.treetank.api.IMetaEntryFactory;
import org.treetank.exception.TTIOException;
import org.treetank.io.LogKey.LogKeyBinding;
import org.treetank.io.LogValue.LogValueBinding;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalCause;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;

/**
 * An LRU cache, based on LinkedHashMap. This cache can hold an
 * possible second cache as a second layer for example for storing data in a
 * persistent way.
 * 
 * @author Sebastian Graf, University of Konstanz
 */
public final class LRULog implements ILog {
    //
    // // START DEBUG CODE
    // private final static File insertFile = new File("/Users/sebi/Desktop/runtimeResults/insert.txt");
    // private final static File getFile = new File("/Users/sebi/Desktop/runtimeResults/get.txt");
    //
    // static final FileWriter insert;
    // static final FileWriter get;
    //
    // static {
    // try {
    // insert = new FileWriter(insertFile);
    // get = new FileWriter(getFile);
    // } catch (IOException e) {
    // throw new RuntimeException(e);
    // }
    // }

    /**
     * Name for the database.
     */
    private static final String NAME = "berkeleyCache";

    /**
     * Binding for the key, which is the databucket.
     */
    private final transient LogKeyBinding mKeyBinding;

    /**
     * Binding for the value which is a bucket with related datas.
     */
    private final transient LogValueBinding mValueBinding;

    /**
     * Berkeley Environment for the database.
     */
    private final transient Environment mEnv;

    /**
     * Berkeley database.
     */
    private final transient Database mDatabase;

    /** Location to the BDB. */
    private final transient File mLocation;

    /** Transient cache for buffering buckets. */
    private final Cache mCache;

    /** DB to select to support non-blocking writes. */
    private int mSelected_db;

    /** Flag if closed. */
    private boolean mClosed;

    /**
     * Creates a new LRU cache.
     * 
     * @param pFile
     *            Location of the cache.
     * @param pDataFac
     *            DataFactory for generating datas adhering to the used interface
     * @param pMetaFac
     *            MetaFactory for generating meta-entries adhering to the used interface
     * @throws TTIOException
     * 
     */
    public LRULog(final File pFile, final IDataFactory pDataFac, final IMetaEntryFactory pMetaFac)
        throws TTIOException {
        mClosed = false;
        mKeyBinding = new LogKeyBinding();
        mValueBinding = new LogValueBinding(pDataFac, pMetaFac);
        mSelected_db = 1;

        final File classicLogLocation =
            new File(pFile, ResourceConfiguration.Paths.TransactionLog.getFile().getName());

        if (classicLogLocation.list().length > 0) {
            mLocation = new File(pFile, "_" + ResourceConfiguration.Paths.TransactionLog.getFile().getName());
            mLocation.mkdirs();
            mSelected_db = 2;
        } else {
            mLocation = classicLogLocation;
        }

        try {
            EnvironmentConfig config = new EnvironmentConfig();
            config.setAllowCreate(true);
            config = config.setSharedCache(false);
            config.setLocking(false);
            config.setCachePercent(20);
            mEnv = new Environment(mLocation, config);
            mEnv.cleanLog();
            final DatabaseConfig dbConfig = new DatabaseConfig();
            dbConfig.setAllowCreate(true);
            mDatabase = mEnv.openDatabase(null, NAME + mSelected_db, dbConfig);

        } catch (final DatabaseException exc) {
            throw new TTIOException(exc);
        }

        mCache =
            CacheBuilder.newBuilder().maximumSize(100).removalListener(
                new RemovalListener() {
                    @Override
                    public void onRemoval(RemovalNotification notification) {
                        if (notification.getCause() != RemovalCause.REPLACED) {
                            insertIntoBDB(notification.getKey(), notification.getValue());
                        }

                    }
                }).build();
    }

    /**
     * {@inheritDoc}
     */
    public synchronized LogValue get(final LogKey pKey) throws TTIOException {
        if (isClosed()) {
            return new LogValue(null, null);
        }
        LogValue val = mCache.getIfPresent(pKey);
        if (val == null || val.getModified() == null) {
            final DatabaseEntry valueEntry = new DatabaseEntry();
            final DatabaseEntry keyEntry = new DatabaseEntry();
            mKeyBinding.objectToEntry(pKey, keyEntry);
            try {
                final OperationStatus status = mDatabase.get(null, keyEntry, valueEntry, LockMode.DEFAULT);
                if (status == OperationStatus.SUCCESS) {
                    val = mValueBinding.entryToObject(valueEntry);
                } else {
                    val = new LogValue(null, null);
                }
                mCache.put(pKey, val);
                // get.write(pKey.getLevel() + "," + pKey.getSeq() + "\n");
                // get.flush();

            } catch (DatabaseException exc) {
                throw new TTIOException(exc);
            }
        }
        return val;
    }

    /**
     * {@inheritDoc}
     */
    public void put(final LogKey pKey, final LogValue pValue) throws TTIOException {
        mCache.put(pKey, pValue);
    }

    /**
     * {@inheritDoc}
     */
    public synchronized void close() throws TTIOException {
        try {
            mClosed = true;
            mDatabase.close();
            mEnv.removeDatabase(null, NAME + mSelected_db);
            mEnv.close();
            IOUtils.recursiveDelete(mLocation);
            mLocation.mkdir();

        } catch (final DatabaseException exc) {
            throw new TTIOException(exc);
        }
    }

    /**
     * {@inheritDoc}
     */
    public synchronized boolean isClosed() {
        return mClosed;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return toStringHelper(this).add("mDatabase", mDatabase).toString();
    }

    /**
     * Returning all elements as Iterator.
     * 
     * @return new LogIterator-instance
     */
    public LogIterator getIterator() {
        Set> entries = mCache.asMap().entrySet();
        for (Entry entry : entries) {
            insertIntoBDB(entry.getKey(), entry.getValue());
        }

        return new LogIterator();
    }

    private void insertIntoBDB(LogKey pKey, LogValue pVal) {
        if (pVal.getModified() != null) {
            final DatabaseEntry valueEntry = new DatabaseEntry();
            final DatabaseEntry keyEntry = new DatabaseEntry();
            mKeyBinding.objectToEntry(pKey, keyEntry);
            mValueBinding.objectToEntry(pVal, valueEntry);
            try {

                // ////TODO DEBUGCODE///////
                // final DatabaseEntry valueOld = new DatabaseEntry();
                // OperationStatus status2 = mDatabase.get(null, keyEntry, valueOld, LockMode.DEFAULT);
                // if (status2 == OperationStatus.SUCCESS) {
                // System.out.println(mDatabase.count());
                // status2 = mDatabase.delete(null, keyEntry);
                // System.out.println(mDatabase.count());
                // status2 = mDatabase.get(null, keyEntry, valueOld, LockMode.DEFAULT);
                // System.out.println(mDatabase.count());
                // mDatabase.sync();
                // System.out.println(mDatabase.count());
                // }

                // OperationStatus status = mDatabase.put(null, keyEntry, valueEntry);
                mDatabase.put(null, keyEntry, valueEntry);
                // insert.write(pKey.getLevel() + "," + pKey.getSeq() + "\n");
                // insert.flush();

            } catch (DatabaseException exc) {
                throw new RuntimeException(exc);
            }
        }
    }

    class LogIterator implements Iterator, Iterable {

        private Cursor mCursor;
        private DatabaseEntry valueEntry;
        private DatabaseEntry keyEntry;

        /**
         * 
         * Constructor.
         * 
         */
        public LogIterator() {
            mCursor = mDatabase.openCursor(null, null);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Iterator iterator() {
            return this;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean hasNext() {
            boolean returnVal = false;
            valueEntry = new DatabaseEntry();
            keyEntry = new DatabaseEntry();
            try {
                final OperationStatus status = mCursor.getNext(keyEntry, valueEntry, LockMode.DEFAULT);
                if (status == OperationStatus.SUCCESS) {
                    returnVal = true;
                }
            } catch (final DatabaseException exc) {
                throw new RuntimeException(exc);
            }
            if (returnVal == false) {
                mCursor.close();
            }
            return returnVal;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public LogValue next() {
            LogValue val;
            if ((val = mCache.getIfPresent(keyEntry)) != null) {
                return val;
            }
            return mValueBinding.entryToObject(valueEntry);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy