TTreeMetaData.java :  » Database-DBMS » axion » org » axiondb » ext » indexes » ttree » Java Open Source

Java Open Source » Database DBMS » axion 
axion » org » axiondb » ext » indexes » ttree » TTreeMetaData.java
/*
 * $Id: TTreeMetaData.java,v 1.1 2005/06/30 01:14:44 ahimanikya Exp $
 * =======================================================================
 * Copyright (c) 2005 Axion Development Team.  All rights reserved.
 *  
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * 1. Redistributions of source code must retain the above 
 *    copyright notice, this list of conditions and the following 
 *    disclaimer. 
 *   
 * 2. 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. 
 *   
 * 3. The names "Tigris", "Axion", nor the names of its contributors may 
 *    not be used to endorse or promote products derived from this 
 *    software without specific prior written permission. 
 *  
 * 4. Products derived from this software may not be called "Axion", nor 
 *    may "Tigris" or "Axion" appear in their names 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 THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS 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.axiondb.ext.indexes.ttree;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.axiondb.AxionException;

/**
 * Metadata for {@link TTree}.
 * 
 * @author Pavels Andrejevs
 */
public final class TTreeMetaData {

    private static class TTreeMetaDataFile {

        public static TTreeMetaDataFile load(File file) throws IOException {
            ObjectInputStream in = null;
            try {
                in = new ObjectInputStream(new FileInputStream(file));
                int value = in.readInt();
                int rootFileId = in.readInt();
                return new TTreeMetaDataFile(value, rootFileId);
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
        }

        private int _rootFileId = 1;

        private int _treeSize = 0;

        private int _value = 0;

        public TTreeMetaDataFile() {
        }

        private TTreeMetaDataFile(int value, int rootFileId) {
            setValue(value);
            setRootFileId(rootFileId);
        }

        public int current() {
            return _value;
        }

        public int getRootFileId() {
            return _rootFileId;
        }

        public int getTreeSize() {
            return _treeSize;
        }

        public int increment() {
            return _value++;
        }

        public void save(File file) throws IOException {
            ObjectOutputStream out = null;
            try {
                out = new ObjectOutputStream(new FileOutputStream(file));
                out.writeInt(_value);
                out.writeInt(_rootFileId);
            } finally {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }

        public void setRootFileId(int rootFileId) {
            _rootFileId = rootFileId;
        }

        public void setTreeSize(int treeSize) {
            _treeSize = treeSize;
        }

        private void setValue(int value) {
            _value = value;
        }
    }

    private File _dataDirectory = null;
    private Map _dirtyNodes = null;
    private TTreeMetaDataFile _file;
    private boolean _memoryOnly = true;
    private TTreeMetaDataFile _metaDataFile = null;
    private String _name = null;

    public TTreeMetaData(String name, File dataDir, boolean memoryOnly) throws AxionException {
        _name = name.toUpperCase();
        _dataDirectory = dataDir;
        _memoryOnly = memoryOnly;
        _dirtyNodes = new HashMap();
        loadMetaDataFile();
    }

    public void clearDirty(AbstractTTreeNode node) {
        clearDirty(node.getFileId());
    }

    public File getDataDirectory() {
        return _dataDirectory;
    }

    public int getDirtyNodeCount() {
        return _dirtyNodes.size();
    }

    public Iterator getDirtyNodes() {
        return _dirtyNodes.values().iterator();
    }

    public TTreeMetaDataFile getFile() {
        return _file;
    }

    public final File getFileById(int fileid) {
        return new File(getDataDirectory(), getName() + "." + fileid);
    }

    public File getMetaDataFile() {
        return new File(getDataDirectory(), getName() + ".CTR");
    }

    public String getName() {
        return _name;
    }

    public int getRootFileId() {
        return _metaDataFile.getRootFileId();
    }

    public int getTreeSize() {
        return _metaDataFile.getTreeSize();
    }

    public boolean hasDirtyNodes() {
        return !_dirtyNodes.isEmpty();
    }

    public int incrementCounter() {
        return _metaDataFile.increment();
    }

    public boolean isMemoryOnly() {
        return _memoryOnly;
    }

    public void saveMetaDataFile() throws AxionException {
        try {
            _metaDataFile.save(getMetaDataFile());
        } catch (IOException e) {
            throw new AxionException(e);
        }
    }

    public void setAllClean() {
        // Clear dirty nodes
        _dirtyNodes.clear();
        // Delete each node file
        for (int i = 1; i < _metaDataFile.current(); i++) {
            File file = getFileById(i);
            if (file.exists()) {
                file.delete();
            }
        }
        // Delete metadata file
        File file = getMetaDataFile();
        if (file.exists()) {
            file.delete();
        }
        newMetaDataFile();
    }

    public void setDataDirectory(File dir) {
        _dataDirectory = dir;
    }

    public void setDirty(AbstractTTreeNode node) {
        setDirty(node.getFileId(), node);
    }

    public void setFile(TTreeMetaDataFile file) {
        _file = file;
    }

    public void setMemoryOnly(boolean memoryOnly) {
        _memoryOnly = memoryOnly;
    }

    public void setRootFileId(int rootFileId) {
        _metaDataFile.setRootFileId(rootFileId);
    }

    public void setTreeSize(int treeSize) {
        _metaDataFile.setTreeSize(treeSize);
    }

    private void clearDirty(int fileId) {
        clearDirty(new Integer(fileId));
    }

    private void clearDirty(Integer fileId) {
        _dirtyNodes.remove(fileId);
    }

    private void loadMetaDataFile() throws AxionException {
        File file = getMetaDataFile();
        if (file.exists()) {
            try {
                _metaDataFile = TTreeMetaDataFile.load(file);
            } catch (IOException e) {
                throw new AxionException(e);
            }
        } else {
            newMetaDataFile();
        }
    }

    private void newMetaDataFile() {
        _metaDataFile = new TTreeMetaDataFile();
        _metaDataFile.increment(); // start from 1
    }

    private void setDirty(int fileId, AbstractTTreeNode node) {
        setDirty(new Integer(fileId), node);
    }

    private void setDirty(Integer fileId, AbstractTTreeNode node) {
        _dirtyNodes.put(fileId, node);
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.