Skip to content

Commit ecbbe48

Browse files
修复最后一行记录搜索不到的问题
1 parent c19132d commit ecbbe48

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ Thumbs.db
3131
# Temporary files
3232
*.tmp
3333
*.temp
34+
dependency-reduced-pom.xml
35+
.DS_Store

src/main/java/net/cz88/czdb/DbSearcher.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ private DataBlock memorySearch(byte[] ip) {
355355

356356
// Calculate the number of index blocks in the search range
357357
// Initialize the search range
358-
int l = 0, h = (eptr - sptr) / blockLen - 1;
358+
int l = 0, h = (eptr - sptr) / blockLen;
359359

360360
// The start IP and end IP of the current index block
361361
byte[] sip = new byte[ipBytesLength], eip = new byte[ipBytesLength];
@@ -368,6 +368,12 @@ private DataBlock memorySearch(byte[] ip) {
368368
while (l <= h) {
369369
int m = (l + h) >> 1;
370370
int p = (int) (sptr + m * blockLen);
371+
372+
// eip 的指针已经超过 eptr 了,说明已经搜索到最后一页并且没找到,直接退出
373+
if (p + ipBytesLength > eptr && m == 1) {
374+
break;
375+
}
376+
371377
System.arraycopy(dbBinStr, p, sip, 0, ipBytesLength);
372378
System.arraycopy(dbBinStr, p + ipBytesLength, eip, 0, ipBytesLength);
373379

@@ -466,7 +472,7 @@ private DataBlock bTreeSearch(byte[] ip) throws IOException {
466472
raf.readFully(iBuffer, 0, iBuffer.length);
467473

468474
int l = 0;
469-
int h = blockLen / blen - 1;
475+
int h = blockLen / blen;
470476
byte[] sip = new byte[ipBytesLength], eip = new byte[ipBytesLength];
471477

472478
int dataPtr = 0;
@@ -475,6 +481,14 @@ private DataBlock bTreeSearch(byte[] ip) throws IOException {
475481
while (l <= h) {
476482
int m = (l + h) >> 1;
477483
int p = m * blen;
484+
485+
// 如果 p 的指针已经超过 blockLen 了,说明已经搜索到最后一页并且没找到,直接退出
486+
// m == 1说明是在header中搜索时没有直接找到,返回了最后一个段的情况,这个段是不满的,所以要这样处理下
487+
// TODO: 也可能有更好的办法
488+
if (p >= blockLen && m == 1) {
489+
break;
490+
}
491+
478492
System.arraycopy(iBuffer, p, sip, 0, ipBytesLength);
479493
System.arraycopy(iBuffer, p + ipBytesLength, eip, 0, ipBytesLength);
480494

0 commit comments

Comments
 (0)