Java Code Examples for org.apache.solr.common.util.StrUtils

The following examples show how to use org.apache.solr.common.util.StrUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: Preference.java    License: Apache License 2.0 6 votes vote down vote up
public Preference(Map<String, Object> m, int idx) {
  this.idx = idx;
  this.original = Utils.getDeepCopy(m, 3);
  sort = Policy.Sort.get(m);
  name = Policy.SortParam.get(m.get(sort.name()).toString());
  Object p = m.getOrDefault("precision", 0);
  precision = p instanceof Number ? ((Number) p).intValue() : Integer.parseInt(p.toString());
  if (precision < 0) {
    throw new RuntimeException("precision must be a positive value ");
  }
  if (precision < name.min || precision > name.max) {
    throw new RuntimeException(StrUtils.formatString("invalid precision value {0} , must lie between {1} and {2}",
        precision, name.min, name.max));
  }

}
 
Example 2
Source Project: lucene-solr   Source File: CdcrVersionReplicationTest.java    License: Apache License 2.0 6 votes vote down vote up
void doRealTimeGet(String ids, String versions) throws Exception {
  Map<String, Object> expectedIds = new HashMap<>();
  List<String> strs = StrUtils.splitSmart(ids, ",", true);
  List<String> verS = StrUtils.splitSmart(versions, ",", true);
  for (int i = 0; i < strs.size(); i++) {
    if (!verS.isEmpty()) {
      expectedIds.put(strs.get(i), Long.valueOf(verS.get(i)));
    }
  }

  QueryResponse rsp = solrServer.query(params("qt", "/get", "ids", ids));
  Map<String, Object> obtainedIds = new HashMap<>();
  for (SolrDocument doc : rsp.getResults()) {
    obtainedIds.put((String) doc.get("id"), doc.get(vfield));
  }

  assertEquals(expectedIds, obtainedIds);
}
 
Example 3
Source Project: lucene-solr   Source File: AutoScalingHandler.java    License: Apache License 2.0 6 votes vote down vote up
private AutoScalingConfig handleRemovePolicy(SolrQueryRequest req, SolrQueryResponse rsp, CommandOperation op,
                                             AutoScalingConfig currentConfig) throws KeeperException, InterruptedException, IOException {
  String policyName = (String) op.getVal("");

  if (op.hasError()) return currentConfig;

  Map<String, List<Clause>> policies = currentConfig.getPolicy().getPolicies();
  if (policies == null || !policies.containsKey(policyName)) {
    op.addError("No policy exists with name: " + policyName);
    return currentConfig;
  }

  cloudManager.getClusterStateProvider().getClusterState().forEachCollection(coll -> {
    if (policyName.equals(coll.getPolicyName()))
      op.addError(StrUtils.formatString("policy : {0} is being used by collection {1}", policyName, coll.getName()));
  });
  if (op.hasError()) return currentConfig;
  policies = new HashMap<>(policies);
  policies.remove(policyName);
  Policy p = currentConfig.getPolicy().withPolicies(policies);
  currentConfig = currentConfig.withPolicy(p);
  return currentConfig;
}
 
Example 4
Source Project: lucene-solr   Source File: SolrParams.java    License: Apache License 2.0 6 votes vote down vote up
/** Like {@link #toQueryString()}, but only replacing enough chars so that
 * the URL may be unambiguously pasted back into a browser.
 * This method can be used to properly log query parameters without
 * making them unreadable.
 * <p>
 * Characters with a numeric value less than 32 are encoded.
 * &amp;,=,%,+,space are encoded.
 */
@Override
public String toString() {
  final StringBuilder sb = new StringBuilder(128);
  try {
    boolean first=true;
    for (final Iterator<String> it = getParameterNamesIterator(); it.hasNext();) {
      final String name = it.next();
      for (String val : getParams(name)) {
        if (!first) sb.append('&');
        first=false;
        StrUtils.partialURLEncodeVal(sb, name);
        sb.append('=');
        StrUtils.partialURLEncodeVal(sb, val);
      }
    }
    return sb.toString();
  } catch (IOException e) {
    // impossible!
    throw new AssertionError(e);
  }
}
 
