@Test public void testMultiLock() { MultiRowUniquenessConstraint unique = new MultiRowUniquenessConstraint(keyspace) .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withTtl(60) .withLockId("abc") .withColumnPrefix("prefix_") .withRow(UNIQUE_CF, "testMultiLock_A") .withRow(UNIQUE_CF, "testMultiLock_B"); ColumnPrefixUniquenessConstraint<String> singleUnique = new ColumnPrefixUniquenessConstraint<String>(keyspace, UNIQUE_CF, "testMultiLock_A") .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withPrefix("prefix_"); try { unique.acquire(); String uniqueColumn = singleUnique.readUniqueColumn(); Assert.assertEquals("abc", uniqueColumn); LOG.info("UniqueColumn: " + uniqueColumn); } catch (Exception e) { Assert.fail(e.getMessage()); } MultiRowUniquenessConstraint unique2 = new MultiRowUniquenessConstraint(keyspace) .withTtl(60) .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withColumnPrefix("prefix_") .withRow(UNIQUE_CF, "testMultiLock_B"); try { unique2.acquire(); Assert.fail(); } catch (Exception e) { LOG.info(e.getMessage()); } try { Assert.assertEquals("abc", singleUnique.readUniqueColumn()); unique.release(); } catch (Exception e) { LOG.error(e.getMessage()); Assert.fail(); } try { unique2.acquire(); } catch (Exception e) { LOG.error(e.getMessage()); Assert.fail(); } try { unique2.release(); } catch (Exception e) { LOG.error(e.getMessage()); Assert.fail(); } }
@Test public void testAcquireAndMutate() throws Exception { final String row = "testAcquireAndMutate"; final String dataColumn = "data"; final String value = "test"; ColumnPrefixUniquenessConstraint<String> unique = new ColumnPrefixUniquenessConstraint<String>(keyspace, UNIQUE_CF, row) .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withUniqueId("def"); try { unique.acquireAndApplyMutation( new Function<MutationBatch, Boolean>() { @Override public Boolean apply(MutationBatch m) { m.withRow(UNIQUE_CF, row).putColumn(dataColumn, value, null); return true; } }); String column = unique.readUniqueColumn(); Assert.assertNotNull(column); } catch (Exception e) { e.printStackTrace(); LOG.error("", e); Assert.fail(); } finally { } ColumnList<String> columns = keyspace.prepareQuery(UNIQUE_CF).getKey(row).execute().getResult(); Assert.assertEquals(2, columns.size()); Assert.assertEquals(value, columns.getStringValue(dataColumn, null)); unique.release(); columns = keyspace.prepareQuery(UNIQUE_CF).getKey(row).execute().getResult(); Assert.assertEquals(1, columns.size()); Assert.assertEquals(value, columns.getStringValue(dataColumn, null)); }
@Test public void testPrefixUniquenessConstraintWithColumn() throws Exception { ColumnPrefixUniquenessConstraint<String> unique = new ColumnPrefixUniquenessConstraint<String>( keyspace, UNIQUE_CF, "testPrefixUniquenessConstraintWithColumn") .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withUniqueId("abc"); ColumnPrefixUniquenessConstraint<String> unique2 = new ColumnPrefixUniquenessConstraint<String>( keyspace, UNIQUE_CF, "testPrefixUniquenessConstraintWithColumn") .withConsistencyLevel(ConsistencyLevel.CL_ONE) .withUniqueId("def"); try { unique.acquire(); String column = unique.readUniqueColumn(); LOG.info("Unique Column: " + column); Assert.assertEquals("abc", column); try { unique2.acquire(); Assert.fail(); } catch (Exception e) { } column = unique.readUniqueColumn(); LOG.info("Unique Column: " + column); Assert.assertEquals("abc", column); } catch (Exception e) { Assert.fail(e.getMessage()); LOG.error(e.getMessage()); } finally { unique.release(); } }