Search in sources :

Example 1 with EmbeddedMetadata

use of org.infinispan.metadata.EmbeddedMetadata in project infinispan by infinispan.

the class CacheEntrySizeCalculator method calculateSize.

@Override
public long calculateSize(K key, InternalCacheEntry<K, V> ice) {
    // This will be non zero when use expiration, but don't want to store the metadata
    long noMetadataSize = 0;
    boolean metadataAware;
    // ImmortalCacheEntry so it has to come before
    if (ice instanceof MetadataImmortalCacheEntry) {
        metadataAware = true;
    } else if (ice instanceof ImmortalCacheEntry) {
        metadataAware = false;
    } else if (ice instanceof MortalCacheEntry) {
        noMetadataSize += 16;
        metadataAware = false;
    } else if (ice instanceof TransientCacheEntry) {
        noMetadataSize += 16;
        metadataAware = false;
    } else if (ice instanceof TransientMortalCacheEntry) {
        noMetadataSize += 32;
        metadataAware = false;
    } else if (ice instanceof MetadataMortalCacheEntry) {
        metadataAware = true;
    } else if (ice instanceof MetadataTransientCacheEntry) {
        metadataAware = true;
    } else {
        metadataAware = ice instanceof MetadataTransientMortalCacheEntry;
    }
    Metadata metadata;
    if (metadataAware) {
        metadata = ice.getMetadata();
        // We don't support other metadata types currently
        if (!(metadata instanceof EmbeddedMetadata)) {
            metadata = null;
        }
    } else {
        metadata = null;
    }
    long keyValueMetadataSize = calculateSize(key, ice.getValue(), metadata, ice.getInternalMetadata());
    return keyValueMetadataSize + noMetadataSize;
}
Also used : MetadataTransientMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientMortalCacheEntry) MetadataMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataMortalCacheEntry) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) MetadataTransientCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientCacheEntry) MetadataTransientMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientMortalCacheEntry) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) Metadata(org.infinispan.metadata.Metadata) MetadataTransientMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientMortalCacheEntry) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) MetadataMortalCacheEntry(org.infinispan.container.entries.metadata.MetadataMortalCacheEntry) MetadataTransientCacheEntry(org.infinispan.container.entries.metadata.MetadataTransientCacheEntry)

Example 2 with EmbeddedMetadata

use of org.infinispan.metadata.EmbeddedMetadata in project infinispan by infinispan.

the class OffHeapEntryFactoryImpl method calculateSize.

@Override
public long calculateSize(WrappedBytes key, WrappedBytes value, Metadata metadata, PrivateMetadata internalMetadata) {
    long totalSize = evictionEnabled ? 24 : 8;
    totalSize += HEADER_LENGTH;
    totalSize += key.getLength() + value.getLength();
    long metadataSize = 0;
    if (metadata instanceof EmbeddedMetadata) {
        EntryVersion version = metadata.version();
        if (version != null) {
            metadataSize = marshall(version).length;
            // We have to write the size of the version
            metadataSize += 4;
        }
        if (metadata.maxIdle() >= 0) {
            metadataSize += 16;
        }
        if (metadata.lifespan() >= 0) {
            metadataSize += 16;
        }
    } else {
        // We have to write the size of the metadata object
        metadataSize += 4;
        metadataSize += marshall(metadata).length;
    }
    long internalMetadataSize = shouldWriteInternalMetadata(internalMetadata) ? marshall(internalMetadata).length : 0;
    return estimateSizeOverhead(totalSize + metadataSize + internalMetadataSize);
}
Also used : EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) EntryVersion(org.infinispan.container.versioning.EntryVersion)

Example 3 with EmbeddedMetadata

use of org.infinispan.metadata.EmbeddedMetadata in project infinispan by infinispan.

the class OffHeapEntryFactoryImpl method create.

