Mercurial > hg > Members > nobuyasu > jungle-network
comparison src/main/java/jungle/app/bbs/CassandraBulletinBoard.java @ 105:f9e29a52efd3
Move some files
author | one |
---|---|
date | Tue, 26 Nov 2013 06:43:10 +0900 |
parents | src/jungle/app/bbs/CassandraBulletinBoard.java@29127ac788a6 |
children |
comparison
equal
deleted
inserted
replaced
104:03bf62bb699e | 105:f9e29a52efd3 |
---|---|
1 package jungle.app.bbs; | |
2 | |
3 import java.util.Collections; | |
4 import java.util.List; | |
5 import java.util.UUID; | |
6 | |
7 import org.apache.cassandra.locator.SimpleStrategy; | |
8 | |
9 import me.prettyprint.cassandra.serializers.StringSerializer; | |
10 import me.prettyprint.cassandra.serializers.UUIDSerializer; | |
11 import me.prettyprint.cassandra.service.CassandraHost; | |
12 import me.prettyprint.cassandra.service.template.SuperCfResult; | |
13 import me.prettyprint.cassandra.service.template.SuperCfUpdater; | |
14 import me.prettyprint.cassandra.service.template.ThriftSuperCfTemplate; | |
15 import me.prettyprint.cassandra.utils.TimeUUIDUtils; | |
16 import me.prettyprint.hector.api.Cluster; | |
17 import me.prettyprint.hector.api.Keyspace; | |
18 import me.prettyprint.hector.api.beans.HSuperColumn; | |
19 import me.prettyprint.hector.api.beans.OrderedSuperRows; | |
20 import me.prettyprint.hector.api.beans.SuperRow; | |
21 import me.prettyprint.hector.api.beans.SuperSlice; | |
22 import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; | |
23 import me.prettyprint.hector.api.ddl.ColumnType; | |
24 import me.prettyprint.hector.api.ddl.ComparatorType; | |
25 import me.prettyprint.hector.api.ddl.KeyspaceDefinition; | |
26 import me.prettyprint.hector.api.factory.HFactory; | |
27 import me.prettyprint.hector.api.query.QueryResult; | |
28 import me.prettyprint.hector.api.query.RangeSuperSlicesQuery; | |
29 import me.prettyprint.hector.api.query.SuperSliceQuery; | |
30 | |
31 public class CassandraBulletinBoard implements BulletinBoard | |
32 { | |
33 private final String address; | |
34 private final String clusterName; | |
35 private final Cluster cluster; | |
36 private final String keyspace; | |
37 | |
38 private static final String COLUMN_FAMILY_BOARD = "boards"; | |
39 | |
40 public CassandraBulletinBoard(String _clusterName,String _address,String _keyspaceName) | |
41 { | |
42 address = _address; | |
43 clusterName = _clusterName; | |
44 keyspace = _keyspaceName; | |
45 cluster = HFactory.getOrCreateCluster(clusterName,address); | |
46 | |
47 initialize(); | |
48 } | |
49 | |
50 private void initialize() | |
51 { | |
52 if(cluster.describeKeyspace(keyspace) == null){ | |
53 KeyspaceDefinition keyspaceDefinition = HFactory.createKeyspaceDefinition(keyspace, | |
54 SimpleStrategy.class.getName(),1,Collections.<ColumnFamilyDefinition> emptyList()); | |
55 cluster.addKeyspace(keyspaceDefinition,false); | |
56 ColumnFamilyDefinition columnFamilyDefinition = HFactory.createColumnFamilyDefinition(keyspace,COLUMN_FAMILY_BOARD,ComparatorType.UUIDTYPE); | |
57 columnFamilyDefinition.setColumnType(ColumnType.SUPER); | |
58 cluster.addColumnFamily(columnFamilyDefinition); | |
59 } | |
60 } | |
61 | |
62 public Iterable<String> getBoards() | |
63 { | |
64 Keyspace ksp = HFactory.createKeyspace(keyspace, cluster); | |
65 RangeSuperSlicesQuery<String,UUID,String,String> query = HFactory.createRangeSuperSlicesQuery(ksp,StringSerializer.get(), | |
66 UUIDSerializer.get(),StringSerializer.get(),StringSerializer.get()); | |
67 query.setColumnFamily(COLUMN_FAMILY_BOARD).setKeys(null,null).setRange(null,null,false,0); | |
68 | |
69 QueryResult<OrderedSuperRows<String,UUID,String,String>> result = query.execute(); | |
70 OrderedSuperRows<String,UUID,String,String> rows = result.get(); | |
71 List<SuperRow<String,UUID,String,String>> list = rows.getList(); | |
72 | |
73 IterableConverter.Converter<String,SuperRow<String,UUID,String,String>> converter | |
74 = new IterableConverter.Converter<String, SuperRow<String,UUID,String,String>>(){ | |
75 public String conv(SuperRow<String, UUID, String, String> _b) { | |
76 return _b.getKey(); | |
77 } | |
78 }; | |
79 | |
80 return new IterableConverter<String,SuperRow<String,UUID,String,String>>(list,converter); | |
81 } | |
82 | |
83 private static final String COLUMN_MESSAGE_AUTHOR = "author"; | |
84 private static final String COLUMN_MESSAGE_BODY = "message"; | |
85 private static final String COLUMN_MESSAGE_EDIT_KEY = "edit"; | |
86 | |
87 public void createBoardMessage(UUID _time,String _name,String _author,String _message,String _editKey) | |
88 { | |
89 Keyspace ksp = HFactory.createKeyspace(keyspace,cluster); | |
90 ThriftSuperCfTemplate<String,UUID,String> template = | |
91 new ThriftSuperCfTemplate<String,UUID,String>(ksp,COLUMN_FAMILY_BOARD,StringSerializer.get(), | |
92 UUIDSerializer.get(),StringSerializer.get()); | |
93 | |
94 SuperCfUpdater<String,UUID,String> updater = template.createUpdater(_name,_time); | |
95 updater.setString(COLUMN_MESSAGE_AUTHOR,_author); | |
96 updater.setString(COLUMN_MESSAGE_BODY,_message); | |
97 updater.setString(COLUMN_MESSAGE_EDIT_KEY,_editKey); | |
98 | |
99 template.update(updater); | |
100 } | |
101 | |
102 public void createBoards(String _name,String _author,String _initMessage,String _editKey) | |
103 { | |
104 UUID time = TimeUUIDUtils.getTimeUUID(0); | |
105 createBoardMessage(time,_name,_author,_initMessage,_editKey); | |
106 } | |
107 | |
108 public Iterable<BoardMessage> getMessages(String _boardName) | |
109 { | |
110 Keyspace ksp = HFactory.createKeyspace(keyspace,cluster); | |
111 SuperSliceQuery<String, UUID, String, String> query = HFactory.createSuperSliceQuery(ksp, StringSerializer.get(), UUIDSerializer.get(), StringSerializer.get(), StringSerializer.get()); | |
112 | |
113 UUID start = TimeUUIDUtils.getTimeUUID(0); | |
114 query.setKey(_boardName).setColumnFamily(COLUMN_FAMILY_BOARD).setRange(start,null,false,100); | |
115 | |
116 QueryResult<SuperSlice<UUID, String, String>> result = query.execute(); | |
117 SuperSlice<UUID,String,String> ss = result.get(); | |
118 List<HSuperColumn<UUID,String,String>> list = ss.getSuperColumns(); | |
119 | |
120 IterableConverter.Converter<BoardMessage,HSuperColumn<UUID,String,String>> converter = | |
121 new IterableConverter.Converter<BoardMessage,HSuperColumn<UUID,String,String>>(){ | |
122 public BoardMessage conv(HSuperColumn<UUID, String, String> _b){ | |
123 UUID uuid = _b.getName(); | |
124 String author = _b.getSubColumnByName(COLUMN_MESSAGE_AUTHOR).getValue(); | |
125 String message = _b.getSubColumnByName(COLUMN_MESSAGE_BODY).getValue(); | |
126 BoardMessageImpl bm = new BoardMessageImpl(author,message,uuid.toString()); | |
127 return bm; | |
128 } | |
129 }; | |
130 | |
131 | |
132 return new IterableConverter<BoardMessage,HSuperColumn<UUID,String,String>>(list,converter); | |
133 } | |
134 | |
135 private static class BoardMessageImpl implements BoardMessage | |
136 { | |
137 private final String author; | |
138 private final String message; | |
139 private final String uuid; | |
140 | |
141 public BoardMessageImpl(String _author,String _message,String _uuid) | |
142 { | |
143 author = _author; | |
144 message = _message; | |
145 uuid = _uuid; | |
146 } | |
147 | |
148 public String getAuthor() | |
149 { | |
150 return author; | |
151 } | |
152 | |
153 public String getMessage() | |
154 { | |
155 return message; | |
156 } | |
157 | |
158 public String getUUID() | |
159 { | |
160 return uuid; | |
161 } | |
162 } | |
163 | |
164 public void createBoardMessage(String _board, String _author, String _message,String _editKey) | |
165 { | |
166 UUID time = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); | |
167 createBoardMessage(time,_board,_author,_message,_editKey); | |
168 } | |
169 | |
170 public void editMessage(String _board,String _uuid,String _author,String _message,String _editKey) | |
171 { | |
172 Keyspace ksp = HFactory.createKeyspace(keyspace, cluster); | |
173 UUID time = UUID.fromString(_uuid); | |
174 ThriftSuperCfTemplate<String,UUID,String> template = | |
175 new ThriftSuperCfTemplate<String,UUID,String>(ksp,COLUMN_FAMILY_BOARD,StringSerializer.get(), | |
176 UUIDSerializer.get(),StringSerializer.get()); | |
177 | |
178 SuperCfResult<String,UUID,String> result = template.querySuperColumn(_board,time); | |
179 String editKey = result.getString(COLUMN_MESSAGE_EDIT_KEY); | |
180 if(!editKey.equals(editKey)){ | |
181 return; | |
182 } | |
183 | |
184 SuperCfUpdater<String, UUID, String> updater = template.createUpdater(_board,time); | |
185 updater.setString(COLUMN_MESSAGE_AUTHOR,_author); | |
186 updater.setString(COLUMN_MESSAGE_BODY,_message); | |
187 updater.setString(COLUMN_MESSAGE_EDIT_KEY,_editKey); | |
188 | |
189 template.update(updater); | |
190 } | |
191 | |
192 } |