Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
reines committed Mar 22, 2010
0 parents commit 224dc10
Show file tree
Hide file tree
Showing 26 changed files with 1,449 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.classpath
.settings
bin/
17 changes: 17 additions & 0 deletions .project
@@ -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 added lib/gson-1.4.jar
Binary file not shown.
Binary file added lib/xpp3_min-1.1.4c.jar
Binary file not shown.
Binary file added lib/xstream-1.3.1.jar
Binary file not shown.
78 changes: 78 additions & 0 deletions src/com/jamierf/persistenthashmap/CachedPersistentHashMap.java
@@ -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();
}

}
35 changes: 35 additions & 0 deletions src/com/jamierf/persistenthashmap/EntryIterator.java
@@ -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);
}
}
101 changes: 101 additions & 0 deletions src/com/jamierf/persistenthashmap/EntrySet.java
@@ -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;
}
}
48 changes: 48 additions & 0 deletions src/com/jamierf/persistenthashmap/KeyIterator.java
@@ -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);
}
}
41 changes: 41 additions & 0 deletions src/com/jamierf/persistenthashmap/KeySet.java
@@ -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();
}
}

0 comments on commit 224dc10

Please sign in to comment.