Skip to content

Commit

Permalink
fix: 1300000xxxx can not be found; optimize: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
EeeMt committed Aug 7, 2020
1 parent 7b967d6 commit c43c37c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
6 changes: 5 additions & 1 deletion CHANGLOG
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
## 1.0.6-202004

- 修复一个bug https://github.com/EeeMt/phone-number-geo/issues/1
- 优化单元测试
- 优化单元测试

## 1.0.7-202004
- 修复1300000xxxx无法查询的问题
- 修复单元测试缺陷
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.ihxq.projects</groupId>
<artifactId>phone-number-geo</artifactId>
<version>1.0.6-202004</version>
<version>1.0.7-202004</version>
<packaging>jar</packaging>
<name>phone-number-geo</name>
<url>https://github.com/EeeMt/phone-number-geo</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public void loadData(byte[] data) {
indicesEndOffset = originalByteBuffer.limit();
}

/**
* 对齐
*/
private int alignPosition(int pos) {
int remain = (pos - indicesStartOffset) % 9;
if (pos - indicesStartOffset < 9) {
return pos - remain;
} else if (remain != 0) {
return pos + 9 - remain;
} else {
return pos;
}
}

@SuppressWarnings("DuplicatedCode")
@Override
public Optional<PhoneNumberInfo> lookup(String phoneNo) {
Expand All @@ -56,6 +70,7 @@ public Optional<PhoneNumberInfo> lookup(String phoneNo) {
int left = indicesStartOffset;
int right = indicesEndOffset;
int mid = (left + right) / 2;
mid = alignPosition(mid);
while (mid >= left && mid <= right) {
if (mid == right) {
return Optional.empty();
Expand All @@ -70,6 +85,7 @@ public Optional<PhoneNumberInfo> lookup(String phoneNo) {

if (compare > 0) {
int tempMid = (mid + left) / 2;
tempMid = alignPosition(tempMid);
right = mid;
int remain = (tempMid - indicesStartOffset) % 9;
if (tempMid - indicesStartOffset < 9) {
Expand All @@ -83,6 +99,7 @@ public Optional<PhoneNumberInfo> lookup(String phoneNo) {
}
} else {
int tempMid = (mid + right) / 2;
tempMid = alignPosition(tempMid);
left = mid;
int remain = (tempMid - indicesStartOffset) % 9;
if (tempMid - indicesStartOffset < 9) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public void loadData(byte[] data) {
/**
* 对齐
*/
private int strictMid(int mid) {
int remain = (mid - indicesStartOffset) % 9;
if (mid - indicesStartOffset < 9) {
return mid - remain;
private int alignPosition(int pos) {
int remain = (pos - indicesStartOffset) % 9;
if (pos - indicesStartOffset < 9) {
return pos - remain;
} else if (remain != 0) {
return mid + 9 - remain;
return pos + 9 - remain;
} else {
return mid;
return pos;
}
}

Expand Down Expand Up @@ -78,26 +78,24 @@ public Optional<PhoneNumberInfo> lookup(String phoneNumber) {
int left = indicesStartOffset;
int right = indicesEndOffset;
int mid = (left + right) / 2;
mid = strictMid(mid);
mid = alignPosition(mid);
while (mid >= left && mid <= right) {
if (mid == right) {
return Optional.empty();
}
int compare = compare(mid, attributionIdentity, byteBuffer);
if (mid == left) {
return Optional.empty();
}

if (compare == 0) {
return extract(phoneNumber, mid, byteBuffer);
} else if (mid == left) {
return Optional.empty();
} else if (compare > 0) {
int tempMid = (mid + left) / 2;
right = mid;
mid = strictMid(tempMid);
mid = alignPosition(tempMid);
} else {
int tempMid = (mid + right) / 2;
left = mid;
mid = strictMid(tempMid);
mid = alignPosition(tempMid);
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public void loadData(byte[] data) {
/**
* 对齐
*/
private int strictMid(int mid) {
int remain = (mid - indicesStartOffset) % 9;
if (mid - indicesStartOffset < 9) {
return mid - remain;
private int alignPosition(int pos) {
int remain = (pos - indicesStartOffset) % 9;
if (pos - indicesStartOffset < 9) {
return pos - remain;
} else if (remain != 0) {
return mid + 9 - remain;
return pos + 9 - remain;
} else {
return mid;
return pos;
}
}

Expand Down Expand Up @@ -79,26 +79,25 @@ public Optional<PhoneNumberInfo> lookup(String phoneNumber) {
int right = indicesEndOffset;
int attributionIdentityPrefix = attributionIdentity / 100_000;
int mid = indicesStartOffset + ((indicesEndOffset - indicesStartOffset) / 7 * (attributionIdentityPrefix - 13));
mid = strictMid(mid);
mid = alignPosition(mid);
while (mid >= left && mid <= right) {
if (mid == right) {
return Optional.empty();
}
int compare = compare(mid, attributionIdentity, byteBuffer);
if (mid == left) {
return Optional.empty();
}

if (compare == 0) {
return extract(phoneNumber, mid, byteBuffer);
} else if (mid == left) {
return Optional.empty();
} else if (compare > 0) {
int tempMid = (mid + left) / 2;
right = mid;
mid = strictMid(tempMid);
mid = alignPosition(tempMid);
} else {
int tempMid = (mid + right) / 2;
left = mid;
mid = strictMid(tempMid);
mid = alignPosition(tempMid);
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public void concurrencyLookup(LookupAlgorithm algorithm) {
@MethodSource("algorithms")
public void lookupFirst(LookupAlgorithm algorithm) {
PhoneNumberLookup phoneNumberLookup = constructLookup(algorithm);
assertNotNull(phoneNumberLookup.lookup("13000000000"));
assertTrue(phoneNumberLookup.lookup("13000000000").isPresent());
}

@ParameterizedTest
@MethodSource("algorithms")
public void lookupLast(LookupAlgorithm algorithm) {
PhoneNumberLookup phoneNumberLookup = constructLookup(algorithm);
assertNotNull(phoneNumberLookup.lookup("19999790000"));
assertTrue(phoneNumberLookup.lookup("19999790000").isPresent());
}

@ParameterizedTest
Expand Down

0 comments on commit c43c37c

Please sign in to comment.