org.grouplens.lenskit.data.snapshot

## Interface PreferenceSnapshot

• All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
AbstractPreferenceSnapshot, PackedPreferenceSnapshot

@ThreadSafe
@DefaultImplementation(value=PackedPreferenceSnapshot.class)
public interface PreferenceSnapshot
extends Closeable
Snapshot of the ratings data for building a recommender.

The recommender build process often needs to take multiple passes over the rating data. In a live system, the data provided by a EventDAO may change between iterations. Therefore, we introduce build contexts — snapshots of the rating data at a particular point in time that can be iterated as many times as necessary to build the recommender.

Implementers have a variety of options for implementing build contexts. They can be in-memory snapshots, database transactions, database clones, or even disk files. Recommender build code does assume, however, that multiple iterations is pretty fast. Therefore, implementations should avoid re-fetching the data over a network connection for each request.

An additional feature provided by build contexts is that of mapping the item and user IDs to consecutive, 0-based indices. The indices may differ from one build context to another.

• ### Method Summary

All Methods
Modifier and Type Method and Description
void close()
Close the build context.
LongCollection getItemIds()
Get the set of item IDs in the snapshot.
FastCollection<IndexedPreference> getRatings()
Get the collection of ratings in the snapshot.
LongCollection getUserIds()
Get the set of user IDs in the snapshot.
FastCollection<IndexedPreference> getUserRatings(long userId)
Get the ratings for a particular user.
IdIndexMapping itemIndex()
Get the item ID index.
IdIndexMapping userIndex()
Get the user ID index.
SparseVector userRatingVector(long userId)
Get the current preferences of a particular user in SparseVector form.
• ### Method Detail

• #### getUserIds

LongCollection getUserIds()
Get the set of user IDs in the snapshot.
Returns:
A set of all known user IDs.
• #### getItemIds

LongCollection getItemIds()
Get the set of item IDs in the snapshot.
Returns:
A set of all known item IDs.
• #### userIndex

IdIndexMapping userIndex()
Get the user ID index.
Returns:
The index mapping between user IDs and user indices.
• #### itemIndex

IdIndexMapping itemIndex()
Get the item ID index.
Returns:
The index mapping between user IDs and user indices.
• #### getRatings

FastCollection<IndexedPreference> getRatings()
Get the collection of ratings in the snapshot. The ratings are returned in an undetermined order. It is guaranteed that no duplicate ratings appear - each (user,item) pair is rated at most once. Each preference's index is also in the range [0,len), where len is the size of this collection.

Modifying the returned indexed preferences will not modify the underlying snapshot.

Returns:
All ratings in the system.
• #### getUserRatings

FastCollection<IndexedPreference> getUserRatings(long userId)
Get the ratings for a particular user. It is guaranteed that no duplicate ratings appear - each (user,item) pair is rated at most once.

Modifying the returned indexed preferences will not modify the underlying snapshot.

Parameters:
userId - The user's ID.
Returns:
The user's ratings, or an empty collection if the user is unknown.
• #### userRatingVector

SparseVector userRatingVector(long userId)
Get the current preferences of a particular user in SparseVector form.
Parameters:
userId - The user's ID.
Returns:
The user's rating vector.
• #### close

void close()
Close the build context. This overrides Closeable.close() to drop the exception that can be thrown.

After the build context has been closed, all methods are allowed to fail. Objects returned from those methods, however, should continue to be valid.

Specified by:
close in interface AutoCloseable
Specified by:
close in interface Closeable