Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleaned up data model. Now store features/categories as objects rathe…

…r than as strings, saves memory.
  • Loading branch information...
commit 0732297b03e41d2ea86a5e2ddfc8053e46006ab8 1 parent 687d6ad
C. Stucchio authored
32 src/org/styloot/hobo/Category.java
View
@@ -0,0 +1,32 @@
+package org.styloot.hobo;
+
+import java.util.*;
+
+public class Category implements Comparable<Category> {
+ public final String name;
+
+ private Category(String nm) {
+ name = nm;
+ }
+
+ public static Category getCategory(String nm) {
+ if (!categories.containsKey(nm)) {
+ categories.put(nm, new Category(nm));
+ }
+ return categories.get(nm);
+ }
+
+ public String toString() {
+ return name;
+ }
+
+ public int compareTo(Category o) {
+ return name.compareTo(o.name);
+ }
+
+ public boolean equals(Object o) {
+ return ((Category)o).name.equals(name);
+ }
+
+ private static Map<String,Category> categories = new HashMap<String,Category>();
+}
52 src/org/styloot/hobo/Feature.java
View
@@ -0,0 +1,52 @@
+package org.styloot.hobo;
+
+import java.util.*;
+
+public class Feature implements Comparable<Feature> {
+ public final String name;
+
+ private Feature(String nm) {
+ name = nm;
+ }
+
+ public static Feature getFeature(String nm) {
+ if (!features.containsKey(nm)) {
+ features.put(nm, new Feature(nm));
+ }
+ return features.get(nm);
+ }
+
+ public static Feature[] getFeatures(String[] features) {
+ if (features == null)
+ return null;
+ Feature[] result = new Feature[features.length];
+ for (int i=0;i<features.length;i++) {
+ result[i] = getFeature(features[i]);
+ }
+ return result;
+ }
+
+ public static Feature[] getFeatures(Collection<String> features) {
+ if (features == null)
+ return null;
+ return getFeatures(features.toArray(new String[0]));
+ }
+
+ public String toString() {
+ return name;
+ }
+
+ public int compareTo(Feature o) {
+ return name.compareTo(o.name);
+ }
+
+ public boolean equals(Object o) {
+ return ((Feature)o).name.equals(name);
+ }
+
+ public static int count() {
+ return features.size();
+ }
+
+ private static Map<String,Feature> features = new HashMap<String,Feature>();
+}
37 src/org/styloot/hobo/FeatureRegistry.java
View
@@ -1,37 +0,0 @@
-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();
- }
-}
6 src/org/styloot/hobo/HoboIndex.java
View
@@ -53,10 +53,10 @@ public HoboIndex(Iterator<Item> items) {
for (Iterator<Item> iter = items; iter.hasNext();) {
Item item = iter.next();
- if (!result.containsKey(item.category)) {
- result.put(item.category, new LinkedList<Item>());
+ if (!result.containsKey(item.category.name)) {
+ result.put(item.category.name, new LinkedList<Item>());
}
- result.get(item.category).add(item);
+ result.get(item.category.name).add(item);
}
return result;
}
24 src/org/styloot/hobo/Item.java
View
@@ -9,13 +9,13 @@
public class Item implements Comparable<Item>{
public Item(String i, String c, Collection<String> f, int q) {
- id = i; category = c; quality = q;
- features = FeatureRegistry.featureId(f.toArray(new String[0]));
+ id = i; category = Category.getCategory(c); quality = q;
+ features = Feature.getFeatures(f);
}
public Item(String i, String c, String[] f, int q) {
- id = i; category = c; quality = q;
- features = FeatureRegistry.featureId(f);
+ id = i; category = Category.getCategory(c); quality = q;
+ features = Feature.getFeatures(f);
}
public boolean hasFeatures(Collection<String> feats) {
@@ -23,19 +23,19 @@ public boolean hasFeatures(Collection<String> feats) {
if (feats == null) {
return true;
}
- return hasFeatures(FeatureRegistry.featureId(feats));
+ return hasFeatures(Feature.getFeatures(feats));
}
- public boolean hasFeatures(int[] featIds) {
- if (featIds == null) {
+ public boolean hasFeatures(Feature[] feats) {
+ if (feats == null) {
return true;
}
- if (features == null && featIds.length > 0) { //We definitely don't have the feature
+ if (features == null && feats.length > 0) { //We definitely don't have the feature
return false;
}
- for (int f : featIds) {
+ for (Feature f : feats) {
boolean found = false;
- for (int f2 : features) {
+ for (Feature f2 : features) {
if (f == f2) {
found = true;
break;
@@ -49,8 +49,8 @@ public boolean hasFeatures(int[] featIds) {
}
public final String id;
- public final String category;
- public final int[] features;
+ public final Category category;
+ public final Feature[] features;
public final int quality;
public int compareTo(Item o) {
6 src/org/styloot/hobo/iterators/FeaturesFilterIterator.java
View
@@ -8,11 +8,11 @@
public class FeaturesFilterIterator extends FilterIterator {
public FeaturesFilterIterator(Iterator<Item> iter, Collection<String> f) {
super(iter);
- featureIds = FeatureRegistry.featureId(f);
+ features = Feature.getFeatures(f);
}
- private int[] featureIds;
+ private Feature[] features;
public boolean predicate(Item item) {
- return item.hasFeatures(featureIds);
+ return item.hasFeatures(features);
}
}
6 src/org/styloot/hobo/thriftserver/Server.java
View
@@ -46,9 +46,11 @@ private void start(){
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])));
+ FileReader infile = new FileReader(args[0]);
+ Collection<Item> items = itemsFromInputStream(new BufferedReader(infile));
+ infile.close();
log.info("Loaded " + items.size() + " items.");
- log.info("Items are indexed with " + FeatureRegistry.size() + " features.");
+ log.info("Items are indexed with " + Feature.count() + " features.");
HoboIndex index = new HoboIndex(items);
log.info("Build HoboIndex.");
Please sign in to comment.
Something went wrong with that request. Please try again.