![JAR search and dependency download from the Maven repository](/logo.png)
io.hyperfoil.core.session.SharedDataImpl Maven / Gradle / Ivy
package io.hyperfoil.core.session;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import io.hyperfoil.api.session.Access;
import io.hyperfoil.api.session.SharedData;
public class SharedDataImpl implements SharedData {
private final Map maps = new HashMap<>();
@Override
public void reserveMap(String key, Access match, int entries) {
SharedMapSet existing = maps.get(key);
if (existing != null) {
if (match != null) {
if (existing instanceof IndexedSharedMapSet) {
((IndexedSharedMapSet) existing).ensureIndex(match).ensureEntries(entries);
} else {
maps.put(key, new IndexedSharedMapSet(existing, match, entries));
}
} else {
existing.ensureEntries(entries);
}
} else {
if (match != null) {
maps.put(key, new IndexedSharedMapSet(match, entries));
} else {
maps.put(key, new SharedMapSet(entries));
}
}
}
@Override
public SharedMap newMap(String key) {
SharedMapSet set = maps.get(key);
return set.newMap();
}
@Override
public SharedMap pullMap(String key) {
return maps.get(key).acquireRandom();
}
@Override
public SharedMap pullMap(String key, Access match, Object value) {
return maps.get(key).acquireRandom(match, value);
}
@Override
public void pushMap(String key, SharedMap sharedMap) {
maps.get(key).insert(sharedMap);
}
@Override
public void releaseMap(String key, SharedMap map) {
map.clear();
maps.get(key).release(map);
}
private static class SharedMapSet {
MapImpl[] unused;
int unusedSize;
int maxEntries;
MapImpl[] maps;
int currentSize;
SharedMapSet(int entries) {
unused = new MapImpl[16];
maps = new MapImpl[16];
maxEntries = entries;
}
SharedMapSet(SharedMapSet set, int entries) {
assert set.currentSize == 0;
assert set.unusedSize == 0;
unused = set.unused;
maps = set.maps;
maxEntries = Math.max(set.maxEntries, entries);
}
void ensureEntries(int entries) {
if (entries > maxEntries) {
assert unusedSize == 0;
maxEntries = entries;
}
}
SharedMap newMap() {
if (unusedSize == 0) {
return new MapImpl(maxEntries, numIndices());
} else {
SharedMap last = unused[--unusedSize];
unused[unusedSize] = null;
return last;
}
}
protected int numIndices() {
return 0;
}
private MapImpl acquireLast() {
if (currentSize <= 0) {
return null;
}
int pos = --currentSize;
MapImpl map = maps[pos];
maps[pos] = null;
return map;
}
public SharedMap acquireRandom(Access match, Object value) {
throw new UnsupportedOperationException("Cannot match " + match + ": not indexed");
}
public SharedMap acquireRandom() {
if (currentSize == 0) {
return null;
}
int pos = ThreadLocalRandom.current().nextInt(currentSize);
if (pos == currentSize - 1) {
return acquireLast();
} else {
SharedMap map = maps[pos];
maps[pos] = acquireLast();
return map;
}
}
public int insert(SharedMap map) {
if (currentSize == maps.length) {
maps = Arrays.copyOf(maps, maps.length * 2);
}
int mainIndex = currentSize++;
assert maps[mainIndex] == null;
maps[mainIndex] = (MapImpl) map;
return mainIndex;
}
public void release(SharedMap map) {
if (unusedSize == unused.length) {
unused = Arrays.copyOf(unused, unused.length * 2);
}
unused[unusedSize++] = (MapImpl) map;
}
}
private static class Positions {
private int[] array = new int[16];
private int size;
int insert(int target) {
if (size == array.length) {
array = Arrays.copyOf(array, array.length * 2);
}
int pos = size++;
array[pos] = target;
return pos;
}
int moveLastTo(int pos) {
assert size != 0;
--size;
if (size == 0) {
return -1;
}
return array[pos] = array[size];
}
}
private static class IndexedSharedMapSet extends SharedMapSet {
private Positions[] unusedPositions = new Positions[16];
private int unusedPositionsSize = 0;
private Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy