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

com.persistit.VolumeStorage Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/**
 * Copyright © 2011-2012 Akiban Technologies, Inc.  All rights reserved.
 * 
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * This program may also be available under different license terms.
 * For more information, see www.akiban.com or contact [email protected].
 * 
 * Contributors:
 * Akiban Technologies, Inc.
 */

package com.persistit;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;

import com.persistit.exception.InUseException;
import com.persistit.exception.InvalidPageAddressException;
import com.persistit.exception.PersistitException;
import com.persistit.exception.PersistitIOException;
import com.persistit.exception.PersistitInterruptedException;
import com.persistit.exception.VolumeClosedException;

/**
 * Manage all details of file I/O for a Volume backing file.
 * 
 * @author peter
 */
abstract class VolumeStorage extends SharedResource {

    private final static Random ID_GENERATOR = new Random();
    protected Volume _volume;

    /**
     * Generate a random positive (non-zero) long value to be used as a
     * validation of a Volume's identity.
     * 
     * @return
     */
    protected static long generateId() {
        return (ID_GENERATOR.nextLong() & 0x0FFFFFFFFFFl) + 1;
    }

    VolumeStorage(final Persistit persistit, final Volume volume) {
        super(persistit);
        _volume = volume;
    }

    /**
     * Returns the path name by which this volume was opened.
     * 
     * @return The path name
     */
    String getPath() {
        return _volume.getSpecification().getPath();
    }

    /**
     * Indicate whether this Volume prohibits updates.
     * 
     * @return true if this Volume prohibits updates.
     */
    boolean isReadOnly() {
        return _volume.getSpecification().isReadOnly();
    }

    /**
     * Indicate whether this is a temporary volume
     * 
     * @return true if this volume is temporary
     */
    abstract boolean isTemp();

    /**
     * @return the channel used to read and write pages of this volume.
     * @throws PersistitIOException
     */
    abstract FileChannel getChannel() throws PersistitIOException;

    /**
     * Create a new Volume backing file according to the
     * {@link Volume}'s volume specification.
     * 
     * @throws PersistitException
     */
    abstract void create() throws PersistitException;

    /**
     * Open an existing Volume backing file.
     * 
     * @throws PersistitException
     */
    abstract void open() throws PersistitException;

    /**
     * @return true if a backing file exists on the specified path.
     * @throws PersistitException
     */
    abstract boolean exists() throws PersistitException;

    /**
     * Delete the backing file for this Volume if it exists.
     * 
     * @return true if there was a file and it was successfully
     *         deleted
     * @throws PersistitException
     */
    abstract boolean delete() throws PersistitException;

    /**
     * Force all file system buffers to disk.
     * 
     * @throws PersistitIOException
     */
    abstract void force() throws PersistitIOException;

    /**
     * Close the file resources held by this Volume. After this
     * method is called no further file I/O is possible.
     * 
     * @throws PersistitException
     */
    abstract void close() throws PersistitException;

    /**
     * Flush metadata to the volume backing store.
     * 
     * @throws PersistitException
     */
    abstract void flush() throws PersistitException;

    abstract void truncate() throws PersistitException;

    abstract boolean isOpened();

    abstract boolean isClosed();

    abstract long getExtentedPageCount();

    abstract long getNextAvailablePage();

    abstract void claimHeadBuffer() throws PersistitException;

    abstract void releaseHeadBuffer();

    abstract void readPage(Buffer buffer) throws PersistitIOException, InvalidPageAddressException,
            VolumeClosedException, InUseException, PersistitInterruptedException;

    abstract void writePage(final Buffer buffer) throws PersistitException;

    abstract void writePage(final ByteBuffer bb, final long page) throws PersistitException;

    abstract long allocNewPage() throws PersistitException;

    abstract void extend(final long pageAddr) throws PersistitException;

    abstract void flushMetaData() throws PersistitException;

    abstract boolean updateMetaData(final byte[] bytes);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy