org.codelibs.elasticsearch.taste.model.GenericBooleanPrefDataModel Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codelibs.elasticsearch.taste.model;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import org.codelibs.elasticsearch.taste.common.FastByIDMap;
import org.codelibs.elasticsearch.taste.common.FastIDSet;
import org.codelibs.elasticsearch.taste.common.LongPrimitiveArrayIterator;
import org.codelibs.elasticsearch.taste.common.LongPrimitiveIterator;
import org.codelibs.elasticsearch.taste.common.Refreshable;
import org.codelibs.elasticsearch.taste.exception.NoSuchItemException;
import org.codelibs.elasticsearch.taste.exception.NoSuchUserException;
import com.google.common.base.Preconditions;
/**
*
* A simple {@link DataModel} which uses given user data as its data source. This implementation
* is mostly useful for small experiments and is not recommended for contexts where performance is important.
*
*/
public final class GenericBooleanPrefDataModel extends AbstractDataModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private final long[] userIDs;
private final FastByIDMap preferenceFromUsers;
private final long[] itemIDs;
private final FastByIDMap preferenceForItems;
private final FastByIDMap> timestamps;
/**
*
* 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.
*
*
* @param userData users to include
*/
public GenericBooleanPrefDataModel(final FastByIDMap userData) {
this(userData, null);
}
/**
*
* 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.
*
*
* @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(final FastByIDMap userData,
final FastByIDMap> timestamps) {
Preconditions.checkArgument(userData != null, "userData is null");
preferenceFromUsers = userData;
preferenceForItems = new FastByIDMap();
FastIDSet itemIDSet = new FastIDSet();
for (final Map.Entry entry : preferenceFromUsers
.entrySet()) {
final long userID = entry.getKey();
final FastIDSet itemIDs = entry.getValue();
itemIDSet.addAll(itemIDs);
final LongPrimitiveIterator it = itemIDs.iterator();
while (it.hasNext()) {
final long itemID = it.nextLong();
FastIDSet userIDs = preferenceForItems.get(itemID);
if (userIDs == null) {
userIDs = new FastIDSet(2);
preferenceForItems.put(itemID, userIDs);
}
userIDs.add(userID);
}
}
itemIDs = itemIDSet.toArray();
itemIDSet = null; // Might help GC -- this is big
Arrays.sort(itemIDs);
userIDs = new long[userData.size()];
int i = 0;
final LongPrimitiveIterator it = userData.keySetIterator();
while (it.hasNext()) {
userIDs[i++] = it.next();
}
Arrays.sort(userIDs);
this.timestamps = timestamps;
}
/**
* Exports the simple user IDs and associated item IDs in the data model.
*
* @return a {@link FastByIDMap} mapping user IDs to {@link FastIDSet}s representing
* that user's associated items
*/
public static FastByIDMap toDataMap(final DataModel dataModel) {
final FastByIDMap data = new FastByIDMap(
dataModel.getNumUsers());
final LongPrimitiveIterator it = dataModel.getUserIDs();
while (it.hasNext()) {
final long userID = it.nextLong();
data.put(userID, dataModel.getItemIDsFromUser(userID));
}
return data;
}
public static FastByIDMap toDataMap(
final FastByIDMap data) {
for (final Map.Entry entry : ((FastByIDMap