-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: optimize the implementation of indexed query engine through…
… query index view (#5233) #### What type of PR is this? /kind improvement /area core /milestone 2.12.x #### What this PR does / why we need it: 通过重构 QueryIndexView 的实现方式来优化 IndexedQueryEngine 的逻辑并简化排序过程 how to test it? 单元测试通过即可,此 PR 的修改都是基于单元测试的基础上对原代码做的重构 #### Does this PR introduce a user-facing change? ```release-note None ```
- Loading branch information
Showing
18 changed files
with
738 additions
and
565 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
api/src/main/java/run/halo/app/extension/index/KeyComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package run.halo.app.extension.index; | ||
|
||
import java.util.Comparator; | ||
import org.springframework.lang.Nullable; | ||
|
||
public class KeyComparator implements Comparator<String> { | ||
public static final KeyComparator INSTANCE = new KeyComparator(); | ||
|
||
@Override | ||
public int compare(@Nullable String a, @Nullable String b) { | ||
if (a == null && b == null) { | ||
return 0; | ||
} else if (a == null) { | ||
// null less than everything | ||
return 1; | ||
} else if (b == null) { | ||
// null less than everything | ||
return -1; | ||
} | ||
|
||
int i = 0; | ||
int j = 0; | ||
while (i < a.length() && j < b.length()) { | ||
if (Character.isDigit(a.charAt(i)) && Character.isDigit(b.charAt(j))) { | ||
// handle number part | ||
int num1 = 0; | ||
int num2 = 0; | ||
while (i < a.length() && Character.isDigit(a.charAt(i))) { | ||
num1 = num1 * 10 + (a.charAt(i++) - '0'); | ||
} | ||
while (j < b.length() && Character.isDigit(b.charAt(j))) { | ||
num2 = num2 * 10 + (b.charAt(j++) - '0'); | ||
} | ||
if (num1 != num2) { | ||
return num1 - num2; | ||
} | ||
} else if (a.charAt(i) != b.charAt(j)) { | ||
// handle non-number part | ||
return a.charAt(i) - b.charAt(j); | ||
} else { | ||
i++; | ||
j++; | ||
} | ||
} | ||
return a.length() - b.length(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
api/src/main/java/run/halo/app/extension/index/query/IsNotNull.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import java.util.NavigableSet; | ||
|
||
public class IsNotNull extends SimpleQuery { | ||
|
||
protected IsNotNull(String fieldName) { | ||
super(fieldName, null); | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
return indexView.getAllIdsForField(fieldName); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
api/src/main/java/run/halo/app/extension/index/query/IsNull.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import java.util.NavigableSet; | ||
|
||
public class IsNull extends SimpleQuery { | ||
|
||
protected IsNull(String fieldName) { | ||
super(fieldName, null); | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
var allIds = indexView.getAllIds(); | ||
var idsForField = indexView.getAllIdsForField(fieldName); | ||
allIds.removeAll(idsForField); | ||
return allIds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.