Last active
April 22, 2018 16:11
-
-
Save plugnburn/8c3bda8d3c7bf80e951c5df977c3c4ae to your computer and use it in GitHub Desktop.
Catalite: Sqlite3-based text indexing and local search engine proof-of-concept in Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Catalite Indexer | |
# Adds new files or directories into the catalog database | |
# Usage: indexer.sh [db] [file|directory] | |
DB='' | |
openDb() { | |
DB="$*" | |
} | |
sqlExec() { | |
sqlite3 $DB "$*" | |
} | |
ensureCatalogTable() { | |
sqlExec 'CREATE TABLE IF NOT EXISTS catalog (id INTEGER PRIMARY KEY, fname VARCHAR(512), data TEXT)' | |
sqlExec 'CREATE UNIQUE INDEX IF NOT EXISTS namer ON catalog (fname)' | |
sqlExec 'CREATE UNIQUE INDEX IF NOT EXISTS searcher ON catalog (data)' | |
} | |
indexFile() { | |
local DSQ="''" | |
local file="${1//\'/$DSQ}" | |
sqlExec "INSERT OR REPLACE INTO catalog (id, fname, data) VALUES (NULL, '${file}', readfile('${file}'))" | |
} | |
indexDir() { | |
local INDIR="$1" | |
for file in $(find $INDIR -type f); do | |
indexFile "$file" | |
done | |
} | |
openDb "$1" | |
INP="$2" | |
if [[ -d "$INP" ]]; then | |
ensureCatalogTable | |
indexDir "$INP" | |
echo "Directory ${INP}: indexing complete" | |
elif [[ -f "$INP" ]]; then | |
ensureCatalogTable | |
indexFile "$INP" | |
echo "File ${INP}: indexing complete" | |
else | |
echo "Invalid file or directory" | |
exit 1 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Catalite Reader | |
# Reads record data (by its id) from the catalog database to stdout | |
# Usage: reader.sh [db] [record id] | |
sqlite3 $1 "SELECT data FROM catalog WHERE id=$2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Catalite Searcher | |
# Outputs all records (id and file name) found in the catalog by a search term | |
# Usage: searcher.sh [db] [search term] | |
DB="$1" | |
shift 1 | |
searchTerm="$*" | |
sqlite3 $DB "SELECT id, fname FROM catalog WHERE data LIKE '%${searchTerm}%'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment