1
|
1 package jp.ac.u_ryukyu.ie.cr.bbs.local;
|
|
2
|
|
3 import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
|
|
4 import me.prettyprint.cassandra.serializers.StringSerializer;
|
|
5 import me.prettyprint.cassandra.serializers.UUIDSerializer;
|
|
6 import me.prettyprint.cassandra.service.template.SuperCfResult;
|
|
7 import me.prettyprint.cassandra.service.template.SuperCfUpdater;
|
|
8 import me.prettyprint.cassandra.service.template.ThriftSuperCfTemplate;
|
|
9 import me.prettyprint.cassandra.utils.TimeUUIDUtils;
|
|
10 import me.prettyprint.hector.api.Cluster;
|
|
11 import me.prettyprint.hector.api.HConsistencyLevel;
|
|
12 import me.prettyprint.hector.api.Keyspace;
|
|
13 import me.prettyprint.hector.api.beans.HSuperColumn;
|
|
14 import me.prettyprint.hector.api.beans.OrderedSuperRows;
|
|
15 import me.prettyprint.hector.api.beans.SuperRow;
|
|
16 import me.prettyprint.hector.api.beans.SuperSlice;
|
|
17 import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
|
|
18 import me.prettyprint.hector.api.ddl.ColumnType;
|
|
19 import me.prettyprint.hector.api.ddl.ComparatorType;
|
|
20 import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
|
|
21 import me.prettyprint.hector.api.factory.HFactory;
|
|
22 import me.prettyprint.hector.api.query.QueryResult;
|
|
23 import me.prettyprint.hector.api.query.RangeSuperSlicesQuery;
|
|
24 import me.prettyprint.hector.api.query.SuperSliceQuery;
|
|
25 import org.apache.cassandra.locator.SimpleStrategy;
|
|
26
|
|
27 import java.util.*;
|
|
28
|
|
29 public class CassandraBulletinBoard implements BulletinBoard
|
|
30 {
|
|
31 private final String address;
|
|
32 private final String clusterName;
|
|
33 private final Cluster cluster;
|
|
34 private final String keyspace;
|
|
35 private final ConfigurableConsistencyLevel configurableConsistencyLevel = new ConfigurableConsistencyLevel();
|
|
36
|
|
37 private static final String COLUMN_FAMILY_BOARD = "boards";
|
|
38 private final int REP_FACTOR;
|
|
39
|
|
40 public CassandraBulletinBoard(String _clusterName,String _address,String _keyspaceName, HConsistencyLevel cLevel,int rep_factor )
|
|
41 {
|
|
42 address = _address;
|
|
43 clusterName = _clusterName;
|
|
44 keyspace = _keyspaceName;
|
|
45 cluster = HFactory.getOrCreateCluster(clusterName,address);
|
|
46 Map<String, HConsistencyLevel> clmap = new HashMap<String, HConsistencyLevel>();
|
|
47 clmap.put(COLUMN_FAMILY_BOARD, cLevel);
|
|
48 configurableConsistencyLevel.setReadCfConsistencyLevels(clmap);
|
|
49 configurableConsistencyLevel.setWriteCfConsistencyLevels(clmap);
|
|
50 REP_FACTOR = rep_factor;
|
|
51 initialize();
|
|
52 }
|
|
53
|
|
54 public CassandraBulletinBoard(String _clusterName,String _address,String _keyspaceName, HConsistencyLevel cLevel)
|
|
55 {
|
|
56 this(_clusterName, _address, _keyspaceName, cLevel, 1);
|
|
57 }
|
|
58
|
|
59
|
|
60
|
|
61 private void initialize()
|
|
62 {
|
|
63 if(cluster.describeKeyspace(keyspace) == null){
|
|
64 KeyspaceDefinition keyspaceDefinition = HFactory.createKeyspaceDefinition(keyspace,
|
|
65 SimpleStrategy.class.getName(),REP_FACTOR, Collections.<ColumnFamilyDefinition> emptyList());
|
|
66 cluster.addKeyspace(keyspaceDefinition,false);
|
|
67 ColumnFamilyDefinition columnFamilyDefinition = HFactory.createColumnFamilyDefinition(keyspace,COLUMN_FAMILY_BOARD,ComparatorType.UUIDTYPE);
|
|
68 columnFamilyDefinition.setColumnType(ColumnType.SUPER);
|
|
69 cluster.addColumnFamily(columnFamilyDefinition);
|
|
70 }
|
|
71 }
|
|
72
|
|
73 public Iterable<String> getBoards()
|
|
74 {
|
|
75 Keyspace ksp = HFactory.createKeyspace(keyspace, cluster, configurableConsistencyLevel);
|
|
76 RangeSuperSlicesQuery<String,UUID,String,String> query = HFactory.createRangeSuperSlicesQuery(ksp,StringSerializer.get(),
|
|
77 UUIDSerializer.get(),StringSerializer.get(),StringSerializer.get());
|
|
78 query.setColumnFamily(COLUMN_FAMILY_BOARD).setKeys(null,null).setRange(null,null,false,0);
|
|
79
|
|
80 QueryResult<OrderedSuperRows<String,UUID,String,String>> result = query.execute();
|
|
81 OrderedSuperRows<String,UUID,String,String> rows = result.get();
|
|
82 List<SuperRow<String,UUID,String,String>> list = rows.getList();
|
|
83
|
|
84 IterableConverter.Converter<String,SuperRow<String,UUID,String,String>> converter
|
|
85 = new IterableConverter.Converter<String, SuperRow<String,UUID,String,String>>(){
|
|
86 public String conv(SuperRow<String, UUID, String, String> _b) {
|
|
87 return _b.getKey();
|
|
88 }
|
|
89 };
|
|
90
|
|
91 return new IterableConverter<String,SuperRow<String,UUID,String,String>>(list,converter);
|
|
92 }
|
|
93
|
|
94 private static final String COLUMN_MESSAGE_AUTHOR = "author";
|
|
95 private static final String COLUMN_MESSAGE_BODY = "message";
|
|
96 private static final String COLUMN_MESSAGE_EDIT_KEY = "edit";
|
|
97
|
|
98 public void createBoardMessage(UUID _time,String _name,String _author,String _message,String _editKey)
|
|
99 {
|
|
100 Keyspace ksp = HFactory.createKeyspace(keyspace, cluster, configurableConsistencyLevel);
|
|
101 ThriftSuperCfTemplate<String,UUID,String> template =
|
|
102 new ThriftSuperCfTemplate<String,UUID,String>(ksp,COLUMN_FAMILY_BOARD,StringSerializer.get(),
|
|
103 UUIDSerializer.get(),StringSerializer.get());
|
|
104
|
|
105 SuperCfUpdater<String,UUID,String> updater = template.createUpdater(_name,_time);
|
|
106 updater.setString(COLUMN_MESSAGE_AUTHOR,_author);
|
|
107 updater.setString(COLUMN_MESSAGE_BODY,_message);
|
|
108 updater.setString(COLUMN_MESSAGE_EDIT_KEY,_editKey);
|
|
109
|
|
110 template.update(updater);
|
|
111 }
|
|
112
|
|
113 public void createBoards(String _name,String _author,String _initMessage,String _editKey)
|
|
114 {
|
|
115 UUID time = TimeUUIDUtils.getTimeUUID(0);
|
|
116 createBoardMessage(time,_name,_author,_initMessage,_editKey);
|
|
117 }
|
|
118
|
|
119 public Iterable<BoardMessage> getMessages(String _boardName)
|
|
120 {
|
|
121 Keyspace ksp = HFactory.createKeyspace(keyspace, cluster, configurableConsistencyLevel);
|
|
122 SuperSliceQuery<String, UUID, String, String> query = HFactory.createSuperSliceQuery(ksp, StringSerializer.get(), UUIDSerializer.get(), StringSerializer.get(), StringSerializer.get());
|
|
123
|
|
124 UUID start = TimeUUIDUtils.getTimeUUID(0);
|
|
125 query.setKey(_boardName).setColumnFamily(COLUMN_FAMILY_BOARD).setRange(start,null,false,100);
|
|
126
|
|
127 QueryResult<SuperSlice<UUID, String, String>> result = query.execute();
|
|
128 SuperSlice<UUID,String,String> ss = result.get();
|
|
129 List<HSuperColumn<UUID,String,String>> list = ss.getSuperColumns();
|
|
130
|
|
131 IterableConverter.Converter<BoardMessage,HSuperColumn<UUID,String,String>> converter =
|
|
132 new IterableConverter.Converter<BoardMessage,HSuperColumn<UUID,String,String>>(){
|
|
133 public BoardMessage conv(HSuperColumn<UUID, String, String> _b){
|
|
134 UUID uuid = _b.getName();
|
|
135 String author = _b.getSubColumnByName(COLUMN_MESSAGE_AUTHOR).getValue();
|
|
136 String message = _b.getSubColumnByName(COLUMN_MESSAGE_BODY).getValue();
|
|
137 BoardMessageImpl bm = new BoardMessageImpl(author,message,uuid.toString());
|
|
138 return bm;
|
|
139 }
|
|
140 };
|
|
141
|
|
142
|
|
143 return new IterableConverter<BoardMessage,HSuperColumn<UUID,String,String>>(list,converter);
|
|
144 }
|
|
145
|
|
146 private static class BoardMessageImpl implements BoardMessage
|
|
147 {
|
|
148 private final String author;
|
|
149 private final String message;
|
|
150 private final String uuid;
|
|
151
|
|
152 public BoardMessageImpl(String _author,String _message,String _uuid)
|
|
153 {
|
|
154 author = _author;
|
|
155 message = _message;
|
|
156 uuid = _uuid;
|
|
157 }
|
|
158
|
|
159 public String getAuthor()
|
|
160 {
|
|
161 return author;
|
|
162 }
|
|
163
|
|
164 public String getMessage()
|
|
165 {
|
|
166 return message;
|
|
167 }
|
|
168
|
|
169 public String getUUID()
|
|
170 {
|
|
171 return uuid;
|
|
172 }
|
|
173 }
|
|
174
|
|
175 public void createBoardMessage(String _board, String _author, String _message,String _editKey)
|
|
176 {
|
|
177 UUID time = TimeUUIDUtils.getUniqueTimeUUIDinMillis();
|
|
178 createBoardMessage(time,_board,_author,_message,_editKey);
|
|
179 }
|
|
180
|
|
181 public void editMessage(String _board,String _uuid,String _author,String _message,String _editKey)
|
|
182 {
|
|
183 Keyspace ksp = HFactory.createKeyspace(keyspace, cluster, configurableConsistencyLevel);
|
|
184 UUID time = UUID.fromString(_uuid);
|
|
185 ThriftSuperCfTemplate<String,UUID,String> template =
|
|
186 new ThriftSuperCfTemplate<String,UUID,String>(ksp,COLUMN_FAMILY_BOARD,StringSerializer.get(),
|
|
187 UUIDSerializer.get(),StringSerializer.get());
|
|
188
|
|
189 SuperCfResult<String,UUID,String> result = template.querySuperColumn(_board,time);
|
|
190 String editKey = result.getString(COLUMN_MESSAGE_EDIT_KEY);
|
|
191 if(!editKey.equals(editKey)){
|
|
192 return;
|
|
193 }
|
|
194
|
|
195 SuperCfUpdater<String, UUID, String> updater = template.createUpdater(_board,time);
|
|
196 updater.setString(COLUMN_MESSAGE_AUTHOR,_author);
|
|
197 updater.setString(COLUMN_MESSAGE_BODY,_message);
|
|
198 updater.setString(COLUMN_MESSAGE_EDIT_KEY,_editKey);
|
|
199
|
|
200 template.update(updater);
|
|
201 }
|
|
202
|
|
203 public String sanitize( String str ) {
|
|
204 if(str==null) {
|
|
205 return str;
|
|
206 }
|
|
207 str = str.replaceAll("&" , "&" );
|
|
208 str = str.replaceAll("<" , "<" );
|
|
209 str = str.replaceAll(">" , ">" );
|
|
210 str = str.replaceAll("\"", """);
|
|
211 str = str.replaceAll("'" , "'" );
|
|
212 return str;
|
|
213 }
|
|
214
|
|
215 public GetAttributeImp getAttribute(String _bname, String _nodeNum, String revisionStr) {
|
|
216 return null; // 未実装 使うときに実装する
|
|
217 }
|
|
218
|
|
219 }
|