use of org.infinispan.commons.marshall.WrappedBytes in project infinispan by infinispan.
the class OffHeapSingleNodeTest method testExpiredEntryCompute.
public void testExpiredEntryCompute() throws IOException, InterruptedException {
Cache<Object, Object> cache = cache(0);
String key = "key";
cache.put(key, "value", 10, TimeUnit.MILLISECONDS);
timeService.advance(20);
DataConversion keyDataConversion = cache.getAdvancedCache().getKeyDataConversion();
WrappedBytes keyWB = (WrappedBytes) keyDataConversion.toStorage(key);
AtomicBoolean invoked = new AtomicBoolean(false);
DataContainer container = cache.getAdvancedCache().getDataContainer();
container.compute(keyWB, (k, e, f) -> {
invoked.set(true);
// Just leave it in there
return e;
});
// Should not have invoked, due to expiration
assertTrue(invoked.get());
assertNotNull(container.peek(keyWB));
// Actually reading it shouldn't return though and actually remove
assertNull(cache.get(key));
// Now that we did a get, the peek shouldn't return anything
assertNull(container.peek(keyWB));
}
use of org.infinispan.commons.marshall.WrappedBytes in project infinispan by infinispan.
the class OffHeapSizeTest method testSizeMatch.
@Test(dataProvider = "sizeMatchData")
public void testSizeMatch(int keyLength, int valueLength, long maxIdle, long lifespan, EntryVersion version) {
OffHeapMemoryAllocator allocator = TestingUtil.extractComponent(cache, OffHeapMemoryAllocator.class);
long beginningSize = allocator.getAllocatedAmount();
// We write directly to data container to avoid transformations
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
byte[] keyBytes = new byte[keyLength];
byte[] valueBytes = new byte[valueLength];
threadLocalRandom.nextBytes(keyBytes);
threadLocalRandom.nextBytes(valueBytes);
WrappedBytes key = new WrappedByteArray(keyBytes);
WrappedBytes value = new WrappedByteArray(valueBytes);
EmbeddedMetadata.Builder metadataBuilder = new EmbeddedMetadata.Builder();
if (maxIdle >= 0) {
metadataBuilder.maxIdle(maxIdle);
}
if (lifespan >= 0) {
metadataBuilder.lifespan(lifespan);
}
if (version != null) {
metadataBuilder.version(version);
}
Metadata metadata = metadataBuilder.build();
KeyValueMetadataSizeCalculator<WrappedBytes, WrappedBytes> calculator = TestingUtil.extractComponent(cache, KeyValueMetadataSizeCalculator.class);
long estimateSize = calculator.calculateSize(key, value, metadata);
cache.getAdvancedCache().getDataContainer().put(key, value, metadata);
long endingSize = allocator.getAllocatedAmount();
assertEquals(endingSize - beginningSize, estimateSize);
}
use of org.infinispan.commons.marshall.WrappedBytes in project infinispan by infinispan.
the class OffHeapConcurrentMapTest method testIterationStartedWithTwoResizes.
public void testIterationStartedWithTwoResizes() {
Set<WrappedBytes> expectedKeys = insertUpToResizeLimitation();
Set<Object> results = new HashSet<>();
Iterator<InternalCacheEntry<WrappedBytes, WrappedBytes>> iterator = map.values().iterator();
assertTrue(iterator.hasNext());
assertTrue(results.add(iterator.next().getKey()));
// Now cause a first and second
for (int i = 0; i < RESIZE_LIMITATION + 2; ++i) {
putInMap(map, valueByteArray);
}
while (iterator.hasNext()) {
assertTrue(results.add(iterator.next().getKey()));
}
for (WrappedBytes expected : expectedKeys) {
assertTrue("Results didn't contain: " + expected, results.contains(expected));
}
}
use of org.infinispan.commons.marshall.WrappedBytes in project infinispan by infinispan.
the class OffHeapConcurrentMapTest method testIterationStartedWithResize1.
public void testIterationStartedWithResize1() {
Set<WrappedBytes> expectedKeys = insertUpToResizeLimitation();
Set<WrappedBytes> results = new HashSet<>();
Iterator<InternalCacheEntry<WrappedBytes, WrappedBytes>> iterator = map.values().iterator();
assertTrue(iterator.hasNext());
assertTrue(results.add(iterator.next().getKey()));
// We can't guarantee this value will be in results as we don't know which bucket it mapped to
putInMap(map, valueByteArray);
while (iterator.hasNext()) {
assertTrue(results.add(iterator.next().getKey()));
}
for (WrappedBytes expected : expectedKeys) {
assertTrue("Results didn't contain: " + expected, results.contains(expected));
}
}
use of org.infinispan.commons.marshall.WrappedBytes in project infinispan by infinispan.
the class BaseStoreTest method testLoadAndStoreBytesValues.
public void testLoadAndStoreBytesValues() throws PersistenceException, IOException, InterruptedException {
assertIsEmpty();
SerializationContext ctx = ProtobufUtil.newSerializationContext();
SerializationContextInitializer sci = TestDataSCI.INSTANCE;
sci.registerSchema(ctx);
sci.registerMarshallers(ctx);
Marshaller userMarshaller = new ProtoStreamMarshaller(ctx);
WrappedBytes key = new WrappedByteArray(userMarshaller.objectToByteBuffer(new Key("key")));
WrappedBytes key2 = new WrappedByteArray(userMarshaller.objectToByteBuffer(new Key("key2")));
WrappedBytes value = new WrappedByteArray(userMarshaller.objectToByteBuffer(new Person()));
assertFalse(cl.contains(key));
PersistenceMarshaller persistenceMarshaller = getMarshaller();
cl.write(MarshalledEntryUtil.create(key, value, persistenceMarshaller));
assertEquals(value, cl.loadEntry(key).getValue());
MarshallableEntry entry = cl.loadEntry(key);
assertTrue("Expected an immortalEntry", entry.getMetadata() == null || entry.expiryTime() == -1 || entry.getMetadata().maxIdle() == -1);
assertContains(key, true);
assertFalse(cl.delete(key2));
assertTrue(cl.delete(key));
}
Aggregations