Example usage for org.apache.solr.request SolrRequestInfo addCloseHook

List of usage examples for org.apache.solr.request SolrRequestInfo addCloseHook

Introduction

In this page you can find the example usage for org.apache.solr.request SolrRequestInfo addCloseHook.

Prototype

public void addCloseHook(Closeable hook) 

Source Link

Usage

From source file:org.vootoo.schema.CrossCoreField.java

License:Apache License

@Override
public ValueSource getValueSource(SchemaField field, QParser parser) {
    SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
    if (info == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Cross-core field [" + field.getName() + "] must have SolrRequestInfo");
    }//from  w w w  .ja  v a  2  s  .  com

    String[] crossCore = dumpCrossSchemaField(field);

    SchemaField uniqueKeyField = schema.getUniqueKeyField();

    final SolrCore targetCore = info.getReq().getCore().getCoreDescriptor().getCoreContainer()
            .getCore(crossCore[0]);
    if (targetCore == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Cross-core field [" + field.getName() + "] miss target core [" + crossCore[0] + "]");
    }

    SchemaField targetField = targetCore.getLatestSchema().getFieldOrNull(crossCore[1]);
    if (targetField == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cross-core field [" + field.getName()
                + "] core [" + crossCore[0] + "] miss field [" + crossCore[1] + "]");
    }

    final RefCounted<SolrIndexSearcher> targetCoreSearcher = targetCore.getSearcher();
    SolrIndexSearcher targetSolrIndexSearcher = targetCoreSearcher.get();

    CrossCoreFieldValueSource ccfvs = new CrossCoreFieldValueSource(uniqueKeyField, targetField, parser,
            targetSolrIndexSearcher);

    info.addCloseHook(new Closeable() {
        @Override
        public void close() throws IOException {
            targetCoreSearcher.decref();
            targetCore.close();
        }
    });

    return ccfvs;
}