table: reduce implicit digest computation in ContentStore

refs #1706

Change-Id: Ic6c4c25f2ef722b9ee37e22362ad57a434bbf821
diff --git a/daemon/table/cs-entry-impl.cpp b/daemon/table/cs-entry-impl.cpp
index c97d692..9e1c1cd 100644
--- a/daemon/table/cs-entry-impl.cpp
+++ b/daemon/table/cs-entry-impl.cpp
@@ -61,6 +61,38 @@
   this->setData(this->getData(), false);
 }
 
+int
+compareQueryWithData(const Name& queryName, const Data& data)
+{
+  bool queryIsFullName = !queryName.empty() && queryName[-1].isImplicitSha256Digest();
+
+  int cmp = queryIsFullName ?
+            queryName.compare(0, queryName.size() - 1, data.getName()) :
+            queryName.compare(data.getName());
+
+  if (cmp != 0) { // Name without digest differs
+    return cmp;
+  }
+
+  if (queryIsFullName) { // Name without digest equals, compare digest
+    return queryName[-1].compare(data.getFullName()[-1]);
+  }
+  else { // queryName is a proper prefix of Data fullName
+    return -1;
+  }
+}
+
+int
+compareDataWithData(const Data& lhs, const Data& rhs)
+{
+  int cmp = lhs.getName().compare(rhs.getName());
+  if (cmp != 0) {
+    return cmp;
+  }
+
+  return lhs.getFullName()[-1].compare(rhs.getFullName()[-1]);
+}
+
 bool
 EntryImpl::operator<(const EntryImpl& other) const
 {
@@ -69,15 +101,15 @@
       return m_queryName < other.m_queryName;
     }
     else {
-      return m_queryName < other.m_queryName;
+      return compareQueryWithData(m_queryName, other.getData()) < 0;
     }
   }
   else {
     if (other.isQuery()) {
-      return this->getFullName() < other.m_queryName;
+      return compareQueryWithData(other.m_queryName, this->getData()) > 0;
     }
     else {
-      return this->getFullName() < other.getFullName();
+      return compareDataWithData(this->getData(), other.getData()) < 0;
     }
   }
 }