Skip to content

Commit

Permalink
Added first version of db4o storage provider and generic key/value pa…
Browse files Browse the repository at this point in the history
…ir provider
  • Loading branch information
germanviscuso committed Jun 17, 2010
1 parent 72a7296 commit e7eaad5
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 972 deletions.
3 changes: 3 additions & 0 deletions .classpath
Expand Up @@ -13,6 +13,8 @@
<classpathentry kind="src" path="test/integration"/>
<classpathentry kind="src" path="test/common"/>
<classpathentry kind="src" path="example/java"/>
<classpathentry kind="src" path="contrib/db4o/test"/>
<classpathentry kind="src" path="contrib/db4o/src/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/catalina-ant.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
Expand Down Expand Up @@ -44,6 +46,7 @@
<classpathentry kind="lib" path="lib/libthrift-0.2.0.jar"/>
<classpathentry kind="lib" path="lib/google-collect-1.0.jar"/>
<classpathentry kind="lib" path="lib/je-4.0.92.jar"/>
<classpathentry kind="lib" path="/Users/germanviscuso/Projects/voldemort/contrib/db4o/lib/db4o-7.12.132.14217-all-java5.jar"/>
<classpathentry kind="lib" path="lib/paranamer-2.1.jar"/>
<classpathentry kind="lib" path="lib/jackson-mapper-asl-1.4.0.jar"/>
<classpathentry kind="lib" path="lib/jackson-core-asl-1.4.0.jar"/>
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ rebalancing.slave.list
server.state
.version
.temp
.DS_Store
27 changes: 25 additions & 2 deletions contrib/db4o/src/java/voldemort/store/db4o/Db4oKeyValuePair.java
@@ -1,9 +1,27 @@
/*
* Copyright 2010 Versant Corporation
*
* 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 voldemort.store.db4o;

import voldemort.utils.Pair;

public class Db4oKeyValuePair<Key, Value> {

private Key key;
private Value value;
protected Key key;
protected Value value;

public Db4oKeyValuePair(Key key, Value value) {
this.key = key;
Expand All @@ -25,4 +43,9 @@ public void setValue(Value value) {
public Value getValue() {
return value;
}

public Pair<Key, Value> toVoldemortPair() {
return Pair.create(getKey(), getValue());
}

}
163 changes: 149 additions & 14 deletions contrib/db4o/src/java/voldemort/store/db4o/Db4oKeyValueProvider.java
@@ -1,11 +1,33 @@
/*
* Copyright 2010 Versant Corporation
*
* 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 voldemort.store.db4o;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.ext.Db4oException;
import com.db4o.query.Query;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class Db4oKeyValueProvider<Key, Value> {

Expand All @@ -15,49 +37,162 @@ public static <Key, Value> Db4oKeyValueProvider<Key, Value> createDb4oKeyValuePr
return new Db4oKeyValueProvider<Key, Value>(container);
}

private Db4oKeyValueProvider(ObjectContainer container) {
this.container = container;
}

private void setContainer(ObjectContainer container) {
Db4oKeyValueProvider(ObjectContainer container) {
this.container = container;
}

private ObjectContainer getContainer() {
if(container == null)
throw new Db4oException("Db4oKeyValueProvider has no associated ObjectContainer");
return container;
}

public Iterator<Key> keyIterator() {
return getKeys().iterator();
}

/*
* Returns a List with all Keys present in the database (no values)
*/
public List<Key> getKeys() {
List<Key> keys = new ArrayList<Key>();
Query query = getContainer().query();
query.constrain(Db4oKeyValuePair.class);
ObjectSet<Db4oKeyValuePair<Key, Value>> result = query.execute();
List<Key> keys = Lists.newArrayList();
ObjectSet<Db4oKeyValuePair<Key, Value>> result = getAll();
while(result.hasNext()) {
Db4oKeyValuePair<Key, Value> pair = result.next();
keys.add(pair.getKey());
}
return keys;
}

/*
* Returns a List with all Values present in the database (no keys)
*/
public List<Value> getValues(Key key) {
List<Value> values = Lists.newArrayList();
ObjectSet<Db4oKeyValuePair<Key, Value>> result = get(key);
while(result.hasNext()) {
Db4oKeyValuePair<Key, Value> pair = result.next();
values.add(pair.getValue());
}
return values;
}

/*
* Returns an Iterator over Key/Value pairs
*/
public Iterator<Db4oKeyValuePair<Key, Value>> pairIterator() {
return getAll().iterator();
}

/*
* Given a Key returns a Set with Key/Value pairs matching the provided Key
*/
public ObjectSet<Db4oKeyValuePair<Key, Value>> get(Key key) {
// return getContainer().queryByExample(new
// Db4oKeyValuePair<Key,Value>(key, null));
Query query = getContainer().query();
query.constrain(Db4oKeyValuePair.class);
query.descend("key").constrain(key);
return query.execute();
}

public void delete(Key key) {
/*
* Deletes all Key/Value pairs matching the provided Key in the database
*/
public long delete(Key key) {
long deleteCount = 0;
ObjectSet<Db4oKeyValuePair<Key, Value>> candidates = get(key);
while(candidates.hasNext()) {
getContainer().delete(candidates.next());
delete(candidates.next());
deleteCount++;
}
return deleteCount;
}

/*
* Deletes a specific pair. The database might have more than one pair with
* the same key so this differs from delete by key. Incurs in some overhead
* because it performs a query to look for the pair.
*/
public long delete(Key key, Value value) {
long deleteCount = 0;
Db4oKeyValuePair<Key, Value> pair = new Db4oKeyValuePair<Key, Value>(key, value);
ObjectSet<Db4oKeyValuePair<Key, Value>> candidates = getContainer().queryByExample(pair);
while(candidates.hasNext()) {
delete(candidates.next());
deleteCount++;
}
return deleteCount;
}

/*
* Assumes pair was fetched from the database and still on db4o's reference
* system. Results in low overhead because it does not perform a query
*/
public void delete(Db4oKeyValuePair<Key, Value> pair) {
getContainer().delete(pair);
}

/*
* Stores a Key/Value pair in the database
*/
public void set(Key key, Value value) {
Db4oKeyValuePair<Key, Value> pair = new Db4oKeyValuePair<Key, Value>(key, value);
set(pair);
}

/*
* Stores a Key/Value pair in the database
*/
public void set(Db4oKeyValuePair<Key, Value> pair) {
getContainer().store(pair);
}

/*
* Returns all Key/Value pairs in the database
*/
public ObjectSet<Db4oKeyValuePair<Key, Value>> getAll() {
return getContainer().queryByExample(null);
}

public Map<Key, List<Value>> getAll(Iterable<Key> keys) {
Map<Key, List<Value>> result = newEmptyHashMap(keys);
for(Key key: keys) {
List<Value> values = getValues(key);
if(!values.isEmpty())
result.put(key, values);
}
return result;
}

private <K, V> HashMap<K, V> newEmptyHashMap(Iterable<?> iterable) {
if(iterable instanceof Collection<?>)
return Maps.newHashMapWithExpectedSize(((Collection<?>) iterable).size());
return Maps.newHashMap();
}

public long truncate() {
long deleteCount = 0;
ObjectSet<Db4oKeyValuePair<Key, Value>> candidates = getAll();
while(candidates.hasNext()) {
delete(candidates.next());
deleteCount++;
}
return deleteCount;
}

public void commit() {
getContainer().commit();
}

public void rollback() {
getContainer().rollback();
}

public void close() {
getContainer().close();
}

public boolean isClosed() {
return getContainer().ext().isClosed();
}

}

0 comments on commit e7eaad5

Please sign in to comment.