nsmcp::AccessRecordStoreSQLite method init (protected)

 <instance of nsmcp::AccessRecordStoreSQLite[i]> init

Defined in /usr/local/ns/tcl/nsmcp/log-traffic-record-store.tcl

Testcases:
No testcase defined.
Source code:
try {
set directory [dict get ${:settings} directory]
if {${:mode} ni {reader writer}} {error "invalid access store mode"}
if {${:mode} eq "writer"} {file mkdir $directory; file attributes $directory -permissions 0700}
set key [ns_crypto::md string -digest sha256 -- [ns_info server]]
set :path [file join $directory access-records-$key.sqlite3]
# Create privately before SQLite opens the file. Never follow a symlink.
if {${:mode} eq "reader" && ![file exists ${:path}]} {error "record-store-not-initialized"}
if {![file exists ${:path}]} {
    if {[catch {open ${:path} {WRONLY CREAT EXCL} 0600} f createOptions]} {
        if {[lindex [dict get $createOptions -errorcode] 1] ne "EEXIST"} {return -options $createOptions $f}
    } else {close $f}
}
file lstat ${:path} st
if {$st(type) ne "file"} {error "record-store-invalid-file"}
if {${:mode} eq "writer"} {file attributes ${:path} -permissions 0600}
set :handle [ns_db gethandle -timeout 0:100000 [dict get ${:settings} pool]]
if {${:handle} eq {}} {return}
if {[ns_db dbtype ${:handle}] ne "sqlite"} {error "record-store-wrong-driver"}
set :db [::nsmcp::DatabaseSession new -handle ${:handle}]
set :prior [dict create busy [${:db} scalar {PRAGMA busy_timeout}] temp [${:db} scalar {PRAGMA temp_store}] queryOnly [${:db} scalar {PRAGMA query_only}]]
foreach row [${:db} execute {PRAGMA database_list}] {
    if {[dict get $row name] eq "nsmcp_records"} {${:db} execute {DETACH DATABASE nsmcp_records}}
}
# Install the short native wait before ATTACH or schema inspection.
${:db} execute {PRAGMA busy_timeout=100}
set path ${:path}
${:db} execute {ATTACH DATABASE $path AS nsmcp_records}
set :attached true
${:db} execute {PRAGMA nsmcp_records.cache_size=-8192; PRAGMA nsmcp_records.mmap_size=0; PRAGMA temp_store=FILE}
if {${:mode} eq "writer"} {
    file stat ${:path} identity
    set key [ns_crypto::md string -digest sha256 -- [list ${:path} $identity(dev) $identity(ino) [dict get ${:settings} maxBytes] [expr {[dict exists ${:settings} unified] && [dict get ${:settings} unified]}] schema3]]
    if {[file size ${:path}]<512 || ![nsv_exists nsmcp:record-initialized $key]} {
        :initialize
        nsv_set nsmcp:record-initialized $key 1
    }
    ${:db} execute {PRAGMA nsmcp_records.synchronous=FULL; PRAGMA nsmcp_records.journal_size_limit=0}
} elseif {[${:db} scalar {SELECT version FROM nsmcp_records.meta}]!=2} {
    error "record-store-schema-not-initialized"
}
if {${:mode} eq "writer"} {${:db} execute {DROP TABLE IF EXISTS temp.nsmcp_ingest_batch; CREATE TEMP TABLE nsmcp_ingest_batch AS SELECT * FROM nsmcp_records.rows WHERE 0}} else {${:db} execute {PRAGMA query_only=ON}}
} on error {message opts} {
    if {$message in {record-store-not-initialized record-store-schema-not-initialized}} {
        set failure [dict create class [string range $message 13 end] diagnosticId {}]
    } elseif {[regexp -nocase {database.*(locked|busy)} $message]} {
        set failure [dict create class database-locked diagnosticId {}]
    } else {set failure [:diagnostic $message $opts]}
    dict set opts -errorcode [list NSMCP RECORD_STORE [dict get $failure class] [dict get $failure diagnosticId]]
    :disconnect
    return -options $opts $message
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: