networklocation: add offline WiFi + cell WPSDB stores with online fallback
- Commit
376d5b4272c3b317bd2de3b0eb4ecbe7d2b798ce- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-11 21:50 UTC (3 months ago)
- Parents
2b2522f
Diff
networklocation/build.gradle.kts
diff --git a/networklocation/build.gradle.kts b/networklocation/build.gradle.ktsindex 368d20bc18..f345e1e50a 100644--- a/networklocation/build.gradle.kts+++ b/networklocation/build.gradle.kts@@ -16,9 +16,10 @@ android { applicationId = "com.vayunmathur.networklocation" } androidResources {- // The geocoder DB must stay uncompressed in the APK so it can be mmap'd directly from- // the asset file descriptor (no unzip, no copy to filesDir).+ // The geocoder DB and the WPSDB stores must stay uncompressed in the APK so they can+ // be mmap'd / pread directly from the asset file descriptor (no unzip, no copy to filesDir). noCompress += "geodb"+ noCompress += "wpsdb" } } @@ -90,6 +91,68 @@ tasks.matching { it.name == "preBuild" }.configureEach { dependsOn(fetchGeocoderDb) } +// The offline WPSDB stores (wifi.wpsdb up to ~2 GB, cells.wpsdb far smaller) are not committed+// (see src/main/assets/.gitignore). Fetch them into assets at build time exactly like the+// geocoder DB: only download when absent, fail soft (warn, leave no partial file) so a network+// hiccup or an intentionally DB-less dev build still compiles and simply runs online-only.+val wpsStoreBaseUrl = "https://data.vayunmathur.com/wps/"+val wpsStoreNames = listOf("wifi.wpsdb", "cells.wpsdb")+val wpsAssetsDir = layout.projectDirectory.dir("src/main/assets").asFile++val fetchWpsStores = tasks.register("fetchWpsStores") {+ // Capture plain String/File locals so the task actions don't hold references to the build+ // script itself — Gradle's configuration cache cannot serialize script object refs.+ val baseUrl = wpsStoreBaseUrl+ val names = wpsStoreNames+ val assetsDir = wpsAssetsDir+ description = "Downloads wifi.wpsdb and cells.wpsdb into src/main/assets if they are missing."+ val outFiles = names.map { File(assetsDir, it) }+ outputs.files(*outFiles.toTypedArray())+ outputs.upToDateWhen { outFiles.all { it.exists() } }+ doLast {+ for (name in names) {+ val outFile = File(assetsDir, name)+ if (outFile.exists()) {+ logger.lifecycle("$name already present (${outFile.length()} bytes); skipping download.")+ continue+ }+ outFile.parentFile.mkdirs()+ val url = baseUrl + name+ val tmp = File(outFile.parentFile, "$name.part")+ tmp.delete()+ logger.lifecycle("Fetching $name from $url (this may be large; first build only)…")+ try {+ val conn = (URI(url).toURL().openConnection() as HttpURLConnection).apply {+ connectTimeout = 30_000+ readTimeout = 60_000+ instanceFollowRedirects = true+ }+ val code = conn.responseCode+ if (code != HttpURLConnection.HTTP_OK) {+ conn.disconnect()+ error("server returned HTTP $code")+ }+ conn.inputStream.use { input -> tmp.outputStream().use { output -> input.copyTo(output, 1 shl 20) } }+ conn.disconnect()+ tmp.renameTo(outFile)+ logger.lifecycle("Fetched $name (${outFile.length()} bytes).")+ } catch (e: Exception) {+ tmp.delete()+ logger.warn(+ "WARNING: could not fetch {} ({}). The APK will build WITHOUT this offline store; " ++ "the provider falls back to online lookups for those beacons. Build it locally " ++ "(wtfps-experiment/RUNBOOK.md) or check {} and rebuild.",+ name, e.message, url,+ )+ }+ }+ }+}++tasks.matching { it.name == "preBuild" }.configureEach {+ dependsOn(fetchWpsStores)+}+ dependencies { // Compile-only stubs for the framework's unbundled provider API (com.android.location.provider). // Provided at runtime by <uses-library>; must NOT be packaged.
networklocation/src/main/assets/.gitignore
diff --git a/networklocation/src/main/assets/.gitignore b/networklocation/src/main/assets/.gitignoreindex 0aed251b84..612ed14731 100644--- a/networklocation/src/main/assets/.gitignore+++ b/networklocation/src/main/assets/.gitignore@@ -2,3 +2,8 @@ # with tools/extract-osm-addresses.sh + the GeocoderGenerator, then upload to # data.vayunmathur.com/geocoder/ ; release builds fetch it back into assets at build time. *.geodb++# The offline WPSDB stores (wifi.wpsdb up to ~2 GB; cells.wpsdb far smaller) are NOT committed.+# Build them with wtfps-experiment (see RUNBOOK.md), upload to data.vayunmathur.com/wps/ ;+# release builds fetch them back into assets at build time (fetchWpsStores).+*.wpsdb
networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.ktnew file mode 100644index 0000000000..531a1f0966--- /dev/null+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/WpsStoreNative.kt@@ -0,0 +1,35 @@+package com.vayunmathur.networklocation++/**+ * JNI bridge to the native offline WPSDB reader in the `networklocation` Rust library+ * (see networklocation/src/main/rust/src/wpsdb.rs). It answers exact key → coordinate+ * lookups over a bundled `WPSDB1` store opened straight from an APK asset fd (no copy to+ * disk), and serves both offline stores the app ships:+ * * `wifi.wpsdb` — 48-bit MAC (BSSID) key → coord+ * * `cells.wpsdb` — 64-bit packed cell key → coord+ *+ * A single generic reader handles both because the WPSDB1 Elias–Fano header carries the key+ * universe width; the key is always passed as a [Long] (a 48-bit MAC and a 64-bit packed+ * cell key both fit).+ *+ * The fully-qualified name MUST stay `com.vayunmathur.networklocation.WpsStoreNative` so the+ * JNI symbol mangling (`Java_com_vayunmathur_networklocation_WpsStoreNative_*`) matches.+ */+object WpsStoreNative {+ /** Whether the `.so` loaded. Guarded so host/unit contexts degrade gracefully. */+ val available: Boolean = runCatching { System.loadLibrary("networklocation") }.isSuccess++ /**+ * Open a WPSDB store from an APK asset file descriptor. [fd] is the (uncompressed) asset's+ * fd, [offset] its start offset within the APK; [length] is reserved for future validation.+ * Returns an opaque handle, or 0 on failure. The native side dups [fd], so the caller may+ * close its own descriptor after this returns.+ */+ external fun open(fd: Int, offset: Long, length: Long): Long++ /** Coordinate `[lat, lon]` for [key], or null if the store does not contain [key]. */+ external fun lookup(handle: Long, key: Long): DoubleArray?++ /** Free the handle and its underlying descriptor. Safe to call with 0. */+ external fun close(handle: Long)+}
networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.ktindex 3e0249cc9e..7b8a91410c 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt@@ -35,6 +35,7 @@ class LocationReportingTask( private val wifi = NearbyWifi(appContext) private val cells = NearbyCells(appContext) private val cache = BeaconCache(appContext)+ private val offlineStore = OfflineBeaconStore(appContext) private val apple = ApplePositioningService() // gs-loc is only consulted for beacons we have never seen; still throttle it so a // burst of new APs (e.g. moving fast) cannot spam Apple.@@ -57,6 +58,7 @@ class LocationReportingTask( fun stop() { scope?.cancel() scope = null+ offlineStore.close() } private suspend fun reportOnce() {@@ -92,8 +94,28 @@ class LocationReportingTask( val cached = cache.get(allIds) val resolved = ArrayList(cached.values) - val missingWifi = wifiIds.filter { it !in cached }- val missingCell = cellIds.filter { it !in cached }+ var missingWifi = wifiIds.filter { it !in cached }+ var missingCell = cellIds.filter { it !in cached }++ // Offline store tier: resolve locally before ever touching the network. Hits are+ // written back into BeaconCache so the in-memory/Room tier front-runs later scans.+ if (missingWifi.isNotEmpty() || missingCell.isNotEmpty()) {+ val offline = withContext(Dispatchers.Default) {+ buildList {+ if (missingWifi.isNotEmpty()) addAll(offlineStore.resolveWifi(missingWifi).values)+ if (missingCell.isNotEmpty()) addAll(offlineStore.resolveCell(missingCell).values)+ }+ }+ if (offline.isNotEmpty()) {+ cache.put(offline)+ resolved.addAll(offline)+ val hitIds = offline.mapTo(HashSet()) { it.id }+ missingWifi = missingWifi.filterNot { it in hitIds }+ missingCell = missingCell.filterNot { it in hitIds }+ }+ }++ // gs-loc fallback: only for beacons still unknown after cache + offline store. if ((missingWifi.isNotEmpty() || missingCell.isNotEmpty()) && networkThrottle.tryAcquire()) { val fresh = withContext(Dispatchers.IO) { buildList {
networklocation/src/main/java/com/vayunmathur/networklocation/provider/OfflineBeaconStore.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/provider/OfflineBeaconStore.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/provider/OfflineBeaconStore.ktnew file mode 100644index 0000000000..4e7b1968b6--- /dev/null+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/provider/OfflineBeaconStore.kt@@ -0,0 +1,134 @@+package com.vayunmathur.networklocation.provider++import android.content.Context+import android.content.res.AssetFileDescriptor+import com.vayunmathur.networklocation.BeaconFix+import com.vayunmathur.networklocation.BeaconId+import com.vayunmathur.networklocation.WpsStoreNative+import java.io.IOException++/**+ * Offline beacon → coordinate resolver over the two bundled WPSDB stores+ * (`wifi.wpsdb`, `cells.wpsdb`), opened straight from their APK asset fds via+ * [WpsStoreNative] (native Rust reader, no copy to disk) — the same pattern as the+ * offline geocoder in `GeocodeService`.+ *+ * Degrades gracefully: if an asset is missing or the native library did not load, the+ * corresponding handle stays 0 and every lookup misses, so the provider falls back to+ * pure-online behaviour and a DB-less dev build still works.+ *+ * The 48-bit MAC packing and 64-bit cell-key packing here MUST stay byte-for-byte identical+ * to `wtfps-experiment/store.py` (`parse_mac` / `pack_cell`), which builds the stores — see+ * FORMAT.md "Cell key packing".+ */+class OfflineBeaconStore(context: Context) {+ private val appContext = context.applicationContext++ private var wifiAfd: AssetFileDescriptor? = null+ private var cellAfd: AssetFileDescriptor? = null+ private var wifiHandle = 0L+ private var cellHandle = 0L++ // Serializes lookups against close(): native close() frees the reader (Box::from_raw),+ // so it must not run while another thread is inside WpsStoreNative.lookup on that handle.+ private val lock = Any()++ init {+ if (WpsStoreNative.available) {+ wifiHandle = openAsset(WIFI_ASSET) { wifiAfd = it }+ cellHandle = openAsset(CELL_ASSET) { cellAfd = it }+ }+ }++ private fun openAsset(name: String, keep: (AssetFileDescriptor) -> Unit): Long = try {+ val fd = appContext.assets.openFd(name)+ keep(fd)+ WpsStoreNative.open(fd.parcelFileDescriptor.fd, fd.startOffset, fd.length)+ } catch (_: IOException) {+ 0L+ }++ /** Resolve WiFi APs present in the offline store. Absent MACs are simply omitted. */+ fun resolveWifi(bssids: List<BeaconId.Wifi>): Map<BeaconId.Wifi, BeaconFix> {+ if (bssids.isEmpty()) return emptyMap()+ synchronized(lock) {+ if (wifiHandle == 0L) return emptyMap()+ val out = HashMap<BeaconId.Wifi, BeaconFix>()+ for (id in bssids) {+ val key = parseMac(id.bssid) ?: continue+ val r = WpsStoreNative.lookup(wifiHandle, key) ?: continue+ if (r.size >= 2) out[id] = BeaconFix(id, r[0], r[1], OFFLINE_ACCURACY_METERS)+ }+ return out+ }+ }++ /** Resolve cell towers present in the offline store. Absent towers are omitted. */+ fun resolveCell(cells: List<BeaconId.Cell>): Map<BeaconId.Cell, BeaconFix> {+ if (cells.isEmpty()) return emptyMap()+ synchronized(lock) {+ if (cellHandle == 0L) return emptyMap()+ val out = HashMap<BeaconId.Cell, BeaconFix>()+ for (id in cells) {+ val r = WpsStoreNative.lookup(cellHandle, packCell(id)) ?: continue+ if (r.size >= 2) out[id] = BeaconFix(id, r[0], r[1], OFFLINE_ACCURACY_METERS)+ }+ return out+ }+ }++ /**+ * Release both handles and their descriptors. Idempotent. Serialized with the resolvers so+ * it never frees a native reader that a concurrent lookup is still dereferencing.+ */+ fun close() {+ synchronized(lock) {+ if (wifiHandle != 0L) {+ WpsStoreNative.close(wifiHandle)+ wifiHandle = 0L+ }+ if (cellHandle != 0L) {+ WpsStoreNative.close(cellHandle)+ cellHandle = 0L+ }+ wifiAfd?.close()+ wifiAfd = null+ cellAfd?.close()+ cellAfd = null+ }+ }++ private companion object {+ const val WIFI_ASSET = "wifi.wpsdb"+ const val CELL_ASSET = "cells.wpsdb"++ // The stores quantize coordinates to a ~20 m global grid (see quantize.py); use a+ // fixed accuracy radius matching that precision for every offline fix.+ const val OFFLINE_ACCURACY_METERS = 20.0++ // Cell key packing — MUST match store.pack_cell:+ // key = (mcc<<54) | (mnc<<44) | (tac<<28) | (cellId & 0x0FFFFFFF)+ // mcc:10 | mnc:10 | tac:16 | cellId:28 (= 64 bits)+ // 5G NCI cell ids (36-bit) are truncated to 28 bits; LTE/UMTS/GSM fit.+ const val CID_MASK = 0x0FFF_FFFFL++ fun packCell(id: BeaconId.Cell): Long =+ ((id.mcc.toLong() and 0x3FF) shl 54) or+ ((id.mnc.toLong() and 0x3FF) shl 44) or+ ((id.tacOrLac.toLong() and 0xFFFF) shl 28) or+ (id.cellId.toLong() and CID_MASK)++ /** Parse "aa:bb:cc:dd:ee:ff" to a 48-bit key (mirrors store.parse_mac); null if bad. */+ fun parseMac(bssid: String): Long? {+ val parts = bssid.split(":")+ if (parts.size != 6) return null+ var v = 0L+ for (p in parts) {+ val b = p.toIntOrNull(16) ?: return null+ if (b < 0 || b > 0xFF) return null+ v = (v shl 8) or b.toLong()+ }+ return v+ }+ }+}
networklocation/src/main/rust/src/lib.rs
diff --git a/networklocation/src/main/rust/src/lib.rs b/networklocation/src/main/rust/src/lib.rsindex 1b4d8d3315..e1f3bbfb7f 100644--- a/networklocation/src/main/rust/src/lib.rs+++ b/networklocation/src/main/rust/src/lib.rs@@ -21,6 +21,7 @@ mod jni; pub mod measurement; pub mod multilateration; pub mod position;+pub mod wpsdb; /// Estimated position. pub struct EstimatedPosition {
networklocation/src/main/rust/src/wpsdb.rs
diff --git a/networklocation/src/main/rust/src/wpsdb.rs b/networklocation/src/main/rust/src/wpsdb.rsnew file mode 100644index 0000000000..ea611611bb--- /dev/null+++ b/networklocation/src/main/rust/src/wpsdb.rs@@ -0,0 +1,486 @@+//! Offline key → coordinate lookup over a packed `WPSDB1` store, exposed via JNI.+//!+//! One generic reader serves both offline stores the app ships (mirroring the offline+//! geocoder pattern in `geocoder.rs`):+//! * `wifi.wpsdb` — 48-bit MAC (BSSID) key → coord+//! * `cells.wpsdb` — 64-bit packed cell key → coord+//!+//! The on-disk format is produced by `wtfps-experiment/store.py` (see FORMAT.md). All+//! multi-byte scalars are little-endian; the bit-packed arrays are LSB-first within each+//! byte (bit `k` of a value lives at `buf[p >> 3] & (1 << (p & 7))`).+//!+//! ```text+//! magic "WPSDB1\0\0" 8 B+//! coord_bits:u32, n:u64+//! EF header: n:u64, l:u8, universe_bits:u8, high_len_bits:u64+//! high_len:u64, high[high_len]+//! low_len:u64, low[low_len]+//! coord: clen:u64, coord[clen] (n * coord_bits bits, bit-packed)+//! ```+//!+//! Membership + index come from the Elias–Fano upper bitvector (`high`), which is held in+//! memory (tens of MB at world scale) with a sampled `select0` index built at open. The+//! `low` and `coord` arrays are read on demand via positional reads straight from the APK+//! asset fd, so device memory stays bounded even for a ~2 GB store.++use std::fs::File;+use std::os::unix::fs::FileExt;+use std::os::unix::io::FromRawFd;++use jni::objects::JClass;+use jni::sys::{jdoubleArray, jint, jlong};+use jni::JNIEnv;++const MAGIC: &[u8; 8] = b"WPSDB1\x00\x00";++// 20 m global grid (mirrors quantize.py): lat 20 bits over [-90,90], lon 21 bits over+// [-180,180]. coord_bits in the header is expected to equal LAT_BITS + LON_BITS.+const LAT_BITS: u32 = 20;+const LON_BITS: u32 = 21;++// select0 sample stride, in bits: `zero_samples[b]` holds the number of zero bits in+// `high[0 .. b*SELECT_SAMPLE)`. 4096 keeps the index small while bounding the in-block+// scan of a select0 to <= 4096 bit tests.+const SELECT_SAMPLE: u64 = 4096;++// --------------------------------------------------------------------------- byte source+/// Positional reader over a region of a file (the APK asset), starting at `base`.+struct Src {+ file: File,+ base: u64,+}+impl Src {+ fn read(&self, pos: u64, len: usize) -> Option<Vec<u8>> {+ let mut b = vec![0u8; len];+ self.file.read_exact_at(&mut b, self.base + pos).ok()?;+ Some(b)+ }+ fn rd_u32(&self, pos: u64) -> Option<u32> {+ let b = self.read(pos, 4)?;+ Some(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))+ }+ fn rd_u64(&self, pos: u64) -> Option<u64> {+ let b = self.read(pos, 8)?;+ Some(u64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]))+ }+ fn rd_u8(&self, pos: u64) -> Option<u8> {+ Some(self.read(pos, 1)?[0])+ }+}++// --------------------------------------------------------------------------- reader+pub struct Reader {+ src: Src,+ coord_bits: u32,+ n: u64,+ l: u8,+ universe_bits: u8,+ high_len_bits: u64,+ high: Vec<u8>,+ /// `zero_samples[b]` = number of zero bits in `high[0 .. b*SELECT_SAMPLE)`.+ zero_samples: Vec<u64>,+ low_off: u64,+ coord_off: u64,+}++impl Reader {+ fn open(fd: i32, offset: i64) -> Option<Reader> {+ let dupfd = unsafe { libc::dup(fd) };+ if dupfd < 0 {+ return None;+ }+ let file = unsafe { File::from_raw_fd(dupfd) };+ Reader::from_src(Src { file, base: offset as u64 })+ }++ /// Open a `.wpsdb` straight from a filesystem path (base offset 0). Test-only: the+ /// production path receives an APK asset fd + offset via `open`.+ #[cfg(test)]+ fn open_path<P: AsRef<std::path::Path>>(path: P) -> Option<Reader> {+ let file = File::open(path).ok()?;+ Reader::from_src(Src { file, base: 0 })+ }++ fn from_src(src: Src) -> Option<Reader> {+ if &src.read(0, 8)?[..] != &MAGIC[..] {+ return None;+ }+ let coord_bits = src.rd_u32(8)?;+ let n = src.rd_u64(12)?;++ // The coord codec is fixed at the 20 m grid (LAT_BITS + LON_BITS); reject any store+ // built with a different resolution rather than silently mis-decoding coordinates.+ if coord_bits != LAT_BITS + LON_BITS {+ return None;+ }++ // EF header ("<QBBQ"): n (repeated), l, universe_bits, high_len_bits.+ let l = src.rd_u8(28)?;+ let universe_bits = src.rd_u8(29)?;+ let high_len_bits = src.rd_u64(30)?;++ let high_len = src.rd_u64(38)?;+ let high = src.read(46, high_len as usize)?;++ let low_len_off = 46 + high_len;+ let low_len = src.rd_u64(low_len_off)?;+ let low_off = low_len_off + 8;++ let clen_off = low_off + low_len;+ let _clen = src.rd_u64(clen_off)?;+ let coord_off = clen_off + 8;++ // Sanity: the upper bitvector must have exactly n set bits, and hold enough bytes.+ if (high.len() as u64) < high_len_bits.div_ceil(8) {+ return None;+ }++ let zero_samples = build_zero_samples(&high, high_len_bits, n);++ Some(Reader {+ src,+ coord_bits,+ n,+ l,+ universe_bits,+ high_len_bits,+ high,+ zero_samples,+ low_off,+ coord_off,+ })+ }++ #[inline]+ fn high_bit(&self, p: u64) -> bool {+ (self.high[(p >> 3) as usize] >> (p & 7)) & 1 == 1+ }++ /// Position of the zero bit whose 0-indexed rank is `j` (i.e. the `(j+1)`-th zero), or+ /// `None` when fewer than `j+1` zeros exist.+ fn select0(&self, j: u64) -> Option<u64> {+ let total_zeros = self.high_len_bits - self.n;+ if j >= total_zeros {+ return None;+ }+ // Largest block b with zero_samples[b] <= j.+ let mut lo = 0usize;+ let mut hi = self.zero_samples.len() - 1;+ while lo < hi {+ let mid = (lo + hi + 1) / 2;+ if self.zero_samples[mid] <= j {+ lo = mid;+ } else {+ hi = mid - 1;+ }+ }+ let mut count = self.zero_samples[lo];+ let mut p = (lo as u64) * SELECT_SAMPLE;+ while p < self.high_len_bits {+ if !self.high_bit(p) {+ if count == j {+ return Some(p);+ }+ count += 1;+ }+ p += 1;+ }+ None+ }++ /// Read `nbits` bits (LSB-first) starting at bit `bit_index` within the section at file+ /// byte offset `section_off`. `nbits <= 64`.+ fn read_bits(&self, section_off: u64, bit_index: u64, nbits: u32) -> Option<u64> {+ if nbits == 0 {+ return Some(0);+ }+ let first_bit = (bit_index & 7) as u64;+ let byte_pos = section_off + (bit_index >> 3);+ let nbytes = ((first_bit + nbits as u64 + 7) / 8) as usize;+ let buf = self.src.read(byte_pos, nbytes)?;+ let mut v = 0u64;+ for k in 0..nbits as u64 {+ let p = first_bit + k;+ if (buf[(p >> 3) as usize] >> (p & 7)) & 1 == 1 {+ v |= 1u64 << k;+ }+ }+ Some(v)+ }++ #[inline]+ fn low(&self, i: u64) -> Option<u64> {+ self.read_bits(self.low_off, i * self.l as u64, self.l as u32)+ }++ #[inline]+ fn coord(&self, i: u64) -> Option<u64> {+ self.read_bits(self.coord_off, i * self.coord_bits as u64, self.coord_bits)+ }++ /// Elias–Fano index of `key`, or `None` if `key` is not in the set.+ fn index(&self, key: u64) -> Option<u64> {+ let l = self.l as u32;+ let hi = if l >= 64 { 0 } else { key >> l };+ let lo = if l == 0 {+ 0+ } else if l >= 64 {+ key+ } else {+ key & ((1u64 << l) - 1)+ };++ // i0 = number of keys with upper < hi; `start` = first high-bit position of bucket hi.+ let (mut i, start) = if hi == 0 {+ (0u64, 0u64)+ } else {+ let s0 = self.select0(hi - 1)?;+ let start = s0 + 1;+ (start - hi, start)+ };++ let mut p = start;+ let mut bucket = hi;+ while p < self.high_len_bits {+ if self.high_bit(p) {+ if bucket != hi {+ break;+ }+ if self.low(i)? == lo {+ return Some(i);+ }+ i += 1;+ } else {+ bucket += 1;+ if bucket > hi {+ break;+ }+ }+ p += 1;+ }+ None+ }++ /// Look up `key`; returns `(lat, lon)` or `None` for an unknown key.+ fn lookup(&self, key: u64) -> Option<(f64, f64)> {+ let i = self.index(key)?;+ let code = self.coord(i)?;+ Some(decode(code))+ }+}++/// Build the sampled `select0` index by scanning the upper bitvector once at open.+/// Padding bits beyond `high_len_bits` in the final byte are NOT counted as zeros.+fn build_zero_samples(high: &[u8], high_len_bits: u64, _n: u64) -> Vec<u64> {+ let mut samples = Vec::with_capacity((high_len_bits / SELECT_SAMPLE + 2) as usize);+ samples.push(0u64); // zeros before bit 0+ let mut zeros = 0u64;+ let mut bitpos = 0u64;+ 'outer: for &byte in high {+ for bit in 0..8u64 {+ if bitpos >= high_len_bits {+ break 'outer;+ }+ if (byte >> bit) & 1 == 0 {+ zeros += 1;+ }+ bitpos += 1;+ if bitpos % SELECT_SAMPLE == 0 {+ samples.push(zeros);+ }+ }+ }+ samples+}++/// Decode a packed coordinate (20 m grid) to `(lat, lon)` — mirrors `quantize.decode`.+fn decode(code: u64) -> (f64, f64) {+ let lat_steps = (1u64 << LAT_BITS) as f64;+ let lon_steps = 1u64 << LON_BITS;+ let li = code >> LON_BITS;+ let oi = code & (lon_steps - 1);+ let lat = (li as f64 + 0.5) / lat_steps * 180.0 - 90.0;+ let lon = (oi as f64 + 0.5) / lon_steps as f64 * 360.0 - 180.0;+ (lat, lon)+}++// --------------------------------------------------------------------------- JNI+type Handle = Reader;++/// `open(fd, offset, length) -> handle` (0 on failure). The native side dups `fd`, so the+/// caller may close its own descriptor after this returns. `length` is reserved (the section+/// framing bounds every read) and kept for API symmetry with `GeocoderNative`.+#[no_mangle]+pub extern "system" fn Java_com_vayunmathur_networklocation_WpsStoreNative_open<'l>(+ _env: JNIEnv<'l>,+ _class: JClass<'l>,+ fd: jint,+ offset: jlong,+ _length: jlong,+) -> jlong {+ match Reader::open(fd, offset) {+ Some(r) => Box::into_raw(Box::new(r)) as jlong,+ None => 0,+ }+}++/// `lookup(handle, key) -> double[2]` (`[lat, lon]`) or null for an unknown key. `key` is a+/// 64-bit value: a 48-bit WiFi MAC or a 64-bit packed cell key both fit.+#[no_mangle]+pub extern "system" fn Java_com_vayunmathur_networklocation_WpsStoreNative_lookup<'l>(+ mut env: JNIEnv<'l>,+ _class: JClass<'l>,+ handle: jlong,+ key: jlong,+) -> jdoubleArray {+ if handle == 0 {+ return std::ptr::null_mut();+ }+ let reader = unsafe { &*(handle as *const Handle) };+ match reader.lookup(key as u64) {+ Some((lat, lon)) => {+ let arr = match env.new_double_array(2) {+ Ok(a) => a,+ Err(_) => return std::ptr::null_mut(),+ };+ if env.set_double_array_region(&arr, 0, &[lat, lon]).is_err() {+ return std::ptr::null_mut();+ }+ arr.into_raw()+ }+ None => std::ptr::null_mut(),+ }+}++/// `close(handle)` — frees the reader and its dup'd fd. Safe to call with 0.+#[no_mangle]+pub extern "system" fn Java_com_vayunmathur_networklocation_WpsStoreNative_close<'l>(+ _env: JNIEnv<'l>,+ _class: JClass<'l>,+ handle: jlong,+) {+ if handle != 0 {+ unsafe {+ drop(Box::from_raw(handle as *mut Handle));+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use std::collections::HashMap;++ /// Locate a real store: `$WPSDB_TEST`, else the committed ~1 MB WiFi sample.+ fn db_path() -> Option<String> {+ if let Ok(p) = std::env::var("WPSDB_TEST") {+ return Some(p);+ }+ let bundled =+ concat!(env!("CARGO_MANIFEST_DIR"), "/../../../../wtfps-experiment/world_store.bin");+ if std::path::Path::new(bundled).exists() {+ Some(bundled.to_string())+ } else {+ eprintln!("skip: no WPSDB sample (set WPSDB_TEST or add world_store.bin)");+ None+ }+ }++ fn open_db() -> Option<Reader> {+ let path = db_path()?;+ match Reader::open_path(&path) {+ Some(r) => Some(r),+ None => panic!("WPSDB at {path} exists but failed magic/parse"),+ }+ }++ /// Reconstruct every (index, key) pair by walking the Elias–Fano upper bitvector — the+ /// inverse of `index()` — so tests can cross-check membership and coord decode against+ /// exactly what the Python builder wrote.+ fn reconstruct(r: &Reader) -> Vec<u64> {+ let l = r.l as u32;+ let mut keys = Vec::with_capacity(r.n as usize);+ let mut idx = 0u64;+ let mut p = 0u64;+ while p < r.high_len_bits && idx < r.n {+ if r.high_bit(p) {+ let upper = p - idx;+ let lo = r.low(idx).unwrap();+ let key = if l >= 64 { lo } else { (upper << l) | lo };+ keys.push(key);+ idx += 1;+ }+ p += 1;+ }+ keys+ }++ #[test]+ fn structure_is_sane() {+ let r = match open_db() {+ Some(r) => r,+ None => return,+ };+ assert!(r.n > 0, "record count must be positive");+ assert_eq!(r.universe_bits, 48, "WiFi sample must have a 48-bit universe");+ assert_eq!(r.coord_bits, LAT_BITS + LON_BITS, "coord_bits must be 41");+ assert!(r.high_len_bits >= r.n, "high bitvector shorter than key count");+ }++ /// Every reconstructed key resolves to its own index, and `lookup` decodes the same+ /// coordinate as reading `coord[i]` directly. This proves the Rust EF/select0 + coord+ /// decode matches the builder byte-for-byte.+ #[test]+ fn known_keys_resolve() {+ let r = match open_db() {+ Some(r) => r,+ None => return,+ };+ let keys = reconstruct(&r);+ assert_eq!(keys.len() as u64, r.n, "reconstructed key count mismatch");+ // ascending & distinct (EF invariant)+ for w in keys.windows(2) {+ assert!(w[0] < w[1], "keys not strictly ascending");+ }+ let step = (keys.len() / 3000).max(1);+ for (i, &key) in keys.iter().enumerate().step_by(step) {+ let idx = r.index(key).unwrap_or_else(|| panic!("known key {key:#x} rejected"));+ assert_eq!(idx as usize, i, "index mismatch for {key:#x}");+ let (lat, lon) = r.lookup(key).expect("known key resolves");+ let (elat, elon) = decode(r.coord(i as u64).unwrap());+ assert_eq!((lat, lon), (elat, elon), "lookup != direct coord decode");+ assert!((-90.0..=90.0).contains(&lat), "lat out of range: {lat}");+ assert!((-180.0..=180.0).contains(&lon), "lon out of range: {lon}");+ }+ }++ /// Random unknown keys are all rejected — zero false positives, mirroring+ /// `store.py::_self_test`.+ #[test]+ fn unknown_keys_rejected() {+ let r = match open_db() {+ Some(r) => r,+ None => return,+ };+ let known: HashMap<u64, ()> = reconstruct(&r).into_iter().map(|k| (k, ())).collect();+ // Simple deterministic LCG so the test needs no rand dep at runtime.+ let mut state = 0x1234_5678_9abc_def0u64;+ let mut next = || {+ state = state.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);+ state+ };+ let universe_mask = if r.universe_bits >= 64 {+ u64::MAX+ } else {+ (1u64 << r.universe_bits) - 1+ };+ let mut fp = 0;+ for _ in 0..20_000 {+ let key = next() & universe_mask;+ if !known.contains_key(&key) && r.lookup(key).is_some() {+ fp += 1;+ }+ }+ assert_eq!(fp, 0, "expected zero false positives, got {fp}");+ }+}