Skip to content

Commit

Permalink
Remove unused imports, add class JavaDoc and add read only config
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Aug 28, 2024
1 parent 0a71906 commit bb566ef
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ For Windows Command Prompt, the syntax is slightly different:
set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres"} && mvn spring-boot:run
```

Run with schematize and indexing turned on:
Run with schematize and indexing turned on (default):
```
set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres","middleware.index.schematize":true,"middleware.index.onStartup":true} && mvn spring-boot:run
```
Expand All @@ -156,6 +156,11 @@ Run with schematize and indexing turned off and cache enabled:
set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres","middleware.index.schematize":false,"middleware.index.onStartup":false,"middleware.index.cacheEnabled":true} && mvn spring-boot:run
```

Run with schematize and indexing turned off and cache enabled read-only:
```
set SPRING_APPLICATION_JSON={"spring.datasource.driver-class-name":"org.postgresql.Driver","spring.datasource.url":"jdbc:postgresql://localhost:5432/scholars","spring.jpa.database-platform":"org.hibernate.dialect.PostgreSQLDialect","spring.sql.init.platform":"postgres","middleware.index.schematize":false,"middleware.index.onStartup":false,"middleware.index.cacheEnabled":true,"middleware.index.cacheReadOnly":true} && mvn spring-boot:run
```

For Windows PowerShell:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class IndexConfig {

private boolean cacheEnabled = false;

private boolean cacheReadOnly = false;

private boolean clearCache = true;

private String cacheLocation = "src/test/resources";
Expand Down Expand Up @@ -106,6 +108,14 @@ public void setCacheEnabled(boolean cacheEnabled) {
this.cacheEnabled = cacheEnabled;
}

public boolean isCacheReadOnly() {
return cacheReadOnly;
}

public void setCacheReadOnly(boolean cacheReadOnly) {
this.cacheReadOnly = cacheReadOnly;
}

public boolean isClearCache() {
return clearCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -37,6 +33,26 @@
import edu.tamu.scholars.middleware.service.builder.ClaimBuilder;
import edu.tamu.scholars.middleware.utility.JavaObjectStorageFileUtility;

/**
* SorlClient cache.
*
* Configure with {@link IndexConfig}.
*
* To enable the cache:
* middleware.index.cacheEnabled: false
*
* To only allow to read from cache:
* middleware.index.cacheReadOnly: false
*
* To clear the cache on startup:
* middleware.index.clearCache: true
*
* Location to store cache:
* middleware.index.cacheLocation: src/test/resources
*
* The cache storage is per API endpoint mapping.
*
*/
public class CachingSolrClient<C extends SolrClient> extends SolrClient {

private static final Logger logger = LoggerFactory.getLogger(CachingSolrClient.class);
Expand Down Expand Up @@ -85,7 +101,7 @@ public CachingSolrClient(C client, JwtTokenService jwtTokenService, IndexConfig

@PostConstruct
public void clearCache() {
if (index.isCacheEnabled()) {
if (index.isCacheEnabled() && !index.isCacheReadOnly()) {
File cacheDirectory = new File(StringUtils.removeEnd(index.getCacheLocation(), FORWARD_SLASH));
if (cacheDirectory.exists() && !cacheDirectory.isDirectory()) {
throw new RuntimeException(String.format("Cache location %s is not a directory!", index.getCacheLocation()));
Expand Down Expand Up @@ -186,18 +202,20 @@ public NamedList<Object> request(SolrRequest<?> request, String collection)
long startTime = System.currentTimeMillis();
response = client.request(request, collection);

JavaObjectStorageFileUtility.writeObject(response, filename);
if (index.isCacheReadOnly()) {
JavaObjectStorageFileUtility.writeObject(response, filename);

logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startTime) / (double) 1000);
logger.info("{}:{}: {} seconds", uuid, "QUERY RESPONSE", (System.currentTimeMillis() - startTime) / (double) 1000);

long startUdateLookupTable = System.currentTimeMillis();
long startUdateLookupTable = System.currentTimeMillis();

innerMap.put(jwt, uuid);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(lookupFile, innerMap);
innerMap.put(jwt, uuid);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(lookupFile, innerMap);

logger.info("{}:{}: {} seconds", uuid, "UPDATE LOOKUP TABLE", (System.currentTimeMillis() - startUdateLookupTable) / (double) 1000);
logger.info("{}:{}: {} seconds", uuid, "UPDATE LOOKUP TABLE", (System.currentTimeMillis() - startUdateLookupTable) / (double) 1000);

logger.info("{}:{}:{} {}", uuid, "REQUEST", uuid, requestAsObject.toPrettyString());
logger.info("{}:{}:{} {}", uuid, "REQUEST", uuid, requestAsObject.toPrettyString());
}
}
logger.info("{}:{}: {} seconds", uuid, "ACTUAL RESPONSE", (System.currentTimeMillis() - start) / (double) 1000);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ middleware:
onStartupDelay: 10000
batchSize: 10000
cacheEnabled: false
cacheReadOnly: false
clearCache: true
cacheLocation: src/test/resources
triplestore:
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ middleware:
enableIndividualOnBatchFail: false
batchSize: 1000
cacheEnabled: false
cacheReadOnly: false
clearCache: true
cacheLocation: src/test/resources
triplestore:
Expand Down

0 comments on commit bb566ef

Please sign in to comment.