Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions nitrite/src/main/java/org/dizitart/no2/filters/StringFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* @since 1.0
*/
public abstract class StringFilter extends ComparableFilter {
private final StringFilterHelper helper = new StringFilterHelper();

/**
* Instantiates a new String filter.
*
Expand All @@ -41,4 +43,15 @@ protected StringFilter(String field, String value) {
public String getStringValue() {
return (String) getValue();
}

/**
* Converts an object to a string safely.
* @param obj the object to convert
* @return the string representation, or empty if null
*/
protected String toStringValue(Object obj) {
return helper.toStringValue(obj);
}

public abstract boolean applyOnString(String value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.dizitart.no2.filters;

public class StringFilterHelper {
public String toStringValue(Object obj) {
return obj != null ? obj.toString() : "";
}
}
25 changes: 18 additions & 7 deletions nitrite/src/main/java/org/dizitart/no2/filters/TextFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,34 @@ public class TextFilter extends StringFilter {
super(field, value);
}

@Override
public boolean applyOnString(String value) {
String searchString = (String) this.getValue();
if (searchString.startsWith("*") || searchString.endsWith("*")) {
searchString = searchString.replace("*", "");
}
return value.toLowerCase().contains(searchString.toLowerCase());
}

/**
* Applies this filter to a document, checking if the specified field contains the search string.
*
* @param element the document pair to filter
* @return true if the field value contains the search string (case-insensitive), false otherwise
* @throws FilterException if the field is not a string
*/
@Override
public boolean apply(Pair<NitriteId, Document> element) {
notNull(getField(), "field cannot be null");
notNull(getStringValue(), "search term cannot be null");
String searchString = getStringValue();
Object docValue = element.getSecond().get(getField());

if (!(docValue instanceof String)) {
throw new FilterException("Text filter can not be applied on non string field " + getField());
}

String docString = (String) docValue;

if (searchString.startsWith("*") || searchString.endsWith("*")) {
searchString = searchString.replace("*", "");
}

return docString.toLowerCase().contains(searchString.toLowerCase());
return applyOnString(docString);
}

@Override
Expand Down Expand Up @@ -194,4 +204,5 @@ private LinkedHashSet<NitriteId> sortedIdsByScore(Map<NitriteId, Integer> unsort
public List<?> applyOnIndex(IndexMap indexMap) {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,37 @@ public MigrationManager(Nitrite nitrite) {
*/
public void doMigrate() {
if (isMigrationNeeded()) {
Integer existingVersion = storeMetadata.getSchemaVersion();
Integer incomingVersion = nitriteConfig.getSchemaVersion();

Queue<Migration> migrationPath = findMigrationPath(existingVersion, incomingVersion);
if (migrationPath == null || migrationPath.isEmpty()) {
// close the database
try {
database.close();
} catch (Exception e) {
throw new NitriteIOException("Failed to close the database", e);
}
Integer currentVersion = storeMetadata.getSchemaVersion();
Integer targetVersion = nitriteConfig.getSchemaVersion();

Queue<Migration> migrationPath = findMigrationPath(currentVersion, targetVersion);
boolean hasValidPath = migrationPath != null && !migrationPath.isEmpty();

throw new MigrationException("Schema version mismatch, no migration path found from version "
+ storeMetadata.getSchemaVersion() + " to " + nitriteConfig.getSchemaVersion());
if (!hasValidPath) {
closeDatabaseAndThrowException( currentVersion, targetVersion);
}
executeMigrationPath(migrationPath);
}
}

int length = migrationPath.size();
for (int i = 0; i < length; i++) {
Migration migration = migrationPath.poll();
if (migration != null) {
Queue<MigrationStep> migrationSteps = migration.steps();
executeMigrationSteps(migrationSteps);
}
private void closeDatabaseAndThrowException(Integer currentVersion, Integer targetVersion) {
// close the database
try {
database.close();
} catch (Exception e) {
throw new NitriteIOException("Failed to close the database", e);
}
throw new MigrationException("Schema version mismatch, no migration path found from version "
+ currentVersion + " to " + targetVersion);
}

private void executeMigrationPath(Queue<Migration> migrationPath) {
int pathLength = migrationPath.size();
for (int i = 0; i < pathLength; i++) {
Migration migration = migrationPath.poll();
if (migration != null) {
Queue<MigrationStep> migrationSteps = migration.steps();
executeMigrationSteps(migrationSteps);
}
}
}
Expand Down