nsmcp::CachedLogSearch method results (public)

 <instance of nsmcp::CachedLogSearch[i]> results handle [ cursor ] \
    [ limit ]

Defined in /usr/local/ns/tcl/nsmcp/log-summary.tcl

Retrieve a bounded page from an unexpired cached search. Validate the handle and page bounds under the mutex and retain coverage and truncation metadata.

Parameters:
handle (required)
Retained job or conversation handle; ownership and expiry checks still apply.
cursor (optional, defaults to "0")
Zero-based cursor into retained results. Defaults to '0'.
limit (optional, defaults to "20")
Maximum number of records or entries to return. Defaults to '20'.
Returns:
The cached page dictionary.

Testcases:
No testcase defined.
Source code:
if {![regexp {^[0-9a-f]{64}$} $handle]} {
    error "invalid result handle"
}
if {![string is entier -strict $cursor] || $cursor < 0 ||
    ![string is integer -strict $limit] || $limit < 1 || $limit > 100} {
    error "expected a nonnegative cursor and a limit between 1 and 100"
}
ns_mutex lock ${:mutex}
try {
    :beginScanMetrics
    set key ${:cacheKey}:search
    set query {}
    if {[nsv_exists ${:store} $key]} {
        set meta [nsv_get ${:store} $key]
        dict for {q candidate} [dict get $meta queries] {
            if {[dict exists $candidate resultHandle] &&
                [dict get $candidate resultHandle] eq $handle} {
                set query $candidate
                break
            }
        }
    }
    if {$query eq ""} {
        return -code error -errorcode {NSMCP RESULT UNKNOWN} "unknown or expired result handle"
    }
    # Result handles created before a live reload have no cap flag.
    set query [dict merge {limitReached false} $query]
    file stat ${:path} current
    set probe [open ${:path} rb]
    try {
        set prefix [:readSource $probe [string length [dict get $meta prefix]]]
    } finally {
        close $probe
    }
    if {[list $current(dev) $current(ino)] ne [dict get $meta identity] ||
        $current(size) < [dict get $meta observedSize] || $prefix ne [dict get $meta prefix]} {
        error "result handle expired after log rotation; repeat the search"
    }
    set end [dict get $query fileBytes]
    if {$cursor > $end} {
        error "cursor is beyond the published results"
    }
    set f [open [dict get $query resultFile] rb]
    try {
        if {$cursor > 0} {
            seek $f [expr {$cursor-1}]
            if {[read $f 1] ne "\n"} {
                error "cursor must be a record boundary"
            }
        }
        set reader [::nsmcp::BufferedResultReader new -channel $f -limit $end -offset $cursor]
        try {
            set records {}
            set bytes 0
            set next $cursor
            set started [clock clicks -microseconds]
            while {$next<$end && [llength $records]<$limit} {
                set line [$reader next]
                set length [expr {[dict get $line end]-[dict get $line start]}]
                if {$bytes+$length>2097152} {
                    break
                }
                incr bytes $length
                lappend records [ns_json parse [encoding convertfrom utf-8 [dict get $line raw]]]
                set next [dict get $line end]
            }
            set stats [$reader statistics]
            dict set :scanPhases resultMaterialization [expr {
                max(0,[clock clicks -microseconds]-$started-[dict get $stats readMicroseconds])
            }]
            dict incr :scanPhases sourceRead [dict get $stats readMicroseconds]
            dict set :scanIO resultReadBytes [dict get $stats readBytes]
            dict set :scanIO resultReadCalls [dict get $stats readCalls]
        } finally {
            $reader destroy
        }
    } finally {
        close $f
    }
    set more [expr {$next < $end}]
    return [dict create resultHandle $handle records $records scanMetrics [:finishScanMetrics page]  nextCursor [expr {$more ? $next : ""}] more $more  resultCount [dict get $query resultCount]  scanIncomplete [dict get $query scanIncomplete] pending [dict get $query pending]  cacheFull [dict get $query cacheFull] limitReached [dict get $query limitReached] generation [dict get $meta generation]]
} finally {
    ns_mutex unlock ${:mutex}
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: