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);
TEMP_USER_ID
Constructor and Description |
---|
PlusAnonymousConcurrentUserDataModel(DataModel delegate,
int maxConcurrentUsers) |
Modifier and Type | Method and Description |
---|---|
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.
|
clearTempPrefs, getDelegate, getItemIDs, getMaxPreference, getMinPreference, getNumItems, hasPreferenceValues, refresh, setTempPrefs
public PlusAnonymousConcurrentUserDataModel(DataModel delegate, int maxConcurrentUsers)
delegate
- Real model where anonymous users will be added tomaxConcurrentUsers
- Maximum allowed number of concurrent anonymous userspublic Long takeAvailableUser()
public boolean releaseUser(Long userID)
userID
- ID of a previously taken anonymous userpublic void setTempPrefs(PreferenceArray prefs, long anonymousUserID)
public void clearTempPrefs(long anonymousUserID)
public LongPrimitiveIterator getUserIDs() throws TasteException
getUserIDs
in interface DataModel
getUserIDs
in class PlusAnonymousUserDataModel
TasteException
- if an error occurs while accessing the datapublic PreferenceArray getPreferencesFromUser(long userID) throws TasteException
getPreferencesFromUser
in interface DataModel
getPreferencesFromUser
in class PlusAnonymousUserDataModel
userID
- ID of user to get prefs forNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic FastIDSet getItemIDsFromUser(long userID) throws TasteException
getItemIDsFromUser
in interface DataModel
getItemIDsFromUser
in class PlusAnonymousUserDataModel
userID
- ID of user to get prefs forNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic PreferenceArray getPreferencesForItem(long itemID) throws TasteException
getPreferencesForItem
in interface DataModel
getPreferencesForItem
in class PlusAnonymousUserDataModel
itemID
- item IDPreference
s expressed for that item, ordered by user ID, as an arrayNoSuchItemException
- if the item does not existTasteException
- if an error occurs while accessing the datapublic Float getPreferenceValue(long userID, long itemID) throws TasteException
DataModel
getPreferenceValue
in interface DataModel
getPreferenceValue
in class PlusAnonymousUserDataModel
userID
- user ID to get pref value fromitemID
- item ID to get pref value forNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic Long getPreferenceTime(long userID, long itemID) throws TasteException
DataModel
getPreferenceTime
in interface DataModel
getPreferenceTime
in class PlusAnonymousUserDataModel
userID
- user ID for preference in questionitemID
- item ID for preference in questionNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic int getNumUsers() throws TasteException
getNumUsers
in interface DataModel
getNumUsers
in class PlusAnonymousUserDataModel
TasteException
- if an error occurs while accessing the datapublic int getNumUsersWithPreferenceFor(long itemID) throws TasteException
getNumUsersWithPreferenceFor
in interface DataModel
getNumUsersWithPreferenceFor
in class PlusAnonymousUserDataModel
itemID
- item ID to check forTasteException
- if an error occurs while accessing the datapublic int getNumUsersWithPreferenceFor(long itemID1, long itemID2) throws TasteException
getNumUsersWithPreferenceFor
in interface DataModel
getNumUsersWithPreferenceFor
in class PlusAnonymousUserDataModel
itemID1
- first item ID to check foritemID2
- second item ID to check forTasteException
- if an error occurs while accessing the datapublic void setPreference(long userID, long itemID, float value) throws TasteException
DataModel
Sets a particular preference (item plus rating) for a user.
setPreference
in interface DataModel
setPreference
in class PlusAnonymousUserDataModel
userID
- user to set preference foritemID
- item to set preference forvalue
- preference valueNoSuchItemException
- if the item does not existNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the datapublic void removePreference(long userID, long itemID) throws TasteException
DataModel
Removes a particular preference for a user.
removePreference
in interface DataModel
removePreference
in class PlusAnonymousUserDataModel
userID
- user from which to remove preferenceitemID
- item to remove preference forNoSuchItemException
- if the item does not existNoSuchUserException
- if the user does not existTasteException
- if an error occurs while accessing the dataCopyright © 2008–2015 The Apache Software Foundation. All rights reserved.