Created
January 6, 2022 16:12
-
-
Save XCaminhante/ea47f4df66e1104f8caf7d8bc23012aa to your computer and use it in GitHub Desktop.
Banco de dados de documentos
This file contains 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 | |
#@+leo-ver=5-thin | |
#@+node:caminhante.20220104131025.1: * @file binstore.sh | |
#@@first | |
#@+others | |
#@+node:caminhante.20220106042659.1: ** Misc | |
#@+node:caminhante.20220104131041.1: *3* function hash | |
function hash { | |
echo -n "$@" | md5sum | cut -d' ' -f1 | |
} | |
#@+node:caminhante.20220106022220.1: *3* function _cut | |
function _cut { | |
echo "$1" | cut -b"$2" | |
} | |
#@+node:caminhante.20220106020413.1: *3* function store-split | |
function store-split { | |
echo "$(_cut $1 1-4)/$(_cut $1 5-8)/$(_cut $1 9-12)/$(_cut $1 13-16)/$(_cut $1 17-20)/$(_cut $1 21-24)/$(_cut $1 25-28)/$(_cut $1 29-32)" | |
} | |
#@+node:caminhante.20220104131419.1: *3* function hashstore | |
function hashstore { | |
local class="$1" | |
shift | |
local h=`hash $@` | |
mkdir -p "$class/$(dirname `store-split "$h"`)/" | |
echo "$class/$(store-split "$h")" | |
} | |
#@+node:caminhante.20220103001917.1: *3* function random | |
function random { | |
xxd -l 16 -c 16 -p < /dev/urandom | |
} | |
#@+node:caminhante.20220103002140.1: *3* function latest-modified | |
function latest-modified { | |
find "$1" -maxdepth 1 -type d -printf "%TFT%TTZ%Tz %p\n" | sort -r | cut -d' ' -f2- | |
} | |
#@+node:caminhante.20220104142030.1: *3* function utcnanodate | |
function utcnanodate { | |
date -u '+%FT%T.%NZ' | |
} | |
#@+node:caminhante.20220104170152.1: *3* function void | |
function void { | |
cat > /dev/null | |
} | |
#@+node:caminhante.20220105174105.1: *3* function atomic-write | |
function atomic-write { | |
local tmp=".tmp$(random)" | |
local dest="$1" | |
cat > "$tmp" | |
mv -f "$tmp" "$dest" | |
} | |
#@+node:caminhante.20220104132503.1: ** Key-value List store | |
# # Examples: | |
# echo "content" | store-keyvalue list key | |
# load-keyvalue list key | cat | |
# remove-keyvalue list key | |
# if keyvalue-exists list key; then ... fi | |
#@+others | |
#@+node:caminhante.20220104132020.1: *3* function store-keyvalue | |
function store-keyvalue { | |
local list="$1" | |
local key="$2" | |
local listpath=`list-store-path "$list"` | |
( echo key:$key; cat ) | atomic-write `list-store-item-path "$listpath" "$key"` | |
} | |
#@+node:caminhante.20220104132225.1: *3* function load-keyvalue | |
function load-keyvalue { | |
local list="$1" | |
local key="$2" | |
local listpath=`list-store-path "$list"` | |
tail -n+2 `list-store-item-path "$listpath" "$key"` | |
} | |
#@+node:caminhante.20220104132311.1: *3* function remove-keyvalue | |
function remove-keyvalue { | |
local list="$1" | |
local key="$2" | |
local listpath=`list-store-path "$list"` | |
[ -z "$key" ] || rm -f `list-store-item-path "$listpath" "$key"` | |
} | |
#@+node:caminhante.20220104132325.1: *3* function keyvalue-exists | |
function keyvalue-exists { | |
local list="$1" | |
local key="$2" | |
local listpath=`list-store-path "$list"` | |
test -e `list-store-item-path "$listpath" "$key"` | |
} | |
#@-others | |
#@+node:caminhante.20220106013643.1: ** Indexed List store | |
# # Examples: | |
# echo "content" | store-list-metadata list | |
# load-list-metadata | cat | |
# echo "content" | append-item list | |
# grep-items list 'a[bc]|def' | cat | |
#@+others | |
#@+node:caminhante.20220106014000.1: *3* function list-store-path | |
function list-store-path { | |
local list="$1" | |
hashstore "./i" "$list" | |
} | |
#@+node:caminhante.20220106014905.1: *3* function list-store-item-path | |
function list-store-item-path { | |
local listpath="$1" | |
local key="$2" | |
hashstore "$listpath" "$key" | |
} | |
#@+node:caminhante.20220106014912.1: *3* function list-store-index-path | |
function list-store-index-path { | |
local listpath="$1" | |
local index="$2" | |
hashstore "$listpath/index" "$index" | |
} | |
#@+node:caminhante.20220106013914.1: *3* function increment-items-count | |
function increment-items-count { | |
local listpath="$1" | |
mkdir -p "$listpath" | |
local cfile="$listpath/count" | |
{ flock -x 3 | |
echo >>"$cfile" | |
printf "%32s" `wc -c "$cfile" | cut -d' ' -f1` | |
} 3>>"$cfile" | |
} | |
#@+node:caminhante.20220106033207.1: *3* function store-list-metadata | |
function store-list-metadata { | |
local list="$1" | |
local listpath=`list-store-path "$list"` | |
atomic-write "$listpath/metadata" | |
} | |
#@+node:caminhante.20220106033216.1: *3* function load-list-metadata | |
function load-list-metadata { | |
local list="$1" | |
local listpath=`list-store-path "$list"` | |
cat "$listpath/metadata" | |
} | |
#@+node:caminhante.20220106024347.1: *3* function items-count | |
function items-count { | |
local list="$1" | |
local listpath=`list-store-path "$list"` | |
local cfile="$listpath/count" | |
{ flock -s 3 | |
wc -c "$cfile" | cut -d' ' -f1 | |
} 3<"$cfile" | |
} | |
#@+node:caminhante.20220106013658.1: *3* function append-item | |
function append-item { | |
local list="$1" | |
local key=`utcnanodate` | |
local listpath=`list-store-path "$list"` | |
local index=`increment-items-count "$listpath"` | |
local file=`list-store-item-path "$listpath" "$key"` | |
( echo key:$key; cat ) | atomic-write "$file" | |
ln "$file" "$(list-store-index-path "$listpath" "$index")" | |
echo $key | |
} | |
#@+node:caminhante.20220106025945.1: *3* function item-of-index | |
function item-of-index { | |
local list="$1" | |
local index="$(printf "%32s" "$2")" | |
local listpath=`list-store-path "$list"` | |
local indexf=`list-store-index-path "$listpath" "$index"` | |
tail -n+2 "$indexf" | |
} | |
#@+node:caminhante.20220106031734.1: *3* function item-of-key | |
function item-of-key { | |
local list="$1" | |
local key="$2" | |
local listpath=`list-store-path "$list"` | |
local file=`list-store-item-path "$listpath" "$key"` | |
tail -n+2 "$file" | |
} | |
#@+node:caminhante.20220106032639.1: *3* function key-of-index | |
function key-of-index { | |
local list="$1" | |
local index="$(printf "%32s" "$2")" | |
local listpath=`list-store-path "$list"` | |
local indexf=`list-store-index-path "$listpath" "$index"` | |
head -n1 "$indexf" | cut -d: -f2- | |
} | |
#@+node:caminhante.20220106033047.1: *3* function list-items | |
function list-items { | |
local list="$1" | |
local count=`items-count "$list"` | |
for ((i=1;i<=count;i++)); do | |
item-of-index "$list" $i | |
done | |
} | |
#@+node:caminhante.20220106033053.1: *3* function grep-items | |
function grep-items { | |
local list="$1" | |
local regex="$2" | |
local listpath=`list-store-path "$list"` | |
local count=`items-count "$list"` | |
for ((i=1;i<=count;i++)); do | |
local index="$(printf "%32s" "$i")" | |
echo $index | |
local indexf=`list-store-index-path "$listpath" "$index"` | |
tail -n+2 "$indexf" | grep -m1 -qE -- "$regex" && tail -n+2 "$indexf" | |
done | |
} | |
#@-others | |
#@-others | |
#@-leo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment