Initial project files
Change-Id: I403713686bb7a5842feb211241de5f2530e0a457 Signed-off-by: Martin Lowe martin.lowe@eclipse-foundation.org
Merge request reports
Activity
68 public <T, M extends SolrBeanMapper<T>> List<T> get(SolrQuery q, M mapper) { 69 List<T> beans = new LinkedList<>(); 70 try { 71 // query solr for documents using the below query 72 QueryResponse r = client.query(q); 73 List<SolrDocument> docs = r.getResults(); 74 75 // log the data to trace, to not burden the logs 76 if (LOGGER.isTraceEnabled()) { 77 LOGGER.trace("Found {} results for query {}", docs.size(), q.toQueryString()); 78 } 79 80 // convert the solr documents using the bean mappers 81 for (SolrDocument doc : docs) { 82 beans.add(mapper.toBean(doc)); 83 } 89 "Error while streaming results for current query: " + q.toString(), e); 90 } 91 } 92 93 @Override 94 public <T, M extends SolrBeanMapper<T>> void add(List<T> beans, M mapper) { 95 // create a list of documents to upload to Solr 96 List<SolrInputDocument> solrDocuments = new ArrayList<>(beans.size()); 97 for (T bean : beans) { 98 // generate the Solr document from the current bean 99 SolrInputDocument solrDoc = mapper.toDocument(bean); 100 if (solrDoc != null) { 101 solrDocuments.add(solrDoc); 102 } 103 } 104 1 /* 2 * Copyright (C) 2019 Eclipse Foundation and others. 3 * 4 * This program and the accompanying materials are made 5 * available under the terms of the Eclipse Public License 2.0 6 * which is available at https://www.eclipse.org/legal/epl-2.0/ 7 * 8 * SPDX-License-Identifier: EPL-2.0 9 */ 10 package org.eclipsefoundation.marketplace.model; 11 12 import javax.ws.rs.core.Response; 13 import javax.ws.rs.core.Response.Status; 14 15 public class Error { 126 throw new IllegalArgumentException(EMPTY_KEY_MESSAGE); 127 } 128 129 // return immediately if theres no value 130 if (value == null) { 131 return; 132 } 133 134 // set the param, creating list if it doesn't exist 135 List<String> param = this.params.get(key); 136 if (param == null) { 137 this.setParam(key, value); 138 } else { 139 param.add(value); 140 } 141 } Created by: mbarbero
while reading addParam and setParam, I've the feeling that they should be merged into somehting like
public void addParam(String key, String value) { if (StringUtils.isBlank(key)) { throw new IllegalArgumentException(EMPTY_KEY_MESSAGE); } if (value == null) { throw new NullPointerException("value must not be null"); } map.computeIfAbsent(key, k -> new ArrayList<>()).add(value); }
and add the
unsetParam(String key)
method as mentioned above.
25 private RawSolrResultMapper() { 26 } 27 28 @Override 29 public RawSolrResult toBean(SolrDocument doc) { 30 RawSolrResult result = new RawSolrResult(); 31 if (doc.getFieldValueMap().isEmpty()) { 32 return result; 33 } 34 35 // due to Solr implementation of getFieldValuesMap, the values must be iterated 36 // over using keyset to pick up all fields, otherwise unsupported operation 37 // exception is thrown. 38 for (String key : doc.getFieldValuesMap().keySet()) { 39 result.setField(key, doc.getFieldValue(key)); 40 } 25 private RawSolrResultMapper() { 26 } 27 28 @Override 29 public RawSolrResult toBean(SolrDocument doc) { 30 RawSolrResult result = new RawSolrResult(); 31 if (doc.getFieldValueMap().isEmpty()) { 32 return result; 33 } 34 35 // due to Solr implementation of getFieldValuesMap, the values must be iterated 36 // over using keyset to pick up all fields, otherwise unsupported operation 37 // exception is thrown. 38 for (String key : doc.getFieldValuesMap().keySet()) { 39 result.setField(key, doc.getFieldValue(key)); 40 } The reason I do this is a limitation of SolrJ when handling a SolrDocument. The map returned by
getFieldValuesMap()
is an anonymous implementation of the Map interface with the following forentrySet()
:@Override public Set<java.util.Map.Entry<String, Collection<Object>>> entrySet() {throw new UnsupportedOperationException();}
While it does require the extra operations of retrieving the value for the key, it is the best available option.
78 Optional<Object> cachedResults = cachingService.get(cacheKey); 79 80 // if cache miss, populate cache, then return the object that is in cache 81 List<Listing> out = null; 82 if (!cachedResults.isPresent()) { 83 // retrieve the results for the parameters, and check that a listing was found 84 List<Listing> results = dao.get(SolrHelper.createQuery(params), new ListingMapper()); 85 out = results; 67 86 68 // retrieve the results for the parameters 69 List<Listing> results = dao.get(SolrHelper.createQuery(params), ListingMapper.INSTANCE); 87 // cache the resulting listings 88 cachingService.put(cacheKey, out); 89 } else { 90 out = (List<Listing>) cachedResults.get(); 91 } Created by: mbarbero
To avoid this whole if cached, return; otherwise create, cache and return , I would define the caching service so that it leverages the
Cache::get(K, Callable<? extends V>)
method. See https://guava.dev/releases/snapshot/api/docs/com/google/common/cache/Cache.html#get-K-java.util.concurrent.Callable-With the comment below about adding a generic type to the CachingService and moving getCacheKey to CachingService, the get method would get 3 parameters: String, QueryParams and Callable<? extends T>
189 * The field name passed into the sort clause must be indexed by Solr, or the 190 * query will fail. Additionally, if the sort order is not set to either asc or 191 * desc, the clause will be skipped, allowing the query to continue. 192 * </p> 193 * 194 * @param sortVal the comma-delimited list of sort clause values 195 * @return a list of Solr sort clauses 196 */ 197 private static List<SortClause> getSortClauses(String sortVal) { 198 // if empty, return immediately 199 if (StringUtils.isBlank(sortVal)) { 200 return Collections.emptyList(); 201 } 202 203 // separate multiple sort clauses by comma 204 String[] sorts = sortVal.split(","); Created by: mbarbero
String::split is quite dangerous (e.g. what the result spliting of ",," or ", ,, "). For all split, I'd suggest you use https://github.com/google/guava/wiki/StringsExplained#splitter
It's not a blocker for this code, but keep that in mind in the future.
104 * @param key string key to add the value to, must not be null 105 */ 106 public void unsetParam(String key) { 107 if (StringUtils.isBlank(key)) { 108 throw new IllegalArgumentException(EMPTY_KEY_MESSAGE); 109 } 110 this.params.remove(key); 111 } 112 113 /** 114 * Returns this QueryParams object as a Map of param values indexed by the param name. 115 * 116 * @return a copy of the internal param map 117 */ 118 public Map<String, List<String>> asMap() { 119 return new HashMap<>(params); 189 * The field name passed into the sort clause must be indexed by Solr, or the 190 * query will fail. Additionally, if the sort order is not set to either asc or 191 * desc, the clause will be skipped, allowing the query to continue. 192 * </p> 193 * 194 * @param sortVal the comma-delimited list of sort clause values 195 * @return a list of Solr sort clauses 196 */ 197 private static List<SortClause> getSortClauses(String sortVal) { 198 // if empty, return immediately 199 if (StringUtils.isBlank(sortVal)) { 200 return Collections.emptyList(); 201 } 202 203 // separate multiple sort clauses by comma 204 String[] sorts = sortVal.split(",");