Example 5
Source Project: lucene-solr   Source File: DataImportHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void inform(SolrCore core) {
  try {
    String name = getPluginInfo().name;
    if (name.startsWith("/")) {
      myName = name.substring(1);
    }
    // some users may have '/' in the handler name. replace with '_'
    myName = myName.replaceAll("/", "_");
    debugEnabled = StrUtils.parseBool((String)initArgs.get(ENABLE_DEBUG), true);
    importer = new DataImporter(core, myName);         
  } catch (Exception e) {
    log.error( DataImporter.MSG.LOAD_EXP, e);
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, DataImporter.MSG.LOAD_EXP, e);
  }
}
 
Example 6
Source Project: lucene-solr   Source File: RequestHandlers.java    License: Apache License 2.0 6 votes vote down vote up
private PluginInfo applyInitParams(SolrConfig config, PluginInfo info) {
  List<InitParams> ags = new ArrayList<>();
  String p = info.attributes.get(InitParams.TYPE);
  if(p!=null) {
    for (String arg : StrUtils.splitSmart(p, ',')) {
      if(config.getInitParams().containsKey(arg)) ags.add(config.getInitParams().get(arg));
      else log.warn("INVALID paramSet {} in requestHandler {}", arg, info);
    }
  }
  for (InitParams args : config.getInitParams().values())
    if(args.matchPath(info.name)) ags.add(args);
  if(!ags.isEmpty()){
    info = info.copy();
    for (InitParams initParam : ags) {
      initParam.apply(info);
    }
  }
  return info;
}
 
Example 7
Source Project: lucene-solr   Source File: HttpSolrCall.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the parameter as a potential comma delimited list of collections, and resolves aliases too.
 * One level of aliases pointing to another alias is supported.
 * De-duplicates and retains the order.
 * {@link #getCollectionsList()}
 */
protected List<String> resolveCollectionListOrAlias(String collectionStr) {
  if (collectionStr == null || collectionStr.trim().isEmpty()) {
    return Collections.emptyList();
  }
  List<String> result = null;
  LinkedHashSet<String> uniqueList = null;
  Aliases aliases = getAliases();
  List<String> inputCollections = StrUtils.splitSmart(collectionStr, ",", true);
  if (inputCollections.size() > 1) {
    uniqueList = new LinkedHashSet<>();
  }
  for (String inputCollection : inputCollections) {
    List<String> resolvedCollections = aliases.resolveAliases(inputCollection);
    if (uniqueList != null) {
      uniqueList.addAll(resolvedCollections);
    } else {
      result = resolvedCollections;
    }
  }
  if (uniqueList != null) {
    return new ArrayList<>(uniqueList);
  } else {
    return result;
  }
}
 
Example 8
Source Project: lucene-solr   Source File: PackageStoreAPI.java    License: Apache License 2.0 6 votes vote down vote up
public static void validateName(String path, boolean failForTrusted) {
  if (path == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "empty path");
  }
  List<String> parts = StrUtils.splitSmart(path, '/', true);
  for (String part : parts) {
    if (part.charAt(0) == '.') {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "cannot start with period");
    }
    for (int i = 0; i < part.length(); i++) {
      for (int j = 0; j < INVALIDCHARS.length(); j++) {
        if (part.charAt(i) == INVALIDCHARS.charAt(j))
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unsupported char in file name: " + part);
      }
    }
  }
  if (failForTrusted &&  TRUSTED_DIR.equals(parts.get(0))) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "trying to write into /_trusted_/ directory");
  }
}
 
Example 9
Source Project: lucene-solr   Source File: UpdateRequestProcessorChain.java    License: Apache License 2.0 6 votes vote down vote up
public static UpdateRequestProcessorChain constructChain(UpdateRequestProcessorChain defaultUrp,
                                                         ProcessorInfo processorInfo, SolrCore core) {
  LinkedList<UpdateRequestProcessorFactory> urps = new LinkedList<>(defaultUrp.chain);
  List<UpdateRequestProcessorFactory> p = getReqProcessors(processorInfo.processor, core);
  List<UpdateRequestProcessorFactory> post = getReqProcessors(processorInfo.postProcessor, core);
  //processor are tried to be inserted before LogUpdateprocessor+DistributedUpdateProcessor
  insertBefore(urps, p, DistributedUpdateProcessorFactory.class, 0);
  //port-processor is tried to be inserted before RunUpdateProcessor
  insertBefore(urps, post, RunUpdateProcessorFactory.class, urps.size() - 1);
  UpdateRequestProcessorChain result = new UpdateRequestProcessorChain(urps, core);
  if (log.isDebugEnabled()) {
    ArrayList<String> names = new ArrayList<>(urps.size());
    for (UpdateRequestProcessorFactory urp : urps) names.add(urp.getClass().getSimpleName());
    if (log.isDebugEnabled()) {
      log.debug("New dynamic chain constructed : {}", StrUtils.join(names, '>'));
    }
  }
  return result;
}
 
