-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Tilmann Zäschke <tilmann.zaeschke@inf.ethz.ch>
- Loading branch information
Showing
5 changed files
with
282 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2024 ETH Zurich | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.scion.internal; | ||
|
||
import java.util.HashMap; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* A simple cache that min/max watermark. Once max watermark is reached, the cache removes entries | ||
* until min watermark is reached. Least recently accessed entries are removed first. | ||
* | ||
* @param <K> The key | ||
* @param <V> The value | ||
*/ | ||
public class SimpleCache<K, V> { | ||
private final TreeMap<Long, Entry> ageMap = new TreeMap<>(); | ||
private final HashMap<K, Entry> lookupMap = new HashMap<>(); | ||
|
||
private int capacity; | ||
private long opCount = 0; | ||
|
||
public SimpleCache(int capacity) { | ||
if (capacity < 1) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.capacity = capacity; | ||
} | ||
|
||
public void put(K key, V value) { | ||
Entry e = lookupMap.get(key); | ||
if (e == null) { | ||
e = new Entry(opCount++, key, value); | ||
checkCapacity(1); | ||
lookupMap.put(key, e); | ||
} else { | ||
ageMap.remove(e.age); | ||
e.age = opCount++; | ||
e.value = value; | ||
} | ||
ageMap.put(e.age, e); | ||
} | ||
|
||
public V get(K key) { | ||
Entry e = lookupMap.get(key); | ||
if (e == null) { | ||
return null; | ||
} | ||
|
||
ageMap.remove(e.age); | ||
e.age = opCount++; | ||
ageMap.put(e.age, e); | ||
|
||
return e.value; | ||
} | ||
|
||
public void clear() { | ||
lookupMap.clear(); | ||
ageMap.clear(); | ||
} | ||
|
||
public void setCapacity(int capacity) { | ||
if (capacity < 1) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.capacity = capacity; | ||
checkCapacity(0); | ||
} | ||
|
||
public int getCapacity() { | ||
return capacity; | ||
} | ||
|
||
private void checkCapacity(int spare) { | ||
while (lookupMap.size() + spare > capacity) { | ||
Entry e = ageMap.pollFirstEntry().getValue(); | ||
lookupMap.remove(e.key); | ||
} | ||
} | ||
|
||
private class Entry { | ||
private long age; | ||
private final K key; | ||
private V value; | ||
|
||
public Entry(long l, K key, V value) { | ||
this.age = l; | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/test/java/org/scion/jpan/internal/SimpleCacheTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2024 ETH Zurich | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.scion.internal; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SimpleCacheTest { | ||
|
||
private static class Pair { | ||
int i; | ||
String s; | ||
|
||
Pair(int i, String s) { | ||
this.i = i; | ||
this.s = s; | ||
} | ||
} | ||
|
||
@Test | ||
void testInsertOnly() { | ||
SimpleCache<Integer, Pair> cache = new SimpleCache<>(10); | ||
List<Pair> list = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 30; i++) { | ||
Pair p = new Pair(i, Integer.toString(i)); | ||
cache.put(i, p); | ||
list.add(p); | ||
} | ||
|
||
// Test latest 10 remain | ||
for (int i = 0; i < list.size(); i++) { | ||
if (i < 20) { | ||
assertNull(cache.get(i)); | ||
} else { | ||
assertEquals(list.get(i), cache.get(i)); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testMultiGet() { | ||
SimpleCache<Integer, Pair> cache = new SimpleCache<>(10); | ||
List<Pair> list = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 30; i++) { | ||
Pair p = new Pair(i, Integer.toString(i)); | ||
cache.put(i, p); | ||
list.add(p); | ||
// keep using the first 5 entries | ||
assertNotNull(cache.get(i % 5)); | ||
} | ||
|
||
// Test latest 5 remain and most used 5 remain | ||
for (int i = 0; i < list.size(); i++) { | ||
if (i >= 5 && i < 25) { | ||
assertNull(cache.get(i)); | ||
} else { | ||
assertEquals(list.get(i), cache.get(i)); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testReduceCapacity() { | ||
SimpleCache<Integer, Pair> cache = new SimpleCache<>(10); | ||
List<Pair> list = new ArrayList<>(); | ||
|
||
for (int i = 0; i < 30; i++) { | ||
Pair p = new Pair(i, Integer.toString(i)); | ||
cache.put(i, p); | ||
list.add(p); | ||
// keep using the first 2 entries | ||
assertNotNull(cache.get(i % 2)); | ||
} | ||
|
||
cache.setCapacity(5); | ||
assertEquals(5, cache.getCapacity()); | ||
|
||
// Test latest 3 remain and most used 2 remain | ||
for (int i = 0; i < list.size(); i++) { | ||
if (i >= 2 && i < 27) { | ||
assertNull(cache.get(i)); | ||
} else { | ||
assertEquals(list.get(i), cache.get(i)); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testSetCapacity() { | ||
SimpleCache<Integer, Pair> cache = new SimpleCache<>(42); | ||
assertEquals(42, cache.getCapacity()); | ||
|
||
cache.setCapacity(100); | ||
assertEquals(100, cache.getCapacity()); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> cache.setCapacity(0)); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> new SimpleCache<>(0)); | ||
} | ||
} |
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