|
| 1 | +package com.alipay.oceanbase.rpc; |
| 2 | + |
| 3 | +import com.alipay.oceanbase.rpc.exception.ObTableException; |
| 4 | +import com.alipay.oceanbase.rpc.mutation.result.MutationResult; |
| 5 | +import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes; |
| 6 | +import com.alipay.oceanbase.rpc.table.ObTable; |
| 7 | +import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil; |
| 8 | +import com.google.protobuf.MapEntry; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.nio.ByteBuffer; |
| 15 | +import java.sql.Connection; |
| 16 | +import java.sql.SQLException; |
| 17 | +import java.sql.Statement; |
| 18 | +import java.sql.Timestamp; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal; |
| 22 | +import static com.alipay.oceanbase.rpc.mutation.MutationFactory.row; |
| 23 | + |
| 24 | +public class ObTableFullTextIndexTest { |
| 25 | + ObTableClient client; |
| 26 | + String tableName = "tbl1"; |
| 27 | + String ttlTableName = "ttl_tbl1"; |
| 28 | + String createTableSQL = "CREATE TABLE IF NOT EXISTS tbl1(id INT, c2 INT, txt text, PRIMARY KEY (id), FULLTEXT INDEX full_idx1_tbl1(txt));"; |
| 29 | + String createTTLTableSQL = "CREATE TABLE IF NOT EXISTS ttl_tbl1(id INT, c2 INT, txt text, expired_ts timestamp(6), PRIMARY KEY (id), FULLTEXT INDEX full_idx1_tbl1(txt)) TTL(expired_ts + INTERVAL 10 SECOND);;"; |
| 30 | + String truncateTableSQL = "truncate table tbl1;"; |
| 31 | + String truncateTTLTableSQL = "truncate table ttl_tbl1;"; |
| 32 | + String dropTableSQL = "drop table tbl1"; |
| 33 | + String idCol = "id"; |
| 34 | + String c2Col = "c2"; |
| 35 | + String txtCol = "txt"; |
| 36 | + String expireTsCol = "expired_ts"; |
| 37 | + @Before |
| 38 | + public void setup() throws Exception { |
| 39 | + executeSQL(createTableSQL); |
| 40 | + executeSQL(createTTLTableSQL); |
| 41 | + final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient(); |
| 42 | + obTableClient.init(); |
| 43 | + this.client = obTableClient; |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + public void teardown() throws Exception { |
| 48 | +// executeSQL(dropTableSQL); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testInsert() throws Exception { |
| 53 | + try{ |
| 54 | + MutationResult res = client.insert(tableName).setRowKey(colVal(idCol, 1)) |
| 55 | + .addMutateRow(row(colVal(txtCol, "OceanBase Database is a native, " + |
| 56 | + "enterprise-level distributed database developed independently by the OceanBase team"))) |
| 57 | + .execute(); |
| 58 | + Assert.assertEquals(1, res.getAffectedRows()); |
| 59 | + } catch (Exception e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testGet() throws Exception { |
| 66 | + try{ |
| 67 | + MutationResult insRes = client.insert(tableName).setRowKey(colVal(idCol, 3)) |
| 68 | + .addMutateRow(row(colVal(txtCol, "OceanBase Database is a native, " + |
| 69 | + "enterprise-level distributed database developed independently by the OceanBase team"))) |
| 70 | + .execute(); |
| 71 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 72 | + |
| 73 | + Map<String, Object> res = client.get(tableName, new Object[] { 3 }, null); |
| 74 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 75 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 76 | + } |
| 77 | + } catch (Exception e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testDel() throws Exception { |
| 84 | + try{ |
| 85 | + MutationResult insRes = client.insert(tableName).setRowKey(colVal(idCol, 4)) |
| 86 | + .addMutateRow(row(colVal(txtCol, "aaa asdjl asdjlakjsdl hello select new fine"))) |
| 87 | + .execute(); |
| 88 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 89 | + // get |
| 90 | + Map<String, Object> res = client.get(tableName, new Object[] { 1 }, null); |
| 91 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 92 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 93 | + } |
| 94 | + // del |
| 95 | + MutationResult delRes = client.delete(tableName).setRowKey(colVal(idCol, 1)).execute(); |
| 96 | + Assert.assertEquals(1, delRes.getAffectedRows()); |
| 97 | + } catch (Exception e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testUpd() throws Exception { |
| 104 | + try{ |
| 105 | + MutationResult insRes = client.insert(tableName).setRowKey(colVal(idCol, 5)) |
| 106 | + .addMutateRow(row(colVal(txtCol, "aaa asdjl asdjlakjsdl hello select new fine"))) |
| 107 | + .execute(); |
| 108 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 109 | + // get |
| 110 | + Map<String, Object> res = client.get(tableName, new Object[] { 5 }, null); |
| 111 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 112 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 113 | + } |
| 114 | + |
| 115 | + // update with fulltext columns |
| 116 | + System.out.println("update with fulltext column"); |
| 117 | + MutationResult updRes = client.update(tableName).setRowKey(colVal(idCol, 5)) |
| 118 | + .addMutateRow(row(colVal(txtCol, "The sun was setting on the horizon, casting a warm golden glow over the tranquil ocean. "))) |
| 119 | + .execute(); |
| 120 | + Assert.assertEquals(1, updRes.getAffectedRows()); |
| 121 | + // get again |
| 122 | + res = client.get(tableName, new Object[] { 5 }, null); |
| 123 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 124 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 125 | + } |
| 126 | + |
| 127 | + // update with non-fulltext columns |
| 128 | + System.out.println("update with non-fulltext column"); |
| 129 | + updRes = client.update(tableName).setRowKey(colVal(idCol, 5)) |
| 130 | + .addMutateRow(row(colVal(c2Col, 10))) |
| 131 | + .execute(); |
| 132 | + Assert.assertEquals(1, updRes.getAffectedRows()); |
| 133 | + // get again |
| 134 | + res = client.get(tableName, new Object[] { 5 }, null); |
| 135 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 136 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 137 | + } |
| 138 | + |
| 139 | + // update all columns |
| 140 | + System.out.println("update all column"); |
| 141 | + updRes = client.update(tableName).setRowKey(colVal(idCol, 5)) |
| 142 | + .addMutateRow(row(colVal(c2Col, 100), |
| 143 | + colVal(txtCol, "As the day came to a close, the peaceful scene served as a reminder of the beauty and serenity that nature has to offer."))) |
| 144 | + .execute(); |
| 145 | + Assert.assertEquals(1, updRes.getAffectedRows()); |
| 146 | + // get again |
| 147 | + res = client.get(tableName, new Object[] { 5 }, null); |
| 148 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 149 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 150 | + } |
| 151 | + } catch (Exception e) { |
| 152 | + e.printStackTrace(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testInsOrUpd() throws Exception { |
| 158 | + try { |
| 159 | + // insertup-insert |
| 160 | + MutationResult insRes = client.insertOrUpdate(tableName).setRowKey(colVal(idCol, 6)) |
| 161 | + .addMutateRow(row(colVal(txtCol, "The sun rose over the horizon, casting a warm glow across the meadow. "))) |
| 162 | + .execute(); |
| 163 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 164 | + // get |
| 165 | + Map<String, Object> res = client.get(tableName, new Object[] { 6 }, null); |
| 166 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 167 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 168 | + } |
| 169 | + |
| 170 | + // insertup-update with non fulltext column |
| 171 | + MutationResult updRes = client.insertOrUpdate(tableName).setRowKey(colVal(idCol, 6)) |
| 172 | + .addMutateRow(row(colVal(c2Col, 100))) |
| 173 | + .execute(); |
| 174 | + Assert.assertEquals(1, updRes.getAffectedRows()); |
| 175 | + // get |
| 176 | + res = client.get(tableName, new Object[] { 6 }, null); |
| 177 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 178 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 179 | + } |
| 180 | + // insertup-update with fulltext column |
| 181 | + updRes = client.insertOrUpdate(tableName).setRowKey(colVal(idCol, 6)) |
| 182 | + .addMutateRow(row(colVal(txtCol, " The birds chirped happily as they flew from tree to tree"))) |
| 183 | + .execute(); |
| 184 | + Assert.assertEquals(1, updRes.getAffectedRows()); |
| 185 | + // get |
| 186 | + res = client.get(tableName, new Object[] { 6 }, null); |
| 187 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 188 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 189 | + } |
| 190 | + |
| 191 | + // insertup-update with all column |
| 192 | + updRes = client.insertOrUpdate(tableName).setRowKey(colVal(idCol, 6)) |
| 193 | + .addMutateRow(row(colVal(c2Col, 106), |
| 194 | + colVal(txtCol, " The birds chirped happily as they flew from tree to tree"))) |
| 195 | + .execute(); |
| 196 | + Assert.assertEquals(1, updRes.getAffectedRows()); |
| 197 | + // get |
| 198 | + res = client.get(tableName, new Object[] { 6 }, null); |
| 199 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 200 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 201 | + } |
| 202 | + |
| 203 | + } catch (Exception e) { |
| 204 | + e.printStackTrace(); |
| 205 | + } finally { |
| 206 | + executeSQL(truncateTableSQL); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testReplace() throws Exception { |
| 212 | + try { |
| 213 | + int id = 7; |
| 214 | + // replace-insert |
| 215 | + MutationResult insRes = client.replace(tableName).setRowKey(colVal(idCol, id)) |
| 216 | + .addMutateRow(row(colVal(txtCol, "The sun rose over the horizon, casting a warm glow across the meadow. "))) |
| 217 | + .execute(); |
| 218 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 219 | + // get |
| 220 | + Map<String, Object> res = client.get(tableName, new Object[] { id }, null); |
| 221 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 222 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 223 | + } |
| 224 | + |
| 225 | + // replace-conflict with non fulltext column |
| 226 | + MutationResult updRes = client.replace(tableName).setRowKey(colVal(idCol, id)) |
| 227 | + .addMutateRow(row(colVal(c2Col, 100))) |
| 228 | + .execute(); |
| 229 | + Assert.assertEquals(2, updRes.getAffectedRows()); |
| 230 | + // get |
| 231 | + res = client.get(tableName, new Object[] { id }, null); |
| 232 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 233 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 234 | + } |
| 235 | + // replace-conflict with fulltext column |
| 236 | + updRes = client.replace(tableName).setRowKey(colVal(idCol, id)) |
| 237 | + .addMutateRow(row(colVal(txtCol, " The birds chirped happily as they flew from tree to tree"))) |
| 238 | + .execute(); |
| 239 | + Assert.assertEquals(2, updRes.getAffectedRows()); |
| 240 | + // get |
| 241 | + res = client.get(tableName, new Object[] { id }, null); |
| 242 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 243 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 244 | + } |
| 245 | + |
| 246 | + // replace-conflict with all column |
| 247 | + updRes = client.replace(tableName).setRowKey(colVal(idCol, id)) |
| 248 | + .addMutateRow(row(colVal(c2Col, 100+id), |
| 249 | + colVal(txtCol, " The birds chirped happily as they flew from tree to tree"))) |
| 250 | + .execute(); |
| 251 | + Assert.assertEquals(2, updRes.getAffectedRows()); |
| 252 | + // get |
| 253 | + res = client.get(tableName, new Object[] { id }, null); |
| 254 | + for (Map.Entry<String, Object> entry: res.entrySet()) { |
| 255 | + System.out.println("key: "+ entry.getKey()+" value: "+ entry.getValue()); |
| 256 | + } |
| 257 | + |
| 258 | + } catch (Exception e) { |
| 259 | + e.printStackTrace(); |
| 260 | + } finally { |
| 261 | + executeSQL(truncateTableSQL); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + public void testTTLInsert() throws Exception { |
| 267 | + try { |
| 268 | + // 写入新行 |
| 269 | + int id = 8; |
| 270 | + Timestamp curTs = new Timestamp(System.currentTimeMillis()); |
| 271 | + Timestamp expireTs = new Timestamp(System.currentTimeMillis() - 1000000); |
| 272 | + MutationResult insRes = client.insert(ttlTableName).setRowKey(colVal(idCol, id + 1)) |
| 273 | + .addMutateRow(row(colVal(expireTsCol, expireTs), |
| 274 | + colVal(txtCol, "The sun rose over the horizon, casting a warm glow across the meadow. "))) |
| 275 | + .execute(); |
| 276 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 277 | + |
| 278 | + insRes = client.insert(ttlTableName).setRowKey(colVal(idCol, id + 2)) |
| 279 | + .addMutateRow(row(colVal(expireTsCol, curTs), |
| 280 | + colVal(txtCol, "The birds chirped happily as they flew from tree to tree"))) |
| 281 | + .execute(); |
| 282 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 283 | + |
| 284 | + // 过期,删除旧行,写入新行 |
| 285 | + insRes = client.insert(ttlTableName).setRowKey(colVal(idCol, id + 1)) |
| 286 | + .addMutateRow(row(colVal(expireTsCol, curTs), |
| 287 | + colVal(txtCol, "Two roads diverged in a wood"))) |
| 288 | + .execute(); |
| 289 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 290 | + |
| 291 | + // get |
| 292 | + Map<String, Object> res = client.get(ttlTableName, new Object[]{id + 1}, null); |
| 293 | + for (Map.Entry<String, Object> entry : res.entrySet()) { |
| 294 | + System.out.println("key: " + entry.getKey() + " value: " + entry.getValue()); |
| 295 | + } |
| 296 | + |
| 297 | + // 未过期 |
| 298 | + // - insert操作 |
| 299 | + try { |
| 300 | + client.insert(ttlTableName).setRowKey(colVal(idCol, id + 2)) |
| 301 | + .addMutateRow(row(colVal(expireTsCol, curTs), |
| 302 | + colVal(txtCol, "I took the one less traveled by"))) |
| 303 | + .execute(); |
| 304 | + } catch (ObTableException e) { |
| 305 | + Assert.assertEquals(ResultCodes.OB_ERR_PRIMARY_KEY_DUPLICATE.errorCode, e.getErrorCode()); |
| 306 | + } |
| 307 | + // - insertup操作 |
| 308 | + insRes = client.insertOrUpdate(ttlTableName).setRowKey(colVal(idCol, id + 2)) |
| 309 | + .addMutateRow(row(colVal(expireTsCol, curTs), |
| 310 | + colVal(txtCol, "I took the one less traveled by"))) |
| 311 | + .execute(); |
| 312 | + Assert.assertEquals(1, insRes.getAffectedRows()); |
| 313 | + |
| 314 | + // get |
| 315 | + res = client.get(ttlTableName, new Object[]{id + 2}, null); |
| 316 | + for (Map.Entry<String, Object> entry : res.entrySet()) { |
| 317 | + System.out.println("key: " + entry.getKey() + " value: " + entry.getValue()); |
| 318 | + } |
| 319 | + } catch(Exception e) { |
| 320 | + e.printStackTrace(); |
| 321 | + } finally { |
| 322 | + // executeSQL(truncateTTLTableSQL); |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + private void executeSQL(String createSQL) throws SQLException { |
| 327 | + Connection connection = ObTableClientTestUtil.getConnection(); |
| 328 | + Statement statement = connection.createStatement(); |
| 329 | + statement.execute(createSQL); |
| 330 | + } |
| 331 | +} |
0 commit comments