use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class VariableLongTest method byteOrderPreservingPositiveBackward.
@Test
public void byteOrderPreservingPositiveBackward() {
long[] scalingFactors = { Long.MAX_VALUE, 1000, 1000000000l };
for (int t = 0; t < 10000000; t++) {
StaticBuffer[] b = new StaticBuffer[2];
long[] l = new long[2];
for (int i = 0; i < 2; i++) {
l[i] = randomPosLong(scalingFactors[random.nextInt(scalingFactors.length)]);
WriteBuffer out = new WriteByteBuffer(11);
VariableLong.writePositiveBackward(out, l[i]);
b[i] = out.getStaticBuffer();
ReadBuffer res = b[i].asReadBuffer();
res.movePositionTo(res.length());
assertEquals(l[i], VariableLong.readPositiveBackward(res));
}
// System.out.println(l[0] + " vs " + l[1]);
assertEquals(Math.signum(Long.compare(l[0], l[1])), Math.signum(b[0].compareTo(b[1])), 0.01);
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class LockCleanerRunnableTest method testDeleteSingleLock.
/**
* Simplest case test of the lock cleaner.
*/
@Test
public void testDeleteSingleLock() throws BackendException {
Instant now = Instant.ofEpochMilli(1L);
Entry expiredLockCol = StaticArrayEntry.of(codec.toLockCol(now, defaultLockRid, TimestampProviders.MILLI), BufferUtil.getIntBuffer(0));
EntryList expiredSingleton = StaticArrayEntryList.of(expiredLockCol);
now = now.plusMillis(1);
del = new StandardLockCleanerRunnable(store, kc, tx, codec, now, TimestampProviders.MILLI);
expect(store.getSlice(eq(ksq), eq(tx))).andReturn(expiredSingleton);
store.mutate(eq(key), eq(ImmutableList.<Entry>of()), eq(ImmutableList.<StaticBuffer>of(expiredLockCol.getColumn())), anyObject(StoreTransaction.class));
ctrl.replay();
del.run();
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class BufferUtilTest method testCompareRandom.
@Test
public void testCompareRandom() {
int trials = 100000;
for (int t = 0; t < trials; t++) {
long val1 = Math.abs(random.nextLong());
long val2 = Math.abs(random.nextLong());
StaticBuffer b1 = BufferUtil.getLongBuffer(val1);
StaticBuffer b2 = BufferUtil.getLongBuffer(val2);
//Compare
assertEquals(val1 + " : " + val2, Math.signum(Longs.compare(val1, val2)), Math.signum(b1.compareTo(b2)), 0.01);
assertEquals(Math.signum(Longs.compare(val2, val1)), Math.signum(b2.compareTo(b1)), 0.01);
assertEquals(0, b1.compareTo(b1));
ByteBuffer bb1 = of(val1);
ByteBuffer bb2 = of(val2);
assertEquals(val1 + " : " + val2, Math.signum(Longs.compare(val1, val2)), Math.signum(ByteBufferUtil.compare(bb1, bb2)), 0.01);
assertEquals(Math.signum(Longs.compare(val2, val1)), Math.signum(ByteBufferUtil.compare(bb2, bb1)), 0.01);
assertEquals(0, ByteBufferUtil.compare(bb1, bb1));
//Mixed Equals
if (0.5 < Math.random())
val2 = val1;
ByteBuffer bb = of(val2);
assertEquals(val1 == val2, BufferUtil.equals(b1, bb));
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class IDManagementTest method edgeTypeIDTest.
@Test
public void edgeTypeIDTest() {
int partitionBits = 16;
IDManager eid = new IDManager(partitionBits);
int trails = 1000000;
assertEquals(eid.getPartitionBound(), (1l << partitionBits));
Serializer serializer = new StandardSerializer();
for (int t = 0; t < trails; t++) {
long count = RandomGenerator.randomLong(1, eid.getSchemaCountBound());
long id;
IDHandler.DirectionID dirID;
RelationCategory type;
if (Math.random() < 0.5) {
id = eid.getSchemaId(IDManager.VertexIDType.UserEdgeLabel, count);
assertTrue(eid.isEdgeLabelId(id));
assertFalse(eid.isSystemRelationTypeId(id));
type = RelationCategory.EDGE;
if (Math.random() < 0.5)
dirID = IDHandler.DirectionID.EDGE_IN_DIR;
else
dirID = IDHandler.DirectionID.EDGE_OUT_DIR;
} else {
type = RelationCategory.PROPERTY;
id = eid.getSchemaId(IDManager.VertexIDType.UserPropertyKey, count);
assertTrue(eid.isPropertyKeyId(id));
assertFalse(eid.isSystemRelationTypeId(id));
dirID = IDHandler.DirectionID.PROPERTY_DIR;
}
assertTrue(eid.isRelationTypeId(id));
StaticBuffer b = IDHandler.getRelationType(id, dirID, false);
// System.out.println(dirID);
// System.out.println(getBinary(id));
// System.out.println(getBuffer(b.asReadBuffer()));
ReadBuffer rb = b.asReadBuffer();
IDHandler.RelationTypeParse parse = IDHandler.readRelationType(rb);
assertEquals(id, parse.typeId);
assertEquals(dirID, parse.dirID);
assertFalse(rb.hasRemaining());
//Inline edge type
WriteBuffer wb = new WriteByteBuffer(9);
IDHandler.writeInlineRelationType(wb, id);
long newId = IDHandler.readInlineRelationType(wb.getStaticBuffer().asReadBuffer());
assertEquals(id, newId);
//Compare to Kryo
DataOutput out = serializer.getDataOutput(10);
IDHandler.writeRelationType(out, id, dirID, false);
assertEquals(b, out.getStaticBuffer());
//Make sure the bounds are right
StaticBuffer[] bounds = IDHandler.getBounds(type, false);
assertTrue(bounds[0].compareTo(b) < 0);
assertTrue(bounds[1].compareTo(b) > 0);
bounds = IDHandler.getBounds(RelationCategory.RELATION, false);
assertTrue(bounds[0].compareTo(b) < 0);
assertTrue(bounds[1].compareTo(b) > 0);
}
}
use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.
the class HBaseStoreManager method normalizeKeyBounds.
/**
* Given a map produced by {@link HTable#getRegionLocations()}, transform
* each key from an {@link HRegionInfo} to a {@link KeyRange} expressing the
* region's start and end key bounds using Titan-partitioning-friendly
* conventions (start inclusive, end exclusive, zero bytes appended where
* necessary to make all keys at least 4 bytes long).
* <p/>
* This method iterates over the entries in its map parameter and performs
* the following conditional conversions on its keys. "Require" below means
* either a {@link Preconditions} invocation or an assertion. HRegionInfo
* sometimes returns start and end keys of zero length; this method replaces
* zero length keys with null before doing any of the checks described
* below. The parameter map and the values it contains are only read and
* never modified.
*
* <ul>
* <li>If an entry's HRegionInfo has null start and end keys, then first
* require that the parameter map is a singleton, and then return a
* single-entry map whose {@code KeyRange} has start and end buffers that
* are both four bytes of zeros.</li>
* <li>If the entry has a null end key (but non-null start key), put an
* equivalent entry in the result map with a start key identical to the
* input, except that zeros are appended to values less than 4 bytes long,
* and an end key that is four bytes of zeros.
* <li>If the entry has a null start key (but non-null end key), put an
* equivalent entry in the result map where the start key is four bytes of
* zeros, and the end key has zeros appended, if necessary, to make it at
* least 4 bytes long, after which one is added to the padded value in
* unsigned 32-bit arithmetic with overflow allowed.</li>
* <li>Any entry which matches none of the above criteria results in an
* equivalent entry in the returned map, except that zeros are appended to
* both keys to make each at least 4 bytes long, and the end key is then
* incremented as described in the last bullet point.</li>
* </ul>
*
* After iterating over the parameter map, this method checks that it either
* saw no entries with null keys, one entry with a null start key and a
* different entry with a null end key, or one entry with both start and end
* keys null. If any null keys are observed besides these three cases, the
* method will die with a precondition failure.
*
* @param raw
* A map of HRegionInfo and ServerName from HBase
* @return Titan-friendly expression of each region's rowkey boundaries
*/
private Map<KeyRange, ServerName> normalizeKeyBounds(NavigableMap<HRegionInfo, ServerName> raw) {
Map.Entry<HRegionInfo, ServerName> nullStart = null;
Map.Entry<HRegionInfo, ServerName> nullEnd = null;
ImmutableMap.Builder<KeyRange, ServerName> b = ImmutableMap.builder();
for (Map.Entry<HRegionInfo, ServerName> e : raw.entrySet()) {
HRegionInfo regionInfo = e.getKey();
byte[] startKey = regionInfo.getStartKey();
byte[] endKey = regionInfo.getEndKey();
if (0 == startKey.length) {
startKey = null;
logger.trace("Converted zero-length HBase startKey byte array to null");
}
if (0 == endKey.length) {
endKey = null;
logger.trace("Converted zero-length HBase endKey byte array to null");
}
if (null == startKey && null == endKey) {
Preconditions.checkState(1 == raw.size());
logger.debug("HBase table {} has a single region {}", tableName, regionInfo);
// Choose arbitrary shared value = startKey = endKey
return b.put(new KeyRange(FOUR_ZERO_BYTES, FOUR_ZERO_BYTES), e.getValue()).build();
} else if (null == startKey) {
logger.debug("Found HRegionInfo with null startKey on server {}: {}", e.getValue(), regionInfo);
Preconditions.checkState(null == nullStart);
nullStart = e;
// I thought endBuf would be inclusive from the HBase javadoc, but in practice it is exclusive
StaticBuffer endBuf = StaticArrayBuffer.of(zeroExtend(endKey));
// Replace null start key with zeroes
b.put(new KeyRange(FOUR_ZERO_BYTES, endBuf), e.getValue());
} else if (null == endKey) {
logger.debug("Found HRegionInfo with null endKey on server {}: {}", e.getValue(), regionInfo);
Preconditions.checkState(null == nullEnd);
nullEnd = e;
// Replace null end key with zeroes
b.put(new KeyRange(StaticArrayBuffer.of(zeroExtend(startKey)), FOUR_ZERO_BYTES), e.getValue());
} else {
Preconditions.checkState(null != startKey);
Preconditions.checkState(null != endKey);
// Convert HBase's inclusive end keys into exclusive Titan end keys
StaticBuffer startBuf = StaticArrayBuffer.of(zeroExtend(startKey));
StaticBuffer endBuf = StaticArrayBuffer.of(zeroExtend(endKey));
KeyRange kr = new KeyRange(startBuf, endBuf);
b.put(kr, e.getValue());
logger.debug("Found HRegionInfo with non-null end and start keys on server {}: {}", e.getValue(), regionInfo);
}
}
// Require either no null key bounds or a pair of them
Preconditions.checkState(!(null == nullStart ^ null == nullEnd));
// Check that every key in the result is at least 4 bytes long
Map<KeyRange, ServerName> result = b.build();
for (KeyRange kr : result.keySet()) {
Preconditions.checkState(4 <= kr.getStart().length());
Preconditions.checkState(4 <= kr.getEnd().length());
}
return result;
}
Aggregations