This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void removeAll(Collection<String> attributesToRemove) { | |
attributesToRemove.forEach(key -> | |
Optional.ofNullable(store.get(key)) | |
.ifPresent(node -> deletedMarker[node.index] = Boolean.TRUE)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cache { | |
private final Map<String, Node> store; | |
private final Node firstEntry; | |
public Cache() { | |
store = null; | |
firstEntry = null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
String value; | |
Integer index; | |
Node next; | |
Node(String value, Integer index) { | |
this.value = value; | |
this.index = index; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |