Permalink
Browse files

Upgrade google collections version. Patch from Ismael.

  • Loading branch information...
1 parent 08d1c9d commit 35581b67f83a0e9b0524cb57d51879339e0ef664 @jkreps jkreps committed Mar 2, 2009
View
@@ -17,7 +17,6 @@
<classpathentry kind="lib" path="lib/commons-lang-2.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging.jar"/>
<classpathentry kind="lib" path="lib/commons-pool-1.4.jar"/>
- <classpathentry kind="lib" path="lib/google-collect-snapshot-20080530.jar" sourcepath="lib/src/google-collections-src-snapshot-20080530.zip"/>
<classpathentry kind="lib" path="lib/jdom.jar"/>
<classpathentry kind="lib" path="lib/je-3.3.62.jar"/>
<classpathentry kind="lib" path="lib/jetty-6.1.6rc0.jar"/>
@@ -34,5 +33,6 @@
<classpathentry kind="lib" path="contrib/batch-indexer/lib/commons-cli-2.0-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="lib/protobuf-java-2.0.3.jar"/>
<classpathentry kind="lib" path="lib/libthrift-20080411p1.jar"/>
+ <classpathentry kind="lib" path="lib/google-collect-snapshot-20090211.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>
Binary file not shown.
Binary file not shown.
@@ -16,14 +16,14 @@
package voldemort.store;
+import java.util.HashMap;
+import java.util.Map;
+
import voldemort.VoldemortException;
import voldemort.utils.ReflectUtils;
import voldemort.versioning.InconsistentDataException;
import voldemort.versioning.ObsoleteVersionException;
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-
/**
* Map error codes to exceptions and vice versa
*
@@ -32,29 +32,35 @@
*/
public class ErrorCodeMapper {
- private BiMap<Short, Class<? extends VoldemortException>> mapping;
+ // These two maps act as a bijection from error codes to exceptions.
+ private Map<Short, Class<? extends VoldemortException>> codeToException;
+ private Map<Class<? extends VoldemortException>, Short> exceptionToCode;
public ErrorCodeMapper() {
- mapping = new HashBiMap<Short, Class<? extends VoldemortException>>();
- mapping.put((short) 1, VoldemortException.class);
- mapping.put((short) 2, InsufficientOperationalNodesException.class);
- mapping.put((short) 3, StoreOperationFailureException.class);
- mapping.put((short) 4, ObsoleteVersionException.class);
- mapping.put((short) 6, UnknownFailure.class);
- mapping.put((short) 7, UnreachableStoreException.class);
- mapping.put((short) 8, InconsistentDataException.class);
+ codeToException = new HashMap<Short, Class<? extends VoldemortException>>();
+ codeToException.put((short) 1, VoldemortException.class);
+ codeToException.put((short) 2, InsufficientOperationalNodesException.class);
+ codeToException.put((short) 3, StoreOperationFailureException.class);
+ codeToException.put((short) 4, ObsoleteVersionException.class);
+ codeToException.put((short) 6, UnknownFailure.class);
+ codeToException.put((short) 7, UnreachableStoreException.class);
+ codeToException.put((short) 8, InconsistentDataException.class);
+
+ exceptionToCode = new HashMap<Class<? extends VoldemortException>, Short>();
+ for(Map.Entry<Short, Class<? extends VoldemortException>> entry: codeToException.entrySet())
+ exceptionToCode.put(entry.getValue(), entry.getKey());
}
public VoldemortException getError(short code, String message) {
- Class<? extends VoldemortException> klass = mapping.get(code);
+ Class<? extends VoldemortException> klass = codeToException.get(code);
if(klass == null)
return new UnknownFailure(Integer.toString(code));
else
return ReflectUtils.construct(klass, new Object[] { message });
}
public short getCode(VoldemortException e) {
- Short code = mapping.inverse().get(e.getClass());
+ Short code = exceptionToCode.get(e.getClass());
if(code == null)
throw new IllegalArgumentException("No mapping code for " + e.getClass());
else
@@ -1,60 +0,0 @@
-/*
- * Copyright 2008-2009 LinkedIn, Inc
- *
- * 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;
-
-import com.google.common.base.Objects;
-
-/**
- * Wrapper class that handles arrays as keys properly.
- *
- * @author jay
- *
- */
-public class KeyWrapper {
-
- private final Object k;
-
- public KeyWrapper(Object k) {
- this.k = k;
- }
-
- public Object get() {
- return k;
- }
-
- @Override
- public boolean equals(Object o) {
- if(this == o)
- return true;
- if(!(o instanceof KeyWrapper))
- return false;
-
- KeyWrapper key = (KeyWrapper) o;
- return Objects.deepEquals(k, key.get());
- }
-
- @Override
- public int hashCode() {
- return Objects.deepHashCode(k);
- }
-
- @Override
- public String toString() {
- return "Key(" + k.toString() + ")";
- }
-
-}
@@ -17,15 +17,15 @@
package voldemort.store.memory;
import java.util.List;
+import java.util.concurrent.ConcurrentMap;
import voldemort.store.StorageConfiguration;
import voldemort.store.StorageEngine;
import voldemort.store.StorageEngineType;
import voldemort.utils.ByteArray;
import voldemort.versioning.Versioned;
-import com.google.common.base.ReferenceType;
-import com.google.common.collect.ReferenceMap;
+import com.google.common.collect.MapMaker;
/**
* Identical to the InMemoryStorageConfiguration except that it creates google
@@ -40,8 +40,8 @@
public void close() {}
public StorageEngine<ByteArray, byte[]> getStore(String name) {
- ReferenceMap<ByteArray, List<Versioned<byte[]>>> backingMap = new ReferenceMap<ByteArray, List<Versioned<byte[]>>>(ReferenceType.STRONG,
- ReferenceType.SOFT);
+ ConcurrentMap<ByteArray, List<Versioned<byte[]>>> backingMap = new MapMaker().softValues()
+ .makeMap();
return new InMemoryStorageEngine<ByteArray, byte[]>(name, backingMap);
}
@@ -48,11 +48,11 @@
import voldemort.utils.ByteUtils;
import voldemort.utils.ClosableIterator;
import voldemort.utils.Pair;
+import voldemort.utils.Utils;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Version;
import voldemort.versioning.Versioned;
-import com.google.common.base.Objects;
import com.google.common.collect.Lists;
/**
@@ -116,7 +116,7 @@ public RandomAccessFileStore(String name,
this.numBackups = numBackups;
this.indexFile = new File(storageDir, name + ".index");
this.dataFile = new File(storageDir, name + ".data");
- this.name = Objects.nonNull(name);
+ this.name = Utils.notNull(name);
this.fdWaitTimeoutMs = fdWaitTimeoutMs;
this.dataFiles = new ArrayBlockingQueue<RandomAccessFile>(numFileHandles);
this.indexFiles = new ArrayBlockingQueue<RandomAccessFile>(numFileHandles);
@@ -121,7 +121,7 @@ else if(!obj.getClass().equals(Slop.class))
Slop slop = (Slop) obj;
return operation == slop.getOperation() && Objects.equal(storeName, getStoreName())
- && key.equals(slop.getKey()) && Objects.deepEquals(value, slop.getValue())
+ && key.equals(slop.getKey()) && Utils.deepEquals(value, slop.getValue())
&& nodeId == slop.getNodeId() && Objects.equal(arrived, slop.getArrived());
}
@@ -18,15 +18,15 @@
import java.util.Iterator;
-import com.google.common.collect.AbstractIterable;
+import com.google.common.collect.Iterables;
/**
* An iterable that always produces the given iterator
*
* @author jay
*
*/
-public class DefaultIterable<V> extends AbstractIterable<V> {
+public class DefaultIterable<V> implements Iterable<V> {
private final Iterator<V> iterator;
@@ -38,4 +38,8 @@ public DefaultIterable(Iterator<V> iterator) {
return this.iterator;
}
+ @Override
+ public String toString() {
+ return Iterables.toString(this);
+ }
}
@@ -23,6 +23,8 @@
import java.util.Collections;
import java.util.List;
+import com.google.common.base.Nullable;
+
/**
* Helper functions FTW!
*
@@ -209,4 +211,107 @@ public static void assertNotNull(String s, Object... objects) {
return t;
}
+ /**
+ * Gets hash code of an object, optionally returns hash code based on the
+ * "deep contents" of array if the object is an array.
+ * <p>
+ * If {@code o} is null, 0 is returned; if {@code o} is an array, the
+ * corresponding {@link Arrays#deepHashCode(Object[])}, or
+ * {@link Arrays#hashCode(int[])} or the like is used to calculate the hash
+ * code.
+ */
+ public static int deepHashCode(@Nullable Object o) {
+ if(o == null) {
+ return 0;
+ }
+ if(!o.getClass().isArray()) {
+ return o.hashCode();
+ }
+ if(o instanceof Object[]) {
+ return Arrays.deepHashCode((Object[]) o);
+ }
+ if(o instanceof boolean[]) {
+ return Arrays.hashCode((boolean[]) o);
+ }
+ if(o instanceof char[]) {
+ return Arrays.hashCode((char[]) o);
+ }
+ if(o instanceof byte[]) {
+ return Arrays.hashCode((byte[]) o);
+ }
+ if(o instanceof short[]) {
+ return Arrays.hashCode((short[]) o);
+ }
+ if(o instanceof int[]) {
+ return Arrays.hashCode((int[]) o);
+ }
+ if(o instanceof long[]) {
+ return Arrays.hashCode((long[]) o);
+ }
+ if(o instanceof float[]) {
+ return Arrays.hashCode((float[]) o);
+ }
+ if(o instanceof double[]) {
+ return Arrays.hashCode((double[]) o);
+ }
+ throw new AssertionError();
+ }
+
+ /**
+ * Determines if two objects are equal as determined by
+ * {@link Object#equals(Object)}, or "deeply equal" if both are arrays.
+ * <p>
+ * If both objects are null, true is returned; if both objects are array,
+ * the corresponding {@link Arrays#deepEquals(Object[], Object[])}, or
+ * {@link Arrays#equals(int[], int[])} or the like are called to determine
+ * equality.
+ * <p>
+ * Note that this method does not "deeply" compare the fields of the
+ * objects.
+ */
+ public static boolean deepEquals(@Nullable Object o1, @Nullable Object o2) {
+ if(o1 == o2) {
+ return true;
+ }
+ if(o1 == null || o2 == null) {
+ return false;
+ }
+
+ Class<?> type1 = o1.getClass();
+ Class<?> type2 = o2.getClass();
+ if(!(type1.isArray() && type2.isArray())) {
+ return o1.equals(o2);
+ }
+ if(o1 instanceof Object[] && o2 instanceof Object[]) {
+ return Arrays.deepEquals((Object[]) o1, (Object[]) o2);
+ }
+ if(type1 != type2) {
+ return false;
+ }
+ if(o1 instanceof boolean[]) {
+ return Arrays.equals((boolean[]) o1, (boolean[]) o2);
+ }
+ if(o1 instanceof char[]) {
+ return Arrays.equals((char[]) o1, (char[]) o2);
+ }
+ if(o1 instanceof byte[]) {
+ return Arrays.equals((byte[]) o1, (byte[]) o2);
+ }
+ if(o1 instanceof short[]) {
+ return Arrays.equals((short[]) o1, (short[]) o2);
+ }
+ if(o1 instanceof int[]) {
+ return Arrays.equals((int[]) o1, (int[]) o2);
+ }
+ if(o1 instanceof long[]) {
+ return Arrays.equals((long[]) o1, (long[]) o2);
+ }
+ if(o1 instanceof float[]) {
+ return Arrays.equals((float[]) o1, (float[]) o2);
+ }
+ if(o1 instanceof double[]) {
+ return Arrays.equals((double[]) o1, (double[]) o2);
+ }
+ throw new AssertionError();
+ }
}
@@ -19,6 +19,8 @@
import java.io.Serializable;
import java.util.Comparator;
+import voldemort.utils.Utils;
+
import com.google.common.base.Objects;
/**
@@ -65,7 +67,7 @@ else if(!(object instanceof Versioned))
Versioned versioned = (Versioned) object;
return Objects.equal(getVersion(), versioned.getVersion())
- && Objects.deepEquals(getValue(), versioned.getValue());
+ && Utils.deepEquals(getValue(), versioned.getValue());
}
@Override
@@ -36,12 +36,12 @@
import voldemort.store.memory.InMemoryStorageEngine;
import voldemort.store.versioned.InconsistencyResolvingStore;
import voldemort.utils.ByteArray;
+import voldemort.utils.Utils;
import voldemort.versioning.Occured;
import voldemort.versioning.VectorClock;
import voldemort.versioning.VectorClockInconsistencyResolver;
import voldemort.versioning.Versioned;
-import com.google.common.base.Objects;
import com.google.common.collect.Maps;
/**
@@ -119,7 +119,7 @@ private int countOccurances(RoutedStore routedStore, ByteArray key, Versioned<by
int count = 0;
for(Store<ByteArray, byte[]> store: routedStore.getInnerStores().values())
try {
- if(store.get(key).size() > 0 && Objects.deepEquals(store.get(key).get(0), value))
+ if(store.get(key).size() > 0 && Utils.deepEquals(store.get(key).get(0), value))
count += 1;
} catch(VoldemortException e) {
// This is normal for the failing store...

0 comments on commit 35581b6

Please sign in to comment.