introduce bitloops
This commit is contained in:
@@ -37,19 +37,19 @@ public class Meta {
|
||||
|
||||
// --- Lookup: w -> i using mmap ---
|
||||
static int findIndexInMapMmap(Path mapFile, long target) throws IOException {
|
||||
try (FileChannel ch = FileChannel.open(mapFile, StandardOpenOption.READ)) {
|
||||
MappedByteBuffer mbb = (MappedByteBuffer) ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size()).order(ORDER);
|
||||
try (var ch = FileChannel.open(mapFile, StandardOpenOption.READ)) {
|
||||
var mbb = (MappedByteBuffer) ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size()).order(ORDER);
|
||||
|
||||
int magic = mbb.getInt(0);
|
||||
int ver = mbb.getInt(4);
|
||||
int n = mbb.getInt(8);
|
||||
var magic = mbb.getInt(0);
|
||||
var ver = mbb.getInt(4);
|
||||
var n = mbb.getInt(8);
|
||||
if (magic != MAP_MAGIC || ver != VERSION) throw new IOException("Bad map file");
|
||||
|
||||
int lo = 0, hi = n - 1;
|
||||
while (lo <= hi) {
|
||||
int mid = (lo + hi) >>> 1;
|
||||
int off = 12 + mid * 8;
|
||||
long key = mbb.getLong(off);
|
||||
var mid = (lo + hi) >>> 1;
|
||||
var off = 12 + mid * 8;
|
||||
var key = mbb.getLong(off);
|
||||
|
||||
if (key < target) lo = mid + 1;
|
||||
else if (key > target) hi = mid - 1;
|
||||
@@ -61,41 +61,41 @@ public class Meta {
|
||||
|
||||
// --- Read record i from shard.data (your format) ---
|
||||
static ShardLem readRecord(Path shardFile, long w, int i) throws IOException {
|
||||
try (FileChannel ch = FileChannel.open(shardFile, StandardOpenOption.READ)) {
|
||||
ByteBuffer hdr = ByteBuffer.allocate(12).order(ORDER);
|
||||
try (var ch = FileChannel.open(shardFile, StandardOpenOption.READ)) {
|
||||
var hdr = ByteBuffer.allocate(12).order(ORDER);
|
||||
ch.read(hdr);
|
||||
hdr.flip();
|
||||
int magic = hdr.getInt();
|
||||
int ver = hdr.getInt();
|
||||
int n = hdr.getInt();
|
||||
var magic = hdr.getInt();
|
||||
var ver = hdr.getInt();
|
||||
var n = hdr.getInt();
|
||||
if (magic != SHARD_MAGIC || ver != VERSION) throw new IOException("Bad shard file");
|
||||
if (i < 0 || i >= n) throw new IndexOutOfBoundsException();
|
||||
|
||||
long tableStart = 12L;
|
||||
long dataStart = 12L + (long) n * 4L;
|
||||
var tableStart = 12L;
|
||||
var dataStart = 12L + (long) n * 4L;
|
||||
|
||||
int offI = readIntAt(ch, tableStart + (long) i * 4L);
|
||||
int offIp = (i + 1 < n)
|
||||
var offI = readIntAt(ch, tableStart + (long) i * 4L);
|
||||
var offIp = (i + 1 < n)
|
||||
? readIntAt(ch, tableStart + (long) (i + 1) * 4L)
|
||||
: (int) (ch.size() - dataStart);
|
||||
|
||||
int len = offIp - offI;
|
||||
ByteBuffer buf = ByteBuffer.allocate(len);
|
||||
var len = offIp - offI;
|
||||
var buf = ByteBuffer.allocate(len);
|
||||
ch.position(dataStart + offI);
|
||||
ch.read(buf);
|
||||
buf.flip();
|
||||
|
||||
String s = StandardCharsets.UTF_8.decode(buf).toString();
|
||||
String[] parts = s.split("\t", 3);
|
||||
var s = StandardCharsets.UTF_8.decode(buf).toString();
|
||||
var parts = s.split("\t", 3);
|
||||
|
||||
int simpel = Integer.parseInt(parts[1]);
|
||||
String[] clues = GSON.fromJson(parts[2], String[].class);
|
||||
var simpel = Integer.parseInt(parts[1]);
|
||||
var clues = GSON.fromJson(parts[2], String[].class);
|
||||
return new ShardLem(w, simpel, clues);
|
||||
}
|
||||
}
|
||||
|
||||
static int readIntAt(FileChannel ch, long pos) throws IOException {
|
||||
ByteBuffer b = ByteBuffer.allocate(4).order(ORDER);
|
||||
var b = ByteBuffer.allocate(4).order(ORDER);
|
||||
ch.position(pos);
|
||||
ch.read(b);
|
||||
b.flip();
|
||||
@@ -105,10 +105,10 @@ public class Meta {
|
||||
// --- Demo main ---
|
||||
public static ShardLem lookup(long w) {
|
||||
try {
|
||||
int i = findIndexInMapMmap(shardMap, Lemma.pack43(w));
|
||||
var i = findIndexInMapMmap(shardMap, Lemma.pack43(w));
|
||||
System.out.println("\nQuery: w=" + w + " -> i=" + i);
|
||||
if (i >= 0) {
|
||||
ShardLem rec = readRecord(shardData, w, i);
|
||||
var rec = readRecord(shardData, w, i);
|
||||
System.out.println(" simpel=" + rec.simpel());
|
||||
System.out.println(" clues=" + Arrays.toString(rec.clues()));
|
||||
return rec;
|
||||
|
||||
Reference in New Issue
Block a user