All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.grouplens.lenskit.eval.traintest.CachingDAOProvider Maven / Gradle / Ivy

There is a newer version: 3.0-T5
Show newest version
/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.eval.traintest;

import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Maps;
import org.grouplens.lenskit.data.dao.*;

import javax.inject.Inject;
import javax.inject.Provider;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;

/**
 * Caching provider to reduce the duplication of DAO instances in evaluations.
 *
 * 

This assumes that each unique DAO object is unchanging. That may or may not be a safe * assumption outside the evaluator!

* * @since 2.1 * @author GroupLens Research */ public abstract class CachingDAOProvider implements Provider { private static final ConcurrentMap,Cache> CACHE_MAP = Maps.newConcurrentMap(); private static void registerProviderClass(Class cls) { Cache cache = CACHE_MAP.get(cls); if (cache == null) { cache = CacheBuilder.newBuilder() .weakKeys() .softValues() .build(); CACHE_MAP.putIfAbsent(cls, cache); } } private final EventDAO eventDAO; CachingDAOProvider(EventDAO dao) { registerProviderClass(getClass()); eventDAO = dao; } protected abstract T create(EventDAO dao); @SuppressWarnings("unchecked") @Override public T get() { Cache cache = CACHE_MAP.get(getClass()); assert cache != null; try { return (T) cache.get(eventDAO, new Callable() { @Override public Object call() { return create(eventDAO); } }); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } } public static final class User extends CachingDAOProvider { @Inject public User(EventDAO dao) { super(dao); } @Override protected PrefetchingUserDAO create(EventDAO dao) { return new PrefetchingUserDAO(dao); } } public static final class Item extends CachingDAOProvider { @Inject public Item(EventDAO dao) { super(dao); } @Override protected PrefetchingItemDAO create(EventDAO dao) { return new PrefetchingItemDAO(dao); } } public static final class UserEvent extends CachingDAOProvider { @Inject public UserEvent(EventDAO dao) { super(dao); } @Override protected PrefetchingUserEventDAO create(EventDAO dao) { return new PrefetchingUserEventDAO(dao); } } public static final class ItemEvent extends CachingDAOProvider { @Inject public ItemEvent(EventDAO dao) { super(dao); } @Override protected PrefetchingItemEventDAO create(EventDAO dao) { return new PrefetchingItemEventDAO(dao); } } }