use of com.cinchapi.concourse.server.model.Value in project concourse by cinchapi.
the class RangeLockServiceTest method testWriteIsRangeBlockedIfReadingEqualValue.
@Test
public void testWriteIsRangeBlockedIfReadingEqualValue() throws InterruptedException {
final Text key = Variables.register("key", TestData.getText());
final Value value = Variables.register("value", TestData.getValue());
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch finishLatch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
rangeLockService.getReadLock(key, Operator.EQUALS, value).lock();
startLatch.countDown();
try {
finishLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
rangeLockService.getReadLock(key, Operator.EQUALS, value).unlock();
}
});
t.start();
startLatch.await();
Assert.assertTrue(rangeLockService.isRangeBlocked(LockType.WRITE, RangeToken.forWriting(key, value)));
finishLatch.countDown();
}
use of com.cinchapi.concourse.server.model.Value in project concourse by cinchapi.
the class RangeTokensTest method testConvertEqualRangeToken.
@Test
public void testConvertEqualRangeToken() {
Text key = TestData.getText();
Operator operator = Operator.EQUALS;
Value value = TestData.getValue();
RangeToken token = RangeToken.forReading(key, operator, value);
Range<Value> range = Iterables.getOnlyElement(RangeTokens.convertToRange(token));
Assert.assertEquals(range, Range.singleton(value));
}
use of com.cinchapi.concourse.server.model.Value in project concourse by cinchapi.
the class ByteableCollectionsTest method testNewVsDeprecatedPerformance.
@Test
public void testNewVsDeprecatedPerformance() throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
List<Value> values = Lists.newArrayList();
while (baos.size() < Math.pow(2, 24)) {
Value value = TestData.getValue();
baos.write(ByteBuffers.getByteArray(value.getBytes()));
values.add(value);
}
ByteBuffer bytes = ByteableCollections.toByteBuffer(values);
Path file = Paths.get(TestData.getTemporaryTestFile());
String $file = file.toString();
FileSystem.writeBytes(bytes, $file);
int bufferSize = 8192;
Benchmark benchmark1 = new Benchmark(TimeUnit.MILLISECONDS) {
@Override
public void action() {
CloseableIterator<ByteBuffer> it = ByteableCollections.stream(file, bufferSize);
while (it.hasNext()) {
Value.fromByteBuffer(it.next());
}
it.closeQuietly();
}
};
Benchmark benchmark2 = new Benchmark(TimeUnit.MILLISECONDS) {
@SuppressWarnings("deprecation")
@Override
public void action() {
Iterator<ByteBuffer> it = ByteableCollections.streamingIterator($file, bufferSize);
while (it.hasNext()) {
Value.fromByteBuffer(it.next());
}
}
};
AtomicDouble avg1 = new AtomicDouble();
AtomicDouble avg2 = new AtomicDouble();
CountUpLatch latch = new CountUpLatch();
Thread t1 = new Thread(() -> {
double avg = benchmark1.average(10);
System.out.println("New: " + avg);
avg1.set(avg);
latch.countUp();
});
Thread t2 = new Thread(() -> {
double avg = benchmark2.average(10);
System.out.println("Deprecated: " + avg);
avg2.set(avg);
latch.countUp();
});
List<Thread> threads = Lists.newArrayList(t1, t2);
Collections.shuffle(threads);
for (Thread thread : threads) {
thread.start();
}
latch.await(2);
Assert.assertTrue(avg1.get() < avg2.get());
}
use of com.cinchapi.concourse.server.model.Value in project concourse by cinchapi.
the class RangeToken method serialize.
/**
* Return the ByteBuffer with the serialized form appropriate for a
* RangeToken that describes {@code key} {@code operator} {@code values}.
*
* @param key
* @param operator
* @param values
* @return the ByteBuffer
*/
private static ByteBuffer serialize(Text key, Operator operator, Value... values) {
int size = 1 + 4 + key.size() + (4 * values.length);
for (Value value : values) {
size += value.size();
}
ByteBuffer bytes = ByteBuffer.allocate(size);
bytes.put(operator != null ? (byte) operator.ordinal() : NULL_OPERATOR);
bytes.putInt(key.size());
key.copyTo(bytes);
for (Value value : values) {
bytes.putInt(value.size());
value.copyTo(bytes);
}
bytes.rewind();
return bytes;
}
use of com.cinchapi.concourse.server.model.Value in project concourse by cinchapi.
the class RangeToken method intersects.
/**
* Return {@code true} if this RangeToken intersects the {@code other}
* RangeToken. This RangeToken is considered to intersect another one if the
* left point of this RangeToken is less than or equal to the right point of
* the other one and the right point of this RangeToken is greater than or
* equal to the left point of the other one.
*
* @param other
* @return {@code true} if this RangeToken intersects the other
*/
public boolean intersects(RangeToken other) {
Value value = other.values[0];
Value myValue = this.values[0];
Operator myOperator = this.operator == null ? Operator.EQUALS : this.operator;
Operator operator = other.operator == null ? Operator.EQUALS : other.operator;
switch(myOperator) {
case EQUALS:
switch(operator) {
case EQUALS:
return myValue.compareTo(value) == 0;
case NOT_EQUALS:
return myValue.compareTo(value) != 0;
case GREATER_THAN:
return myValue.compareTo(value) > 0;
case GREATER_THAN_OR_EQUALS:
return myValue.compareTo(value) >= 0;
case LESS_THAN:
return myValue.compareTo(value) < 0;
case LESS_THAN_OR_EQUALS:
return myValue.compareTo(value) <= 0;
case BETWEEN:
return myValue.compareTo(value) >= 0 && myValue.compareTo(other.values[1]) < 0;
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case NOT_EQUALS:
switch(operator) {
case EQUALS:
return myValue.compareTo(value) != 0;
case NOT_EQUALS:
case GREATER_THAN:
case GREATER_THAN_OR_EQUALS:
case LESS_THAN:
case LESS_THAN_OR_EQUALS:
return true;
case BETWEEN:
return myValue.compareTo(value) != 0 || myValue.compareTo(other.values[1]) != 0;
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case GREATER_THAN:
switch(operator) {
case EQUALS:
case LESS_THAN:
case LESS_THAN_OR_EQUALS:
return value.compareTo(myValue) > 0;
case NOT_EQUALS:
case GREATER_THAN:
case GREATER_THAN_OR_EQUALS:
return true;
case BETWEEN:
return other.values[1].compareTo(myValue) > 0;
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case GREATER_THAN_OR_EQUALS:
switch(operator) {
case EQUALS:
case LESS_THAN_OR_EQUALS:
return value.compareTo(myValue) >= 0;
case LESS_THAN:
return value.compareTo(myValue) > 0;
case NOT_EQUALS:
case GREATER_THAN:
case GREATER_THAN_OR_EQUALS:
return true;
case BETWEEN:
// end of range
return other.values[1].compareTo(myValue) > 0;
// included for BETWEEN
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case LESS_THAN:
switch(operator) {
case EQUALS:
case GREATER_THAN:
case GREATER_THAN_OR_EQUALS:
return value.compareTo(myValue) < 0;
case NOT_EQUALS:
case LESS_THAN:
case LESS_THAN_OR_EQUALS:
return true;
case BETWEEN:
return value.compareTo(myValue) < 0;
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case LESS_THAN_OR_EQUALS:
switch(operator) {
case EQUALS:
case GREATER_THAN_OR_EQUALS:
return value.compareTo(myValue) <= 0;
case LESS_THAN:
case LESS_THAN_OR_EQUALS:
case NOT_EQUALS:
return true;
case GREATER_THAN:
return value.compareTo(myValue) < 0;
case BETWEEN:
return value.compareTo(myValue) <= 0;
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case BETWEEN:
Value myOtherValue = this.values[1];
switch(operator) {
case EQUALS:
return value.compareTo(myValue) >= 0 && value.compareTo(myOtherValue) < 0;
case NOT_EQUALS:
return value.compareTo(myValue) != 0 || value.compareTo(myOtherValue) != 0;
case GREATER_THAN:
case GREATER_THAN_OR_EQUALS:
return value.compareTo(myOtherValue) < 0;
case LESS_THAN:
return value.compareTo(myValue) > 0;
case LESS_THAN_OR_EQUALS:
return value.compareTo(myValue) >= 0;
case BETWEEN:
return myOtherValue.compareTo(value) >= 0 && myValue.compareTo(other.values[1]) <= 0;
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
case REGEX:
case NOT_REGEX:
return true;
default:
throw new UnsupportedOperationException();
}
}
Aggregations