Skip to content

Commit e3d6005

Browse files
authored
Code smells/refactored to remove broken hierarchy code smell, long method ,decompose conditional and introduce explaining variable (#1120)
* Refactor(Pull-up method):Pull-up toStringValue to StringFilter for hierarchy enhancement * Extract StringFilterHelper for modularity * Add applyOnString to StringFilter for polymorphism * Refactor MigrationManager.doMigrate() with Extract Method, Decompose Conditional, and Introduce Explaining Variable
1 parent 2f8659f commit e3d6005

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

nitrite/src/main/java/org/dizitart/no2/filters/StringFilter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* @since 1.0
2424
*/
2525
public abstract class StringFilter extends ComparableFilter {
26+
private final StringFilterHelper helper = new StringFilterHelper();
27+
2628
/**
2729
* Instantiates a new String filter.
2830
*
@@ -41,4 +43,15 @@ protected StringFilter(String field, String value) {
4143
public String getStringValue() {
4244
return (String) getValue();
4345
}
46+
47+
/**
48+
* Converts an object to a string safely.
49+
* @param obj the object to convert
50+
* @return the string representation, or empty if null
51+
*/
52+
protected String toStringValue(Object obj) {
53+
return helper.toStringValue(obj);
54+
}
55+
56+
public abstract boolean applyOnString(String value);
4457
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.dizitart.no2.filters;
2+
3+
public class StringFilterHelper {
4+
public String toStringValue(Object obj) {
5+
return obj != null ? obj.toString() : "";
6+
}
7+
}

nitrite/src/main/java/org/dizitart/no2/filters/TextFilter.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,34 @@ public class TextFilter extends StringFilter {
4848
super(field, value);
4949
}
5050

51+
@Override
52+
public boolean applyOnString(String value) {
53+
String searchString = (String) this.getValue();
54+
if (searchString.startsWith("*") || searchString.endsWith("*")) {
55+
searchString = searchString.replace("*", "");
56+
}
57+
return value.toLowerCase().contains(searchString.toLowerCase());
58+
}
59+
60+
/**
61+
* Applies this filter to a document, checking if the specified field contains the search string.
62+
*
63+
* @param element the document pair to filter
64+
* @return true if the field value contains the search string (case-insensitive), false otherwise
65+
* @throws FilterException if the field is not a string
66+
*/
5167
@Override
5268
public boolean apply(Pair<NitriteId, Document> element) {
5369
notNull(getField(), "field cannot be null");
5470
notNull(getStringValue(), "search term cannot be null");
55-
String searchString = getStringValue();
5671
Object docValue = element.getSecond().get(getField());
5772

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

6277
String docString = (String) docValue;
63-
64-
if (searchString.startsWith("*") || searchString.endsWith("*")) {
65-
searchString = searchString.replace("*", "");
66-
}
67-
68-
return docString.toLowerCase().contains(searchString.toLowerCase());
78+
return applyOnString(docString);
6979
}
7080

7181
@Override
@@ -194,4 +204,5 @@ private LinkedHashSet<NitriteId> sortedIdsByScore(Map<NitriteId, Integer> unsort
194204
public List<?> applyOnIndex(IndexMap indexMap) {
195205
return null;
196206
}
207+
197208
}

nitrite/src/main/java/org/dizitart/no2/migration/MigrationManager.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,37 @@ public MigrationManager(Nitrite nitrite) {
4848
*/
4949
public void doMigrate() {
5050
if (isMigrationNeeded()) {
51-
Integer existingVersion = storeMetadata.getSchemaVersion();
52-
Integer incomingVersion = nitriteConfig.getSchemaVersion();
53-
54-
Queue<Migration> migrationPath = findMigrationPath(existingVersion, incomingVersion);
55-
if (migrationPath == null || migrationPath.isEmpty()) {
56-
// close the database
57-
try {
58-
database.close();
59-
} catch (Exception e) {
60-
throw new NitriteIOException("Failed to close the database", e);
61-
}
51+
Integer currentVersion = storeMetadata.getSchemaVersion();
52+
Integer targetVersion = nitriteConfig.getSchemaVersion();
53+
54+
Queue<Migration> migrationPath = findMigrationPath(currentVersion, targetVersion);
55+
boolean hasValidPath = migrationPath != null && !migrationPath.isEmpty();
6256

63-
throw new MigrationException("Schema version mismatch, no migration path found from version "
64-
+ storeMetadata.getSchemaVersion() + " to " + nitriteConfig.getSchemaVersion());
57+
if (!hasValidPath) {
58+
closeDatabaseAndThrowException( currentVersion, targetVersion);
6559
}
60+
executeMigrationPath(migrationPath);
61+
}
62+
}
6663

67-
int length = migrationPath.size();
68-
for (int i = 0; i < length; i++) {
69-
Migration migration = migrationPath.poll();
70-
if (migration != null) {
71-
Queue<MigrationStep> migrationSteps = migration.steps();
72-
executeMigrationSteps(migrationSteps);
73-
}
64+
private void closeDatabaseAndThrowException(Integer currentVersion, Integer targetVersion) {
65+
// close the database
66+
try {
67+
database.close();
68+
} catch (Exception e) {
69+
throw new NitriteIOException("Failed to close the database", e);
70+
}
71+
throw new MigrationException("Schema version mismatch, no migration path found from version "
72+
+ currentVersion + " to " + targetVersion);
73+
}
74+
75+
private void executeMigrationPath(Queue<Migration> migrationPath) {
76+
int pathLength = migrationPath.size();
77+
for (int i = 0; i < pathLength; i++) {
78+
Migration migration = migrationPath.poll();
79+
if (migration != null) {
80+
Queue<MigrationStep> migrationSteps = migration.steps();
81+
executeMigrationSteps(migrationSteps);
7482
}
7583
}
7684
}

0 commit comments

Comments
 (0)