Skip to content

Commit

Permalink
SOLR memoization support
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed Jul 7, 2014
1 parent d9b7038 commit c29a875
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 413 deletions.
2 changes: 1 addition & 1 deletion BUILD-README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
For build project, we use gradle build tool. For more information, see http://www.gradle.org/.
For build project, we use gradle build tool (version 2.+). For more information, see http://www.gradle.org/.

Display all tasks:

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ subprojects {
ext.guiceversion ='3.0'
ext.postgresversion = '8.4-702.jdbc4'
ext.slf4jversion='1.6.0'
ext.jerseyversion = '1.12'
ext.jerseyversion = '1.17.1'

ext.generatedSources = "generated-sources"

Expand Down
22 changes: 8 additions & 14 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,17 @@ dependencies {
compile "commons-dbcp:commons-dbcp:1.4"
compile "net.sf.json-lib:json-lib:2.3:jdk15"

/*
compile "com.sun.jersey.contribs:jersey-guice:${ext.jerseyversion}"
compile "com.sun.jersey:jersey-client:${ext.jerseyversion}"
compile "com.sun.jersey.contribs:jersey-apache-client:${ext.jerseyversion}"
compile "com.sun.jersey:jersey-json:${ext.jerseyversion}"
*/

compile 'javax.ws.rs:jsr311-api:1.1.1'

compile "com.sun.jersey:jersey-server:${jerseyversion}"
compile "com.sun.jersey:jersey-servlet:${jerseyversion}"
compile "com.sun.jersey:jersey-client:${jerseyversion}"

compile "com.sun.jersey.contribs:jersey-apache-client:${jerseyversion}"
compile "com.sun.jersey.contribs:jersey-guice:${jerseyversion}"

// dependencies in maven profile
compile module("com.sun.jersey:jersey-server:1.12") {
dependency("com.sun.jersey:jersey-core:1.12")
dependency("com.sun.jersey:jersey-servlet:1.12")
dependency("com.sun.jersey:jersey-client:1.12")
dependency("com.sun.jersey.contribs:jersey-apache-client:1.12")
dependency("com.sun.jersey:jersey-json:1.12")
dependency("com.sun.jersey.contribs:jersey-guice:1.12")
}

testCompile "xmlunit:xmlunit:1.3"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import cz.incad.kramerius.rest.api.k5.client.impl.SolrMemoizationImpl;
import cz.incad.kramerius.rest.api.k5.client.item.ItemResource;
import cz.incad.kramerius.rest.api.k5.client.item.decorators.CollectionsDecorator;
import cz.incad.kramerius.rest.api.k5.client.item.decorators.RelsExtRightsFlag;
import cz.incad.kramerius.rest.api.k5.client.item.decorators.SolrRightsFlag;
import cz.incad.kramerius.rest.api.k5.client.item.decorators.HandleDecorate;
import cz.incad.kramerius.rest.api.k5.client.item.decorators.ItemSolrRootModelDecorate;
import cz.incad.kramerius.rest.api.k5.client.item.decorators.ItemSolrRootPidDecorate;
Expand Down Expand Up @@ -131,7 +131,7 @@ public static void decoratorsBindings(Multibinder<JSONDecorator> decs) {
decs.addBinding().to(SolrDataNode.class);
decs.addBinding().to(CollectionsDecorator.class);
decs.addBinding().to(ReplicatedFromDecorator.class);
decs.addBinding().to(RelsExtRightsFlag.class);
decs.addBinding().to(SolrRightsFlag.class);


// item, display
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,29 @@

import com.google.inject.Inject;


public class JSONDecoratorsAggregate {

Map<String, JSONDecorator> decoratorsMap = new HashMap<String, JSONDecorator>();
List<String> keys = new ArrayList<String>();
Map<String, JSONDecorator> decoratorsMap = new HashMap<String, JSONDecorator>();
List<String> keys = new ArrayList<String>();

@Inject
public JSONDecoratorsAggregate(Set<JSONDecorator> decs) {
super();
for (JSONDecorator p : decs) {
String k = p.getKey();
if (keys.contains(k)) {
keys.remove(k);
}
keys.add(k);
this.decoratorsMap.put(k, p);
String k = p.getKey();
if (keys.contains(k)) {
keys.remove(k);
}
keys.add(k);
this.decoratorsMap.put(k, p);
}
}

public List<JSONDecorator> getDecorators() {
List<JSONDecorator> decorators = new ArrayList<JSONDecorator>();
for (String k: this.keys) {
decorators.add(this.decoratorsMap.get(k));
}
return decorators;
List<JSONDecorator> decorators = new ArrayList<JSONDecorator>();
for (String k : this.keys) {
decorators.add(this.decoratorsMap.get(k));
}
return decorators;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cz.incad.kramerius.rest.api.k5.client;

import java.io.IOException;
import java.util.Map;

import org.w3c.dom.Element;

/**
* SOLR memoization support
* @author pavels
*/
public interface SolrMemoization {

/**
* Remember document from index
* @param pid PID
* @param elm W3C Element represents indexed document
*/
public void rememberIndexedDoc(String pid, Element elm);

/**
* Returns remembered document
* @param pid PID
*/
public Element getRememberedIndexedDoc(String pid);

/**
* Ask SOLR for the index document
* @param pid PID
* @return
* @throws IOException
*/
public Element askForIndexDocument(String pid) throws IOException;

/**
* Clear remembered elements
*/
public void clearMemo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cz.incad.kramerius.rest.api.k5.client;

import java.util.List;

public interface SolrResultsAware {

public List<String> getFieldList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import cz.incad.kramerius.processes.annotations.DefaultParameterValue;
import cz.incad.kramerius.rest.api.exceptions.GenericApplicationException;
import cz.incad.kramerius.rest.api.k5.client.JSONDecoratorsAggregate;
import cz.incad.kramerius.rest.api.k5.client.SolrMemoization;
import cz.incad.kramerius.rest.api.k5.client.utils.JSONUtils;
import cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils;
import cz.incad.kramerius.security.User;
Expand Down Expand Up @@ -63,6 +64,9 @@ public class FeederResource {
@Inject
SolrAccess solrAccess;

@Inject
SolrMemoization solrMemo;

@Inject
Provider<User> userProvider;

Expand Down Expand Up @@ -115,7 +119,7 @@ public boolean acceptElement(Element element) {
.fromResource(FeederResource.class)
.path("newest").build(pid).toString();
JSONObject mdis = JSONUtils.pidAndModelDesc(pid,
fedoraAccess, uriString,
uriString, this.solrMemo,
this.decoratorsAggregate, uriString);
jsonArray.add(mdis);
} catch (IOException ex) {
Expand Down Expand Up @@ -161,8 +165,8 @@ public Response mostdesirable(
String uriString = UriBuilder
.fromResource(FeederResource.class)
.path("mostdesirable").build(pid).toString();
JSONObject mdis = JSONUtils.pidAndModelDesc(pid, fedoraAccess,
uriString, this.decoratorsAggregate, uriString);
JSONObject mdis = JSONUtils.pidAndModelDesc(pid,
uriString, this.solrMemo, this.decoratorsAggregate, uriString);
jsonArray.add(mdis);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cz.incad.kramerius.rest.api.k5.client.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.google.inject.Inject;

import cz.incad.kramerius.SolrAccess;
import cz.incad.kramerius.rest.api.k5.client.SolrMemoization;
import cz.incad.kramerius.rest.api.k5.client.utils.PIDSupport;
import cz.incad.kramerius.utils.XMLUtils;

public class SolrMemoizationImpl implements SolrMemoization{

@Inject
SolrAccess solrAccess;

private Map<String, Element> elms = new HashMap<String, Element>();

@Override
public void rememberIndexedDoc(String pid, Element elm) {
this.elms.put(pid, elm);
}

@Override
public Element getRememberedIndexedDoc(String pid) {
return elms.get(pid);
}

@Override
public synchronized Element askForIndexDocument(String pid) throws IOException {
if (!this.elms.containsKey(pid)) {
if (PIDSupport.isComposedPID(pid)) {
pid = PIDSupport.convertToSOLRType(pid);
}
Document doc = solrAccess.getSolrDataDocument(pid);
Element result = XMLUtils.findElement(doc.getDocumentElement(), "result");
if (result != null) {
Element d = XMLUtils.findElement(result, "doc");
this.elms.put(pid, d);
}
}
return this.elms.get(pid);
}

@Override
public void clearMemo() {
this.elms.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Response children(@PathParam("pid") String pid) {
String uri = UriBuilder.fromResource(ItemResource.class)
.path("{pid}/children").build(pid).toString();
JSONObject jsonObject = JSONUtils.pidAndModelDesc(repPid,
fedoraAccess, uri.toString(),
uri.toString(),this.solrMemoization,
this.decoratorsAggregate, uri);
jsonArray.add(jsonObject);
}
Expand Down Expand Up @@ -249,8 +249,8 @@ private JSON siblings(String pid, ObjectPidsPath onePath)
String uriString = UriBuilder.fromResource(ItemResource.class)
.path("{pid}/siblings").build(pid).toString();
p = PIDSupport.convertToK4Type(p);
JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, fedoraAccess,
uriString, this.decoratorsAggregate, uriString);
JSONObject jsonObject = JSONUtils.pidAndModelDesc(p,
uriString,this.solrMemoization, this.decoratorsAggregate, uriString);
pathArray.add(jsonObject);
}
object.put("path", pathArray);
Expand All @@ -261,8 +261,7 @@ private JSON siblings(String pid, ObjectPidsPath onePath)
String uriString = UriBuilder.fromResource(ItemResource.class)
.path("{pid}/siblings").build(pid).toString();
p = PIDSupport.convertToK4Type(p);
JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, fedoraAccess,
uriString, this.decoratorsAggregate, uriString);
JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, uriString, this.solrMemoization, this.decoratorsAggregate, uriString);

jsonObject.put("selected", p.equals(pid));
jsonArray.add(jsonObject);
Expand Down Expand Up @@ -375,8 +374,7 @@ public Response basic(@PathParam("pid") String pid) {

JSONObject jsonObject = new JSONObject();
String uriString = basicURL(pid);
JSONUtils.pidAndModelDesc(pid, jsonObject,
this.fedoraAccess, uriString,
JSONUtils.pidAndModelDesc(pid, jsonObject,uriString, this.solrMemoization,
this.decoratorsAggregate, null);

return Response.ok().entity(jsonObject.toString()).build();
Expand All @@ -389,7 +387,7 @@ public Response basic(@PathParam("pid") String pid) {

String uriString = basicURL(pid);
JSONUtils.pidAndModelDesc(pid, jsonObject,
this.fedoraAccess, uriString,
uriString, this.solrMemoization,
this.decoratorsAggregate, null);

return Response.ok().entity(jsonObject.toString())
Expand Down Expand Up @@ -424,7 +422,6 @@ public static String basicURL(String pid) {


private List<String> solrChildren(String parentPid, List<String> fList) throws IOException {
LOGGER.info("\t children");
List<Document> docs = new ArrayList<Document>();
List<Map<String, String>> ll = new ArrayList<Map<String, String>>();
int rows = 10000;
Expand Down Expand Up @@ -487,8 +484,9 @@ public int compare(Map<String, String> o1, Map<String, String> o2) {
});

List<String> values = new ArrayList<String>();
for (Map<String, String> m : ll) { LOGGER.info("index =="+m.get("index"));
values.add(m.get("pid"));}
for (Map<String, String> m : ll) {
values.add(m.get("pid"));
}
return values;
}

Expand Down
Loading

0 comments on commit c29a875

Please sign in to comment.