CaptureIndex: add paged queries by URL key

The index only looks up the latest capture of a single URL. Add a
query for all captures whose URL key equals or starts with a given key,
returned in batches so that no cursor stays open between them.
This commit is contained in:
Alex Osborne committed 2026-11-26 17:39:10 +09:00
1 parent bdaffcc077
commit d9999f804b
3 files changed
+69

No files matched your search

@@ -26,8 +26,10 @@ import static org.archive.modules.recrawl.RecrawlAttributeConstants.A_WARC_FILEN
import static org.archive.modules.recrawl.RecrawlAttributeConstants.A_WARC_FILE_OFFSET;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -51,6 +53,7 @@ import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.CursorConfig;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.OperationStatus;
@@ -192,6 +195,32 @@ public class BdbCaptureIndex implements CaptureIndex, Lifecycle {
return find(urlKey(canonicalUri), c -> true);
}
@Override
public List<Capture> query(String urlKey, boolean exact, Capture after, int limit) {
byte[] prefix = new TupleOutput().writeString(urlKey).toByteArray();
if (!exact) {
// Omit the tuple string terminator to include longer URL keys.
prefix = Arrays.copyOf(prefix, prefix.length - 1);
}
DatabaseEntry key = new DatabaseEntry(prefix);
if (after != null) {
var last = key(after.urlKey(), after.time());
// Composite keys end with a fixed-width timestamp. An extra byte seeks past it.
key.setData(Arrays.copyOf(last.getData(), last.getSize() + 1));
}
var value = new DatabaseEntry();
List<Capture> batch = new ArrayList<>();
try (Cursor cursor = captures.openCursor(null, CursorConfig.READ_COMMITTED)) {
OperationStatus status = cursor.getSearchKeyRange(key, value, null);
while (status == OperationStatus.SUCCESS && startsWith(key, prefix)) {
batch.add(decode(key, value));
if (batch.size() == limit) break;
status = cursor.getNext(key, value, null);
}
}
return batch;
}
private Capture find(String urlKey, Predicate<Capture> predicate) {
byte[] prefix = new TupleOutput().writeString(urlKey).toByteArray();
var key = new DatabaseEntry(prefix);
@@ -19,6 +19,8 @@
package org.archive.modules.index;
import java.util.List;
import org.archive.io.warc.WARCRecordInfo;
import org.archive.modules.CrawlURI;
@@ -42,4 +44,15 @@ public interface CaptureIndex {
* @param canonicalUri the URL as canonicalized by the frontier
*/
Capture latest(String canonicalUri);
/**
* Returns captures in index order: by URL key, then newest first. Call again with the last
* capture returned as {@code after} to get the next batch.
*
* @param urlKey the URL key to match
* @param exact whether the URL key must match exactly rather than as a prefix
* @param after the capture to start after, or null to start at the beginning
* @param limit the maximum number of captures to return
*/
List<Capture> query(String urlKey, boolean exact, Capture after, int limit);
}
@@ -119,6 +119,33 @@ public class BdbCaptureIndexTest {
assertEquals(3000, index.latest("http://example.org/a").time());
}
@Test
void pagesExactAndPrefixQueriesInIndexOrder() throws Exception {
String url = "http://example.org/a";
String key = "http://(org,example,)/a";
var response = List.of(record(WARCRecordType.response, UUID.randomUUID(), 100, 100));
int batchSize = 512;
int count = batchSize + 1;
for (int time = 1; time <= count; time++) {
index.add(curi(url, time), response);
}
index.add(curi(url + "b", 1), response);
index.add(curi("http://other.org/", 1), response);
var first = index.query(key, true, null, batchSize);
assertEquals(count - 1, first.size());
for (int i = 0; i < first.size(); i++) assertEquals(count - i, first.get(i).time());
Capture after = first.get(first.size() - 1);
var last = index.query(key, true, after, batchSize);
assertEquals(1, last.size());
assertEquals(1, last.get(0).time());
assertTrue(index.query(key, true, last.get(0), batchSize).isEmpty());
assertEquals(List.of(url, url + "b"), index.query(key, false, after, batchSize)
.stream().map(Capture::url).toList());
assertEquals(3, index.query("", false, after, batchSize).size());
assertTrue(index.query(key + "/missing", true, null, batchSize).isEmpty());
}
@Test
void skipsNonGetAndUncanonicalized() throws Exception {
CrawlURI post = curi("http://example.org/post", 1000);