* @throws Exception if an exception occurs
*/
private void query(AsnContext context) throws Exception {
// initialize
AsnOperation operation = context.getOperation();
AsnAssertionSet asnSet = operation.getAssertionSet();
AsnValueType vType = asnSet.getValueType();
String subject = operation.getSubject().getURN();
String predicate = vType.getRdfPredicate();
// build a query to match all occurrences of the subject/predicate pair
BooleanQuery query = new BooleanQuery();
Query qSubject = new TermQuery(new Term(AsnConstants.FIELD_RDF_SUBJECT,subject));
Query qPredicate = new TermQuery(new Term(AsnConstants.FIELD_RDF_PREDICATE,predicate));
query.add(qSubject,BooleanClause.Occur.MUST);
query.add(qPredicate,BooleanClause.Occur.MUST);
// sort on descending timestamp
String tsField = AsnConstants.FIELD_SYS_TIMESTAMP;
Sort sortOption = new Sort(new SortField(tsField,SortField.STRING,true));
// determine the start and end positions
int startRecord = context.getRequestOptions().getStartRecord() - 1;
int maxRecords = context.getRequestOptions().getMaxRecords();
if (startRecord < 0) startRecord = 0;
int recordsPerPage = maxRecords;
if (recordsPerPage <= 0) recordsPerPage = 1;
int hitsToReturn = startRecord + recordsPerPage;
int nextRecord = 0;
int numDocs = 0;
IndexReader reader = null;
IndexSearcher searcher = null;
try {
// make the reader and searcher, execute the search
reader = this.getIndexAdapter().makeIndexReader();
searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(query,null,hitsToReturn,sortOption);
ScoreDoc[] scoreDocs = null;
int totalHits = topDocs.totalHits;
if (maxRecords > 0) {
scoreDocs = topDocs.scoreDocs;
if ((scoreDocs != null) && (scoreDocs.length) > 0) {
numDocs = scoreDocs.length;
if (totalHits > numDocs) {
nextRecord = numDocs + 1;
}
}
}
// root property for the response
String rootSubject = subject;
String roorPredicate = operation.getPredicate().getURN()+"response";
AsnProperty rootProp = new AsnProperty(rootSubject,roorPredicate,null);
// hit count and next record
String queryPfx = asnSet.getURNPrefix()+":query";
rootProp.getChildren().add(new AsnProperty(null,queryPfx+":hits",""+totalHits));