Skip to content

Commit

Permalink
Merge branch '21-database-filter-is-filtering-everything-out-if-there…
Browse files Browse the repository at this point in the history
…-is-an-index' into 'master'

Filtering over indexed field

Closes #21

See merge request bright-giant/sirius/sirius-libs!5
  • Loading branch information
Martin Engler-Lukajewski committed Sep 26, 2023
2 parents 541d427 + 3d1c4ab commit f3897a4
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import de.unijena.bioinf.spectraldb.SpectralNoSQLDatabase;
import de.unijena.bioinf.spectraldb.entities.Ms2ReferenceSpectrum;
import de.unijena.bioinf.storage.blob.file.FileBlobStorage;
import de.unijena.bioinf.storage.db.nosql.Filter;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -218,4 +219,15 @@ public void getChemDbDateTest() throws ChemicalDatabaseException {
public void annotateCompoundsTest() throws ChemicalDatabaseException {
chemDb.annotateCompounds(List.of());
}

@Test
public void testIndexFilter() throws IOException {
List<FingerprintCandidateWrapper> candidates = chemDb.getStorage().findStr(new Filter().gt("mass", 0.0), FingerprintCandidateWrapper.class).toList();
assertEquals(21, candidates.size());
candidates = chemDb.getStorage().findStr(new Filter().gt("mass", 9999.0), FingerprintCandidateWrapper.class).toList();
assertTrue(candidates.isEmpty());
candidates = chemDb.getStorage().findStr(new Filter().gt("mass", 500.0), FingerprintCandidateWrapper.class).toList();
assertEquals(2, candidates.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
import java.util.Arrays;
import java.util.Deque;

/**
* A class to specify filtering criteria during {@link Database}'s
* find, count and remove operations.
* <p>
* Each filtering criteria is based on a field in the object.
* Please note that is the users responsibility to make sure that filter methods
* with Object value parameters are called with the same value type as the field type.
* Otherwise, the filtering might fail.
*/
public class Filter {

public enum FilterType {
Expand Down Expand Up @@ -65,61 +74,123 @@ public static Filter build(){

public Filter() {}

/**
* Concatenate multiple filters using an and operation, for example: {@code Filter.build().and().eq(...).gt(...).in(...)end()}.
* To avoid confusion, the end should be marked with {@link #end()}.
*
* @return Filter object.
*/
public Filter and() {
this.filterChain.addLast(new FilterElement(FilterType.AND));
return this;
}

/**
* Concatenate multiple filters using an or operation, for example: {@code Filter.build().or().eq(...).gt(...).in(...)end()}.
* To avoid confusion, the end should be marked with {@link #end()}.
*
* @return Filter object.
*/
public Filter or() {
this.filterChain.addLast(new FilterElement(FilterType.OR));
return this;
}

/**
* Mark the end of multiple filters concatenated by {@link #and()} or {@link #or()}.
*
* @return Filter object.
*/
public Filter end() {
this.filterChain.addLast(new FilterElement(FilterType.END));
return this;
}

/**
* Negate the following filter.
*
* @return Filter object.
*/
public Filter not() {
this.filterChain.addLast(new FilterElement(FilterType.NOT));
return this;
}

/**
* Equality filter. Please make sure that value is of the same type as the field type.
*
* @return Filter object.
*/
public Filter eq(String field, Object value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.EQ, field, value));
return this;
}

/**
* Greater than filter. Please make sure that value is of the same type as the field type.
*
* @return Filter object.
*/
public Filter gt(String field, Object value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.GT, field, value));
return this;
}

/**
* Greater than equals filter. Please make sure that value is of the same type as the field type.
*
* @return Filter object.
*/
public Filter gte(String field, Object value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.GTE, field, value));
return this;
}

/**
* Less then filter. Please make sure that value is of the same type as the field type.
*
* @return Filter object.
*/
public Filter lt(String field, Object value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.LT, field, value));
return this;
}

/**
* Less than equals filter. Please make sure that value is of the same type as the field type.
*
* @return Filter object.
*/
public Filter lte(String field, Object value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.LTE, field, value));
return this;
}

/**
* Full text search filter.
*
* @return Filter object.
*/
public Filter text(String field, String value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.TEXT, field, value));
return this;
}

/**
* Regex search filter.
*
* @return Filter object.
*/
public Filter regex(String field, String value) {
this.filterChain.addLast(new FieldFilterElement(FilterType.REGEX, field, value));
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inByte(String field, byte... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -129,6 +200,11 @@ public Filter inByte(String field, byte... values) {
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inShort(String field, short... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -138,16 +214,31 @@ public Filter inShort(String field, short... values) {
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inInt(String field, int... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.IN, field, Arrays.stream(values).boxed().toArray()));
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inLong(String field, long... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.IN, field, Arrays.stream(values).boxed().toArray()));
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inFloat(String field, float... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -157,11 +248,21 @@ public Filter inFloat(String field, float... values) {
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inDouble(String field, double... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.IN, field, Arrays.stream(values).boxed().toArray()));
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inBool(String field, boolean... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -171,6 +272,11 @@ public Filter inBool(String field, boolean... values) {
return this;
}

/**
* In filter, field value may be one of the given values.
*
* @return Filter object.
*/
public Filter inChar(String field, char... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -180,11 +286,21 @@ public Filter inChar(String field, char... values) {
return this;
}

/**
* In filter, field may be one of the values.
*
* @return Filter object.
*/
public Filter in(String field, Object... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.IN, field, values));
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInByte(String field, byte... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -194,6 +310,11 @@ public Filter notInByte(String field, byte... values) {
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInShort(String field, short... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -203,16 +324,31 @@ public Filter notInShort(String field, short... values) {
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInInt(String field, int... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.NOT_IN, field, Arrays.stream(values).boxed().toArray()));
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInLong(String field, long... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.NOT_IN, field, Arrays.stream(values).boxed().toArray()));
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInFloat(String field, float... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -222,11 +358,21 @@ public Filter notInFloat(String field, float... values) {
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInDouble(String field, double... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.NOT_IN, field, Arrays.stream(values).boxed().toArray()));
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInBool(String field, boolean... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -236,6 +382,11 @@ public Filter notInBool(String field, boolean... values) {
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notInChar(String field, char... values) {
Object[] objects = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Expand All @@ -245,11 +396,24 @@ public Filter notInChar(String field, char... values) {
return this;
}

/**
* Negated in filter, field value must not be one of the values.
*
* @return Filter object.
*/
public Filter notIn(String field, Object... values) {
this.filterChain.addLast(new FieldFilterElement(FilterType.NOT_IN, field, values));
return this;
}

/**
* Array and list matching filter. The following filter will be applied to all entries in the field.
* The array/list elements must be referenced using "$".
* <p>
* Example usage: {@code Filter.build().elemMatch("arrField").eq("$", value) }
*
* @return Filter object
*/
public Filter elemMatch(String field) {
this.filterChain.addLast(new FieldFilterElement(FilterType.ELEM_MATCH, field));
return this;
Expand Down

0 comments on commit f3897a4

Please sign in to comment.