Example 10
Source Project: lucene-solr   Source File: FacetParser.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
public List<String> getStringList(Map<String, Object> args, String paramName, boolean decode) {
  Object o = args.get(paramName);
  if (o == null) {
    return null;
  }
  if (o instanceof List) {
    return (List<String>)o;
  }
  if (o instanceof String) {
    // TODO: SOLR-12539 handle spaces in b/w comma & value ie, should the values be trimmed before returning??
    return StrUtils.splitSmart((String)o, ",", decode);
  }

  throw err("Expected list of string or comma separated string values for '" + paramName +
      "', received " + o.getClass().getSimpleName() + "=" + o);
}
 
Example 11
Source Project: lucene-solr   Source File: FiltersQParser.java    License: Apache License 2.0 6 votes vote down vote up
protected void exclude(Collection<QParser> clauses) {
  Set<String> tagsToExclude = new HashSet<>();
  String excludeTags = localParams.get("excludeTags");
  if (excludeTags != null) {
    tagsToExclude.addAll(StrUtils.splitSmart(excludeTags, ','));
  }
  @SuppressWarnings("rawtypes")
  Map tagMap = (Map) req.getContext().get("tags");
  final Collection<QParser> excludeSet;
  if (tagMap != null && !tagMap.isEmpty() && !tagsToExclude.isEmpty()) {
    excludeSet = excludeSet(tagMap, tagsToExclude);
  } else {
    excludeSet = Collections.emptySet();
  }
  clauses.removeAll(excludeSet);
}
 
Example 12
Source Project: lucene-solr   Source File: MetricsHandler.java    License: Apache License 2.0 6 votes vote down vote up
private List<MetricType> parseMetricTypes(SolrParams params) {
  String[] typeStr = params.getParams(TYPE_PARAM);
  List<String> types = Collections.emptyList();
  if (typeStr != null && typeStr.length > 0)  {
    types = new ArrayList<>();
    for (String type : typeStr) {
      types.addAll(StrUtils.splitSmart(type, ','));
    }
  }

  List<MetricType> metricTypes = Collections.singletonList(MetricType.all); // include all metrics by default
  try {
    if (types.size() > 0) {
      metricTypes = types.stream().map(String::trim).map(MetricType::valueOf).collect(Collectors.toList());
    }
  } catch (IllegalArgumentException e) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid metric type in: " + types +
        " specified. Must be one of " + MetricType.SUPPORTED_TYPES_MSG, e);
  }
  return metricTypes;
}
 
Example 13
Source Project: lucene-solr   Source File: PivotFacetProcessor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * returns the {@link StatsField} instances that should be computed for a pivot
 * based on the 'stats' local params used.
 *
 * @return A list of StatsFields to compute for this pivot, or the empty list if none
 */
private static List<StatsField> getTaggedStatsFields(StatsInfo statsInfo,
                                                     String statsLocalParam) {
  if (null == statsLocalParam || null == statsInfo) {
    return Collections.emptyList();
  }
  
  List<StatsField> fields = new ArrayList<>(7);
  List<String> statsAr = StrUtils.splitSmart(statsLocalParam, ',');

  // TODO: for now, we only support a single tag name - we reserve using 
  // ',' as a possible delimiter for logic related to only computing stats
  // at certain levels -- see SOLR-6663
  if (1 < statsAr.size()) {
    String msg = StatsParams.STATS + " local param of " + FacetParams.FACET_PIVOT + 
      "may not include tags separated by a comma - please use a common tag on all " + 
      StatsParams.STATS_FIELD + " params you wish to compute under this pivot";
    throw new SolrException(ErrorCode.BAD_REQUEST, msg);
  }

  for(String stat : statsAr) {
    fields.addAll(statsInfo.getStatsFieldsByTag(stat));
  }
  return fields;
}
 
