PhysicalFileAccessNologSynchroImpl.java :  » Database-DBMS » jalisto » org » objectweb » jalisto » se » storage » raf » nolog » synchro » Java Open Source

Java Open Source » Database DBMS » jalisto 
jalisto » org » objectweb » jalisto » se » storage » raf » nolog » synchro » PhysicalFileAccessNologSynchroImpl.java
/*
 * Jalisto - JAva LIght STOrage
 * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Xcalia
 * 71, rue Desnouettes
 * 75014 Paris - France
 * http://www.xcalia.com
 */
package org.objectweb.jalisto.se.storage.raf.nolog.synchro;

import org.objectweb.jalisto.se.api.JalistoProperties;
import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess;
import org.objectweb.jalisto.se.api.internal.JalistoObject;
import org.objectweb.jalisto.se.exception.JalistoException;
import org.objectweb.jalisto.se.impl.InFileAddress;
import org.objectweb.jalisto.se.JalistoFactory;
import org.objectweb.jalisto.se.impl.trace.Trace;
import org.objectweb.jalisto.se.storage.raf.*;

import java.io.*;
import java.util.Collection;

public class PhysicalFileAccessNologSynchroImpl implements PluggablePhysicalFileAccess {

    private void init(int initialSize, JalistoProperties properties)
            throws IOException, RecordsFileException {
        int nbrDbFiles = properties.getInstanceDbFileNumber();
        if (nbrDbFiles > 1) {
            files = new RecordsFile[nbrDbFiles + 2];
            files[0] = new RecordsFile(properties.getSystemDbFilePath());
            files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
            files[1] = new RecordsFile(properties.getOidDbFilePath());
            files[1].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());

            for (int i = 0; i < nbrDbFiles; i++) {
                files[i + 2] = new RecordsFile(properties.getInstanceDbFilePathAt(i));
                files[i + 2].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
            }
        } else {
            files = new RecordsFile[1];
            files[0] = new RecordsFile(properties.getSystemDbFilePath());
            files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
        }
    }

    private void init(String accessFlags, JalistoProperties properties)
            throws IOException, RecordsFileException {
        int nbrDbFiles = properties.getInstanceDbFileNumber();
        if (nbrDbFiles > 1) {
            files = new RecordsFile[nbrDbFiles + 2];
            files[0] = new RecordsFile(properties.getSystemDbFilePath(), accessFlags);
            files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
            files[1] = new RecordsFile(properties.getOidDbFilePath(), accessFlags);
            files[1].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());

            for (int i = 0; i < nbrDbFiles; i++) {
                files[i + 2] = new RecordsFile(properties.getInstanceDbFilePathAt(i), accessFlags);
                files[i + 2].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
            }
        } else {
            files = new RecordsFile[1];
            files[0] = new RecordsFile(properties.getSystemDbFilePath(), accessFlags);
            files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
        }
    }

    public void init(JalistoProperties properties) {
        try {
            String dbPath = properties.getDbFileFullName();
            File f = new File(dbPath);
            this.isNewBase = (!f.exists());
            trace = JalistoFactory.getInternalFactory().getTracer(properties);
            if (isNewBase && properties.isReadOnlyImplementation()) {
                throw new JalistoException("cannot create a database instance in read only mode");
            }
            if (isNewBase) {
                init(properties.getInitialSize(), properties);
            } else {
                init("rw", properties);
            }
            for (int i = 0; i < files.length; i++) {
                files[i].setTrace(trace);
            }
            trace.println(Trace.PHYSICAL, "new instance with {0} underlying files", new Integer(files.length));
        } catch (Exception e) {
            throw new JalistoException(e);
        }
    }

    public boolean isNewBase() {
        return isNewBase;
    }

    public void reorganize() {
        trace.println(Trace.PHYSICAL, "synchro reorganize : ");
        for (int i = 0; i < files.length; i++) {
            try {
                files[i].reorganize();
            } catch (Exception e) {
                throw new JalistoException(
                        "error during reorganization of a physical store file : " + files[i].toString(), e);
            }
        }
    }

    public Collection getKeysStartingWith(String filter) {
        trace.println(Trace.PHYSICAL, "synchro getKeysStartingWith : filter = {0}", filter);
        if (files.length > 1) {
            Collection result = files[0].getKeysStartingWith(filter);
            for (short i = 1; i < files.length; i++) {
                result.addAll(files[i].getKeysStartingWith(filter));
            }
            return result;
        } else {
            return files[0].getKeysStartingWith(filter);
        }
    }

    public void insertFileObject(InFileAddress ifa, JalistoObject fo) {
        trace.println(Trace.PHYSICAL, "synchro insertFileObject({0}, {1})", ifa, fo);
        int i = 0;
        RecordWriter rw = null;
        RecordReader rr = null;
        try {
            rw = new RecordWriter(ifa.getAddress());
            rw.writeObject(fo);
            if (files.length > 1) {
                i = ifa.getFileIndex();
            }
            files[i].insertRecord(rw);
            rw.reset();
        } catch (Exception e) {
            Object v;
            try {
                rr = files[i].readRecord(ifa.getAddress());
                v = rr.readObject();
                rr.reset();
            } catch (Exception e2) {
                throw new JalistoException("could not insert object " + fo, e);
            }
            throw new JalistoException("could not insert object " + fo +
                                           ";\n\t\tvalue already in base : " + String.valueOf(v), e);
//        } finally {
//            if (rr != null) {
//                rr.reset();
//            }
//            if (rw != null) {
//                rw.reset();
//            }
        }
    }

    public JalistoObject readFileObjectAt(InFileAddress ifa) {
        trace.println(Trace.PHYSICAL, "synchro readFileObjectAt : ");
        JalistoObject result;
        int i = 0;
        RecordReader rr = null;
        try {
            if (files.length > 1) {
                i = ifa.getFileIndex();
            }
            rr = files[i].readRecord(ifa.getAddress());
            result = (JalistoObject) rr.readObject();
            result.setIfa(ifa);
        } catch (Exception e) {
            throw new JalistoException("could not read object at " + ifa, e);
//        } finally {
//            if (rr != null) {
//                rr.reset();
//            }
        }
        return result;
    }

    public void updateFileObject(InFileAddress ifa, JalistoObject fo) {
        trace.println(Trace.PHYSICAL, "synchro updateFileObject({0}, {1})", ifa, fo);
        RecordWriter rw = null;
        try {
            rw = new RecordWriter(ifa.getAddress());
            rw.writeObject(fo);
            int i = 0;
            if (files.length > 1) {
                i = ifa.getFileIndex();
            }
            files[i].updateRecord(rw);
            rw.reset();
        } catch (Exception e) {
            throw new JalistoException("could not update object " + fo, e);
//        } finally {
//            if (rw != null) {
//                rw.reset();
//            }
        }
    }

    public void deleteFileObject(InFileAddress ifa) {
        trace.println(Trace.PHYSICAL, "synchro deleteFileObject : ");
        try {
            int i = 0;
            if (files.length > 1) {
                i = ifa.getFileIndex();
            }
            files[i].deleteRecord(ifa.getAddress());
        } catch (Exception e) {
            throw new JalistoException("could not delete object at " + ifa, e);
        }
    }


    public String toString() {
        RecordsFileInspector rfi = new RecordsFileInspector();
        StringWriter sw = new StringWriter();
        PrintWriter out = new PrintWriter(sw);
        try {
            rfi.inspectRecordsFile(out, files[0]);
        } catch (Exception e) {
            out.println("error during inspection of the database : " + e.getMessage());
        }
        out.flush();
        return sw.toString();
    }

    public void startCommit() {
    }

    public void commit() {
    }

    public void rollback() {
    }

    public void close() {
    }

    public void open() {
    }


    private RecordsFile[] files;
    private boolean isNewBase;
    private Trace trace;
}

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.