org.apache.mahout.cf.taste.impl.model
Class PlusAnonymousConcurrentUserDataModel

java.lang.Object
  extended by org.apache.mahout.cf.taste.impl.model.PlusAnonymousUserDataModel
      extended by org.apache.mahout.cf.taste.impl.model.PlusAnonymousConcurrentUserDataModel
All Implemented Interfaces:
Serializable, Refreshable, DataModel

public final class PlusAnonymousConcurrentUserDataModel
extends PlusAnonymousUserDataModel

This is a special thread-safe version of PlusAnonymousUserDataModel which allow multiple concurrent anonymous requests.

To use it, you have to estimate the number of concurrent anonymous users of your application. The pool of users with the given size will be created. For each anonymous recommendations request, a user has to be taken from the pool and returned back immediately afterwards.

If no more users are available in the pool, anonymous recommendations cannot be produced.

Setup:
 int concurrentUsers = 100;
 DataModel realModel = ..
 PlusAnonymousConcurrentUserDataModel plusModel =
   new PlusAnonymousConcurrentUserDataModel(realModel, concurrentUsers);
 Recommender recommender = ...;
 
Real-time recommendation:
 PlusAnonymousConcurrentUserDataModel plusModel =
   (PlusAnonymousConcurrentUserDataModel) recommender.getDataModel();

 // Take the next available anonymous user from the pool
 Long anonymousUserID = plusModel.takeAvailableUser();

 PreferenceArray tempPrefs = ..
 tempPrefs.setUserID(0, anonymousUserID);
 tempPrefs.setItemID(0, itemID);
 plusModel.setTempPrefs(tempPrefs, anonymousUserID);

 // Produce recommendations
 recommender.recommend(anonymousUserID, howMany);

 // It is very IMPORTANT to release user back to the pool
 plusModel.releaseUser(anonymousUserID);
 

See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.apache.mahout.cf.taste.impl.model.PlusAnonymousUserDataModel
TEMP_USER_ID
 
Constructor Summary
PlusAnonymousConcurrentUserDataModel(DataModel delegate, int maxConcurrentUsers)
           
 
Method Summary
 void clearTempPrefs(long anonymousUserID)
          Clears temporary preferences for a given anonymous user.
 FastIDSet getItemIDsFromUser(long userID)
           
 int getNumUsers()
           
 int getNumUsersWithPreferenceFor(long itemID)
           
 int getNumUsersWithPreferenceFor(long itemID1, long itemID2)
           
 PreferenceArray getPreferencesForItem(long itemID)
           
 PreferenceArray getPreferencesFromUser(long userID)
           
 Long getPreferenceTime(long userID, long itemID)
          Retrieves the time at which a preference value from a user and item was set, if known.
 Float getPreferenceValue(long userID, long itemID)
          Retrieves the preference value for a single user and item.
 LongPrimitiveIterator getUserIDs()
           
 boolean releaseUser(Long userID)
          Release previously taken anonymous user and return it to the pool.
 void removePreference(long userID, long itemID)
           Removes a particular preference for a user.
 void setPreference(long userID, long itemID, float value)
           Sets a particular preference (item plus rating) for a user.
 void setTempPrefs(PreferenceArray prefs, long anonymousUserID)
          Sets temporary preferences for a given anonymous user.
 Long takeAvailableUser()
          Take the next available concurrent anonymous users from the pool.
 
Methods inherited from class org.apache.mahout.cf.taste.impl.model.PlusAnonymousUserDataModel
clearTempPrefs, getDelegate, getItemIDs, getMaxPreference, getMinPreference, getNumItems, hasPreferenceValues, refresh, setTempPrefs
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PlusAnonymousConcurrentUserDataModel

public PlusAnonymousConcurrentUserDataModel(DataModel delegate,
                                            int maxConcurrentUsers)
Parameters:
delegate - Real model where anonymous users will be added to
maxConcurrentUsers - Maximum allowed number of concurrent anonymous users
Method Detail

takeAvailableUser

public Long takeAvailableUser()
Take the next available concurrent anonymous users from the pool.

Returns:
User ID or null if no more users are available

releaseUser

public boolean releaseUser(Long userID)
Release previously taken anonymous user and return it to the pool.

Parameters:
userID - ID of a previously taken anonymous user
Returns:
true if the user was previously taken, false otherwise

setTempPrefs

public void setTempPrefs(PreferenceArray prefs,
                         long anonymousUserID)
Sets temporary preferences for a given anonymous user.


clearTempPrefs

