Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Created FeatureRegistry, which avoids storing duplicated features.

  • Loading branch information...
commit 687d6ad7e4759872424afe6482104c7518cb5be2 1 parent 7860ea3
@stucchio authored
View
37 src/org/styloot/hobo/FeatureRegistry.java
@@ -0,0 +1,37 @@
+package org.styloot.hobo;
+
+import java.util.*;
+
+public class FeatureRegistry {
+ private static Map<String,Integer> featureIds = new HashMap<String,Integer>();
+ private static int count = 0;
+
+ public static int featureId(String feature) {
+ if (featureIds.containsKey(feature)) {
+ return featureIds.get(feature);
+ } else {
+ featureIds.put(feature, count);
+ count += 1;
+ return (count - 1);
+ }
+ }
+
+ public static int[] featureId(String[] features) {
+ if (features == null) {
+ return null;
+ }
+ int[] result = new int[features.length];
+ for (int i=0;i<features.length;i++) {
+ result[i] = featureId(features[i]);
+ }
+ return result;
+ }
+
+ public static int[] featureId(Collection<String> features) {
+ return featureId(features.toArray(new String[0]));
+ }
+
+ public static int size() {
+ return featureIds.size();
+ }
+}
View
30 src/org/styloot/hobo/Item.java
@@ -1,26 +1,42 @@
package org.styloot.hobo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.util.*;
+import org.styloot.hobo.*;
+
public class Item implements Comparable<Item>{
public Item(String i, String c, Collection<String> f, int q) {
id = i; category = c; quality = q;
- features = f.toArray(new String[0]);
+ features = FeatureRegistry.featureId(f.toArray(new String[0]));
}
public Item(String i, String c, String[] f, int q) {
id = i; category = c; quality = q;
- features = f;
+ features = FeatureRegistry.featureId(f);
}
public boolean hasFeatures(Collection<String> feats) {
+ log.warn("Calling item.hasFeatures(Collection<String> features) - will be innefficient.");
if (feats == null) {
return true;
}
- for (String f : feats) {
+ return hasFeatures(FeatureRegistry.featureId(feats));
+ }
+
+ public boolean hasFeatures(int[] featIds) {
+ if (featIds == null) {
+ return true;
+ }
+ if (features == null && featIds.length > 0) { //We definitely don't have the feature
+ return false;
+ }
+ for (int f : featIds) {
boolean found = false;
- for (String f2 : features) {
- if (f.equals(f2)) {
+ for (int f2 : features) {
+ if (f == f2) {
found = true;
break;
}
@@ -34,7 +50,7 @@ public boolean hasFeatures(Collection<String> feats) {
public final String id;
public final String category;
- public final String[] features;
+ public final int[] features;
public final int quality;
public int compareTo(Item o) {
@@ -44,6 +60,8 @@ public int compareTo(Item o) {
return id.compareTo(o.id);
}
+ private static final Logger log = LoggerFactory.getLogger(Item.class);
+
public static void main(String[] args) {
Vector<String> f = new Vector<String>();
f.add("foo");
View
6 src/org/styloot/hobo/iterators/FeaturesFilterIterator.java
@@ -8,11 +8,11 @@
public class FeaturesFilterIterator extends FilterIterator {
public FeaturesFilterIterator(Iterator<Item> iter, Collection<String> f) {
super(iter);
- features = f;
+ featureIds = FeatureRegistry.featureId(f);
}
- Collection<String> features;
+ private int[] featureIds;
public boolean predicate(Item item) {
- return item.hasFeatures(features);
+ return item.hasFeatures(featureIds);
}
}
View
1  src/org/styloot/hobo/thriftserver/Server.java
@@ -48,6 +48,7 @@ public static void main(String args[]) throws IOException {
log.info("Loading list of items from " + args[0]);
Collection<Item> items = itemsFromInputStream(new BufferedReader(new FileReader(args[0])));
log.info("Loaded " + items.size() + " items.");
+ log.info("Items are indexed with " + FeatureRegistry.size() + " features.");
HoboIndex index = new HoboIndex(items);
log.info("Build HoboIndex.");
Please sign in to comment.
Something went wrong with that request. Please try again.