Mercurial > hg > Database > jungle-network
changeset 38:d8ee57a1c2c6
add pom.xml and bbs
line wrap: on
line diff
--- a/.classpath Mon Jul 08 20:10:47 2013 +0900 +++ b/.classpath Mon Jul 08 20:25:58 2013 +0900 @@ -1,15 +1,26 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="src" path="src"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="src" output="target/classes" path="src"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="lib" path="/Users/nobuyasu/workspace/Alice/Alice.jar"/> <classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/> <classpathentry kind="lib" path="lib/functionaljava.jar"/> <classpathentry kind="lib" path="lib/guava-12.0-sources.jar"/> <classpathentry kind="lib" path="lib/guava-12.0.jar"/> <classpathentry kind="lib" path="/jungle-core/target/jungle-core-0.0.1-SNAPSHOT.jar"/> - <classpathentry kind="lib" path="lib/jetty-6.1.26-sources.jar"/> - <classpathentry kind="lib" path="lib/jetty-6.1.26.jar"/> - <classpathentry kind="lib" path="lib/servlet-api-2.5.jar"/> - <classpathentry kind="output" path="bin"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="output" path="target/classes"/> </classpath>
--- a/.project Mon Jul 08 20:10:47 2013 +0900 +++ b/.project Mon Jul 08 20:25:58 2013 +0900 @@ -10,8 +10,14 @@ <arguments> </arguments> </buildCommand> + <buildCommand> + <name>org.eclipse.m2e.core.maven2Builder</name> + <arguments> + </arguments> + </buildCommand> </buildSpec> <natures> + <nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>
--- a/.settings/org.eclipse.jdt.core.prefs Mon Jul 08 20:10:47 2013 +0900 +++ b/.settings/org.eclipse.jdt.core.prefs Mon Jul 08 20:25:58 2013 +0900 @@ -1,5 +1,5 @@ -#Sun Jun 30 04:20:41 JST 2013 +eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 -eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.source=1.6 -org.eclipse.jdt.core.compiler.compliance=1.6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.settings/org.eclipse.m2e.core.prefs Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pom.xml Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,54 @@ +<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>jungle-network</groupId> + <artifactId>jungle-network</artifactId> + <version>0.0.1-SNAPSHOT</version> + <build> + <sourceDirectory>src</sourceDirectory> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.0</version> + <configuration> + <source>1.6</source> + <target>1.6</target> + </configuration> + </plugin> + </plugins> + </build> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + </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.2</version> + </dependency> + <dependency> + <groupId>jungle</groupId> + <artifactId>jungle-core</artifactId> + <version>0.0.1-SNAPSHOT</version> + </dependency> + </dependencies> +</project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/jungle/remote/RemoteConfig.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,24 @@ +package alice.jungle.remote; +import alice.daemon.Config; + + +public class RemoteConfig extends Config { + + public String hostname; + public int connectPort = 10000; + public String key; + + public RemoteConfig(String[] args) { + super(args); + for (int i = 0; i< args.length; i++) { + if ("-h".equals(args[i])) { + hostname = args[++i]; + } else if ("-cp".equals(args[i])) { + connectPort = Integer.parseInt(args[++i]); + } else if ("-key".equals(args[i])) { + key = args[++i]; + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/App.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,46 @@ +package jungle.test.bbs; + +import javax.servlet.Servlet; + +import org.mortbay.jetty.Server; +import org.mortbay.jetty.servlet.ServletHandler; +import org.mortbay.jetty.servlet.ServletHolder; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) throws Exception + { + BulletinBoard cassaBBS = null; + if(args.length == 0){ + cassaBBS = new JungleBulletinBoard(); + }else{ + cassaBBS = new CassandraBulletinBoard("test-cluster","localhost:9160","cassaBBS2"); + } + + + String createBoardMessagePath = "/createBoardMessage"; + String createBoardPath = "/createBoard"; + String editMessagePath = "/editMessage"; + String showBoardMessagePath = "/showBoardMessage"; + + Servlet createBoardMessage = new CreateBoardMessageServlet(cassaBBS); + Servlet createBoard = new CreateBoardServlet(cassaBBS); + Servlet editBoardMessage = new EditMessageServlet(cassaBBS); + Servlet index = new ShowBoardsServlet(cassaBBS,createBoardPath,showBoardMessagePath); + Servlet board = new ShowBoardMessageServlet(cassaBBS,createBoardMessagePath,editMessagePath); + + Server serv = new Server(8080); + ServletHandler context = new ServletHandler(); + context.addServletWithMapping(new ServletHolder(createBoardMessage),createBoardMessagePath); + context.addServletWithMapping(new ServletHolder(createBoard),createBoardPath); + context.addServletWithMapping(new ServletHolder(editBoardMessage),editMessagePath); + context.addServletWithMapping(new ServletHolder(index),"/"); + context.addServletWithMapping(new ServletHolder(board),showBoardMessagePath); + serv.addHandler(context); + serv.start(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/BoardMessage.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,8 @@ +package jungle.test.bbs; + +public interface BoardMessage +{ + public String getUUID(); + public String getAuthor(); + public String getMessage(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/BulletinBoard.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,11 @@ +package jungle.test.bbs; + +public interface BulletinBoard +{ + public Iterable<String> getBoards(); + public void createBoards(String _name,String _author,String _initMessage,String _editKey); + public void createBoardMessage(String _board,String _author,String _message,String _editKey); + public void editMessage(String _board,String _uuid,String _author,String _message,String _editKey); + + public Iterable<BoardMessage> getMessages(String _boardName); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/CassandraBulletinBoard.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,192 @@ +package jungle.test.bbs; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import org.apache.cassandra.locator.SimpleStrategy; + +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.cassandra.service.CassandraHost; +import me.prettyprint.cassandra.service.template.SuperCfResult; +import me.prettyprint.cassandra.service.template.SuperCfUpdater; +import me.prettyprint.cassandra.service.template.ThriftSuperCfTemplate; +import me.prettyprint.cassandra.utils.TimeUUIDUtils; +import me.prettyprint.hector.api.Cluster; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.beans.HSuperColumn; +import me.prettyprint.hector.api.beans.OrderedSuperRows; +import me.prettyprint.hector.api.beans.SuperRow; +import me.prettyprint.hector.api.beans.SuperSlice; +import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; +import me.prettyprint.hector.api.ddl.ColumnType; +import me.prettyprint.hector.api.ddl.ComparatorType; +import me.prettyprint.hector.api.ddl.KeyspaceDefinition; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.query.QueryResult; +import me.prettyprint.hector.api.query.RangeSuperSlicesQuery; +import me.prettyprint.hector.api.query.SuperSliceQuery; + +public class CassandraBulletinBoard implements BulletinBoard +{ + private final String address; + private final String clusterName; + private final Cluster cluster; + private final String keyspace; + + private static final String COLUMN_FAMILY_BOARD = "boards"; + + public CassandraBulletinBoard(String _clusterName,String _address,String _keyspaceName) + { + address = _address; + clusterName = _clusterName; + keyspace = _keyspaceName; + cluster = HFactory.getOrCreateCluster(clusterName,address); + + initialize(); + } + + private void initialize() + { + if(cluster.describeKeyspace(keyspace) == null){ + KeyspaceDefinition keyspaceDefinition = HFactory.createKeyspaceDefinition(keyspace, + SimpleStrategy.class.getName(),1,Collections.<ColumnFamilyDefinition> emptyList()); + cluster.addKeyspace(keyspaceDefinition,false); + ColumnFamilyDefinition columnFamilyDefinition = HFactory.createColumnFamilyDefinition(keyspace,COLUMN_FAMILY_BOARD,ComparatorType.UUIDTYPE); + columnFamilyDefinition.setColumnType(ColumnType.SUPER); + cluster.addColumnFamily(columnFamilyDefinition); + } + } + + public Iterable<String> getBoards() + { + Keyspace ksp = HFactory.createKeyspace(keyspace, cluster); + RangeSuperSlicesQuery<String,UUID,String,String> query = HFactory.createRangeSuperSlicesQuery(ksp,StringSerializer.get(), + UUIDSerializer.get(),StringSerializer.get(),StringSerializer.get()); + query.setColumnFamily(COLUMN_FAMILY_BOARD).setKeys(null,null).setRange(null,null,false,0); + + QueryResult<OrderedSuperRows<String,UUID,String,String>> result = query.execute(); + OrderedSuperRows<String,UUID,String,String> rows = result.get(); + List<SuperRow<String,UUID,String,String>> list = rows.getList(); + + IterableConverter.Converter<String,SuperRow<String,UUID,String,String>> converter + = new IterableConverter.Converter<String, SuperRow<String,UUID,String,String>>(){ + public String conv(SuperRow<String, UUID, String, String> _b) { + return _b.getKey(); + } + }; + + return new IterableConverter<String,SuperRow<String,UUID,String,String>>(list,converter); + } + + private static final String COLUMN_MESSAGE_AUTHOR = "author"; + private static final String COLUMN_MESSAGE_BODY = "message"; + private static final String COLUMN_MESSAGE_EDIT_KEY = "edit"; + + public void createBoardMessage(UUID _time,String _name,String _author,String _message,String _editKey) + { + Keyspace ksp = HFactory.createKeyspace(keyspace,cluster); + ThriftSuperCfTemplate<String,UUID,String> template = + new ThriftSuperCfTemplate<String,UUID,String>(ksp,COLUMN_FAMILY_BOARD,StringSerializer.get(), + UUIDSerializer.get(),StringSerializer.get()); + + SuperCfUpdater<String,UUID,String> updater = template.createUpdater(_name,_time); + updater.setString(COLUMN_MESSAGE_AUTHOR,_author); + updater.setString(COLUMN_MESSAGE_BODY,_message); + updater.setString(COLUMN_MESSAGE_EDIT_KEY,_editKey); + + template.update(updater); + } + + public void createBoards(String _name,String _author,String _initMessage,String _editKey) + { + UUID time = TimeUUIDUtils.getTimeUUID(0); + createBoardMessage(time,_name,_author,_initMessage,_editKey); + } + + public Iterable<BoardMessage> getMessages(String _boardName) + { + Keyspace ksp = HFactory.createKeyspace(keyspace,cluster); + SuperSliceQuery<String, UUID, String, String> query = HFactory.createSuperSliceQuery(ksp, StringSerializer.get(), UUIDSerializer.get(), StringSerializer.get(), StringSerializer.get()); + + UUID start = TimeUUIDUtils.getTimeUUID(0); + query.setKey(_boardName).setColumnFamily(COLUMN_FAMILY_BOARD).setRange(start,null,false,100); + + QueryResult<SuperSlice<UUID, String, String>> result = query.execute(); + SuperSlice<UUID,String,String> ss = result.get(); + List<HSuperColumn<UUID,String,String>> list = ss.getSuperColumns(); + + IterableConverter.Converter<BoardMessage,HSuperColumn<UUID,String,String>> converter = + new IterableConverter.Converter<BoardMessage,HSuperColumn<UUID,String,String>>(){ + public BoardMessage conv(HSuperColumn<UUID, String, String> _b){ + UUID uuid = _b.getName(); + String author = _b.getSubColumnByName(COLUMN_MESSAGE_AUTHOR).getValue(); + String message = _b.getSubColumnByName(COLUMN_MESSAGE_BODY).getValue(); + BoardMessageImpl bm = new BoardMessageImpl(author,message,uuid.toString()); + return bm; + } + }; + + + return new IterableConverter<BoardMessage,HSuperColumn<UUID,String,String>>(list,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; + } + } + + public void createBoardMessage(String _board, String _author, String _message,String _editKey) + { + UUID time = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); + createBoardMessage(time,_board,_author,_message,_editKey); + } + + public void editMessage(String _board,String _uuid,String _author,String _message,String _editKey) + { + Keyspace ksp = HFactory.createKeyspace(keyspace, cluster); + UUID time = UUID.fromString(_uuid); + ThriftSuperCfTemplate<String,UUID,String> template = + new ThriftSuperCfTemplate<String,UUID,String>(ksp,COLUMN_FAMILY_BOARD,StringSerializer.get(), + UUIDSerializer.get(),StringSerializer.get()); + + SuperCfResult<String,UUID,String> result = template.querySuperColumn(_board,time); + String editKey = result.getString(COLUMN_MESSAGE_EDIT_KEY); + if(!editKey.equals(editKey)){ + return; + } + + SuperCfUpdater<String, UUID, String> updater = template.createUpdater(_board,time); + updater.setString(COLUMN_MESSAGE_AUTHOR,_author); + updater.setString(COLUMN_MESSAGE_BODY,_message); + updater.setString(COLUMN_MESSAGE_EDIT_KEY,_editKey); + + template.update(updater); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/CreateBoardMessageServlet.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,40 @@ +package jungle.test.bbs; + +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class CreateBoardMessageServlet extends HttpServlet +{ + private final BulletinBoard bbs; + private static final String PARAM_BOARD_NAME = "bname"; + private static final String PARAM_BOARD_AUTHOR = "author"; + private static final String PARAM_BOARD_MESSAGE= "msg"; + private static final String PARAM_BOARD_EDITKEY = "key"; + + private static final long serialVersionUID = 1L; + + public CreateBoardMessageServlet(BulletinBoard _bbs) + { + bbs = _bbs; + } + + @Override + public void doPost(HttpServletRequest _req,HttpServletResponse _res) + { + String boardName = _req.getParameter(PARAM_BOARD_NAME); + String author = _req.getParameter(PARAM_BOARD_AUTHOR); + String msg = _req.getParameter(PARAM_BOARD_MESSAGE); + String key = _req.getParameter(PARAM_BOARD_EDITKEY); + + try{ + bbs.createBoardMessage(boardName,author,msg,key); + PrintWriter pw = _res.getWriter(); + pw.write("successfully written"); + }catch(Exception _e){ + _res.setStatus(500); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/CreateBoardServlet.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,39 @@ +package jungle.test.bbs; + +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class CreateBoardServlet extends HttpServlet +{ + private final BulletinBoard bbs; + private static final String PARAM_BOARD_NAME = "bname"; + private static final String PARAM_BOARD_AUTHOR = "author"; + private static final String PARAM_BOARD_INITMESSAGE= "msg"; + private static final String PARAM_BOARD_EDITKEY = "key"; + + private static final long serialVersionUID = 1L; + + public CreateBoardServlet(BulletinBoard _bbs) + { + bbs = _bbs; + } + + public void doPost(HttpServletRequest _req,HttpServletResponse _res) + { + String boardName = _req.getParameter(PARAM_BOARD_NAME); + String author = _req.getParameter(PARAM_BOARD_AUTHOR); + String msg = _req.getParameter(PARAM_BOARD_INITMESSAGE); + String key = _req.getParameter(PARAM_BOARD_EDITKEY); + + try{ + bbs.createBoards(boardName,author,msg,key); + PrintWriter pw = _res.getWriter(); + pw.write("successfully written"); + }catch(Exception _e){ + _res.setStatus(500); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/EditMessageServlet.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,64 @@ +package jungle.test.bbs; + +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class EditMessageServlet extends HttpServlet +{ + private final BulletinBoard bbs; + private static final String PARAM_BOARD_NAME = "bname"; + private static final String PARAM_BOARD_MSGID = "uuid"; + private static final String PARAM_BOARD_AUTHOR = "author"; + private static final String PARAM_BOARD_MESSAGE= "msg"; + private static final String PARAM_BOARD_EDITKEY = "key"; + + private static final long serialVersionUID = 1L; + + public EditMessageServlet(BulletinBoard _bbs) + { + bbs = _bbs; + } + + public void doGet(HttpServletRequest _req,HttpServletResponse _res) + { + String bname = _req.getParameter(PARAM_BOARD_NAME); + String uuid = _req.getParameter(PARAM_BOARD_MSGID); + + + try{ + PrintWriter pw = _res.getWriter(); + pw.write("<html><body><h1>edit message</h1>"); + pw.write("<form method='POST'\n"); + pw.write("<p>Author : <input type='text' name='author'/>" + + "<input type='hidden' name='key' value='"+uuid+"'/>" + + "<input type='hidden' name='bname' value='"+bname+"'</p>\n"); + pw.write("<p>Message<br/> <input type='textarea' name='msg'/> </p>\n"); + pw.write("<p><input type='submit' value='submit'/></p>\n"); + pw.write("</body></html>"); + pw.flush(); + }catch(Exception _e){ + _res.setStatus(500); + } + } + + public void doPost(HttpServletRequest _req,HttpServletResponse _res) + { + String boardName = _req.getParameter(PARAM_BOARD_NAME); + String author = _req.getParameter(PARAM_BOARD_AUTHOR); + String msg = _req.getParameter(PARAM_BOARD_MESSAGE); + String key = _req.getParameter(PARAM_BOARD_EDITKEY); + String uuid = _req.getParameter(PARAM_BOARD_MSGID); + + try{ + bbs.editMessage(boardName, uuid, author, msg, key); + PrintWriter pw = _res.getWriter(); + pw.write("successfully written"); + pw.flush(); + }catch(Exception _e){ + _res.setStatus(500); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/HectorSample.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,70 @@ +package jungle.test.bbs; + +import java.util.List; +import java.util.UUID; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.cassandra.service.template.SuperCfUpdater; +import me.prettyprint.cassandra.service.template.ThriftSuperCfTemplate; +import me.prettyprint.cassandra.utils.TimeUUIDUtils; +import me.prettyprint.hector.api.*; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; +import me.prettyprint.hector.api.beans.OrderedSuperRows; +import me.prettyprint.hector.api.beans.SuperRow; +import me.prettyprint.hector.api.beans.SuperSlice; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.query.QueryResult; +import me.prettyprint.hector.api.query.RangeSuperSlicesQuery; +import me.prettyprint.hector.api.query.SuperSliceQuery; + +public class HectorSample +{ + public static void main(String _args[]) + { + Cluster myCluster = HFactory.getOrCreateCluster("test-cluster","localhost:9160"); + + Keyspace ksp = HFactory.createKeyspace("cassaBBS",myCluster); + /* + ColumnFamilyDefinition newCF = HFactory.createColumnFamilyDefinition("DEMO","ccc",ComparatorType.UUIDTYPE); + newCF.setColumnType(ColumnType.SUPER); + myCluster.addColumnFamily(newCF); + */ + + ThriftSuperCfTemplate<String,UUID,String> template = + new ThriftSuperCfTemplate<String,UUID,String>(ksp,"boards",StringSerializer.get(), + UUIDSerializer.get(),StringSerializer.get()); + UUID time = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); + SuperCfUpdater<String,UUID,String> updater = template.createUpdater("board1",TimeUUIDUtils.getTimeUUID(0)); + updater.setString("name","peter"); + updater.setString("message",time.toString()); + + template.update(updater); + + UUID start = TimeUUIDUtils.getTimeUUID(0); + + SuperSliceQuery<String, UUID, String, String> sq = HFactory.createSuperSliceQuery(ksp, StringSerializer.get(), UUIDSerializer.get(), StringSerializer.get(), StringSerializer.get()); + sq.setKey("board1").setColumnFamily("ccc").setRange(start,null,false,100); + + QueryResult<SuperSlice<UUID,String,String>> results = sq.execute(); + + SuperSlice<UUID,String,String> ss = results.get(); + List<HSuperColumn<UUID,String,String>> list = ss.getSuperColumns(); + for(HSuperColumn<UUID,String,String> sc : list){ + HColumn<String,String> sub = sc.getSubColumnByName("name"); + System.out.println(sub.getValue()); + sub = sc.getSubColumnByName("message"); + System.out.println(sub.getValue()); + } + + RangeSuperSlicesQuery<String,UUID,String,String> rsq = HFactory.createRangeSuperSlicesQuery(ksp,StringSerializer.get(), + UUIDSerializer.get(),StringSerializer.get(),StringSerializer.get()); + rsq.setKeys(null,null).setRange(null,null,false,0).setColumnFamily("ccc"); + + QueryResult<OrderedSuperRows<String,UUID,String,String>> rsqResult = rsq.execute(); + OrderedSuperRows<String, UUID, String, String> rows = rsqResult.get(); + for(SuperRow<String, UUID, String, String> row : rows.getList()){ + System.out.println(row.getKey()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/IterableConverter.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,52 @@ +package jungle.test.bbs; + +import java.util.Iterator; + +public class IterableConverter<A,B> implements Iterable<A> +{ + private final Iterable<B> iterable; + private final Converter<A,B> converter; + + public IterableConverter(Iterable<B> _iterable,Converter<A,B> _converter) + { + iterable = _iterable; + converter = _converter; + } + + public Iterator<A> iterator() + { + return new IteratorConverter<A,B>(iterable.iterator(),converter); + } + + private static final class IteratorConverter<A,B> implements Iterator<A> + { + private final Iterator<B> iterator; + private final Converter<A,B> converter; + + public IteratorConverter(Iterator<B> _iterator,Converter<A,B> _converter) + { + iterator = _iterator; + converter = _converter; + } + + public boolean hasNext() + { + return iterator.hasNext(); + } + + public A next() + { + return converter.conv(iterator.next()); + } + + public void remove() + { + iterator.remove(); + } + } + + public static interface Converter<A,B> + { + public A conv(B _b); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/JungleBulletinBoard.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,209 @@ +package jungle.test.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(); + } + + JungleTreeEditor editor; + do{ + Node node = tree.getRootNode(); + int size = node.getChildren().size(); + DefaultNodePath path = new DefaultNodePath(); + + 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(); + }while(editor.success().isA()); + } + + public void editMessage(String _board,String _uuid,final String _author,final String _message,final String _editKey) + { + JungleTreeEditor editor = null; + do{ + DefaultNodePath path = new DefaultNodePath(); + path = path.add(Integer.parseInt(_uuid)); + + JungleTree tree = jungle.getTreeByName(_board); + 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(); + }while(editor.success().isA()); + } + + 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; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/ShowBoardMessageServlet.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,60 @@ +package jungle.test.bbs; + +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class ShowBoardMessageServlet extends HttpServlet +{ + /** + * + */ + private static final long serialVersionUID = 1L; + private final BulletinBoard bbs; + private final String createBoardMessagePath; + private final String editMessagePath; + + private static final String PARAM_BOARD_NAME = "bname"; + + public ShowBoardMessageServlet(BulletinBoard _bbs,String _createBoardMessagePath, String _editMessagePath) + { + bbs = _bbs; + createBoardMessagePath = _createBoardMessagePath; + editMessagePath = _editMessagePath; + } + + public void doGet(HttpServletRequest _req,HttpServletResponse _res) + { + String bname = _req.getParameter(PARAM_BOARD_NAME); + + try{ + printBoard(bname,_res.getWriter()); + }catch(Exception _e){ + _res.setStatus(500); + } + + } + + private void printBoard(String _bname,PrintWriter _pw) throws Exception + { + _pw.write("<html><body>\n"); + _pw.write("<h1>"+_bname+"</h1>\n"); + + _pw.write("<form action='"+createBoardMessagePath+"' method='POST'\n"); + _pw.write("<p>Author : <input type='text' name='author'/> <input type='hidden' name='bname' value='"+_bname+"'/> EditKey : <input type='text' name='key'/></p>\n"); + _pw.write("<p>Message<br/> <input type='textarea' name='msg'/> </p>\n"); + _pw.write("<p><input type='submit' value='submit'/></p>\n"); + + for(BoardMessage msg : bbs.getMessages(_bname)){ + _pw.write("<hr/>"); + _pw.write("<p><b>"+msg.getAuthor()+"</b></p>"); + _pw.write("<p>"+msg.getMessage()+"</p>"); + _pw.write("<small><a href='"+editMessagePath+"?bname="+_bname+"&uuid="+msg.getUUID()+"'>edit</a></small>"); + } + + _pw.write("</body></html>"); + _pw.flush(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/bbs/ShowBoardsServlet.java Mon Jul 08 20:25:58 2013 +0900 @@ -0,0 +1,55 @@ +package jungle.test.bbs; + +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class ShowBoardsServlet extends HttpServlet +{ + /** + * + */ + private static final long serialVersionUID = 1L; + private final BulletinBoard bbs; + private final String createBoardPath; + private final String showBoardMessagePath; + + public ShowBoardsServlet(BulletinBoard _bbs, String _createBoardPath,String _showBoardMessagePath) + { + bbs = _bbs; + createBoardPath = _createBoardPath; + showBoardMessagePath = _showBoardMessagePath; + } + + public void doGet(HttpServletRequest _req,HttpServletResponse _res) + { + try{ + printBoard(_res.getWriter()); + }catch(Exception _e){ + _res.setStatus(500); + } + + } + + private void printBoard(PrintWriter _pw) throws Exception + { + _pw.write("<html><body>\n"); + _pw.write("<h1>BBS</h1>\n"); + _pw.write("<form action='"+createBoardPath+"' method='POST'\n"); + _pw.write("<p>Create new board.</p>"); + _pw.write("<p>BoardName : <input type='text' name='bname'/></p>\n"); + _pw.write("<p>Author : <input type='text' name='author'/> EditKey : <input type='text' name='key'/></p>\n"); + _pw.write("<p>Message<br/> <input type='textarea' name='msg'/> </p>\n"); + _pw.write("<p><input type='submit' value='submit'/></p><hr/>\n"); + + _pw.write("<h2>list of boards</h2>"); + for(String board : bbs.getBoards()){ + _pw.write("<p><a href='"+showBoardMessagePath+"?bname="+board+"'>"+board+"</a></p>"); + } + + _pw.write("</body></html>"); + _pw.flush(); + } +}