@Override
public long create(WrappedBytes key, int hashCode, InternalCacheEntry<WrappedBytes, WrappedBytes> ice) {
    byte type;
    boolean shouldWriteMetadataSize = false;
    byte[] metadataBytes;
    Metadata metadata = ice.getMetadata();
    if (metadata instanceof EmbeddedMetadata) {
        EntryVersion version = metadata.version();
        byte[] versionBytes;
        if (version != null) {
            type = HAS_VERSION;
            shouldWriteMetadataSize = true;
            try {
                versionBytes = marshaller.objectToByteBuffer(version);
            } catch (IOException | InterruptedException e) {
                throw new CacheException(e);
            }
        } else {
            type = 0;
            versionBytes = Util.EMPTY_BYTE_ARRAY;
        }
        long lifespan = metadata.lifespan();
        long maxIdle = metadata.maxIdle();
        if (lifespan < 0 && maxIdle < 0) {
            type |= IMMORTAL;
            metadataBytes = versionBytes;
        } else if (lifespan > -1 && maxIdle < 0) {
            type |= MORTAL;
            metadataBytes = new byte[16 + versionBytes.length];
            Bits.putLong(metadataBytes, 0, lifespan);
            Bits.putLong(metadataBytes, 8, ice.getCreated());
            System.arraycopy(versionBytes, 0, metadataBytes, 16, versionBytes.length);
        } else if (lifespan < 0) {
            type |= TRANSIENT;
            metadataBytes = new byte[16 + versionBytes.length];
            Bits.putLong(metadataBytes, 0, maxIdle);
            Bits.putLong(metadataBytes, 8, ice.getLastUsed());
            System.arraycopy(versionBytes, 0, metadataBytes, 16, versionBytes.length);
        } else {
            type |= TRANSIENT_MORTAL;
            metadataBytes = new byte[32 + versionBytes.length];
            Bits.putLong(metadataBytes, 0, lifespan);
            Bits.putLong(metadataBytes, 8, maxIdle);
            Bits.putLong(metadataBytes, 16, ice.getCreated());
            Bits.putLong(metadataBytes, 24, ice.getLastUsed());
            System.arraycopy(versionBytes, 0, metadataBytes, 32, versionBytes.length);
        }
    } else {
        type = CUSTOM;
        shouldWriteMetadataSize = true;
        metadataBytes = marshall(metadata);
    }
    int keySize = key.getLength();
    int metadataSize = metadataBytes.length;
    WrappedBytes value = ice.getValue();
    int valueSize = value != null ? value.getLength() : 0;
    byte[] internalMetadataBytes = shouldWriteInternalMetadata(ice.getInternalMetadata()) ? marshall(ice.getInternalMetadata()) : Util.EMPTY_BYTE_ARRAY;
    int internalMetadataSize = internalMetadataBytes.length;
    // Eviction requires 2 additional pointers at the beginning
    int offset = evictionEnabled ? 16 : 0;
    long totalSize = offHeapEntrySize(evictionEnabled, shouldWriteMetadataSize, keySize, valueSize, metadataSize, internalMetadataSize);
    long memoryAddress = allocator.allocate(totalSize);
    // Write the empty linked address pointer first
    MEMORY.putLong(memoryAddress, offset, 0);
    offset += 8;
    MEMORY.putByte(memoryAddress, offset, type);
    offset += 1;
    MEMORY.putInt(memoryAddress, offset, hashCode);
    offset += 4;
    MEMORY.putInt(memoryAddress, offset, key.getLength());
    offset += 4;
    if (shouldWriteMetadataSize) {
        MEMORY.putInt(memoryAddress, offset, metadataBytes.length);
        offset += 4;
    }
    MEMORY.putInt(memoryAddress, offset, valueSize);
    offset += 4;
    MEMORY.putInt(memoryAddress, offset, internalMetadataSize);
    offset += 4;
    MEMORY.putBytes(key.getBytes(), key.backArrayOffset(), memoryAddress, offset, keySize);
    offset += keySize;
    MEMORY.putBytes(metadataBytes, 0, memoryAddress, offset, metadataSize);
    offset += metadataSize;
    if (valueSize > 0) {
        MEMORY.putBytes(value.getBytes(), value.backArrayOffset(), memoryAddress, offset, valueSize);
        offset += valueSize;
    }
    if (internalMetadataSize > 0) {
        MEMORY.putBytes(internalMetadataBytes, 0, memoryAddress, offset, internalMetadataSize);
        offset += internalMetadataSize;
    }
    assert offset == totalSize;
    return memoryAddress;
}
Also used : EntryVersion(org.infinispan.container.versioning.EntryVersion) CacheException(org.infinispan.commons.CacheException) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) Metadata(org.infinispan.metadata.Metadata) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) IOException(java.io.IOException) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) WrappedBytes(org.infinispan.commons.marshall.WrappedBytes)

Aggregations

EmbeddedMetadata (org.infinispan.metadata.EmbeddedMetadata)3 EntryVersion (org.infinispan.container.versioning.EntryVersion)2 Metadata (org.infinispan.metadata.Metadata)2 PrivateMetadata (org.infinispan.metadata.impl.PrivateMetadata)2 IOException (java.io.IOException)1 CacheException (org.infinispan.commons.CacheException)1 WrappedBytes (org.infinispan.commons.marshall.WrappedBytes)1 MetadataImmortalCacheEntry (org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry)1 MetadataMortalCacheEntry (org.infinispan.container.entries.metadata.MetadataMortalCacheEntry)1 MetadataTransientCacheEntry (org.infinispan.container.entries.metadata.MetadataTransientCacheEntry)1 MetadataTransientMortalCacheEntry (org.infinispan.container.entries.metadata.MetadataTransientMortalCacheEntry)1