-
Notifications
You must be signed in to change notification settings - Fork 3
HathiTrust Database Build Process
This document explains how the local HathiTrust database is built and used for offline book reconciliation.
The HathiTrust strategy uses a local SQLite database instead of making live API calls. This provides:
- Fast searching - Full-text search via SQLite FTS5 is much faster than remote API calls
- Offline capability - Works without internet once the database is built
- No rate limits - Search as much as you need without API restrictions
The database must be built before using the HathiTrust reconciliation strategy. This is a one-time process that downloads and processes the full HathiTrust metadata dump.
The database build process (lib/strategies_hathitrust_build_db.py) runs in three main stages:
- Fetches the HathiTrust file list from
https://www.hathitrust.org/files/hathifiles/hathi_file_list.json - Scans for the newest "full" dump file (contains complete metadata, not incremental updates)
- Returns the download URL for the latest dump
- Downloads the gzipped TSV file (typically 1-2 GB compressed)
- Saves to the HathiTrust data directory
- Progress is tracked and reported via status updates
This is the most complex stage:
Creates two tables in hathitrust.db:
records table - Stores the consolidated book metadata:
| Column | Description |
|---|---|
ht_bib_key |
Primary key (HathiTrust bibliographic key) |
htid |
HathiTrust item IDs (pipe-delimited if multiple) |
access |
Access level (allow/deny) |
description |
Volume/edition description |
oclc_num |
OCLC numbers (pipe-delimited) |
isbn |
ISBNs (pipe-delimited) |
issn |
ISSNs (pipe-delimited) |
lccn |
Library of Congress Control Numbers (pipe-delimited) |
title |
Book title |
rights_date_used |
Publication/rights date |
lang |
Language codes (pipe-delimited) |
author |
Author name |
author_title table - FTS5 full-text search index:
| Column | Description |
|---|---|
rowid |
Links to ht_bib_key in records table |
author |
Author name (searchable) |
title |
Title (searchable) |
The dump file contains one line per item (physical/digital copy), but multiple items can belong to the same work (bibliographic record). The build process:
-
Filters records - Only keeps:
- Books (
bib_fmt = 'BK') - Records with an author (skips anonymous works)
- Books (
-
Consolidates items - Groups items by
ht_bib_key:- Collects all
htidvalues into a list - Aggregates OCLC numbers, ISBNs, ISSNs, LCCNs, languages
- Uses the best access level (if any item allows access, the record shows "allow")
- Collects all
-
Batches to disk - Writes records in batches of 50,000 to temporary pickle files to manage memory usage
- Reads each batch file and inserts records into SQLite
- Populates both the
recordstable and the FTS5author_titleindex - Commits every 10,000 records for durability
- Cleans up batch files and the downloaded gzip after completion
The database is stored in the application's data directory:
| Platform | Location |
|---|---|
| macOS | ~/Library/Application Support/BookReconciler/data/hathi/hathitrust.db |
| Windows | %APPDATA%\BookReconciler\data\hathi\hathitrust.db |
| Linux | ~/.bookreconciler/data/hathi/hathitrust.db |
| Development | PROJECT_ROOT/data/hathi/hathitrust.db |
Typical build results (as of the code comments):
| Metric | Count |
|---|---|
| Total lines in dump | ~18.8 million |
| Lines after filtering (books with authors) | ~10 million |
| Final consolidated records | ~7.3 million |
The build process takes approximately 30-60 minutes depending on download speed and system performance.
The build process writes status updates to build_status.json in the HathiTrust data directory. Status includes:
{
"status": "processing",
"message": "Processed 500000 records from 750000 lines",
"timestamp": "2024-01-15T10:30:00.000000",
"progress": 500000,
"total": 7300000
}Status values:
-
starting- Initializing -
finding_dump- Searching for latest dump URL -
found_dump- Found dump file -
downloading- Downloading dump file -
download_complete- Download finished -
preparing- Creating database tables -
processing- Reading and inserting records -
finalizing- Final database commit -
complete- Build successful -
error- Build failed (check message for details)
When you use the HathiTrust reconciliation strategy:
-
FTS5 Search - The
author_titlevirtual table is searched using SQLite's full-text search:SELECT rowid FROM author_title WHERE author MATCH ? AND title MATCH ? ORDER BY rank;
-
Retrieve Records - Matching
rowidvalues are used to fetch full records from therecordstable -
Fuzzy Scoring - Results are scored using token sort ratio (same as other strategies) to rank matches
To rebuild the database (e.g., to get updated HathiTrust data):
- The build process automatically removes any existing database before starting
- Trigger a new build from the web interface or API
- The old database is replaced with the new one upon successful completion
The download may have failed. Check your internet connection and try again.
The download was corrupted. Delete any partial downloads in the HathiTrust data directory and try again.
- Verify the database exists and is not empty
- Check that SQLite FTS5 is available (the code verifies this at startup)
- Try broader search terms
The database is typically 2-4 GB. This is normal given the ~7 million records with full-text search indexes.