public void testMaxSize() { FastByIDMap<String> map = new FastByIDMap<String>(); map.Put(4, "bang"); Assert.AreEqual(1, map.Count()); map.Put(47L, "bang"); Assert.AreEqual(2, map.Count()); Assert.IsNull(map.Get(500000L)); map.Put(47L, "buzz"); Assert.AreEqual(2, map.Count()); Assert.AreEqual("buzz", map.Get(47L)); }
internal static IDataModel BuildModel(IList <UserItem> userItems, bool isReviewBased) { FastByIDMap <IList <IPreference> > userPreferencesMap = new FastByIDMap <IList <IPreference> >(); foreach (var userItem in userItems) { var userPreferences = userPreferencesMap.Get(userItem.UserId); if (userPreferences == null) { userPreferences = new List <IPreference>(3); userPreferencesMap.Put(userItem.UserId, userPreferences); } if (isReviewBased) { userPreferences.Add(new GenericPreference(userItem.UserId, userItem.ItemId, userItem.Rating)); } else { userPreferences.Add(new BooleanPreference(userItem.UserId, userItem.ItemId)); } } var resultUserPreferences = new FastByIDMap <IPreferenceArray>(userPreferencesMap.Count()); foreach (var entry in userPreferencesMap.EntrySet()) { var prefList = (List <IPreference>)entry.Value; resultUserPreferences.Put(entry.Key, isReviewBased ? new GenericUserPreferenceArray(prefList) : (IPreferenceArray) new BooleanUserPreferenceArray(prefList)); } return(new GenericDataModel(resultUserPreferences)); }
public void testClear() { FastByIDMap<long?> map = new FastByIDMap<long?>(); map.Put(500000L, 2L); map.Clear(); Assert.AreEqual(0, map.Count()); Assert.True(map.IsEmpty()); Assert.IsNull(map.Get(500000L)); }
protected int userIndex(long userID) { int?userIndex = userIDMapping.Get(userID); if (userIndex == null) { userIndex = userIDMapping.Count(); userIDMapping.Put(userID, userIndex); } return(userIndex.Value); }
protected int itemIndex(long itemID) { int?itemIndex = itemIDMapping.Get(itemID); if (itemIndex == null) { itemIndex = itemIDMapping.Count(); itemIDMapping.Put(itemID, itemIndex); } return(itemIndex.Value); }
/// <summary>Swaps, in-place, <see cref="IList<T>"/>s for arrays in map values.</summary> /// <returns>input value</returns> public static FastByIDMap <IPreferenceArray> ToDataMap(FastByIDMap <IList <IPreference> > data, bool byUser) { var newData = new FastByIDMap <IPreferenceArray>(data.Count()); foreach (var entry in data.EntrySet()) { var prefList = entry.Value; newData.Put(entry.Key, byUser ? (IPreferenceArray) new GenericUserPreferenceArray(prefList) : new GenericItemPreferenceArray(prefList)); } return(newData); }
public static FastByIDMap <FastIDSet> toDataMap(FastByIDMap <IPreferenceArray> data) { var res = new FastByIDMap <FastIDSet>(data.Count()); foreach (var entry in data.EntrySet()) { IPreferenceArray prefArray = entry.Value; int size = prefArray.Length(); FastIDSet itemIDs = new FastIDSet(size); for (int i = 0; i < size; i++) { itemIDs.Add(prefArray.GetItemID(i)); } res.Put(entry.Key, itemIDs); } return(res); }
/// <p> /// Creates a new {@link GenericDataModel} from the given users (and their preferences). This /// {@link DataModel} retains all this information in memory and is effectively immutable. /// </p> /// /// @param userData users to include /// @param timestamps optionally, provided timestamps of preferences as milliseconds since the epoch. /// User IDs are mapped to maps of item IDs to long timestamps. public GenericBooleanPrefDataModel(FastByIDMap <FastIDSet> userData, FastByIDMap <FastByIDMap <DateTime?> > timestamps) { //Preconditions.checkArgument(userData != null, "userData is null"); this.preferenceFromUsers = userData; this.preferenceForItems = new FastByIDMap <FastIDSet>(); FastIDSet itemIDSet = new FastIDSet(); foreach (var entry in preferenceFromUsers.EntrySet()) { long userID = entry.Key; FastIDSet itemIDs1 = entry.Value; itemIDSet.AddAll(itemIDs1); var it = itemIDs1.GetEnumerator(); while (it.MoveNext()) { long itemID = it.Current; FastIDSet userIDs1 = preferenceForItems.Get(itemID); if (userIDs1 == null) { userIDs1 = new FastIDSet(2); preferenceForItems.Put(itemID, userIDs1); } userIDs1.Add(userID); } } this.itemIDs = itemIDSet.ToArray(); itemIDSet = null; // Might help GC -- this is big Array.Sort(itemIDs); this.userIDs = new long[userData.Count()]; int i = 0; var it1 = userData.Keys.GetEnumerator(); while (it1.MoveNext()) { userIDs[i++] = it1.Current; } Array.Sort(userIDs); this.timestamps = timestamps; }
public IDataModel Load() { var hasPrefVal = !String.IsNullOrEmpty(PrefValFld); FastByIDMap <IList <IPreference> > data = new FastByIDMap <IList <IPreference> >(); using (var dbRdr = SelectCmd.ExecuteReader()) { while (dbRdr.Read()) { long userID = Convert.ToInt64(dbRdr[UserIdFld]); long itemID = Convert.ToInt64(dbRdr[ItemIdFld]); var userPrefs = data.Get(userID); if (userPrefs == null) { userPrefs = new List <IPreference>(3); data.Put(userID, userPrefs); } if (hasPrefVal) { var prefVal = Convert.ToSingle(dbRdr[PrefValFld]); userPrefs.Add(new GenericPreference(userID, itemID, prefVal)); } else { userPrefs.Add(new BooleanPreference(userID, itemID)); } } } var newData = new FastByIDMap <IPreferenceArray>(data.Count()); foreach (var entry in data.EntrySet()) { var prefList = (List <IPreference>)entry.Value; newData.Put(entry.Key, hasPrefVal ? (IPreferenceArray) new GenericUserPreferenceArray(prefList) : (IPreferenceArray) new BooleanUserPreferenceArray(prefList)); } return(new GenericDataModel(newData)); }
private IDataModel GetDataModel() { var cacheKey = "RecommenderDataModel"; IDataModel dataModel = _memoryCache.Get <IDataModel>(cacheKey); if (dataModel != null) { return(dataModel); } var movieRatings = _unitOfWork.MovieRatingRepository.GetAll(); FastByIDMap <IList <IPreference> > data = new FastByIDMap <IList <IPreference> >(); foreach (var movieRating in movieRatings) { var userPreferences = data.Get(movieRating.UserId); if (userPreferences == null) { userPreferences = new List <IPreference>(3); data.Put(movieRating.UserId, userPreferences); } userPreferences.Add(new BooleanPreference(movieRating.UserId, movieRating.MovieId)); } var newData = new FastByIDMap <IPreferenceArray>(data.Count()); foreach (var entry in data.EntrySet()) { var prefList = (List <IPreference>)entry.Value; newData.Put(entry.Key, (IPreferenceArray) new BooleanUserPreferenceArray(prefList)); } dataModel = new GenericDataModel(newData); _memoryCache.Set(cacheKey, dataModel); return(new GenericDataModel(newData)); }
/// <summary> /// Creates a new <see cref="GenericDataModel"/> from the given users (and their preferences). This /// <see cref="IDataModel"/> retains all this information in memory and is effectively immutable. /// </summary> /// <param name="userData">users to include; (see also <see cref="GenericDataModel.ToDataMap(FastByIDMap, bool)"/>)</param> /// <param name="timestamps">timestamps optionally, provided timestamps of preferences as milliseconds since the epoch. User IDs are mapped to maps of item IDs to long timestamps.</param> public GenericDataModel(FastByIDMap <IPreferenceArray> userData, FastByIDMap <FastByIDMap <DateTime?> > timestamps) { //Preconditions.checkArgument(userData != null, "userData is null"); this.preferenceFromUsers = userData; FastByIDMap <IList <IPreference> > prefsForItems = new FastByIDMap <IList <IPreference> >(); FastIDSet itemIDSet = new FastIDSet(); int currentCount = 0; float maxPrefValue = float.NegativeInfinity; float minPrefValue = float.PositiveInfinity; foreach (var entry in preferenceFromUsers.EntrySet()) { IPreferenceArray prefs = entry.Value; prefs.SortByItem(); foreach (IPreference preference in prefs) { long itemID = preference.GetItemID(); itemIDSet.Add(itemID); var prefsForItem = prefsForItems.Get(itemID); if (prefsForItem == null) { prefsForItem = new List <IPreference>(2); prefsForItems.Put(itemID, prefsForItem); } prefsForItem.Add(preference); float value = preference.GetValue(); if (value > maxPrefValue) { maxPrefValue = value; } if (value < minPrefValue) { minPrefValue = value; } } if (++currentCount % 10000 == 0) { log.Info("Processed {0} users", currentCount); } } log.Info("Processed {0} users", currentCount); setMinPreference(minPrefValue); setMaxPreference(maxPrefValue); this.itemIDs = itemIDSet.ToArray(); itemIDSet = null; // Might help GC -- this is big Array.Sort(itemIDs); this.preferenceForItems = ToDataMap(prefsForItems, false); foreach (var entry in preferenceForItems.EntrySet()) { entry.Value.SortByUser(); } this.userIDs = new long[userData.Count()]; int i = 0; foreach (var v in userData.Keys) { userIDs[i++] = v; } Array.Sort(userIDs); this.timestamps = timestamps; }
public int numUsers() { return(userIDMapping.Count()); }
public void testVersusHashMap() { FastByIDMap<String> actual = new FastByIDMap<String>(); IDictionary<long, string> expected = new Dictionary<long,string>(1000000); var r = RandomUtils.getRandom(); for (int i = 0; i < 1000000; i++) { double d = r.nextDouble(); long key = (long) r.nextInt(100); if (d < 0.4) { Assert.AreEqual( expected.ContainsKey(key)?expected[key]:null, actual.Get(key)); } else { if (d < 0.7) { var expectedOldVal = expected.ContainsKey(key) ? expected[key] : null; expected[key] = "bang"; Assert.AreEqual(expectedOldVal, actual.Put(key, "bang")); } else { var expectedOldVal = expected.ContainsKey(key) ? expected[key] : null; expected.Remove(key); Assert.AreEqual(expectedOldVal, actual.Remove(key)); } Assert.AreEqual(expected.Count, actual.Count()); Assert.AreEqual(expected.Count==0, actual.IsEmpty()); } } }
public void testSizeEmpty() { FastByIDMap<long> map = new FastByIDMap<long>(); Assert.AreEqual(0, map.Count()); Assert.True(map.IsEmpty()); map.Put(500000L, 2L); Assert.AreEqual(1, map.Count()); Assert.False(map.IsEmpty()); map.Remove(500000L); Assert.AreEqual(0, map.Count()); Assert.True(map.IsEmpty()); }
public int numItems() { return(itemIDMapping.Count()); }
/// <p> /// Creates a new {@link GenericDataModel} from the given users (and their preferences). This /// {@link DataModel} retains all this information in memory and is effectively immutable. /// </p> /// /// @param userData users to include /// @param timestamps optionally, provided timestamps of preferences as milliseconds since the epoch. /// User IDs are mapped to maps of item IDs to long timestamps. public GenericBooleanPrefDataModel(FastByIDMap<FastIDSet> userData, FastByIDMap<FastByIDMap<DateTime?>> timestamps) { //Preconditions.checkArgument(userData != null, "userData is null"); this.preferenceFromUsers = userData; this.preferenceForItems = new FastByIDMap<FastIDSet>(); FastIDSet itemIDSet = new FastIDSet(); foreach (var entry in preferenceFromUsers.EntrySet()) { long userID = entry.Key; FastIDSet itemIDs1 = entry.Value; itemIDSet.AddAll(itemIDs1); var it = itemIDs1.GetEnumerator(); while (it.MoveNext()) { long itemID = it.Current; FastIDSet userIDs1 = preferenceForItems.Get(itemID); if (userIDs1 == null) { userIDs1 = new FastIDSet(2); preferenceForItems.Put(itemID, userIDs1); } userIDs1.Add(userID); } } this.itemIDs = itemIDSet.ToArray(); itemIDSet = null; // Might help GC -- this is big Array.Sort(itemIDs); this.userIDs = new long[userData.Count()]; int i = 0; var it1 = userData.Keys.GetEnumerator(); while (it1.MoveNext()) { userIDs[i++] = it1.Current; } Array.Sort(userIDs); this.timestamps = timestamps; }
public static FastByIDMap<FastIDSet> toDataMap(FastByIDMap<IPreferenceArray> data) { var res = new FastByIDMap<FastIDSet>( data.Count() ); foreach (var entry in data.EntrySet()) { IPreferenceArray prefArray = entry.Value; int size = prefArray.Length(); FastIDSet itemIDs = new FastIDSet(size); for (int i = 0; i < size; i++) { itemIDs.Add(prefArray.GetItemID(i)); } res.Put( entry.Key, itemIDs ); } return res; }
/// <summary>Swaps, in-place, <see cref="IList<T>"/>s for arrays in map values.</summary> /// <returns>input value</returns> public static FastByIDMap<IPreferenceArray> ToDataMap(FastByIDMap<IList<IPreference>> data, bool byUser) { var newData = new FastByIDMap<IPreferenceArray>( data.Count() ); foreach (var entry in data.EntrySet()) { var prefList = entry.Value; newData.Put( entry.Key, byUser ? (IPreferenceArray) new GenericUserPreferenceArray(prefList) : new GenericItemPreferenceArray(prefList) ); } return newData; }
/// <summary> /// Creates a new <see cref="GenericDataModel"/> from the given users (and their preferences). This /// <see cref="IDataModel"/> retains all this information in memory and is effectively immutable. /// </summary> /// <param name="userData">users to include; (see also <see cref="GenericDataModel.ToDataMap(FastByIDMap, bool)"/>)</param> /// <param name="timestamps">timestamps optionally, provided timestamps of preferences as milliseconds since the epoch. User IDs are mapped to maps of item IDs to long timestamps.</param> public GenericDataModel(FastByIDMap<IPreferenceArray> userData, FastByIDMap<FastByIDMap<DateTime?>> timestamps) { //Preconditions.checkArgument(userData != null, "userData is null"); this.preferenceFromUsers = userData; FastByIDMap<IList<IPreference>> prefsForItems = new FastByIDMap<IList<IPreference>>(); FastIDSet itemIDSet = new FastIDSet(); int currentCount = 0; float maxPrefValue = float.NegativeInfinity; float minPrefValue = float.PositiveInfinity; foreach (var entry in preferenceFromUsers.EntrySet()) { IPreferenceArray prefs = entry.Value; prefs.SortByItem(); foreach (IPreference preference in prefs) { long itemID = preference.GetItemID(); itemIDSet.Add(itemID); var prefsForItem = prefsForItems.Get(itemID); if (prefsForItem == null) { prefsForItem = new List<IPreference>(2); prefsForItems.Put(itemID, prefsForItem); } prefsForItem.Add(preference); float value = preference.GetValue(); if (value > maxPrefValue) { maxPrefValue = value; } if (value < minPrefValue) { minPrefValue = value; } } if (++currentCount % 10000 == 0) { log.Info("Processed {0} users", currentCount); } } log.Info("Processed {0} users", currentCount); setMinPreference(minPrefValue); setMaxPreference(maxPrefValue); this.itemIDs = itemIDSet.ToArray(); itemIDSet = null; // Might help GC -- this is big Array.Sort(itemIDs); this.preferenceForItems = ToDataMap(prefsForItems, false); foreach (var entry in preferenceForItems.EntrySet()) { entry.Value.SortByUser(); } this.userIDs = new long[userData.Count()]; int i = 0; foreach (var v in userData.Keys) { userIDs[i++] = v; } Array.Sort(userIDs); this.timestamps = timestamps; }
public static FastByIDMap<IPreferenceArray> ToDataMap(FastByIDMap<IList<IPreference>> data,long userId,long itemId, bool byUser) { var newData = new FastByIDMap<IPreferenceArray>(data.Count()); preferenceFromUsersRemoved = new FastByIDMap<IPreference>(); foreach (var entry in data.EntrySet()) { var prefList = entry.Value; if (entry.Key==userId) { preferenceFromUsersRemoved.Put(userId, prefList.FirstOrDefault(i => i.GetItemID() == itemId)); prefList.Remove(prefList.FirstOrDefault(i => i.GetItemID() == itemId)); } newData.Put(entry.Key, byUser ? (IPreferenceArray)new GenericUserPreferenceArray(prefList) : new GenericItemPreferenceArray(prefList)); } return newData; }