-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_operations.sh
50 lines (36 loc) · 1.33 KB
/
index_operations.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
function create_index() {
local collection="$1"
local field="$2"
local value="$3"
local record_file="$4"
local index_dir="${DATA_DIR}/${collection}/indexes"
mkdir -p "$index_dir" || handle_error "Failed to create index directory: $index_dir" 1
local index_file="${index_dir}/${field}"
echo "$value:$record_file" >> "$index_file" || handle_error "Failed to create index entry for field: [$field] on record: [$record_file]" 1
}
function search_index() {
local collection="$1"
local record_id="$2"
local index_file="${DATA_DIR}/${collection}/indexes/id"
[[ ! -e "$index_file" ]] && handle_error "Index file does not exist: $index_file" 1
local record_file
record_file=$(grep "^$record_id:" "$index_file" | cut -d':' -f2-)
[[ -z "$record_file" ]] && handle_error "Record not found in index: $record_id" 1
echo "$record_file"
}
function get_indexed_fields() {
local collection="$1"
local index_dir="${DATA_DIR}/${collection}/indexes"
local indexed_fields=""
if [[ -d "$index_dir" ]]; then
for index_file in "$index_dir"/*; do
if [[ -f "$index_file" ]]; then
local field
field=$(basename "$index_file")
indexed_fields+=" $field"
fi
done
fi
echo "$indexed_fields"
}