Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Map API for LevelDB

This is an implementation of java.util.Map and java.util.SortedMap on top of LevelDB. Everything stored in these maps will be persistent and saved to disk in a LevelDB database.

Status

Both the java.util.Map and java.util.SortedMap interface are implemented. We pass both the MapInterfaceTest and SortedMapInterface tests from Guava. Please report an issue if you encounter any problems.

Usage

Map

Create a Map with Strings as keys and values backed by LevelDB:

StoredMap<String, String> map = LevelDBMapFactory.createMap(newFile("/path/to/directory"), newStringBinding());
map.put("key", "value");
map.put("key2", "value2");
// Print "value"System.out.println(map.get("key"));
// Close the databasemap.close();
// Reopen the database from diskmap = LevelDBMapFactory.createMap(newFile("/path/to/directory"), newStringBinding()))
// Print "value2"System.out.println(map.get("key2"));
// Close the database againmap.close();

Note that you have to call close on the map explicitly since its LevelDB database needs to be closed.

The StoredMap is an implementation of java.util.Map and can be used as such:

Map<String, String> readOnly = Collections.unmodifiableMap(map);

It is possible to use any serializable object as a value:

StoredMap<String, Integer> map = LevelDBMapFactory.createMap(newFile("/path/to/directory"), newObjectSerializableBinding<Integer>());
map.put("key", 10);
map.put("key2", newInteger(5));
intcount = 0;
for(Map.Entry<String, Integer> entry : map.entrySet())
{
System.out.println(entry.getKey());
count += entry.getValue();
}
// Print 15System.out.println(count);
map.close();
try(StoredMap<String, POJO> pojoMap = LevelDBMapFactory.createMap(
newFile("/path/to/directory"), newObjectSerializableBinding<POJO>()))
{
pojoMap.put("key", newPOJO());
}

Serializable objects can also be used as keys:

StoredMap<Student, Grade> grades = LevelDBMapFactory.createMap(newFile("/path/to/directory"),
newObjectSerializableBinding<Student>(), newObjectSerializableBinding<Grade>());
grades.put(john, Grade.A);
grades.put(sam, Grade.C);
grades.close();

SortedMap

The SortedMap interface is also supported through a set of factory methods:

// Create a SortedMap using Strings as Comparable keysStoredSortedMap<String, String> dict = LevelDBMapFactory.createSortedMap(newFile("/path/to/directory"), newStringBinding());
dict.put("zwaaien", "to wave");
dict.put("boek", "book");
// Print "boek" and then "zwaaien"for(Stringkey : dict.keySet())
{
System.out.println(key);
}
dict.close();

It is possible to supply a java.util.Comparator:

StoredSortedMap<String, String> reverseSortedMap = LevelDBMapFactory.createSortedMap(newFile("/path/to/directory"),
newStringBinding(), newStringBinding(), newComparator<String>()
{
@Overridepublicintcompare(Stringstring1, Stringstring2)
{
returnstring2.compareTo(string1);
}
});
reverseSortedMap.put("Amsterdam", "Europe");
reverseSortedMap.put("Washington", "North America");
reverseSortedMap.put("Tokyo", "Asia");
// Print "Washington", "Tokyo" and finally "Amsterdam"for(Stringkey : reverseSortedMap.keySet())
{
System.out.println(key);
}
reverseSortedMap.close();

Wrapping an existing LevelDB instance

An open instance of a LevelDB object can also be used to create a Map:

Optionsoptions = newOptions();
options.createIfMissing(true);
DBdb = Iq80DBFactory.factory.open(newFile("/path/to/directory"), options);
db.put(newString("key").getBytes(), newString("value").getBytes());
EntryBinding<String> stringBinding = newStringBinding();
Map<String, String> map = LevelDBMapFactory.createMapForDB(db, stringBinding, stringBinding);
// Print "value"System.out.println(map.get("key"));
map.close();

About

An implementation of java.util.Map and java.util.SortedMap on top of LevelDB

Resources

Stars

6 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages