Batched Key Fetch Callbacks
Problem statement
Simulate calls to startFetch(key, callback). The parallel arrays callTimes, keys, and callbackIds describe those calls in nondecreasing timestamp order. Each row [key, value] in keyValues supplies the successful response value for that key.
When a call arrives while no timer is pending, schedule one batch flush exactly 100 milliseconds later. Every call whose timestamp is strictly earlier than that deadline joins the pending batch. The timeout runs before a call stamped exactly at the deadline, so that call starts a new half-open window.
At each flush, issue exactly one request in the form GET:/get?keys=key1,key2,.... Include each requested key once, ordered by its first call in that batch. Model the response as available synchronously at the flush.
After the request event, invoke every registered callback in original input order. A callback receives the value mapped to its key, including when several callbacks requested the same key. Record an invocation as CALL:callbackId:value.
Return one string row per HTTP batch. The request event is the first string in its row, followed by that batch's callback events. If there are no calls, return an empty matrix.
Function
batchFetchCallbacks(callTimes: int[], keys: String[], callbackIds: String[], keyValues: String[][]) → String[][]Examples
Example 1
callTimes = [0,30,70,120]keys = ["foo","bar","foo","foo"]callbackIds = ["cb1","cb2","cb3","cb4"]keyValues = [["foo","123"],["bar","abs"]]return = [["GET:/get?keys=foo,bar","CALL:cb1:123","CALL:cb2:abs","CALL:cb3:123"],["GET:/get?keys=foo","CALL:cb4:123"]]The call at time 0 schedules a flush at time 100. The calls at times 30 and 70 join that batch, with foo appearing once in the request while both of its callbacks receive 123. The call at time 120 starts the second request.
Example 2
callTimes = [5,104,105,105]keys = ["a","a","b","a"]callbackIds = ["first","second","third","fourth"]keyValues = [["a","A"],["b","B"]]return = [["GET:/get?keys=a","CALL:first:A","CALL:second:A"],["GET:/get?keys=b,a","CALL:third:B","CALL:fourth:A"]]The first half-open window is [5,105). The timeout runs before calls stamped 105, so those two calls start a new window and preserve their input order.
Example 3
callTimes = [10,10,50,250,349]keys = ["z","x","z","x","y"]callbackIds = ["c0","c1","c2","c3","c4"]keyValues = [["x","vx"],["y","vy"],["z","vz"]]return = [["GET:/get?keys=z,x","CALL:c0:vz","CALL:c1:vx","CALL:c2:vz"],["GET:/get?keys=x,y","CALL:c3:vx","CALL:c4:vy"]]Equal-time calls keep their input order. After the first flush, time 250 starts a fresh window ending at 350, so the call at 349 joins it.
Constraints
0 <= callTimes.length = keys.length = callbackIds.length <= 200000.0 <= callTimes[i] <= 10^9, andcallTimesis nondecreasing.keyValuescontains exactly one row for every distinct key inkeys.- Keys contain 1 to 20 lowercase ASCII letters, digits, or underscores and do not contain commas.
- Callback IDs contain 1 to 20 ASCII letters, digits, or underscores and do not contain colons.
- Response values contain 1 to 20 ASCII letters, digits, underscores, or hyphens and do not contain colons.