Skip to content

Commit

Permalink
feat: add new key comparator for index entry
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Jan 15, 2024
1 parent f880bd4 commit 191f974
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,47 @@ public IndexEntryImpl(IndexDescriptor indexDescriptor) {
Comparator<String> keyComparator() {
var order = indexDescriptor.getSpec().getOrder();
if (IndexSpec.OrderType.ASC.equals(order)) {
return Comparator.naturalOrder();
return KeyComparator.INSTANCE;
} else if (IndexSpec.OrderType.DESC.equals(order)) {
return Comparator.reverseOrder();
} else {
throw new IllegalArgumentException("Invalid order: " + order);
}
}

static class KeyComparator implements Comparator<String> {
public static final KeyComparator INSTANCE = new KeyComparator();

@Override
public int compare(String a, String b) {
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();
}
}

@Override
public void acquireReadLock() {
this.rwl.readLock().lock();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package run.halo.app.extension.index;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -105,4 +108,66 @@ void keyOrder() {
Map.entry("slug-4", "fake-name-3"));
assertThat(entry2.indexedKeys()).containsSequence("slug-1", "slug-2", "slug-3", "slug-4");
}

@Test
void keyComparator() {
var comparator = IndexEntryImpl.KeyComparator.INSTANCE;
String[] strings = {"103", "101", "102", "1011", "1013", "1021", "1022", "1012", "1023"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(
new String[] {"101", "102", "103", "1011", "1012", "1013", "1021", "1022", "1023"});

Arrays.sort(strings, comparator.reversed());
assertThat(strings).isEqualTo(
new String[] {"1023", "1022", "1021", "1013", "1012", "1011", "103", "102", "101"});

// but if we use natural order, the result is:
Arrays.sort(strings, Comparator.naturalOrder());
assertThat(strings).isEqualTo(
new String[] {"101", "1011", "1012", "1013", "102", "1021", "1022", "1023", "103"});
}

@Test
void keyComparator2() {
var comparator = IndexEntryImpl.KeyComparator.INSTANCE;
String[] strings =
{"moment-101", "moment-102", "moment-103", "moment-1011", "moment-1013", "moment-1021",
"moment-1022", "moment-1012", "moment-1023"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(new String[] {"moment-101", "moment-102", "moment-103",
"moment-1011", "moment-1012", "moment-1013", "moment-1021", "moment-1022",
"moment-1023"});

// 测试日期排序
strings =
new String[] {"2022-01-15", "2022-02-01", "2021-12-25", "2022-01-01", "2022-01-02"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(
new String[] {"2021-12-25", "2022-01-01", "2022-01-02", "2022-01-15", "2022-02-01"});

// 测试字母和数字混合排序
strings = new String[] {"abc123", "abc45", "abc9", "abc100", "abc20"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(
new String[] {"abc9", "abc20", "abc45", "abc100", "abc123"});

// 测试纯字母排序
strings = new String[] {"xyz", "abc", "def", "abcde", "xyzabc"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(new String[] {"abc", "abcde", "def", "xyz", "xyzabc"});

// 测试空字符串
strings = new String[] {"", "abc", "123", "xyz"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(new String[] {"", "123", "abc", "xyz"});

// 测试相同字符串
strings = new String[] {"abc", "abc", "abc", "abc"};
Arrays.sort(strings, comparator);
assertThat(strings).isEqualTo(new String[] {"abc", "abc", "abc", "abc"});

// 测试 null 元素
assertThatThrownBy(() -> Arrays.sort(new String[] {null, "abc", "123", "xyz"}, comparator))
.isInstanceOf(NullPointerException.class);
}
}

0 comments on commit 191f974

Please sign in to comment.