summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2010-08-09 14:45:12 -0700
committerShawn O. Pearce <sop@google.com>2010-08-09 14:45:12 -0700
commit9f22858b40f6b138ce5cd0f32f2200679f0baedd (patch)
treea0bac520b2ec01127e6065b20df9d3190e50ddda
parent43287dd36f973de13d1d1968a32f37b48320ba48 (diff)
downloadgwtorm-9f22858b40f6b138ce5cd0f32f2200679f0baedd.tar.gz
Use fetchRow for single get by key
Implementations of our NoSQL backend are likely to optimize the GenericSchema.fetchRow method, but might not optimize their GenericAccess.scanPrimaryKey routine. Redirect the single key lookup through the fetchRow method instead. Change-Id: If7b47c66077a2e24e6116f7f8035cf73ef260e6e Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gwtorm/nosql/generic/GenericAccess.java32
1 files changed, 8 insertions, 24 deletions
diff --git a/src/main/java/com/google/gwtorm/nosql/generic/GenericAccess.java b/src/main/java/com/google/gwtorm/nosql/generic/GenericAccess.java
index 524cb12..f82c76e 100644
--- a/src/main/java/com/google/gwtorm/nosql/generic/GenericAccess.java
+++ b/src/main/java/com/google/gwtorm/nosql/generic/GenericAccess.java
@@ -63,39 +63,23 @@ public abstract class GenericAccess<T, K extends Key<?>> extends
/**
* Lookup a single entity via its primary key.
- * <p>
- * The default implementation of this method performs a scan over the primary
- * key with {@link #scanPrimaryKey(byte[], byte[], int, boolean)}, '\0'
- * appended onto the fromKey and a result limit of 2.
- * <p>
- * If multiple records are discovered {@link OrmDuplicateKeyException} is
- * thrown back to the caller.
*
* @param key the primary key instance; must not be null.
* @return the entity; null if no entity has this key.
* @throws OrmException the data lookup failed.
- * @throws OrmDuplicateKeyException more than one row matched in the scan.
+ * @throws OrmDuplicateKeyException more than one row was identified in the
+ * key scan.
*/
@Override
public T get(K key) throws OrmException, OrmDuplicateKeyException {
- final IndexKeyBuilder dst = new IndexKeyBuilder();
- encodePrimaryKey(dst, key);
-
- final byte[] fromKey = dst.toByteArray();
-
- dst.nul();
- final byte[] toKey = dst.toByteArray();
-
- Iterator<T> r = scanPrimaryKey(fromKey, toKey, 2, false).iterator();
- if (!r.hasNext()) {
+ byte[] bin = db.fetchRow(dataRowKey(key));
+ if (bin != null) {
+ T obj = getObjectCodec().decode(bin);
+ cache().put(primaryKey(obj), bin);
+ return obj;
+ } else {
return null;
}
-
- T obj = r.next();
- if (r.hasNext()) {
- throw new OrmDuplicateKeyException("Duplicate " + getRelationName());
- }
- return obj;
}
/**