Example 14
Source Project: lucene-solr   Source File: Rule.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public static Map parseRule(String s) {
  Map<String, String> result = new LinkedHashMap<>();
  s = s.trim();
  List<String> keyVals = StrUtils.splitSmart(s, ',');
  for (String kv : keyVals) {
    List<String> keyVal = StrUtils.splitSmart(kv, ':');
    if (keyVal.size() != 2) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid rule. should have only key and val in : " + kv);
    }
    if (keyVal.get(0).trim().length() == 0 || keyVal.get(1).trim().length() == 0) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid rule. should have key and val in : " + kv);
    }
    result.put(keyVal.get(0).trim(), keyVal.get(1).trim());
  }
  return result;
}
 
Example 15
Source Project: lucene-solr   Source File: CloudReplicaSource.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private void withShardsParam(Builder builder, String shardsParam) {
  List<String> sliceOrUrls = StrUtils.splitSmart(shardsParam, ",", true);
  this.slices = new String[sliceOrUrls.size()];
  this.replicas = new List[sliceOrUrls.size()];

  ClusterState clusterState = builder.zkStateReader.getClusterState();

  for (int i = 0; i < sliceOrUrls.size(); i++) {
    String sliceOrUrl = sliceOrUrls.get(i);
    if (sliceOrUrl.indexOf('/') < 0) {
      // this is a logical shard
      this.slices[i] = sliceOrUrl;
      replicas[i] = findReplicas(builder, shardsParam, clusterState, clusterState.getCollection(builder.collection).getSlice(sliceOrUrl));
    } else {
      // this has urls
      this.replicas[i] = StrUtils.splitSmart(sliceOrUrl, "|", true);
      builder.replicaListTransformer.transform(replicas[i]);
      builder.hostChecker.checkWhitelist(clusterState, shardsParam, replicas[i]);
    }
  }
}
 
Example 16
Source Project: lucene-solr   Source File: QueryElevationComponent.java    License: Apache License 2.0 6 votes vote down vote up
protected Elevation getElevation(ResponseBuilder rb) {
  SolrParams localParams = rb.getQparser().getLocalParams();
  String queryString = localParams == null ? rb.getQueryString() : localParams.get(QueryParsing.V);
  if (queryString == null || rb.getQuery() == null) {
    return null;
  }

  SolrParams params = rb.req.getParams();
  String paramElevatedIds = params.get(QueryElevationParams.IDS);
  String paramExcludedIds = params.get(QueryElevationParams.EXCLUDE);
  try {
    if (paramElevatedIds != null || paramExcludedIds != null) {
      List<String> elevatedIds = paramElevatedIds != null ? StrUtils.splitSmart(paramElevatedIds,",", true) : Collections.emptyList();
      List<String> excludedIds = paramExcludedIds != null ? StrUtils.splitSmart(paramExcludedIds, ",", true) : Collections.emptyList();
      return new ElevationBuilder().addElevatedIds(elevatedIds).addExcludedIds(excludedIds).build();
    } else {
      IndexReader reader = rb.req.getSearcher().getIndexReader();
      return getElevationProvider(reader, rb.req.getCore()).getElevationForQuery(queryString);
    }
  } catch (Exception e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error loading elevation", e);
  }
}
 
Example 17
Source Project: lucene-solr   Source File: StatsField.java    License: Apache License 2.0 6 votes vote down vote up
/** special for percentiles **/
boolean parseParams(StatsField sf) {
  String percentileParas = sf.localParams.get(this.name());
  if (percentileParas != null) {
    List<Double> percentiles = new ArrayList<Double>();
    try {
      for (String percentile : StrUtils.splitSmart(percentileParas, ',')) {
        percentiles.add(Double.parseDouble(percentile));
      }
      if (!percentiles.isEmpty()) {
        sf.percentilesList.addAll(percentiles);
        sf.tdigestCompression = sf.localParams.getDouble("tdigestCompression", 
                                                         sf.tdigestCompression);
        return true;
      }
    } catch (NumberFormatException e) {
      throw new SolrException(ErrorCode.BAD_REQUEST, "Unable to parse "
          + StatsParams.STATS_FIELD + " local params: " + sf.localParams + " due to: "
          + e.getMessage(), e);
    }

  }
  return false;
}
 
