Example usage for org.apache.solr.core CoreDescriptor getCollectionName

List of usage examples for org.apache.solr.core CoreDescriptor getCollectionName

Introduction

In this page you can find the example usage for org.apache.solr.core CoreDescriptor getCollectionName.

Prototype

public String getCollectionName() 

Source Link

Usage

From source file:edu.harvard.gis.hhypermap.bop.solrplugins.DateShardRoutingSearchHandler.java

License:Apache License

private void addShardsParamIfWeCan(SolrQueryRequest req) {
    CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
    CoreContainer coreContainer = coreDescriptor.getCoreContainer();
    if (!coreContainer.isZooKeeperAware()) {
        return;/*from w  ww .  jav a2  s.co  m*/
    }
    if (!req.getParams().getBool("distrib", true)) {
        return;
    }
    final String shards = req.getParams().get(ShardParams.SHARDS);
    if (shards != null) {
        return; // we already have the shards
    }

    String startStrParam = req.getParams().get(START_PARAM);
    String endStrParam = req.getParams().get(END_PARAM);

    Instant startInst = // null means open-ended ('*')
            startStrParam == null ? null : DateMathParser.parseMath(null, startStrParam).toInstant();
    Instant endInst = endStrParam == null ? null : DateMathParser.parseMath(null, endStrParam).toInstant();

    if (startInst == null && endInst == null) {
        return;
    }

    ZkController zkController = coreContainer.getZkController();
    String collection = req.getParams().get("collection", coreDescriptor.getCollectionName());
    List<Slice> slices = getOrderedSlices(zkController, collection);
    if (slices.size() <= 1) {
        return;
    }

    List<String> routeShardNames = new ArrayList<>(); // the result
    boolean findingStart = (startInst != null);
    String prevShardName = null;
    Instant prevShardStartKey = null;

    for (Slice slice : slices) {
        String name = slice.getName();
        Instant shardStartKey = parseStartKeyFromShardName(name);
        if (prevShardStartKey != null && prevShardStartKey.isAfter(shardStartKey)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "shards not in order? " + slice);
        }
        if (findingStart) {
            // As we advance shards, is this one finally > the 'start'? If so, accept it.
            if (shardStartKey.isAfter(startInst)) {
                if (prevShardName != null) {
                    routeShardNames.add(prevShardName);
                }
                findingStart = false; // thus findingEnd
            }
        }
        if (!findingStart && endInst == null) { // take all the remainder since 'end' is null
            routeShardNames.add(name);
        } else if (!findingStart) { // findingEnd
            if (shardStartKey.isAfter(endInst)) {
                break;
            }
            routeShardNames.add(name);
        }

        prevShardName = name;
        prevShardStartKey = shardStartKey;
    }
    if (findingStart) {
        routeShardNames.add(prevShardName);
    }

    if (routeShardNames.size() == slices.size()) {
        return;
    }

    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
    String shardsValue = StrUtils.join(routeShardNames, ',');
    params.set(ShardParams.SHARDS, shardsValue);
    req.setParams(params);
    log.debug("Set shards: {}", shardsValue);
}