Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 224dc10
Showing
26 changed files
with
1,449 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.classpath | ||
.settings | ||
bin/ |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>persistenthashmap</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
78 changes: 78 additions & 0 deletions
78
src/com/jamierf/persistenthashmap/CachedPersistentHashMap.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,78 @@ | ||
package com.jamierf.persistenthashmap; | ||
|
||
import java.io.File; | ||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import com.jamierf.persistenthashmap.serializers.OOSSerializer; | ||
import com.jamierf.persistenthashmap.serializers.ObjectSerializer; | ||
|
||
public class CachedPersistentHashMap<K extends Serializable, V extends Serializable> extends PersistentHashMap<K, V> { | ||
|
||
protected HashMap<K, V> cache; | ||
|
||
public CachedPersistentHashMap(File root) { | ||
this (root, new OOSSerializer()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public CachedPersistentHashMap(File root, ObjectSerializer serializer) { | ||
super(root, serializer); | ||
|
||
cache = new HashMap<K, V>(); | ||
|
||
Iterator<Map.Entry<K, V>> iterator = new EntryIterator(this); | ||
while (iterator.hasNext()) { | ||
Map.Entry<K, V> e = iterator.next(); | ||
cache.put(e.getKey(), e.getValue()); | ||
} | ||
} | ||
|
||
public synchronized boolean containsKey(Object key) { | ||
return cache.containsKey(key); | ||
} | ||
|
||
public synchronized boolean containsValue(Object v) { | ||
return cache.containsValue(v); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public synchronized V get(Object key) { | ||
if (cache.containsKey(key)) | ||
return cache.get(key); | ||
|
||
V value = super.get(key); | ||
if (value != null) | ||
cache.put((K) key, value); | ||
|
||
return value; | ||
} | ||
|
||
public synchronized V put(K key, V value) { | ||
cache.put(key, value); | ||
|
||
return super.put(key, value); | ||
} | ||
|
||
public synchronized V remove(Object key) { | ||
cache.remove(key); | ||
|
||
return super.remove(key); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return cache.isEmpty(); | ||
} | ||
|
||
public int size() { | ||
return cache.size(); | ||
} | ||
|
||
public synchronized void clear() { | ||
cache.clear(); | ||
super.clear(); | ||
} | ||
|
||
} |
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,35 @@ | ||
package com.jamierf.persistenthashmap; | ||
|
||
import java.io.Serializable; | ||
import java.util.AbstractMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
class EntryIterator<K extends Serializable, V extends Serializable> implements Iterator<Map.Entry<K, V>> { | ||
|
||
private PersistentHashMap<K, V> map; | ||
private Iterator<K> iterator; | ||
private K current; | ||
|
||
public EntryIterator(PersistentHashMap<K, V> map) { | ||
this.map = map; | ||
|
||
iterator = new KeyIterator<K, V>(map); | ||
current = null; | ||
} | ||
|
||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
public Map.Entry<K, V> next() { | ||
current = iterator.next(); | ||
|
||
return new AbstractMap.SimpleEntry<K, V>(current, map.get(current)); | ||
} | ||
|
||
public void remove() { | ||
if (current != null) | ||
map.remove(current); | ||
} | ||
} |
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,101 @@ | ||
package com.jamierf.persistenthashmap; | ||
|
||
import java.io.Serializable; | ||
import java.util.AbstractSet; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
class EntrySet<K extends Serializable, V extends Serializable> extends AbstractSet<Map.Entry<K, V>> { | ||
|
||
private PersistentHashMap<K, V> map; | ||
|
||
public EntrySet(PersistentHashMap<K, V> map) { | ||
this.map = map; | ||
} | ||
|
||
public Iterator<Map.Entry<K, V>> iterator() { | ||
return new EntryIterator<K, V>(map); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public boolean contains(Object o) { | ||
if (!(o instanceof Map.Entry)) | ||
return false; | ||
|
||
Map.Entry<K, V> e = (Map.Entry<K, V>) o; | ||
V value = map.get(e.getKey()); | ||
return value != null && value.equals(e.getValue()); | ||
} | ||
|
||
public int size() { | ||
return map.size(); | ||
} | ||
|
||
public void clear() { | ||
map.clear(); | ||
} | ||
|
||
public boolean add(Map.Entry<K, V> e) { | ||
map.put(e.getKey(), e.getValue()); | ||
return true; | ||
} | ||
|
||
public boolean addAll(Collection<? extends Map.Entry<K, V>> c) { | ||
for (Map.Entry<K, V> e : c) | ||
map.put(e.getKey(), e.getValue()); | ||
|
||
return true; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public boolean containsAll(Collection<?> c) { | ||
Collection<Map.Entry<K, V>> ec = (Collection<Map.Entry<K, V>>) c; | ||
for (Map.Entry<K, V> e : ec) | ||
if (!contains(e)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return map.isEmpty(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public boolean remove(Object o) { | ||
if (!(o instanceof Map.Entry)) | ||
return false; | ||
|
||
Map.Entry<K, V> e = (Map.Entry<K, V>) o; | ||
if (!contains(e)) | ||
return false; | ||
|
||
map.remove(e.getKey()); | ||
return true; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public boolean removeAll(Collection<?> c) { | ||
Collection<Map.Entry<K, V>> ec = (Collection<Map.Entry<K, V>>) c; | ||
for (Map.Entry<K, V> e : ec) | ||
remove(e); | ||
|
||
return true; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public boolean retainAll(Collection<?> c) { | ||
Collection<Map.Entry<K, V>> ec = (Collection<Map.Entry<K, V>>) c; | ||
Iterator<Map.Entry<K, V>> iterator = iterator(); | ||
|
||
Map.Entry<K, V> entry = null; | ||
while (iterator.hasNext()) { | ||
entry = iterator.next(); | ||
if (!ec.contains(entry)) | ||
remove(entry); | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.jamierf.persistenthashmap; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.Iterator; | ||
|
||
class KeyIterator<K extends Serializable, V extends Serializable> implements Iterator<K> { | ||
|
||
private PersistentHashMap<K, V> map; | ||
private Iterator<File> iterator; | ||
private K current; | ||
|
||
public KeyIterator(PersistentHashMap<K, V> map) { | ||
this.map = map; | ||
|
||
iterator = map.new FileIterator(map.keyStore); | ||
current = null; | ||
} | ||
|
||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public K next() { | ||
try { | ||
File keyFile = iterator.next(); | ||
if (keyFile == null) | ||
return null; | ||
|
||
return (K) map.readObject(keyFile); | ||
} | ||
catch (ClassNotFoundException cnfe) { | ||
return null; | ||
} | ||
catch (IOException ioe) { | ||
return null; | ||
} | ||
} | ||
|
||
public void remove() { | ||
if (current == null) | ||
return; | ||
|
||
map.remove(current); | ||
} | ||
} |
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,41 @@ | ||
package com.jamierf.persistenthashmap; | ||
|
||
import java.io.Serializable; | ||
import java.util.AbstractSet; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
class KeySet<K extends Serializable, V extends Serializable> extends AbstractSet<K> { | ||
|
||
private PersistentHashMap<K, V> map; | ||
|
||
public KeySet(PersistentHashMap<K, V> map) { | ||
this.map = map; | ||
} | ||
|
||
public boolean contains(Object o) { | ||
return map.containsKey(o); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public boolean containsAll(Collection<?> c) { | ||
Collection<K> ec = (Collection<K>) c; | ||
for (K key : ec) | ||
if (!contains(key)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return map.isEmpty(); | ||
} | ||
|
||
public Iterator<K> iterator() { | ||
return new KeyIterator<K, V>(map); | ||
} | ||
|
||
public int size() { | ||
return map.size(); | ||
} | ||
} |
Oops, something went wrong.