Example 18
Source Project: lucene-solr   Source File: TermsComponent.java    License: Apache License 2.0 6 votes vote down vote up
protected void checkShardsWhitelist(final ResponseBuilder rb, final List<String> lst) {
  final List<String> urls = new LinkedList<String>();
  for (final String ele : lst) {
    urls.addAll(StrUtils.splitSmart(ele, '|'));
  }
  
  if (whitelistHostChecker.isWhitelistHostCheckingEnabled() && rb.req.getCore().getCoreContainer().getZkController() == null && !whitelistHostChecker.hasExplicitWhitelist()) {
    throw new SolrException(ErrorCode.FORBIDDEN, "TermsComponent "+HttpShardHandlerFactory.INIT_SHARDS_WHITELIST
        +" not configured but required when using the '"+ShardParams.SHARDS+"' parameter with the TermsComponent."
        +HttpShardHandlerFactory.SET_SOLR_DISABLE_SHARDS_WHITELIST_CLUE);
  } else {
    ClusterState cs = null;
    if (rb.req.getCore().getCoreContainer().getZkController() != null) {
      cs = rb.req.getCore().getCoreContainer().getZkController().getClusterState();
    }
    whitelistHostChecker.checkWhitelist(cs, urls.toString(), urls);
  }
}
 
Example 19
Source Project: lucene-solr   Source File: RealTimeGetComponent.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for creating a new ShardRequest for the specified ids, based on the params 
 * specified for the current request.  The new ShardRequest does not yet know anything about 
 * which shard/slice it will be sent to.
 */
private ShardRequest createShardRequest(final ResponseBuilder rb, final List<String> ids) {
  final ShardRequest sreq = new ShardRequest();
  sreq.purpose = 1;
  sreq.params = new ModifiableSolrParams(rb.req.getParams());

  // TODO: how to avoid hardcoding this and hit the same handler?
  sreq.params.set(ShardParams.SHARDS_QT,"/get");      
  sreq.params.set(DISTRIB,false);

  sreq.params.remove(ShardParams.SHARDS);
  sreq.params.remove(ID);
  sreq.params.remove("ids");
  sreq.params.set("ids", StrUtils.join(ids, ','));
  
  return sreq;
}
 
Example 20
Source Project: lucene-solr   Source File: ConfigOverlay.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public static Class checkEditable(String path, boolean isXpath, List<String> hierarchy) {
  List<String> parts = StrUtils.splitSmart(path, isXpath ? '/' : '.');
  Object obj = editable_prop_map;
  for (int i = 0; i < parts.size(); i++) {
    String part = parts.get(i);
    boolean isAttr = isXpath && part.startsWith("@");
    if (isAttr) {
      part = part.substring(1);
    }
    if (hierarchy != null) hierarchy.add(part);
    if (obj == null) return null;
    if (i == parts.size() - 1) {
      if (obj instanceof Map) {
        Map map = (Map) obj;
        Object o = map.get(part);
        return checkType(o, isXpath, isAttr);
      }
      return null;
    }
    obj = ((Map) obj).get(part);
  }
  return null;
}
 