public void clearTempPrefs(long anonymousUserID)
Clears temporary preferences for a given anonymous user.


getUserIDs

public LongPrimitiveIterator getUserIDs()
                                 throws TasteException
Specified by:
getUserIDs in interface DataModel
Overrides:
getUserIDs in class PlusAnonymousUserDataModel
Returns:
all user IDs in the model, in order
Throws:
TasteException - if an error occurs while accessing the data

getPreferencesFromUser

public PreferenceArray getPreferencesFromUser(long userID)
                                       throws TasteException
Specified by:
getPreferencesFromUser in interface DataModel
Overrides:
getPreferencesFromUser in class PlusAnonymousUserDataModel
Parameters:
userID - ID of user to get prefs for
Returns:
user's preferences, ordered by item ID
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getItemIDsFromUser

public FastIDSet getItemIDsFromUser(long userID)
                             throws TasteException
Specified by:
getItemIDsFromUser in interface DataModel
Overrides:
getItemIDsFromUser in class PlusAnonymousUserDataModel
Parameters:
userID - ID of user to get prefs for
Returns:
IDs of items user expresses a preference for
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getPreferencesForItem

public PreferenceArray getPreferencesForItem(long itemID)
                                      throws TasteException
Specified by:
getPreferencesForItem in interface DataModel
Overrides:
getPreferencesForItem in class PlusAnonymousUserDataModel
Parameters:
itemID - item ID
Returns:
all existing Preferences expressed for that item, ordered by user ID, as an array
Throws:
NoSuchItemException - if the item does not exist
TasteException - if an error occurs while accessing the data

getPreferenceValue

public Float getPreferenceValue(long userID,
                                long itemID)
                         throws TasteException
Description copied from interface: DataModel
Retrieves the preference value for a single user and item.

Specified by:
getPreferenceValue in interface DataModel
Overrides:
getPreferenceValue in class PlusAnonymousUserDataModel
Parameters:
userID - user ID to get pref value from
itemID - item ID to get pref value for
Returns:
preference value from the given user for the given item or null if none exists
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getPreferenceTime

public Long getPreferenceTime(long userID,
                              long itemID)
                       throws TasteException
Description copied from interface: DataModel
Retrieves the time at which a preference value from a user and item was set, if known. Time is expressed in the usual way, as a number of milliseconds since the epoch.

Specified by:
getPreferenceTime in interface DataModel
Overrides:
getPreferenceTime in class PlusAnonymousUserDataModel
Parameters:
userID - user ID for preference in question
itemID - item ID for preference in question
Returns:
time at which preference was set or null if no preference exists or its time is not known
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getNumUsers

public int getNumUsers()
                throws TasteException
Specified by:
getNumUsers in interface DataModel
Overrides:
getNumUsers in class PlusAnonymousUserDataModel
Returns:
total number of users known to the model.
Throws:
TasteException - if an error occurs while accessing the data

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long itemID)
                                 throws TasteException
Specified by:
getNumUsersWithPreferenceFor in interface DataModel
Overrides:
getNumUsersWithPreferenceFor in class PlusAnonymousUserDataModel
Parameters:
itemID - item ID to check for
Returns:
the number of users who have expressed a preference for the item
Throws:
TasteException - if an error occurs while accessing the data

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long itemID1,
                                        long itemID2)
                                 throws TasteException
Specified by:
getNumUsersWithPreferenceFor in interface DataModel
Overrides:
getNumUsersWithPreferenceFor in class PlusAnonymousUserDataModel
Parameters:
itemID1 - first item ID to check for
itemID2 - second item ID to check for
Returns:
the number of users who have expressed a preference for the items
Throws:
TasteException - if an error occurs while accessing the data

setPreference

public void setPreference(long userID,
                          long itemID,
                          float value)
                   throws TasteException
Description copied from interface: DataModel

Sets a particular preference (item plus rating) for a user.

Specified by:
setPreference in interface DataModel
Overrides:
setPreference in class PlusAnonymousUserDataModel
Parameters:
userID - user to set preference for
itemID - item to set preference for
value - preference value
Throws:
NoSuchItemException - if the item does not exist
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

removePreference

public void removePreference(long userID,
                             long itemID)
                      throws TasteException
Description copied from interface: DataModel

Removes a particular preference for a user.

Specified by:
removePreference in interface DataModel
Overrides:
removePreference in class PlusAnonymousUserDataModel
Parameters:
userID - user from which to remove preference
itemID - item to remove preference for
Throws:
NoSuchItemException - if the item does not exist
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.