Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 32:dd7776c8b6d5
commit
line wrap: on
line diff
--- /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; + } +}
--- /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 { + +}
--- /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<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); + + 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 extends Operation> + { + 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<Erorr,Object> parse(ByteBuffer _buf); + } +}
--- /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(); +}
--- 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(); -}
--- 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
--- 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<DefaultNode> { private final List<DefaultNode> children; private final TreeMap<String,ByteBuffer> attrs; @@ -46,7 +46,7 @@ } @Override - public Either<Error,EditableNode> addNewChildAt(int _pos) + public Either<Error,DefaultNode> addNewChildAt(int _pos) { if(!check(_pos)){ return DefaultEither.newA(OUT_OF_RANGE); @@ -54,13 +54,13 @@ P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); List<DefaultNode> 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<Error,EditableNode> deleteChildAt(int _pos) + public Either<Error,DefaultNode> deleteChildAt(int _pos) { if(!check(_pos)){ return DefaultEither.newA(OUT_OF_RANGE); @@ -69,7 +69,7 @@ P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); List<DefaultNode> newChildren = split._1().init().append(split._2()); - EditableNode newNode = new DefaultNode(newChildren,attrs); + DefaultNode newNode = new DefaultNode(newChildren,attrs); return DefaultEither.newB(newNode); }
--- 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; - } -}
--- 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; }
--- 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 <T extends EditableNode<T>> Either<Error,T> edit(T _e); -} +} \ No newline at end of file
--- 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<T extends TraversableNode<T>> implements Evaluation<T> +public class DefaultEvaluation implements Evaluation { private final Result result; - private final Evaluator<T> evaluator; + private final Evaluator evaluator; - public DefaultEvaluation(Result _result,Evaluator<T> _evaluator) + public DefaultEvaluation(Result _result,Evaluator _evaluator) { result = _result; evaluator = _evaluator; @@ -18,7 +18,7 @@ } @Override - public Evaluator<T> evaluator() + public Evaluator evaluator() { return evaluator; }
--- 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<T extends TraversableNode<T>> implements Evaluator<T> +public class DefaultEvaluator implements Evaluator { private final NodePath path; @@ -13,14 +13,14 @@ } @Override - public Evaluation<T> evaluate(T _current,int _pos) + public <T extends TraversableNode<T>> Evaluation evaluate(T _current,int _pos) { Pair<Integer,NodePath> pop = path.pop(); if(pop == null){ - return new DefaultEvaluation<T>(Result.BREAK,null); + return new DefaultEvaluation(Result.BREAK,null); } - DefaultEvaluator<T> nextEvaluator = new DefaultEvaluator<T>(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<T>(result,nextEvaluator); + return new DefaultEvaluation(result,nextEvaluator); } }
--- 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<T extends TraversableNode<T>> implements Traverser<T> +public class DefaultTraverser implements Traverser { @Override - public Either<Error,Traversal<T>> traverse(final T _root,Evaluator<T> _evaluator) + public <T extends TraversableNode<T>> Either<Error,Traversal<T>> traverse(final T _root,Evaluator _evaluator) { TraversableChildren<T> wrapper = new TraversableChildren<T>(){ @Override @@ -47,11 +47,11 @@ return DefaultEither.newB(traversal); } - private Either<Error,List<Direction<T>>> _traverse(TraversableChildren<T> _chs,Evaluator<T> _evaluator) + private <T extends TraversableNode<T>> Either<Error,List<Direction<T>>> _traverse(TraversableChildren<T> _chs,Evaluator _evaluator) { int pos = 0; for(T ch : _chs){ - Evaluation<T> 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<Error,List<Direction<T>>> _accept(final T _current,final int _pos,Evaluator<T> _evaluator) + private <T extends TraversableNode<T>> Either<Error,List<Direction<T>>> _accept(final T _current,final int _pos,Evaluator _evaluator) { TraversableChildren<T> chs = _current.children(); Either<Error,List<Direction<T>>> either = _traverse(chs,_evaluator);
--- 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<T extends TraversableNode<T>> +public interface Evaluation { public Result result(); - public Evaluator<T> evaluator(); + public Evaluator evaluator(); }
--- 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<T extends TraversableNode<T>> +public interface Evaluator { - public Evaluation<T> evaluate(T _current,int _pos); + public <T extends TraversableNode<T>> Evaluation evaluate(T _current,int _pos); }
--- 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<T extends TraversableNode<T>> { - public Either<Error,Traversal<T>> traverse(Evaluator<T> _e); + public Either<Error,Traversal<T>> traverse(Evaluator _e); }
--- 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<T extends TraversableNode<T>> +public interface Traverser { - public Either<Error,Traversal<T>> traverse(T _root,Evaluator<T> _evaluator); + public <T extends TraversableNode<T>> Either<Error,Traversal<T>> traverse(T _root,Evaluator _evaluator); }