view src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultJournal.java @ 104:f9a0e7069811

delete worning halfway
author one
date Fri, 12 Sep 2014 16:22:22 +0900
parents 715a9fbf02fc
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;

/*
 * text based journal.
 * 
 * non terminal symbols
 * 
 * JOURNAL => CHANGELISTS
 * CHANGE_LISTS => CHANGE_LIST | CHANGE_LISTS
 * CHANGE_LISTS => *EMPTY*
 * 
 * CHANGE_LIST => OPERATIONS
 * OPERATIONS => OPERATION CR | OPERATIONS
 * OPERATIONS => *EMPTY*

 * OPERATION => APPEND_CHILD | DELETE_CHILD | PUT_ATTRIBUTE | DELETE_ATTRIBUTE | COMMIT
 * APPEND_CHILD => APPEND_CHILD_ID NODE_PATH INSERT_POSITION
 * DELETE_CHILD => DELETE_CHILD_ID NODE_PATH DELETE_POSITION
 * PUT_ATTRIBUTE => PUT_ATTRIBUTE_ID NODE_PATH KEY VALUE
 * DELETE_ATTRIBUTE => DELTE_ATTRIBUTE_ID NODE_PATH KEY
 * COMMIT => COMMIT_ID
 * 
 * NODE_PATH => POSITION "." NODE_PATH | POSITION
 * 
 * terminal symbols
 * 
 * APPEND_CHILD_ID = "APPEND_CHILD"
 * DELETE_CHILD_ID = "DELETE_CHILD"
 * PUT_ATTRIBUTE_ID = "PUT_ATTRIBUTE"
 * SET_ATTRIBUTE_ID = "SET_ATTRIBUTE"
 * COMMIT_ID => "COMMIT"
 * CR => \n
 * INSERT_POSITION => INTEGER
 * DELETE_POSITION => INTEGER
 * KEY => "*"
 * VALUE => "*"
 * 
 */

public class DefaultJournal implements Journal
{
	private final ChangeListWriter writer;
	
	public static void main(String args[]) throws Exception
	{
		RandomAccessFile f = new RandomAccessFile("./miscs/1.dat","rw");
		MappedByteBuffer a = f.getChannel().map(MapMode.READ_WRITE,10,10);
		MappedByteBuffer b = f.getChannel().map(MapMode.READ_WRITE,10,10);
		MappedByteBuffer c = f.getChannel().map(MapMode.READ_WRITE,20,10);
		
		a.putInt(5);
		System.out.println(b.getInt());
		c.putInt(2);
		a.force();
		b.force();
		c.force();
	
		System.out.println(b.capacity());
		MappedByteBuffer d = f.getChannel().map(MapMode.READ_WRITE,50,10);
		d.putInt(2);
		d.force();
		System.out.println(d.getInt());
	}
	
	public static final int READ_BUFFER_SIZE = 1000000;  // 1MB
	public static final int WRITE_BUFFER_SIZE = 1000000; // 1MB
	
	private DefaultJournal()
	{
		writer = null;
	}
	
	public static Either<Error,DefaultJournal> newInstance(String fileName)
	{
		RandomAccessFile journal = null;
		try{
			journal = new RandomAccessFile(fileName,"rw");
			FileChannel ch = journal.getChannel();
			long length = journal.length();
			ByteBuffer readBuf = ch.map(MapMode.READ_ONLY,0,READ_BUFFER_SIZE);
			ByteBuffer writeBuf = ch.map(MapMode.READ_WRITE,length,WRITE_BUFFER_SIZE);
			
		}catch(FileNotFoundException _e){
			_e.printStackTrace();
			return null; // should return Error here.
		}catch(IOException _e){
			_e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public ChangeListReader getReader()
	{
		return null;
	}

	@Override
	public ChangeListWriter getWriter()
	{
		return writer;
	}
}