# HG changeset patch # User Shoshi TAMAKI # Date 1358351045 -32400 # Node ID dd7776c8b6d5540f1e448219d7c2a4a2679d813c # Parent 418088ca2b30ab4037cbfbc7681239de1038d7a5 commit diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultChangeListReader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultChangeListReader.java Thu Jan 17 00:44:05 2013 +0900 @@ -0,0 +1,32 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent; + +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; + +// not thread safe +public class DefaultChangeListReader implements ChangeListReader +{ + private long pos; + private ByteBuffer buf; + private FileChannel ch; + + DefaultChangeListReader(FileChannel _ch,ByteBuffer _buf,long _pos) + { + pos = _pos; + buf = _buf; + ch = _ch; + } + + @Override + public ChangeListReader newReader() + { + return new DefaultChangeListReader(ch,buf.asReadOnlyBuffer(),pos); + } + + @Override + public ChangeList read() + { + return null; + } +} diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultChangeListWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultChangeListWriter.java Thu Jan 17 00:44:05 2013 +0900 @@ -0,0 +1,5 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent; + +public class DefaultChangeListWriter { + +} diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultJournal.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/DefaultJournal.java Thu Jan 17 00:44:05 2013 +0900 @@ -0,0 +1,170 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent; + +import java.io.File; +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 java.nio.channels.WritableByteChannel; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; +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"); + f.setLength(20); + MappedByteBuffer a = f.getChannel().map(MapMode.READ_WRITE,0,10); + MappedByteBuffer b = f.getChannel().map(MapMode.READ_WRITE,10,10); + f.setLength(30); + MappedByteBuffer c = f.getChannel().map(MapMode.READ_WRITE,20,10); + + a.putInt(1); + b.putInt(1); + 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() + { + + } + + public static Either 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); + + ChangeListReader reader = new DefaultChangeListReader(ch); + ChangeListWriter writer = new DefaultChangeListWriter(ch); + + }catch(FileNotFoundException _e){ + _e.printStackTrace(); + return null; // should return Error here. + }catch(IOException _e){ + _e.printStackTrace(); + } + return null; + } + + private static class Writer implements ChangeListWriter + { + + } + + @Override + public ChangeListReader getReader() + { + return null; + } + + @Override + public ChangeListWriter getWriter() + { + return writer; + } + + public interface OperationParser + { + T parse(String _line); + } + + public static enum TagID + { + APPEND_CHILD(from16(0x05)), + DELETE_CHILD(from16(0x04)), + PUT_ATTRIBUTE(from16(0x03)), + DELETE_ATTRIBUTE(from16(0x02)), + COMMIT(from16(0x01)); + + public static ByteBuffer from16(int... _is) + { + ByteBuffer buf = ByteBuffer.allocate(_is.length); + for(int i : _is){ + buf.put((byte)i); + } + + buf.rewind(); + return buf; + } + + private final ByteBuffer id; + + public TagID(ByteBuffer _id) + { + id = _id; + } + + public ByteBuffer getID() + { + return id; + } + }; + + private static interface Parser + { + public Either parse(ByteBuffer _buf); + } +} diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/Journal.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/Journal.java Thu Jan 17 00:44:05 2013 +0900 @@ -0,0 +1,7 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent; + +public interface Journal +{ + public ChangeListReader getReader(); + public ChangeListWriter getWriter(); +} diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/PersistentProvider.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/persistent/PersistentProvider.java Wed Jan 16 03:23:57 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent; - -public interface PersistentProvider -{ - public ChangeListReader getReader(); - public ChangeListWriter getWriter(); -} diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChangeSet.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChangeSet.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChangeSet.java Thu Jan 17 00:44:05 2013 +0900 @@ -1,6 +1,5 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Tree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; @@ -12,11 +11,11 @@ private final long revision; private final ChangeList changeList; - private final DefaultTree tree; + private final DefaultNode root; - public DefaultChangeSet(DefaultTree _tree,ChangeList _changeList,String _uuid,long _revision,ChangeSet _prev) + public DefaultChangeSet(DefaultNode _root,ChangeList _changeList,String _uuid,long _revision,ChangeSet _prev) { - tree = _tree; + root = _root; uuid = _uuid; revision = _revision; prev = _prev; @@ -48,9 +47,9 @@ } @Override - public DefaultTree getTree() + public DefaultNode getRoot() { - return tree; + return root; } @Override diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Thu Jan 17 00:44:05 2013 +0900 @@ -13,7 +13,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableWrapper; -public class DefaultChildren implements EditableChildren +public class DefaultChildren implements EditableChildren { private final List children; private final TreeMap attrs; @@ -46,7 +46,7 @@ } @Override - public Either addNewChildAt(int _pos) + public Either addNewChildAt(int _pos) { if(!check(_pos)){ return DefaultEither.newA(OUT_OF_RANGE); @@ -54,13 +54,13 @@ P2,List> split = children.splitAt(_pos); List newChildren = split._1().snoc(new DefaultNode()).append(split._2()); - EditableNode newNode = new DefaultNode(newChildren,attrs); + DefaultNode newNode = new DefaultNode(newChildren,attrs); return DefaultEither.newB(newNode); } @Override - public Either deleteChildAt(int _pos) + public Either deleteChildAt(int _pos) { if(!check(_pos)){ return DefaultEither.newA(OUT_OF_RANGE); @@ -69,7 +69,7 @@ P2,List> split = children.splitAt(_pos); List newChildren = split._1().init().append(split._2()); - EditableNode newNode = new DefaultNode(newChildren,attrs); + DefaultNode newNode = new DefaultNode(newChildren,attrs); return DefaultEither.newB(newNode); } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTree.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTree.java Wed Jan 16 03:23:57 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Tree; - -public class DefaultTree implements Tree -{ - private final DefaultNode root; - - public static final DefaultTree EMPTY = new DefaultTree(); - - public DefaultTree() - { - root = new DefaultNode(); - } - - @Override - public DefaultNode getRoot() - { - return root; - } -} diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Thu Jan 17 00:44:05 2013 +0900 @@ -2,11 +2,10 @@ import java.nio.ByteBuffer; import java.util.Iterator; - import fj.data.List; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TraversableTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditorError; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.Logger; @@ -37,7 +36,7 @@ } @Override - public TraversableTree getTree() + public Node getRoot() { return null; } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java Thu Jan 17 00:44:05 2013 +0900 @@ -6,4 +6,4 @@ public interface NodeEditor { public > Either edit(T _e); -} +} \ No newline at end of file diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluation.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluation.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluation.java Thu Jan 17 00:44:05 2013 +0900 @@ -1,11 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public class DefaultEvaluation> implements Evaluation +public class DefaultEvaluation implements Evaluation { private final Result result; - private final Evaluator evaluator; + private final Evaluator evaluator; - public DefaultEvaluation(Result _result,Evaluator _evaluator) + public DefaultEvaluation(Result _result,Evaluator _evaluator) { result = _result; evaluator = _evaluator; @@ -18,7 +18,7 @@ } @Override - public Evaluator evaluator() + public Evaluator evaluator() { return evaluator; } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluator.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluator.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluator.java Thu Jan 17 00:44:05 2013 +0900 @@ -3,7 +3,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; -public class DefaultEvaluator> implements Evaluator +public class DefaultEvaluator implements Evaluator { private final NodePath path; @@ -13,14 +13,14 @@ } @Override - public Evaluation evaluate(T _current,int _pos) + public > Evaluation evaluate(T _current,int _pos) { Pair pop = path.pop(); if(pop == null){ - return new DefaultEvaluation(Result.BREAK,null); + return new DefaultEvaluation(Result.BREAK,null); } - DefaultEvaluator nextEvaluator = new DefaultEvaluator(pop.right()); + DefaultEvaluator nextEvaluator = new DefaultEvaluator(pop.right()); int head = pop.left(); Result result = Result.CONTINUE; @@ -28,6 +28,6 @@ result = Result.ACCEPT; } - return new DefaultEvaluation(result,nextEvaluator); + return new DefaultEvaluation(result,nextEvaluator); } } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Thu Jan 17 00:44:05 2013 +0900 @@ -7,10 +7,10 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import fj.data.List; -public class DefaultTraverser> implements Traverser +public class DefaultTraverser implements Traverser { @Override - public Either> traverse(final T _root,Evaluator _evaluator) + public > Either> traverse(final T _root,Evaluator _evaluator) { TraversableChildren wrapper = new TraversableChildren(){ @Override @@ -47,11 +47,11 @@ return DefaultEither.newB(traversal); } - private Either>> _traverse(TraversableChildren _chs,Evaluator _evaluator) + private > Either>> _traverse(TraversableChildren _chs,Evaluator _evaluator) { int pos = 0; for(T ch : _chs){ - Evaluation e = _evaluator.evaluate(ch,pos); + Evaluation e = _evaluator.evaluate(ch,pos); Result r = e.result(); if(r == Result.ACCEPT){ return _accept(ch,pos,e.evaluator()); @@ -73,7 +73,7 @@ return DefaultEither.newA(TraverserError.PATH_NOT_FOUND); } - private Either>> _accept(final T _current,final int _pos,Evaluator _evaluator) + private > Either>> _accept(final T _current,final int _pos,Evaluator _evaluator) { TraversableChildren chs = _current.children(); Either>> either = _traverse(chs,_evaluator); diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluation.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluation.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluation.java Thu Jan 17 00:44:05 2013 +0900 @@ -1,7 +1,7 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public interface Evaluation> +public interface Evaluation { public Result result(); - public Evaluator evaluator(); + public Evaluator evaluator(); } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluator.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluator.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluator.java Thu Jan 17 00:44:05 2013 +0900 @@ -1,6 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public interface Evaluator> +public interface Evaluator { - public Evaluation evaluate(T _current,int _pos); + public > Evaluation evaluate(T _current,int _pos); } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversable.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversable.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversable.java Thu Jan 17 00:44:05 2013 +0900 @@ -5,5 +5,5 @@ public interface Traversable> { - public Either> traverse(Evaluator _e); + public Either> traverse(Evaluator _e); } diff -r 418088ca2b30 -r dd7776c8b6d5 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traverser.java --- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traverser.java Wed Jan 16 03:23:57 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traverser.java Thu Jan 17 00:44:05 2013 +0900 @@ -3,7 +3,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public interface Traverser> +public interface Traverser { - public Either> traverse(T _root,Evaluator _evaluator); + public > Either> traverse(T _root,Evaluator _evaluator); }