use of org.apache.solr.client.solrj.io.stream.CloudSolrStream in project lucene-solr by apache.
the class ModelCache method getModel.
public Tuple getModel(String zkHost, String collection, String modelID, long checkMillis) throws IOException {
Model model = null;
long currentTime = new Date().getTime();
synchronized (this) {
model = models.get(modelID);
if (model != null && ((currentTime - model.getLastChecked()) <= checkMillis)) {
return model.getTuple();
}
if (model != null) {
//model is expired
models.remove(modelID);
}
}
//Model is not in cache or has expired so fetch the model
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "name_s:" + modelID);
params.set("fl", "terms_ss, idfs_ds, weights_ds, iteration_i, _version_");
params.set(SORT, "iteration_i desc");
StreamContext streamContext = new StreamContext();
streamContext.setSolrClientCache(solrClientCache);
CloudSolrStream stream = new CloudSolrStream(zkHost, collection, params);
stream.setStreamContext(streamContext);
Tuple tuple = null;
try {
stream.open();
tuple = stream.read();
if (tuple.EOF) {
return null;
}
} finally {
stream.close();
}
synchronized (this) {
//check again to see if another thread has updated the same model
Model m = models.get(modelID);
if (m != null) {
Tuple t = m.getTuple();
long v = t.getLong(VERSION_FIELD);
if (v >= tuple.getLong(VERSION_FIELD)) {
return t;
} else {
models.put(modelID, new Model(tuple, currentTime));
return tuple;
}
} else {
models.put(modelID, new Model(tuple, currentTime));
return tuple;
}
}
}
Aggregations