Example 21
Source Project: lucene-solr   Source File: CreateAliasCmd.java    License: Apache License 2.0 6 votes vote down vote up
private void validateAllCollectionsExistAndNoDuplicates(List<String> collectionList, ZkStateReader zkStateReader) {
  final String collectionStr = StrUtils.join(collectionList, ',');

  if (new HashSet<>(collectionList).size() != collectionList.size()) {
    throw new SolrException(BAD_REQUEST,
        String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', since it contains duplicates", collectionStr));
  }
  ClusterState clusterState = zkStateReader.getClusterState();
  Set<String> aliasNames = zkStateReader.getAliases().getCollectionAliasListMap().keySet();
  for (String collection : collectionList) {
    if (clusterState.getCollectionOrNull(collection) == null && !aliasNames.contains(collection)) {
      throw new SolrException(BAD_REQUEST,
          String.format(Locale.ROOT,  "Can't create collection alias for collections='%s', '%s' is not an existing collection or alias", collectionStr, collection));
    }
  }
}
 
Example 22
Source Project: lucene-solr   Source File: Assign.java    License: Apache License 2.0 6 votes vote down vote up
public static List<String> getLiveOrLiveAndCreateNodeSetList(final Set<String> liveNodes, final ZkNodeProps message, final Random random) {
  List<String> nodeList;
  final String createNodeSetStr = message.getStr(CREATE_NODE_SET);
  final List<String> createNodeList = (createNodeSetStr == null) ? null :
      StrUtils.splitSmart((OverseerCollectionMessageHandler.CREATE_NODE_SET_EMPTY.equals(createNodeSetStr) ?
          "" : createNodeSetStr), ",", true);

  if (createNodeList != null) {
    nodeList = new ArrayList<>(createNodeList);
    nodeList.retainAll(liveNodes);
    if (message.getBool(OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE,
        OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE_DEFAULT)) {
      Collections.shuffle(nodeList, random);
    }
  } else {
    nodeList = new ArrayList<>(liveNodes);
    Collections.shuffle(nodeList, random);
  }

  return nodeList;
}
 
Example 23
Source Project: lucene-solr   Source File: PluginBag.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void init(PluginInfo info) {
  name = info.attributes.get(NAME);
  url = info.attributes.get("url");
  sig = info.attributes.get("sig");
  if(url == null) {
    Object v = info.attributes.get("version");
    if (name == null || v == null) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "runtimeLib must have name and version");
    }
    version = String.valueOf(v);
  } else {
    sha512 = info.attributes.get("sha512");
    if(sha512 == null){
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "runtimeLib with url must have a 'sha512' attribute");
    }
    ByteBuffer buf = null;
    buf = coreContainer.getBlobRepository().fetchFromUrl(name, url);

    String digest = BlobRepository.sha512Digest(buf);
    if(!sha512.equals(digest))  {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, StrUtils.formatString(BlobRepository.INVALID_JAR_MSG, url, sha512, digest)  );
    }
    log.info("dynamic library verified {}, sha512: {}", url, sha512);
  }
}
 
Example 24
/**
 * Checks if the configuration declares this node as a slave.
 *
 * @param core the hosting {@link SolrCore} instance.
 * @return true if the content store must be set in read only mode, false otherwise.
 */
@SuppressWarnings("rawtypes")
boolean isSlaveModeEnabledFor(SolrCore core)
{
    Predicate<PluginInfo> onlyReplicationHandler =
            plugin -> "/replication".equals(plugin.name)
                    || plugin.className.endsWith(ReplicationHandler.class.getSimpleName());

    Function<NamedList, Boolean> isSlaveModeEnabled =
            params ->  ofNullable(params)
                    .map(configuration -> {
                        Object enable = configuration.get("enable");
                        return enable == null ||
                                (enable instanceof String ? StrUtils.parseBool((String)enable) : Boolean.TRUE.equals(enable));})
                    .orElse(false);

    return core.getSolrConfig().getPluginInfos(SolrRequestHandler.class.getName())
            .stream()
            .filter(PluginInfo::isEnabled)
            .filter(onlyReplicationHandler)
            .findFirst()
            .map(plugin -> plugin.initArgs)
            .map(params -> params.get("slave"))
            .map(NamedList.class::cast)
            .map(isSlaveModeEnabled)
            .orElse(false);
}
 
Example 25
Source Project: examples   Source File: ZooKeeperInspector.java    License: Apache License 2.0 5 votes vote down vote up
private String checkForAlias(SolrZkClient zkClient, String collection) throws KeeperException,
    InterruptedException {
  byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
  Aliases aliases = ClusterState.load(aliasData);
  String alias = aliases.getCollectionAlias(collection);
  if (alias != null) {
    List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
    if (aliasList.size() > 1) {
      throw new IllegalArgumentException(
          "collection cannot be an alias that maps to multiple collections");
    }
    collection = aliasList.get(0);
  }
  return collection;
}
 
