You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@vxquery.apache.org by pr...@apache.org on 2014/02/17 19:05:48 UTC
[6/6] git commit: Converted a search method to a hashing method.
Small improvement.
Converted a search method to a hashing method. Small improvement.
Project: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/commit/8e304a70
Tree: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/tree/8e304a70
Diff: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/diff/8e304a70
Branch: refs/heads/prestonc/parser
Commit: 8e304a7013ed9509fe1a22b48dd2f5d79edd42fb
Parents: 9ca8e18
Author: Preston Carman <pr...@apache.org>
Authored: Mon Feb 17 08:55:02 2014 -0800
Committer: Preston Carman <pr...@apache.org>
Committed: Mon Feb 17 08:55:02 2014 -0800
----------------------------------------------------------------------
.../builders/nodes/DictionaryBuilder.java | 50 +++++++++++++++-----
1 file changed, 39 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-vxquery/blob/8e304a70/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/nodes/DictionaryBuilder.java
----------------------------------------------------------------------
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/nodes/DictionaryBuilder.java b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/nodes/DictionaryBuilder.java
index 20b7333..67a7ff8 100644
--- a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/nodes/DictionaryBuilder.java
+++ b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/nodes/DictionaryBuilder.java
@@ -19,6 +19,12 @@ package org.apache.vxquery.datamodel.builders.nodes;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import javax.swing.text.html.HTMLDocument.Iterator;
import org.apache.vxquery.util.GrowableIntArray;
@@ -44,6 +50,8 @@ public class DictionaryBuilder {
private final UTF8StringPointable tempStringPointable;
+ private final TreeMap<String, Integer> hashSlotIndexes;
+
private final IValueReferenceVector sortedStringsVector = new IValueReferenceVector() {
@Override
public int getStart(int index) {
@@ -77,13 +85,15 @@ public class DictionaryBuilder {
tempStringData = new ByteArrayAccessibleOutputStream();
tempOut = new DataOutputStream(tempStringData);
tempStringPointable = (UTF8StringPointable) UTF8StringPointable.FACTORY.createPointable();
+ hashSlotIndexes = new TreeMap<String, Integer>();
}
-
+
public void reset() {
stringEndOffsets.clear();
sortedSlotIndexes.clear();
dataBuffer.reset();
tempStringData.reset();
+ hashSlotIndexes.clear();
}
public void write(ArrayBackedValueStorage abvs) throws IOException {
@@ -96,23 +106,41 @@ public class DictionaryBuilder {
for (int i = 0; i < entryCount; ++i) {
out.writeInt(entryOffsets[i]);
}
- int[] sortedOffsets = sortedSlotIndexes.getArray();
- for (int i = 0; i < entryCount; ++i) {
- out.writeInt(sortedOffsets[i]);
+ if (hashSlotIndexes.isEmpty()) {
+ int[] sortedOffsets = sortedSlotIndexes.getArray();
+ for (int i = 0; i < entryCount; ++i) {
+ out.writeInt(sortedOffsets[i]);
+ }
+ } else {
+ // Get a set of the entries
+ Set set = hashSlotIndexes.entrySet();
+ // Get an iterator
+ java.util.Iterator it = set.iterator();
+ // Display elements
+ while (it.hasNext()) {
+ Map.Entry me = (Map.Entry) it.next();
+ out.writeInt((Integer) me.getValue());
+ }
+
}
out.write(dataBuffer.getByteArray(), 0, dataBuffer.size());
IntegerPointable.setInteger(abvs.getByteArray(), sizeOffset, abvs.getLength() - sizeOffset);
}
public int lookup(String str) {
- tempStringData.reset();
- try {
- tempOut.writeUTF(str);
- } catch (IOException e) {
- throw new IllegalStateException(e);
+ Integer slotIndex = hashSlotIndexes.get(str);
+ if (slotIndex == null) {
+ try {
+ dataBufferOut.writeUTF(str);
+ slotIndex = stringEndOffsets.getSize();
+ dataBufferOut.writeInt(slotIndex);
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ stringEndOffsets.append(dataBuffer.size());
+ hashSlotIndexes.put(str, slotIndex);
}
- tempStringPointable.set(tempStringData.getByteArray(), 0, tempStringData.size());
- return lookup(tempStringPointable);
+ return slotIndex;
}
public int lookup(UTF8StringPointable str) {