Search in sources :

Example 1 with WrappedBytes

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));
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataContainer(org.infinispan.container.DataContainer) WrappedBytes(org.infinispan.commons.marshall.WrappedBytes)

Example 2 with WrappedBytes

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);
}
Also used : WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) WrappedBytes(org.infinispan.commons.marshall.WrappedBytes) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) Metadata(org.infinispan.metadata.Metadata) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) SingleCacheManagerTest(org.infinispan.test.SingleCacheManagerTest) Test(org.testng.annotations.Test)

Example 3 with WrappedBytes

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));
    }
}
Also used : WrappedBytes(org.infinispan.commons.marshall.WrappedBytes) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) HashSet(java.util.HashSet)

Example 4 with WrappedBytes

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));
    }
}
Also used : WrappedBytes(org.infinispan.commons.marshall.WrappedBytes) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) HashSet(java.util.HashSet)

Example 5 with WrappedBytes

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));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) TestObjectStreamMarshaller(org.infinispan.marshall.TestObjectStreamMarshaller) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) ProtoStreamMarshaller(org.infinispan.commons.marshall.ProtoStreamMarshaller) Marshaller(org.infinispan.commons.marshall.Marshaller) WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) WrappedBytes(org.infinispan.commons.marshall.WrappedBytes) ProtoStreamMarshaller(org.infinispan.commons.marshall.ProtoStreamMarshaller) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) SerializationContextInitializer(org.infinispan.protostream.SerializationContextInitializer) Person(org.infinispan.test.data.Person) Key(org.infinispan.test.data.Key)

Aggregations

WrappedBytes (org.infinispan.commons.marshall.WrappedBytes)18 WrappedByteArray (org.infinispan.commons.marshall.WrappedByteArray)6 StampedLock (java.util.concurrent.locks.StampedLock)5 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)3 EmbeddedMetadata (org.infinispan.metadata.EmbeddedMetadata)3 Metadata (org.infinispan.metadata.Metadata)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 CacheException (org.infinispan.commons.CacheException)2 Marshaller (org.infinispan.commons.marshall.Marshaller)2 ProtoStreamMarshaller (org.infinispan.commons.marshall.ProtoStreamMarshaller)2 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)2 EntryVersion (org.infinispan.container.versioning.EntryVersion)2 DataConversion (org.infinispan.encoding.DataConversion)2 TestObjectStreamMarshaller (org.infinispan.marshall.TestObjectStreamMarshaller)2 PersistenceMarshaller (org.infinispan.marshall.persistence.PersistenceMarshaller)2 PrivateMetadata (org.infinispan.metadata.impl.PrivateMetadata)2 SerializationContext (org.infinispan.protostream.SerializationContext)2 SerializationContextInitializer (org.infinispan.protostream.SerializationContextInitializer)2 Key (org.infinispan.test.data.Key)2