Author: srowen
Date: Tue Oct 28 03:35:51 2008
New Revision: 708515

URL: http://svn.apache.org/viewvc?rev=708515&view=rev
Log:
Reduced Item allocation through some simple caching

Modified:
    
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java

Modified: 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
URL: 
http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java?rev=708515&r1=708514&r2=708515&view=diff
==============================================================================
--- 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
 (original)
+++ 
lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/model/file/FileDataModel.java
 Tue Oct 28 03:35:51 2008
@@ -54,7 +54,7 @@
  * The file will be periodically reloaded if a change is detected.</p>
  *
  * <p>It is possible and likely useful to subclass this class and customize 
its behavior to accommodate
- * application-specific needs and input formats. See [EMAIL PROTECTED] 
#processLine(String, Map)},
+ * application-specific needs and input formats. See [EMAIL PROTECTED] 
#processLine(String, Map, Map)},
  * [EMAIL PROTECTED] #buildItem(String)}, [EMAIL PROTECTED] #buildUser(String, 
List)}
  * and [EMAIL PROTECTED] #buildPreference(User, Item, double)}.</p>
  */
@@ -115,10 +115,11 @@
 
   private void processFile(Map<String, List<Preference>> data) {
     log.info("Reading file info...");
+    Map<String, Item> itemCache = new FastMap<String, Item>(1001);
     for (String line : new FileLineIterable(dataFile)) {
       if (line.length() > 0) {
         log.debug("Read line: {}", line);
-        processLine(line, data);
+        processLine(line, data, itemCache);
       }
     }
   }
@@ -140,7 +141,7 @@
    * @see #buildPreference(User, Item, double)
    * @see #buildItem(String)
    */
-  protected void processLine(String line, Map<String, List<Preference>> data) {
+  protected void processLine(String line, Map<String, List<Preference>> data, 
Map<String, Item> itemCache) {
     int commaOne = line.indexOf((int) ',');
     int commaTwo = line.indexOf((int) ',', commaOne + 1);
     if (commaOne < 0 || commaTwo < 0) {
@@ -154,8 +155,12 @@
       prefs = new ArrayList<Preference>();
       data.put(userID, prefs);
     }
-    Item item = buildItem(itemID);
-      log.debug("Read item '{}' for user ID '{}'", item, userID);
+    Item item = itemCache.get(itemID);
+    if (item == null) {
+      item = buildItem(itemID);
+      itemCache.put(itemID, item);
+    }
+    log.debug("Read item '{}' for user ID '{}'", item, userID);
     prefs.add(buildPreference(null, item, preferenceValue));
   }
 


Reply via email to