Project

General

Profile

1
#!/bin/bash -e
2
. "$(dirname "${BASH_SOURCE[0]}")"/../../../lib/runscripts/table_dir.run
3
. "$(dirname "${BASH_SOURCE[0]}")"/../../../lib/sh/binsearch.sh
4

    
5
if self_not_included; then
6

    
7

    
8
#### import resuming
9

    
10

    
11
### is_pkey_imported()
12

    
13
table=raw_occurrence_record
14

    
15
is_pkey_imported__int() # usage: pkey=# is_pkey_imported__int
16
{
17
	echo_func; kw_params pkey; : "${pkey?}"; mk_table_esc
18
	test "$pkey" || { log++ echo_run echo 0; return; }
19
	
20
	use_local_remote
21
	data_only=1 mysql_ANSI <<<"SELECT COUNT(*) FROM $table_esc WHERE id = $pkey"
22
}
23

    
24
func_override is_pkey_imported__int__no_cache
25
is_pkey_imported__int() # caches the last result for efficiency
26
{
27
	local cache_key="$(declare -p pkey) $*"; load_cache
28
	if ! cached; then save_cache "$(${FUNCNAME}__no_cache "$@")" || return; fi
29
	echo_cached_value
30
}
31

    
32
is_pkey_imported()
33
{ echo_func; local int; int="$(is_pkey_imported__int)"; int2bool "$int"; }
34

    
35

    
36
get_pkey_at_pos() # usage: i=# get_pkey_at_pos
37
{
38
	echo_func; log++; kw_params i; : "${i?}"; mk_table_esc
39
	piped_cmd echo_run tail -c +"$i" "$top_file"\
40
	|echo_run sed -n '/^INSERT INTO '"$table_esc"' VALUES \(([0-9]+),.*$/{
41
s//\1/p
42
q # stop after first match
43
}'
44
}
45

    
46
is_pkey_at_pos_imported()
47
{ echo_func; local pkey; pkey="$(get_pkey_at_pos)"; is_pkey_imported; }
48

    
49
import_resume_pos() # usage: [min=#] [max=#] import_resume_pos
50
{
51
	echo_func; kw_params min max; local min="${min-0}"
52
	if ! isset max; then local max; max="$(file_size "$top_file")"; fi
53
	binsearch is_pkey_at_pos_imported
54
}
55
# import_resume_pos() takes 6:07 min to run, with 37 iterations
56

    
57
fi
(7-7/12)