Mercurial > hg > Members > shoshi > jungle > bulletinboard
changeset 1:babb46537b8a
added junglebulletinboard
author | Shoshi TAMAKI |
---|---|
date | Sat, 09 Feb 2013 19:43:46 +0900 |
parents | e7a9b10dd844 |
children | 5f80e6019970 |
files | pom.xml src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/App.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/JungleBulletinBoard.java src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/AppTest.java target/classes/META-INF/MANIFEST.MF target/classes/META-INF/maven/jp.ac.u_ryukyu.ie.cr.shoshi.jungle/bulletinboard/pom.properties target/classes/META-INF/maven/jp.ac.u_ryukyu.ie.cr.shoshi.jungle/bulletinboard/pom.xml |
diffstat | 7 files changed, 229 insertions(+), 64 deletions(-) [+] |
line wrap: on
line diff
--- a/pom.xml Thu Feb 07 00:26:21 2013 +0900 +++ b/pom.xml Sat Feb 09 19:43:46 2013 +0900 @@ -41,6 +41,11 @@ <artifactId>uuid</artifactId> <version>3.3</version> </dependency> + <dependency> + <groupId>jungle</groupId> + <artifactId>jungle-core</artifactId> + <version>0.0.1-SNAPSHOT</version> + </dependency> </dependencies> <repositories> <repository>
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/App.java Thu Feb 07 00:26:21 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/App.java Sat Feb 09 19:43:46 2013 +0900 @@ -14,7 +14,13 @@ { public static void main( String[] args ) throws Exception { - BulletinBoard cassaBBS = new CassandraBulletinBoard("test-cluster","localhost:9160","cassaBBS2"); + BulletinBoard cassaBBS = null; + if(args[0].equals("cassandra")){ + cassaBBS = new CassandraBulletinBoard("test-cluster","localhost:9160","cassaBBS2"); + }else if(args[0].equals("jungle")){ + cassaBBS = new JungleBulletinBoard(); + } + String createBoardMessagePath = "/createBoardMessage"; String createBoardPath = "/createBoard";
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/JungleBulletinBoard.java Sat Feb 09 19:43:46 2013 +0900 @@ -0,0 +1,205 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs; + +import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicInteger; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; + +public class JungleBulletinBoard implements BulletinBoard +{ + private final Jungle jungle; + + public JungleBulletinBoard() + { + jungle = new DefaultJungle(null,"hoge",new DefaultTreeEditor(new DefaultTraverser())); + jungle.createNewTree("boards"); + } + + public Iterable<String> getBoards() + { + JungleTree tree = jungle.getTreeByName("boards"); + Node node = tree.getRootNode(); + Children<Node> chs = node.getChildren(); + + IterableConverter.Converter<String,Node> converter = new IterableConverter.Converter<String,Node>(){ + public String conv(Node _b) { + ByteBuffer e = _b.getAttributes().get("name"); + return new String(e.array()); + } + }; + + return new IterableConverter<String,Node>(chs,converter); + } + + public void createBoards(final String _name,final String _author,final String _initMessage,final String _editKey) + { + if(null == jungle.createNewTree(_name)){ + throw new IllegalStateException(); + } + + JungleTree tree = jungle.getTreeByName("boards"); + JungleTreeEditor editor = tree.getTreeEditor(); + DefaultNodePath root = new DefaultNodePath(); + Either<Error,JungleTreeEditor> either = editor.addNewChildAt(root,0); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + + either = editor.putAttribute(root.add(0),"name",ByteBuffer.wrap(_name.getBytes())); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + Either<Error,JungleTreeEditor> result = editor.success(); + if(result.isA()){ + throw new IllegalStateException(); + } + + tree = jungle.getTreeByName(_name); + editor = tree.getTreeEditor(); + either = editor.addNewChildAt(root,0); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + + NodeEditor e = new NodeEditor(){ + public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ + _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); + _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_initMessage.getBytes())).b(); + _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); + return DefaultEither.newB(_e); + } + }; + + either = editor.edit(root.add(0),e); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + editor.success(); + } + + public void createBoardMessage(final String _board,final String _author,final String _message,final String _editKey) + { + JungleTree tree = jungle.getTreeByName(_board); + if(tree == null){ + throw new IllegalStateException(); + } + + Node node = tree.getRootNode(); + int size = node.getChildren().size(); + DefaultNodePath path = new DefaultNodePath(); + + JungleTreeEditor editor = tree.getTreeEditor(); + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path,size); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + + NodeEditor e = new NodeEditor(){ + public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ + _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); + _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); + _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); + return DefaultEither.newB(_e); + } + }; + + path = path.add(size); + either = editor.edit(path,e); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + editor.success(); + } + + public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) + { + DefaultNodePath path = new DefaultNodePath(); + path = path.add(Integer.parseInt(_uuid)); + + JungleTree tree = jungle.getTreeByName(_board); + JungleTreeEditor editor = tree.getTreeEditor(); + NodeEditor e = new NodeEditor(){ + public <T extends EditableNode<T>> Either<Error, T> edit(T _e){ + _e = _e.getAttributes().put("author",ByteBuffer.wrap(_author.getBytes())).b(); + _e = _e.getAttributes().put("mes",ByteBuffer.wrap(_message.getBytes())).b(); + _e = _e.getAttributes().put("key",ByteBuffer.wrap(_editKey.getBytes())).b(); + return DefaultEither.newB(_e); + } + }; + + Either<Error,JungleTreeEditor> either = editor.edit(path,e); + if(either.isA()){ + throw new IllegalStateException(); + } + editor = either.b(); + editor.success(); + } + + public Iterable<BoardMessage> getMessages(String _boardName) + { + JungleTree tree = jungle.getTreeByName(_boardName); + Node node = tree.getRootNode(); + Children<Node> chs = node.getChildren(); + + final AtomicInteger counter = new AtomicInteger(0); + IterableConverter.Converter<BoardMessage,Node> converter = new IterableConverter.Converter<BoardMessage,Node>(){ + public BoardMessage conv(Node _b) { + String uuid = Integer.toString(counter.get()); + String author = new String(_b.getAttributes().get("author").array()); + String message = new String(_b.getAttributes().get("mes").array()); + counter.incrementAndGet(); + return new BoardMessageImpl(author,message,uuid); + } + }; + + return new IterableConverter<BoardMessage,Node>(chs,converter); + } + + private static class BoardMessageImpl implements BoardMessage + { + private final String author; + private final String message; + private final String uuid; + + public BoardMessageImpl(String _author,String _message,String _uuid) + { + author = _author; + message = _message; + uuid = _uuid; + } + + public String getAuthor() + { + return author; + } + + public String getMessage() + { + return message; + } + + public String getUUID() + { + return uuid; + } + } +}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/AppTest.java Thu Feb 07 00:26:21 2013 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/bbs/AppTest.java Sat Feb 09 19:43:46 2013 +0900 @@ -1,5 +1,7 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs; +import fj.P2; +import fj.data.List; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -10,6 +12,16 @@ public class AppTest extends TestCase { + public static void main(String _args[]) + { + List<Integer> list = List.range(0,5); + P2<List<Integer>, List<Integer>> split = list.splitAt(3); + System.out.println(list); + System.out.println(list.init()); + System.out.println(split._1()); + System.out.println(split._2()); + } + /** * Create the test case *
--- a/target/classes/META-INF/MANIFEST.MF Thu Feb 07 00:26:21 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -Manifest-Version: 1.0 -Built-By: shoshi -Build-Jdk: 1.6.0_37 -Created-By: Maven Integration for Eclipse -
--- a/target/classes/META-INF/maven/jp.ac.u_ryukyu.ie.cr.shoshi.jungle/bulletinboard/pom.properties Thu Feb 07 00:26:21 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -#Generated by Maven Integration for Eclipse -#Thu Feb 07 00:25:48 JST 2013 -version=0.0.1-SNAPSHOT -groupId=jp.ac.u_ryukyu.ie.cr.shoshi.jungle -m2e.projectName=bulletinboard -m2e.projectLocation=/Users/shoshi/Documents/eclipse/jungle/bulletinboard -artifactId=bulletinboard
--- a/target/classes/META-INF/maven/jp.ac.u_ryukyu.ie.cr.shoshi.jungle/bulletinboard/pom.xml Thu Feb 07 00:26:21 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - - <groupId>jp.ac.u_ryukyu.ie.cr.shoshi.jungle</groupId> - <artifactId>bulletinboard</artifactId> - <version>0.0.1-SNAPSHOT</version> - <packaging>jar</packaging> - - <name>bulletinboard</name> - <url>http://maven.apache.org</url> - - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - </properties> - - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>3.8.1</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.mortbay.jetty</groupId> - <artifactId>jetty</artifactId> - <version>6.1.26</version> - </dependency> - <dependency> - <groupId>org.hectorclient</groupId> - <artifactId>hector-core</artifactId> - <version>1.1-2</version> - </dependency> - <dependency> - <groupId>org.apache.cassandra</groupId> - <artifactId>cassandra-all</artifactId> - <version>1.2.1</version> - </dependency> - <dependency> - <groupId>com.eaio.uuid</groupId> - <artifactId>uuid</artifactId> - <version>3.3</version> - </dependency> - </dependencies> - <repositories> - <repository> - <id>eaio.com</id> - <url>http://eaio.com/maven2</url> - </repository> - </repositories> -</project>