@Override public E put(K key, E value) { // check if key already there; otherwise may end up with two entries // with same key Entry entry = data[(key.hashCode() & 0x7FFFFFFF) % data.length]; while (entry != null && !entry.key.equals(key)) entry = entry.next; if (entry == null) addEntry(new Entry(key, value)); else entry.value = value; return value; }
/** * Called to add an entry object into the data array. Assumes that * no entry with the same key already exists in the data array. */ private Entry<K, E> addEntry(Entry<K, E> newEntry) { if (entries >= max) prune(); else if (((float) entries) / data.length > threshold) rehash(data.length*2 + 1); int ix = (newEntry.key.hashCode() & 0x7FFFFFFF) % data.length; if (data[ix] == null) data[ix] = newEntry; else { newEntry.next = data[ix]; data[ix] = newEntry; } entries++; return newEntry; }
public void testPrune() { index = new CachedIndex(new SameIndex(), 250, 5, true); for (int ix = 0; ix < 10000; ix++) { String key = Integer.toString((int) (Math.random() * 500)); assertTrue("didn't find value", index.get(key).equals(key)); } assertTrue("number of keys in index too high", index.getKeyNumber() <= 250); }
public void testExpand() { index = new CachedIndex(new EmptyIndex(), 1000, 5, true); check("larsga", "Lars Marius Garshol"); check("grove", "Geir Ove Gronmo"); check("tine", "Tine Holst"); check("sylvias", "Sylvia Schwab"); check("pepper", "Steve Pepper"); check("hca", "Hans Christian Alsos"); check("niko", "Niko Schmuck"); check("pam", "Pamela Gennusa"); check("kal", "Kal Ahmed"); check("murray", "Murray Woodman"); lookfor("larsga", "Lars Marius Garshol"); lookfor("grove", "Geir Ove Gronmo"); lookfor("tine", "Tine Holst"); lookfor("sylvias", "Sylvia Schwab"); lookfor("pepper", "Steve Pepper"); lookfor("hca", "Hans Christian Alsos"); lookfor("niko", "Niko Schmuck"); lookfor("pam", "Pamela Gennusa"); lookfor("kal", "Kal Ahmed"); lookfor("murray", "Murray Woodman"); assertTrue("non-existent key found", index.get("dummy") == null); }
public void testEmpty() { assertTrue("found key in empty index", index.get("larsga") == null); }
private void check(String key, String value) { index.put(key, value); lookfor(key, value); }
@Override protected void setUp() { index = new CachedIndex(new EmptyIndex()); }
private void lookfor(String key, String value) { String found = (String) index.get(key); assertTrue("did not find value on lookup", found != null); assertTrue("found '" + found + "' on lookup, expected '" + value + "'", found.equals(value)); }
public void testFindMore() { check("larsga", "Lars Marius Garshol"); check("grove", "Geir Ove Gronmo"); check("tine", "Tine Holst"); check("sylvias", "Sylvia Schwab"); check("pepper", "Steve Pepper"); check("hca", "Hans Christian Alsos"); check("niko", "Niko Schmuck"); check("pam", "Pamela Gennusa"); check("kal", "Kal Ahmed"); check("murray", "Murray Woodman"); lookfor("larsga", "Lars Marius Garshol"); lookfor("grove", "Geir Ove Gronmo"); lookfor("tine", "Tine Holst"); lookfor("sylvias", "Sylvia Schwab"); lookfor("pepper", "Steve Pepper"); lookfor("hca", "Hans Christian Alsos"); lookfor("niko", "Niko Schmuck"); lookfor("pam", "Pamela Gennusa"); lookfor("kal", "Kal Ahmed"); lookfor("murray", "Murray Woodman"); assertTrue("non-existent key found", index.get("dummy") == null); }
@Override public E get(K key) { Entry entry = data[(key.hashCode() & 0x7FFFFFFF) % data.length]; while (entry != null && !entry.key.equals(key)) entry = entry.next; lookups++; if (entry == null) { // not found E result = fallback.get(key); if (result == null && !nulls) return null; // do not store null values entry = addEntry(new Entry(key, result)); } else { hits++; entry.hits++; } return (E) entry.value; }