Skip to content

Instantly share code, notes, and snippets.

View rebizant's full-sized avatar

Rebizant rebizant

View GitHub Profile
@Test
void sampleTest() {
int size = 5000000;
List<String> data = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
data.add("sample-value-" + i);
}
Cache cache = CacheFactory.prepareCache(data);
FilteringHandler handler = new FilteringHandler(cache);
List<String> attributesToRemove = data.subList(101, 100000);
public List<String> getAttributes(String attribute, int quantity) {
if (quantity <= 0) {
return Collections.emptyList();
}
if (attribute == null) {
return getElements(quantity, firstElement);
}
Node node = store.get(attribute);
public void removeAll(Collection<String> attributesToRemove) {
attributesToRemove.forEach(key ->
Optional.ofNullable(store.get(key))
.ifPresent(node -> deletedMarker[node.index] = Boolean.TRUE));
}
public class FilteringHandler {
private final Boolean[] deletedMarker;
private final Node firstElement;
private final Map<String, Node> store;
public FilteringHandler(Cache cache) {
this.firstElement = cache.getFirstEntry();
this.store = cache.getStore();
this.deletedMarker = new Boolean[store.size()];
public class CacheFactory {
public static Cache prepareCache(List<String> values) {
if (values == null || values.isEmpty()) {
return new Cache();
}
Map<String, Node> store = new HashMap<>(values.size());
Node previousNode = new Node(values.get(0), 0);
Cache cache = new Cache(store, previousNode);
public class Cache {
private final Map<String, Node> store;
private final Node firstEntry;
public Cache() {
store = null;
firstEntry = null;
}
class Node {
String value;
Integer index;
Node next;
Node(String value, Integer index) {
this.value = value;
this.index = index;
}
public class Main {
public static void main(String[] args) throws Exception{
Main main = new Main();
for(int i = 0; i < 1000 ; i++){
main.convert(i);
}
}