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>
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 @@
 
   /**
    * 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;
   }
 
   /**