-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_manager.sh
93 lines (80 loc) · 3.34 KB
/
transaction_manager.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
function generate_transaction_id() {
echo "$(date +%s%N)_$$"
}
function begin_transaction() {
local transaction_id
transaction_id=$(generate_transaction_id)
mkdir -p "${DATA_DIR}/transactions/${transaction_id}"
echo "$transaction_id"
}
function log_transaction() {
local transaction_id="$1"
local operation="$2"
local collection="$3"
local record_id="$4"
echo "${operation}:${collection}:${record_id}" >> "${DATA_DIR}/transactions/${transaction_id}/log"
}
function commit_transaction() {
local transaction_id="$1"
local transaction_dir="${DATA_DIR}/transactions/${transaction_id}"
# move records from transaction directory to data directory
while IFS= read -r record_log; do
local operation
operation=$(echo "$record_log" | cut -d':' -f1)
local collection
collection=$(echo "$record_log" | cut -d':' -f2)
local record_id
record_id=$(echo "$record_log" | cut -d':' -f3)
case "$operation" in
"create")
mv "${transaction_dir}/${RECORD_FILE_PREFIX}${record_id}" "${DATA_DIR}/${collection}/" \
|| handle_error "Failed to commit 'create' operation for record: [$record_id] in collection: [$collection]" 1
;;
"update")
mv "${transaction_dir}/${RECORD_FILE_PREFIX}${record_id}" "${DATA_DIR}/${collection}/" \
|| handle_error "Failed to commit 'update' operation for record: [$record_id] in collection: [$collection]" 1
;;
"delete")
mv "${transaction_dir}/${RECORD_FILE_PREFIX}${record_id}" "${DATA_DIR}/${collection}/" \
|| handle_error "Failed to commit 'delete' operation for record ${record_id}" 1
;;
*)
handle_error "Invalid operation in transaction log: $operation" 1
;;
esac
done < "$transaction_dir/log"
rm -rf "$transaction_dir"
}
function rollback_transaction() {
local transaction_id="$1"
local transaction_dir="${DATA_DIR}/transactions/${transaction_id}"
# revert changes done by transaction
while IFS= read -r record_log; do
local operation
operation=$(echo "$record_log" | cut -d':' -f1)
local collection
collection=$(echo "$record_log" | cut -d':' -f2)
local record_id
record_id=$(echo "$record_log" | cut -d':' -f3)
case "$operation" in
"create")
rm -f "${transaction_dir}/${RECORD_FILE_PREFIX}${record_id}"
;;
"update")
mv "${transaction_dir}/${RECORD_FILE_PREFIX}${record_id}.bak" \
"${DATA_DIR}/${collection}/${RECORD_FILE_PREFIX}${record_id}" \
|| handle_error "Failed to rollback 'update' operation for record ${record_id}" 1
;;
"delete")
mv "${transaction_dir}/${RECORD_FILE_PREFIX}${record_id}" \
"${DATA_DIR}/${collection}/${RECORD_FILE_PREFIX}${record_id}" \
|| handle_error "Failed to rollback 'delete' operation for record ${record_id}" 1
;;
*)
handle_error "Invalid operation in transaction log: $operation" 1
;;
esac
done < "$transaction_dir/log"
rm -rf "$transaction_dir"
}