diff --git a/vipra-cmd/src/main/java/de/vipra/cmd/file/JGibbFilebase.java b/vipra-cmd/src/main/java/de/vipra/cmd/file/JGibbFilebase.java index 094f7fab12df4b991372bd87dd6d21bdee8de483..5f205147c8756c5d1fb04ca9d42e0a2a60a720f5 100644 --- a/vipra-cmd/src/main/java/de/vipra/cmd/file/JGibbFilebase.java +++ b/vipra-cmd/src/main/java/de/vipra/cmd/file/JGibbFilebase.java @@ -24,7 +24,7 @@ public class JGibbFilebase extends Filebase { if (!articles.isEmpty()) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(modelFile))); for (ArticleFull article : articles) - writer.write(article.getProcessedText()); + writer.write(article.getProcessedText() + "\n"); writer.close(); } } diff --git a/vipra-cmd/src/main/java/de/vipra/cmd/option/ModelingCommand.java b/vipra-cmd/src/main/java/de/vipra/cmd/option/ModelingCommand.java index 25d948904b78de46aff916dcc581f4485a6b4e43..9df500b5419884e8549fb881127cf2687e35636d 100644 --- a/vipra-cmd/src/main/java/de/vipra/cmd/option/ModelingCommand.java +++ b/vipra-cmd/src/main/java/de/vipra/cmd/option/ModelingCommand.java @@ -107,7 +107,7 @@ public class ModelingCommand implements Command { while (indexIter.hasNext() && topicRefsListIter.hasNext()) { // get article from database String id = indexIter.next(); - ArticleFull article = dbArticles.getSingle(MongoUtils.objectId(id), "processedText"); + ArticleFull article = dbArticles.getSingle(MongoUtils.objectId(id), true); if (article == null) { log.error("no article found in db for id " + id); continue; diff --git a/vipra-rest/pom.xml b/vipra-rest/pom.xml index 215641500b1089801d76dfc51e3409bbd7d4967a..a66565870f1128e28a58b4b9dead195710e8d5d6 100644 --- a/vipra-rest/pom.xml +++ b/vipra-rest/pom.xml @@ -76,13 +76,6 @@ <scope>runtime</scope> </dependency> - <!-- Caching --> - <dependency> - <groupId>org.ehcache</groupId> - <artifactId>ehcache</artifactId> - <version>3.0.0.m4</version> - </dependency> - <!-- MongoDB Database Adapter --> <dependency> <groupId>org.mongodb</groupId> diff --git a/vipra-rest/src/main/java/de/vipra/rest/CacheAdapter.java b/vipra-rest/src/main/java/de/vipra/rest/CacheAdapter.java deleted file mode 100644 index 2e48b3341aa72d46a73e325a631ad41244473f30..0000000000000000000000000000000000000000 --- a/vipra-rest/src/main/java/de/vipra/rest/CacheAdapter.java +++ /dev/null @@ -1,138 +0,0 @@ -package de.vipra.rest; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.ehcache.Cache; -import org.ehcache.CacheManager; -import org.ehcache.config.CacheConfiguration; -import org.ehcache.config.CacheConfigurationBuilder; -import org.ehcache.config.CacheRuntimeConfiguration; -import org.ehcache.exceptions.BulkCacheLoadingException; -import org.ehcache.exceptions.BulkCacheWritingException; -import org.ehcache.exceptions.CacheLoadingException; -import org.ehcache.exceptions.CacheWritingException; - -import de.vipra.util.AbstractCache; - -public class CacheAdapter<T, U> implements AbstractCache<T, U> { - - public static class NullCache<T, U> implements Cache<T, U> { - - @Override - public Iterator<org.ehcache.Cache.Entry<T, U>> iterator() { - return null; - } - - @Override - public U get(T key) throws CacheLoadingException { - return null; - } - - @Override - public void put(T key, U value) throws CacheWritingException {} - - @Override - public boolean containsKey(T key) { - return false; - } - - @Override - public void remove(T key) throws CacheWritingException {} - - @Override - public Map<T, U> getAll(Set<? extends T> keys) throws BulkCacheLoadingException { - return null; - } - - @Override - public void putAll(Map<? extends T, ? extends U> entries) throws BulkCacheWritingException {} - - @Override - public void removeAll(Set<? extends T> keys) throws BulkCacheWritingException {} - - @Override - public void clear() {} - - @Override - public U putIfAbsent(T key, U value) throws CacheLoadingException, CacheWritingException { - return null; - } - - @Override - public boolean remove(T key, U value) throws CacheWritingException { - return false; - } - - @Override - public U replace(T key, U value) throws CacheLoadingException, CacheWritingException { - return null; - } - - @Override - public boolean replace(T key, U oldValue, U newValue) throws CacheLoadingException, CacheWritingException { - return false; - } - - @Override - public CacheRuntimeConfiguration<T, U> getRuntimeConfiguration() { - return null; - } - - } - - public static final Logger log = LogManager.getLogger(CacheAdapter.class); - - private final Cache<T, U> cache; - - public CacheAdapter(Cache<T, U> cache) { - this.cache = cache; - } - - public CacheAdapter(CacheManager manager, String name, Class<T> keyClass, Class<U> valueClass) { - Cache<T, U> cache = manager.getCache(name, keyClass, valueClass); - if (cache == null) { - CacheConfiguration<T, U> config = CacheConfigurationBuilder.newCacheConfigurationBuilder() - .buildConfig(keyClass, valueClass); - try { - cache = manager.createCache(name, config); - } catch (IllegalArgumentException e) { - cache = manager.getCache(name, keyClass, valueClass); - } - } - if (cache == null) { - log.error("could not create cache, not using cache"); - cache = new NullCache<>(); - } - this.cache = cache; - } - - @Override - public U get(T t) { - return cache.get(t); - } - - @Override - public void put(T t, U u) { - cache.put(t, u); - } - - @Override - public void remove(T t) { - cache.remove(t); - } - - @Override - public boolean contains(T t) { - return cache.containsKey(t); - } - - @Override - public void clear() { - cache.clear(); - } - -} diff --git a/vipra-rest/src/main/java/de/vipra/rest/provider/InitializationListener.java b/vipra-rest/src/main/java/de/vipra/rest/provider/InitializationListener.java deleted file mode 100644 index 3c82ec125aaae21816e8d88042ce99d9e54bb701..0000000000000000000000000000000000000000 --- a/vipra-rest/src/main/java/de/vipra/rest/provider/InitializationListener.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.vipra.rest.provider; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.ehcache.CacheManager; -import org.ehcache.CacheManagerBuilder; - -public class InitializationListener implements ServletContextListener { - - public static final Logger log = LogManager.getLogger(InitializationListener.class); - - @Override - public void contextDestroyed(ServletContextEvent sce) { - log.info("jersey servlet context destroyed"); - ServletContext ctx = sce.getServletContext(); - CacheManager manager = (CacheManager) ctx.getAttribute("cacheManager"); - if (manager != null) - manager.close(); - } - - @Override - public void contextInitialized(ServletContextEvent sce) { - log.info("jersey servlet context initialized"); - ServletContext ctx = sce.getServletContext(); - CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder().build(true); - ctx.setAttribute("cachemanager", manager); - } - -} diff --git a/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java b/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java index 59fb5f4114e9356f43431ef9b8a5a06cc21298b5..a5733b72762fc2a78859341186adc8944e67f8ba 100644 --- a/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java +++ b/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java @@ -24,9 +24,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.bson.types.ObjectId; -import org.ehcache.CacheManager; -import de.vipra.rest.CacheAdapter; import de.vipra.rest.Messages; import de.vipra.rest.model.APIError; import de.vipra.rest.model.Wrapper; @@ -49,11 +47,6 @@ public class ArticleResource { public ArticleResource(@Context ServletContext servletContext) throws ConfigException, IOException { Config config = Config.getConfig(); dbArticles = MongoService.getDatabaseService(config, ArticleFull.class); - - CacheManager manager = (CacheManager) servletContext.getAttribute("cachemanager"); - CacheAdapter<ObjectId, ArticleFull> cache = new CacheAdapter<>(manager, "articlecacle", ObjectId.class, - ArticleFull.class); - dbArticles.withCache(cache); } @GET @@ -99,7 +92,7 @@ public class ArticleResource { ArticleFull article; try { - article = dbArticles.getSingle(MongoUtils.objectId(id), StringUtils.getFields(fields)); + article = dbArticles.getSingle(MongoUtils.objectId(id), false, StringUtils.getFields(fields)); } catch (Exception e) { e.printStackTrace(); res.addError(new APIError(Response.Status.BAD_REQUEST, "Error", e.getMessage())); diff --git a/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java b/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java index 4383bd44a4680f9841f03a1274b87e34e0d3c916..4d2512d0680b5d6953f9400f19cab4689235478d 100644 --- a/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java +++ b/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java @@ -20,9 +20,7 @@ import javax.ws.rs.core.UriInfo; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bson.types.ObjectId; -import org.ehcache.CacheManager; -import de.vipra.rest.CacheAdapter; import de.vipra.rest.Messages; import de.vipra.rest.model.APIError; import de.vipra.rest.model.Wrapper; @@ -52,11 +50,6 @@ public class TopicResource { Config config = Config.getConfig(); dbTopics = MongoService.getDatabaseService(config, TopicFull.class); dbArticles = MongoService.getDatabaseService(config, ArticleFull.class); - - CacheManager manager = (CacheManager) servletContext.getAttribute("cachemanager"); - CacheAdapter<ObjectId, TopicFull> cache = new CacheAdapter<>(manager, "topiccache", ObjectId.class, - TopicFull.class); - dbTopics.withCache(cache); } @GET @@ -102,7 +95,7 @@ public class TopicResource { TopicFull topic; try { - topic = dbTopics.getSingle(MongoUtils.objectId(id), StringUtils.getFields(fields)); + topic = dbTopics.getSingle(MongoUtils.objectId(id), false, StringUtils.getFields(fields)); } catch (Exception e) { e.printStackTrace(); res.addError(new APIError(Response.Status.BAD_REQUEST, "Error", e.getMessage())); diff --git a/vipra-rest/src/main/java/de/vipra/rest/resource/WordResource.java b/vipra-rest/src/main/java/de/vipra/rest/resource/WordResource.java index f4997b69f26236c09ad6120b855e59c5eac292aa..ad2abe9657655038fec44e26ba267b41684fa69d 100644 --- a/vipra-rest/src/main/java/de/vipra/rest/resource/WordResource.java +++ b/vipra-rest/src/main/java/de/vipra/rest/resource/WordResource.java @@ -17,9 +17,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.bson.types.ObjectId; -import org.ehcache.CacheManager; -import de.vipra.rest.CacheAdapter; import de.vipra.rest.Messages; import de.vipra.rest.model.APIError; import de.vipra.rest.model.Wrapper; @@ -44,10 +42,6 @@ public class WordResource { Config config = Config.getConfig(); dbWords = MongoService.getDatabaseService(config, Word.class); dbTopics = MongoService.getDatabaseService(config, TopicFull.class); - - CacheManager manager = (CacheManager) servletContext.getAttribute("cachemanager"); - CacheAdapter<String, Word> cache = new CacheAdapter<>(manager, "wordcache", String.class, Word.class); - dbWords.withCache(cache); } @GET @@ -92,7 +86,7 @@ public class WordResource { Word word; try { - word = dbWords.getSingle(id, StringUtils.getFields(fields)); + word = dbWords.getSingle(id, false, StringUtils.getFields(fields)); } catch (Exception e) { e.printStackTrace(); res.addError(new APIError(Response.Status.BAD_REQUEST, "Error", e.getMessage())); diff --git a/vipra-rest/src/main/webapp/WEB-INF/web.xml b/vipra-rest/src/main/webapp/WEB-INF/web.xml index deeb51568135d036e0569ff763acd0521a6458a1..e8660b868690c13a40de15847bd89f76b1439ea1 100644 --- a/vipra-rest/src/main/webapp/WEB-INF/web.xml +++ b/vipra-rest/src/main/webapp/WEB-INF/web.xml @@ -14,8 +14,4 @@ <servlet-name>jersey</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> - - <listener> - <listener-class>de.vipra.rest.provider.InitializationListener</listener-class> - </listener> </web-app> \ No newline at end of file diff --git a/vipra-util/src/main/java/de/vipra/util/AbstractCache.java b/vipra-util/src/main/java/de/vipra/util/AbstractCache.java deleted file mode 100644 index 3d22013c835d842d71b714210c1dc225b01bfc3a..0000000000000000000000000000000000000000 --- a/vipra-util/src/main/java/de/vipra/util/AbstractCache.java +++ /dev/null @@ -1,55 +0,0 @@ -package de.vipra.util; - -/** - * Abstract cache interface. This interface is used to abstract a specific cache - * implementation. - * - * @param <T> - * Cache key type - * @param <U> - * Cache value type - */ -public interface AbstractCache<T, U> { - - /** - * Return the value from the cache, identified by the key t - * - * @param t - * key of value to be returned - * @return found value, or null - */ - U get(T t); - - /** - * Insert a value into the cache, with key t. - * - * @param t - * the key to be used for insertion - * @param u - * the value to be inserted - */ - void put(T t, U u); - - /** - * Removes a value from the cache, identified by key t - * - * @param t - * the key to be removed - */ - void remove(T t); - - /** - * Returns true if the specified key is found in the cache - * - * @param t - * the key to be searched - * @return true if key is found - */ - boolean contains(T t); - - /** - * Clears the cache of all keys and values - */ - void clear(); - -} diff --git a/vipra-util/src/main/java/de/vipra/util/service/MongoService.java b/vipra-util/src/main/java/de/vipra/util/service/MongoService.java index 912be211f6e54187e9459023cb106fcd87b535b8..d5dcc1f7849cb499c988d2f0f344150ed44fcbd0 100644 --- a/vipra-util/src/main/java/de/vipra/util/service/MongoService.java +++ b/vipra-util/src/main/java/de/vipra/util/service/MongoService.java @@ -7,7 +7,6 @@ import java.util.List; import org.mongodb.morphia.Datastore; import org.mongodb.morphia.query.Query; -import de.vipra.util.AbstractCache; import de.vipra.util.Config; import de.vipra.util.ListUtils; import de.vipra.util.Mongo; @@ -23,7 +22,6 @@ public class MongoService<Type extends Model<IdType>, IdType> implements Service private final Class<Type> clazz; private final String[] ignoredFieldsSingle; private final String[] ignoredFieldsMulti; - private AbstractCache<IdType, Type> cache; public MongoService(Mongo mongo, Class<Type> clazz) { this.datastore = mongo.getDatastore(); @@ -46,20 +44,13 @@ public class MongoService<Type extends Model<IdType>, IdType> implements Service } @Override - public Type getSingle(IdType id, String... fields) { - Type t = null; - if (cache == null || (fields != null && fields.length > 0) || !cache.contains(id)) { - Query<Type> q = datastore.createQuery(clazz).field("_id").equal(id); - if (fields != null && fields.length > 0) - q.retrievedFields(true, fields); - else if (ignoredFieldsSingle.length > 0) - q.retrievedFields(false, ignoredFieldsSingle); - t = q.get(); - if (cache != null && (fields == null || fields.length == 0)) - cache.put(id, t); - } else { - t = cache.get(id); - } + public Type getSingle(IdType id, boolean allFields, String... fields) { + Query<Type> q = datastore.createQuery(clazz).field("_id").equal(id); + if (fields != null && fields.length > 0) + q.retrievedFields(true, fields); + else if (!allFields && ignoredFieldsSingle.length > 0) + q.retrievedFields(false, ignoredFieldsSingle); + Type t = q.get(); return t; } @@ -109,8 +100,6 @@ public class MongoService<Type extends Model<IdType>, IdType> implements Service @Override public Type createSingle(Type t) throws DatabaseException { datastore.save(t); - if (cache != null) - cache.put(t.getId(), t); return t; } @@ -124,23 +113,17 @@ public class MongoService<Type extends Model<IdType>, IdType> implements Service @Override public long deleteSingle(IdType id) throws DatabaseException { int deleted = datastore.delete(id).getN(); - if (cache != null) - cache.remove(id); return deleted; } @Override public void updateSingle(Type t) throws DatabaseException { datastore.save(t); - if (cache != null) - cache.put(t.getId(), t); } @Override public void drop() { datastore.getCollection(clazz).drop(); - if (cache != null) - cache.clear(); } @Override @@ -148,11 +131,6 @@ public class MongoService<Type extends Model<IdType>, IdType> implements Service return datastore.getCount(clazz); } - @Override - public void withCache(AbstractCache<IdType, Type> cache) { - this.cache = cache; - } - public static <Type extends Model<IdType>, IdType> MongoService<Type, IdType> getDatabaseService(Config config, Class<Type> clazz) throws ConfigException { Mongo mongo = config.getMongo(); diff --git a/vipra-util/src/main/java/de/vipra/util/service/Service.java b/vipra-util/src/main/java/de/vipra/util/service/Service.java index a02b434337c2c016be9ebce5e2254f8e4097888c..b193737c2ef447afc360c366d849887daef720e0 100644 --- a/vipra-util/src/main/java/de/vipra/util/service/Service.java +++ b/vipra-util/src/main/java/de/vipra/util/service/Service.java @@ -3,7 +3,6 @@ package de.vipra.util.service; import java.util.ArrayList; import java.util.List; -import de.vipra.util.AbstractCache; import de.vipra.util.Pair; import de.vipra.util.model.Model; @@ -30,7 +29,7 @@ public interface Service<Type extends Model<IdType>, IdType, E extends Exception * @return retrieved entity or null * @throws E */ - Type getSingle(IdType id, String... fields) throws E; + Type getSingle(IdType id, boolean allFields, String... fields) throws E; /** * @see {@link Service#getMultiple(QueryBuilder)} @@ -118,15 +117,6 @@ public interface Service<Type extends Model<IdType>, IdType, E extends Exception */ long count() throws E; - /** - * Enables caching on entities - * - * @param cache - * cache to be used - * @see {@link AbstractCache} - */ - void withCache(AbstractCache<IdType, Type> cache); - /** * QueryBuilder instances are used to create complex queries for use with * the getMultiple method