se.l4.silo.index.IndexQueryImpl Maven / Gradle / Ivy
The newest version!
package se.l4.silo.index;
import se.l4.silo.FetchResult;
import se.l4.silo.StorageException;
import se.l4.silo.index.IndexQueryRequest.Op;
import se.l4.silo.query.IndexQuery;
import se.l4.silo.query.QueryRunner;
public class IndexQueryImpl
implements IndexQuery
{
private final QueryRunner runner;
private IndexQueryRequest request;
private String field;
public IndexQueryImpl(QueryRunner runner)
{
this.runner = runner;
request = new IndexQueryRequest();
}
@Override
public FetchResult run()
{
return runner.fetchResults(request, r -> r.getData());
}
@Override
public IndexQuery field(String name)
{
this.field = name;
return this;
}
private IndexQuery add(Op op, Object value)
{
if(field == null)
{
throw new StorageException("No field given");
}
request.addCritera(field, op, value);
field = null;
return this;
}
@Override
public IndexQuery isEqualTo(Object value)
{
return add(Op.EQUAL, value);
}
@Override
public IndexQuery isMoreThan(Number number)
{
return add(Op.MORE_THAN, number);
}
@Override
public IndexQuery isLessThan(Number number)
{
return add(Op.LESS_THAN, number);
}
@Override
public IndexQuery isLessThanOrEqualTo(Number number)
{
return add(Op.LESS_THAN_OR_EQUAL_TO, number);
}
@Override
public IndexQuery isMoreThanOrEqualTo(Number number)
{
return add(Op.MORE_THAN_OR_EQUAL_TO, number);
}
private IndexQuery addSort(boolean ascending)
{
if(field == null)
{
throw new StorageException("No field given");
}
request.addSort(field, ascending);
field = null;
return this;
}
@Override
public IndexQuery sortAscending()
{
return addSort(true);
}
@Override
public IndexQuery sortDescending()
{
return addSort(false);
}
@Override
public IndexQuery sort(boolean ascending)
{
return addSort(ascending);
}
@Override
public IndexQuery reverseDefaultSort()
{
request.setReverseDefaultSort(true);
return this;
}
@Override
public IndexQuery limit(long limit)
{
request.setLimit(limit);
return this;
}
@Override
public IndexQuery offset(long offset)
{
request.setOffset(offset);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy