MCPcopy Create free account
hub / github.com/Luce-Org/lucebox-hub / lookup

Method lookup

server/src/server/disk_prefix_cache.cpp:369–420  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

367 if (nlen < 4 || sname.compare(nlen - 4, 4, ".dkv") != 0) continue;
368
369 // Read the header to get the layout_id.
370 std::string fpath = se.path().string();
371 FILE * f = std::fopen(fpath.c_str(), "rb");
372 if (!f) continue;
373
374 DiskCacheHeader hdr{};
375 if (read_header(f, hdr) && std::memcmp(hdr.magic, "DKVC", 4) == 0) {
376 std::memcpy(layout_id_.data(), hdr.layout_id, 16);
377 layout_known_ = true;
378 layout_from_disk_ = true; // unverified — must be confirmed by learn_layout()
379 layout_dir_ = subdir;
380 std::fclose(f);
381 scan_directory();
382 return;
383 }
384 std::fclose(f);
385 }
386 }
387}
388
389// ─── Lookup ─────────────────────────────────────────────────────────────
390
391bool DiskPrefixCache::lookup(const std::vector<int32_t> & prompt_ids, int slot) {
392 if (disabled() || !layout_known_) return false;
393
394 PrefixHash hash = hash_prefix(prompt_ids.data(), (int)prompt_ids.size());
395
396 std::lock_guard<std::mutex> lock(mu_);
397 int idx = find_entry(hash);
398 if (idx < 0) return false;
399
400 auto & entry = entries_[idx];
401 if (!read_file(entry.path, slot)) {
402 // File is corrupt or incompatible — remove it.
403 std::fprintf(stderr, "[disk-cache] lookup failed, removing %s\n",
404 entry.path.c_str());
405 std::remove(entry.path.c_str());
406 total_bytes_ -= entry.file_size;
407 entries_.erase(entries_.begin() + idx);
408 return false;
409 }
410
411 if (layout_from_disk_) {
412 const std::array<uint8_t, 16> disk_id = layout_id_;
413 auto ref = backend_.snapshot_ref(slot);
414 if (!ref.ctx) {
415 backend_.snapshot_free(slot);
416 return false;
417 }
418 compute_layout_id(ref.ctx);
419 if (std::memcmp(disk_id.data(), layout_id_.data(), 16) != 0) {
420 std::fprintf(stderr,
421 "[disk-cache] adopted layout mismatch: disk=%s model=%s\n",
422 hex(disk_id.data(), 16).c_str(),
423 hex(layout_id_.data(), 16).c_str());

Callers

nothing calls this directly

Calls 9

hash_prefixFunction · 0.85
hexFunction · 0.85
mkdir_pFunction · 0.85
now_unixFunction · 0.85
clearMethod · 0.80
dataMethod · 0.45
sizeMethod · 0.45
snapshot_refMethod · 0.45
snapshot_freeMethod · 0.45

Tested by

no test coverage detected