Example 26
Source Project: lucene-solr   Source File: ReplicaVariable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String postValidate(Condition condition) {
  Object val = condition.clause.getThirdTag().val;
  boolean isNodesetObjectList = condition.clause.nodeSetPresent &&  (val instanceof List) && ((List)val).get(0) instanceof Condition ;
  if(condition.clause.nodeSetPresent ){
    if(condition.computedType == ComputedType.EQUAL){
      if(!isNodesetObjectList) return " 'nodeset' must have an array value when 'replica': '#EQUAL` is used";
    } else {
      if(isNodesetObjectList){
        return "cannot use array value for nodeset if replica : '#EQUAL' is not used";
      }

    }

  }

  if (condition.computedType == ComputedType.EQUAL) {
    if (condition.getClause().tag != null &&
        (condition.getClause().tag.op == Operand.WILDCARD || condition.getClause().tag.op == Operand.IN)) {
      return null;
    } else {
      return "'replica': '#EQUAL` must be used with 'node':'#ANY'";
    }
  } else if (condition.computedType == ComputedType.ALL) {
    if(isNodesetObjectList) return "replica: '#ALL' cannot be used with a list of values in nodeset";
    if (condition.getClause().tag != null && (condition.getClause().getTag().op == Operand.IN ||
        condition.getClause().getTag().op == Operand.WILDCARD)) {
      return StrUtils.formatString("array value or wild card cannot be used for tag {0} with replica : '#ALL'",
          condition.getClause().tag.getName());
    }
  } else {
    return checkNonEqualOp(condition);
  }

  return null;
}
 
Example 27
Source Project: lucene-solr   Source File: DocRouter.java    License: Apache License 2.0 5 votes vote down vote up
/** This method is consulted to determine what slices should be queried for a request when
 *  an explicit shards parameter was not used.
 *  This method accepts a multi-valued shardKeys parameter (normally comma separated from the shard.keys request parameter)
 *  and aggregates the slices returned by getSearchSlicesSingle for each shardKey.
 **/
public Collection<Slice> getSearchSlices(String shardKeys, SolrParams params, DocCollection collection) {
  if (shardKeys == null || shardKeys.indexOf(',') < 0) {
    return getSearchSlicesSingle(shardKeys, params, collection);
  }

  List<String> shardKeyList = StrUtils.splitSmart(shardKeys, ",", true);
  HashSet<Slice> allSlices = new HashSet<>();
  for (String shardKey : shardKeyList) {
    allSlices.addAll( getSearchSlicesSingle(shardKey, params, collection) );
  }
  return allSlices;
}
 
Example 28
protected List<String> getAliasList(CloudSolrClient cloudSolrClient, String collectionAlias) {
  ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();
  Aliases aliases = zkStateReader.getAliases();
  String collectionsInAlias = aliases.getCollectionAlias(collectionAlias);
  log.info("Looked up collection list "+collectionsInAlias+" for collection collectionsInAlias: "+collectionAlias);
  return (collectionsInAlias != null) ? StrUtils.splitSmart(collectionsInAlias, ",", true) : new ArrayList<String>(0);
}
 
Example 29
Source Project: lucene-solr   Source File: JSONTestUtil.java    License: Apache License 2.0 5 votes vote down vote up
public boolean seek(String seekPath) {
  if (path == null) return true;
  if (seekPath.startsWith("/")) {
    seekPath = seekPath.substring(1);
  }
  if (seekPath.endsWith("/")) {
    seekPath = seekPath.substring(0,seekPath.length()-1);
  }
  List<String> pathList = StrUtils.splitSmart(seekPath, "/", false);
  return seek(pathList);
}
 
Example 30
Source Project: lucene-solr   Source File: AbstractFullDistribZkTestBase.java    License: Apache License 2.0 5 votes vote down vote up
void doQuery(String expectedDocs, String... queryParams) throws Exception {
  Set<String> expectedIds = new HashSet<>( StrUtils.splitSmart(expectedDocs, ",", true) );

  QueryResponse rsp = cloudClient.query(params(queryParams));
  Set<String> obtainedIds = new HashSet<>();
  for (SolrDocument doc : rsp.getResults()) {
    obtainedIds.add((String) doc.get("id"));
  }

  assertEquals(expectedIds, obtainedIds);
}