use of org.infinispan.commons.io.ByteBufferFactory in project infinispan by infinispan.
the class SingleFileStore method readFromDisk.
private MarshallableEntry<K, V> readFromDisk(FileEntry fe, Object key, boolean loadValue, boolean loadMetadata) {
org.infinispan.commons.io.ByteBuffer valueBb = null;
// If we only require the key, then no need to read disk
if (!loadValue && !loadMetadata) {
try {
return entryFactory.create(key);
} finally {
fe.unlock();
}
}
final byte[] data;
try {
// load serialized data from disk
data = new byte[fe.keyLen + fe.dataLen + (loadMetadata ? fe.metadataLen + fe.internalMetadataLen : 0)];
// The entry lock will prevent clear() from truncating the file at this point
channel.read(ByteBuffer.wrap(data), fe.offset + KEY_POS_LATEST);
} catch (Exception e) {
throw new PersistenceException(e);
} finally {
// No need to keep the lock for deserialization.
// FileEntry is immutable, so its members can't be changed by another thread.
fe.unlock();
}
if (log.isTraceEnabled())
log.tracef("Read entry %s at %d:%d", key, fe.offset, fe.actualSize());
ByteBufferFactory factory = ctx.getByteBufferFactory();
org.infinispan.commons.io.ByteBuffer keyBb = factory.newByteBuffer(data, 0, fe.keyLen);
if (loadValue) {
valueBb = factory.newByteBuffer(data, fe.keyLen, fe.dataLen);
}
if (loadMetadata) {
long created = -1;
long lastUsed = -1;
org.infinispan.commons.io.ByteBuffer metadataBb = null;
org.infinispan.commons.io.ByteBuffer internalMetadataBb = null;
int offset = fe.keyLen + fe.dataLen;
if (fe.metadataLen > 0) {
int metaLength = fe.metadataLen - TIMESTAMP_BYTES;
metadataBb = factory.newByteBuffer(data, offset, metaLength);
offset += metaLength;
ByteBuffer buffer = ByteBuffer.wrap(data, offset, TIMESTAMP_BYTES);
if (fe.expiryTime > 0) {
offset += TIMESTAMP_BYTES;
created = buffer.getLong();
lastUsed = buffer.getLong();
}
}
if (fe.internalMetadataLen > 0) {
internalMetadataBb = factory.newByteBuffer(data, offset, fe.internalMetadataLen);
}
return entryFactory.create(keyBb, valueBb, metadataBb, internalMetadataBb, created, lastUsed);
}
return entryFactory.create(keyBb, valueBb);
}
use of org.infinispan.commons.io.ByteBufferFactory in project keycloak by keycloak.
the class PersistenceManagerImpl method start.
@Override
@Start
public void start() {
enabled = configuration.persistence().usingStores();
if (!enabled)
return;
preloaded = false;
segmentCount = configuration.clustering().hash().numSegments();
isInvalidationCache = configuration.clustering().cacheMode().isInvalidation();
long stamp = lock.writeLock();
try {
Completable storeStartup = Flowable.fromIterable(configuration.persistence().stores()).concatMapSingle(storeConfiguration -> {
NonBlockingStore<?, ?> actualStore = storeFromConfiguration(storeConfiguration);
NonBlockingStore<?, ?> nonBlockingStore;
if (storeConfiguration.async().enabled()) {
nonBlockingStore = new AsyncNonBlockingStore<>(actualStore);
} else {
nonBlockingStore = actualStore;
}
InitializationContextImpl ctx = new InitializationContextImpl(storeConfiguration, cache.wired(), keyPartitioner, persistenceMarshaller, timeService, byteBufferFactory, marshallableEntryFactory, nonBlockingExecutor, globalConfiguration, blockingManager, nonBlockingManager);
CompletionStage<Void> stage = nonBlockingStore.start(ctx).whenComplete((ignore, t) -> {
// On exception, just put a status with only the store - this way we can still invoke stop on it later
if (t != null) {
stores.add(new StoreStatus(nonBlockingStore, null, null));
}
});
return Completable.fromCompletionStage(stage).toSingle(() -> new StoreStatus(nonBlockingStore, storeConfiguration, updateCharacteristics(nonBlockingStore, nonBlockingStore.characteristics(), storeConfiguration)));
}).doOnNext(stores::add).delay(status -> {
if (status.config.purgeOnStartup()) {
return Flowable.fromCompletable(Completable.fromCompletionStage(status.store.clear()));
}
return Flowable.empty();
}).ignoreElements();
long interval = configuration.persistence().availabilityInterval();
if (interval > 0) {
storeStartup = storeStartup.doOnComplete(() -> availabilityTask = nonBlockingManager.scheduleWithFixedDelay(this::pollStoreAvailability, interval, interval, MILLISECONDS));
}
// Blocks here waiting for stores and availability task to start if needed
storeStartup.blockingAwait();
allSegmentedOrShared = allStoresSegmentedOrShared();
} catch (Throwable t) {
log.debug("PersistenceManagerImpl encountered an exception during startup of stores", t);
throw t;
} finally {
lock.unlockWrite(stamp);
}
}
use of org.infinispan.commons.io.ByteBufferFactory in project datawave by NationalSecurityAgency.
the class AccumuloCacheStore method decodeEntry.
private MarshalledEntry<K, V> decodeEntry(Map.Entry<Key, Value> entry, Object key, boolean loadValue, boolean loadMetadata) {
if (entry != null) {
ByteBufferFactory bbFactory = ctx.getByteBufferFactory();
ByteBuffer buffer = ByteBuffer.wrap(entry.getValue().get(), 0, entry.getValue().getSize());
int valueSize = buffer.getInt();
int metadataSize = buffer.getInt();
org.infinispan.commons.io.ByteBuffer valueBytes = null;
if (loadValue) {
valueBytes = bbFactory.newByteBuffer(entry.getValue().get(), buffer.position(), valueSize);
}
org.infinispan.commons.io.ByteBuffer metadataBytes = null;
if (metadataSize > 0 && loadMetadata) {
// Skip over value if we didn't read it in.
if (!loadValue)
buffer.position(buffer.position() + valueSize);
metadataBytes = bbFactory.newByteBuffer(entry.getValue().get(), buffer.position() + valueSize, metadataSize);
}
// noinspection unchecked
return ctx.getMarshalledEntryFactory().newMarshalledEntry(key, valueBytes, metadataBytes);
} else {
return null;
}
}
Aggregations