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

org.zoodb.jdo.internal.server.StorageRootFile Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2009-2013 Tilmann Zaeschke. All rights reserved.
 * 
 * This file is part of ZooDB.
 * 
 * ZooDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ZooDB 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ZooDB.  If not, see .
 * 
 * See the README and COPYING files for further information. 
 */
package org.zoodb.jdo.internal.server;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.ArrayList;

import org.zoodb.api.ZooDebug;
import org.zoodb.jdo.api.impl.DBStatistics;
import org.zoodb.jdo.internal.server.index.FreeSpaceManager;
import org.zoodb.jdo.internal.util.DBLogger;
import org.zoodb.jdo.internal.util.PrimLongMapLI;

/**
 * A common root for multiple file views. Each view accesses its own page,
 * the root contains the common file resource.
 * 
 * @author Tilmann Zaeschke
 *
 */
public final class StorageRootFile implements StorageChannel {

	private final ArrayList viewsIn = new ArrayList();
	private final ArrayList viewsOut = new ArrayList();

	private final FreeSpaceManager fsm;
	private final RandomAccessFile raf;
	private final FileLock fileLock;
	private final FileChannel fc;
	// use LONG to enforce long-arithmetic in calculations
	private final long PAGE_SIZE;

	private int statNRead; 
	private int statNWrite; 
	private final PrimLongMapLI statNReadUnique = new PrimLongMapLI();
	private long txId;

	public StorageRootFile(String dbPath, String options, int pageSize, FreeSpaceManager fsm) {
		this.fsm = fsm;
		PAGE_SIZE = pageSize;
		File file = new File(dbPath);
		if (!file.exists()) {
			throw DBLogger.newUser("DB file does not exist: " + dbPath);
		}
		try {
			raf = new RandomAccessFile(file, options);
			fc = raf.getChannel();
			try {
				//tryLock is supposed to return null, but it throws an Exception
				fileLock = fc.tryLock();
				if (ZooDebug.isTesting()) {
					ZooDebug.registerFile(fc);
				}
			} catch (OverlappingFileLockException e) {
				fc.close();
				raf.close();
				throw DBLogger.newUser(
						"The file is already accessed by another process: " + dbPath);
			}
		} catch (IOException e) {
			throw DBLogger.newFatal("Error opening database: " + dbPath, e);
		}
	}

	@Override
	public void acquireLock(long txId) {
		this.txId = txId;
	}
	
	@Override
	public long getTxId() {
		return this.txId;
	}
	
	@Override
	public final void close() {
		flush();
		try {
			fc.force(true);
			fileLock.release();
			fc.close();
			raf.close();
		} catch (IOException e) {
			throw DBLogger.newFatal("Error closing database file.", e);
		}
	}

	@Override
	public final StorageChannelInput getReader(boolean autoPaging) {
		StorageChannelInput in = new StorageReader(this, autoPaging);
		viewsIn.add(in);
		return in;
	}
	
	@Override
	public final StorageChannelOutput getWriter(boolean autoPaging) {
		StorageChannelOutput out = new StorageWriter(this, fsm, autoPaging);
		viewsOut.add(out);
		return out;
	}
	
	/**
	 * Not a true flush, just writes the stuff...
	 */
	@Override
	public final void flush() {
		//flush associated splits.
		for (StorageChannelOutput paf: viewsOut) {
			//flush() only writers
			paf.flush();
		}
		for (StorageChannelInput paf: viewsIn) {
			paf.reset();
		}
		try {
			fc.force(false);
		} catch (IOException e) {
			throw DBLogger.newFatal("Error writing database file.", e);
		}
	}

	@Override
	public final void readPage(ByteBuffer buf, long pageId) {
		try {
			fc.read(buf, pageId * PAGE_SIZE);
			if (DBStatistics.isEnabled()) {
				statNRead++;
				statNReadUnique.put(pageId, null);
			}
		} catch (IOException e) {
			throw DBLogger.newFatal("Error loading Page: " + pageId, e);
		}
	}

	@Override
	public final void write(ByteBuffer buf, long pageId) {
		try {
			if (pageId<0) {
				return;
			}
			if (DBStatistics.isEnabled()) {
				statNWrite++;
			}
			fc.write(buf, pageId * PAGE_SIZE);
		} catch (IOException e) {
			throw DBLogger.newFatal("Error writing page: " + pageId, e);
		}
	}

	@Override
	public final int statsGetReadCount() {
		return statNRead;
	}

	@Override
	public int statsGetReadCountUnique() {
		int ret = statNReadUnique.size();
		statNReadUnique.clear();
		return ret;
	}

	@Override
	public final int statsGetWriteCount() {
		return statNWrite;
	}

	@Override
	public final int getPageSize() {
		return (int) PAGE_SIZE;
	}

	@Override
	public void reportFreePage(int pageId) {
		fsm.reportFreePage(pageId);
	}

	@Override
	public int statsGetPageCount() {
		try {
			return (int) (raf.length() / PAGE_SIZE);
		} catch (IOException e) {
			throw DBLogger.newFatal("", e);
		}
	}

}