[Mulgara-svn] r2086 - in trunk/src/jar: query/java/org/mulgara/query tuples/java/org/mulgara/store/tuples
pag at mulgara.org
pag at mulgara.org
Thu Jan 5 19:10:13 UTC 2012
Author: pag
Date: 2012-01-05 19:10:12 +0000 (Thu, 05 Jan 2012)
New Revision: 2086
Modified:
trunk/src/jar/query/java/org/mulgara/query/AnswerOperations.java
trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
Log:
Added common functions to calculate hashcodes from large data sets
Modified: trunk/src/jar/query/java/org/mulgara/query/AnswerOperations.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/AnswerOperations.java 2012-01-05 19:09:13 UTC (rev 2085)
+++ trunk/src/jar/query/java/org/mulgara/query/AnswerOperations.java 2012-01-05 19:10:12 UTC (rev 2086)
@@ -151,4 +151,29 @@
}
}
}
+
+ public static int hashCode(Answer a) throws TuplesException {
+ // Clone the arguments to avoid interfering with their cursors
+ a = (Answer)a.clone();
+
+ // Check that columns match
+ int numberOfVariables = a.getNumberOfVariables();
+ int result = numberOfVariables;
+
+ try {
+ // Check that all row values match
+ a.beforeFirst();
+ while (a.next()) {
+ // Verify that the rows match
+ for (int i = 0; i < numberOfVariables; i++) {
+ Object o = a.getObject(i);
+ if (o != null) result ^= o.hashCode();
+ }
+ }
+ } finally {
+ // Close our clones
+ a.close();
+ }
+ return result;
+ }
}
Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java 2012-01-05 19:09:13 UTC (rev 2085)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java 2012-01-05 19:10:12 UTC (rev 2086)
@@ -1180,6 +1180,34 @@
return buff;
}
+ /**
+ * Calculates a consistent hash code for a tuples.
+ * @param t The tuples to get the hash code for.
+ * @return The hash code value.
+ */
+ public static int hashCode(Tuples t) {
+ t = (Tuples)t.clone();
+ int result = t.getVariables().hashCode();
+ try {
+ t.beforeFirst();
+ int cols = t.getNumberOfVariables();
+ while (t.next()) {
+ for (int i = 0; i < cols; i++) {
+ long val = t.getColumnValue(i);
+ result ^= (int)(val ^ (val >>> 32));
+ }
+ }
+ } catch (TuplesException e) {
+ throw new RuntimeException(e.toString(), e);
+ } finally {
+ try {
+ if (t != null) t.close();
+ } catch (TuplesException ex) {
+ throw new RuntimeException(ex.toString(), ex);
+ }
+ }
+ return result;
+ }
/**
* Find the list of variables which appear in both the lhs and rhs tuples.
More information about the Mulgara-svn
mailing list