Skip to content

Commit

Permalink
remove yield! tail recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Dec 11, 2022
1 parent 32b3915 commit f9dfe73
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Equinox.DynamoStore/DynamoStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -452,39 +452,39 @@ type Container(tableName, createContext : (RequestMetrics -> unit) -> TableConte
let pk = Batch.tableKeyForStreamTip stream
let! item = context.UpdateItemAsync(pk, updateExpr, ?precondition = precondition) |> Async.startImmediateAsTask ct
return item |> Batch.ofSchema, rm.Consumed }
member _.QueryBatches(stream, consistentRead, minN, maxI, backwards, batchSize, ct) : IAsyncEnumerable<int * StopwatchInterval * Batch array * RequestConsumption> =
member _.QueryBatches(stream, consistentRead, minN, maxI, backwards, batchSize, ct) : IAsyncEnumerable<int * StopwatchInterval * Batch array * RequestConsumption> = taskSeq {
let compile = (createContext ignore).Template.PrecomputeConditionalExpr
let kc = match maxI with
| Some maxI -> compile <@ fun (b : Batch.Schema) -> b.p = stream && b.i < maxI @>
| None -> compile <@ fun (b : Batch.Schema) -> b.p = stream @>
let fc = match minN with
| Some minN -> compile <@ fun (b : Batch.Schema) -> b.n > minN @> |> Some
| None -> None
let rec aux (i, le) = taskSeq {
let mutable index, lastEvaluated, more = 0, None, true
while more do
// TOCONSIDER could avoid projecting `p`
let rm = Metrics()
let context = createContext rm.Add
let! t, res = context.QueryPaginatedAsync(kc, ?filterCondition = fc, limit = batchSize, ?exclusiveStartKey = le,
let! t, res = context.QueryPaginatedAsync(kc, ?filterCondition = fc, limit = batchSize, ?exclusiveStartKey = lastEvaluated,
scanIndexForward = not backwards, consistentRead = consistentRead)
|> Stopwatch.timeAsync ct
yield i, t, Array.map Batch.ofSchema res.Records, rm.Consumed
match res.LastEvaluatedKey with
| None -> ()
| le -> yield! aux (i + 1, le) }
aux (0, None)
member internal _.QueryIAndNOrderByNAscending(stream, maxItems, ct) : IAsyncEnumerable<int * StopwatchInterval * BatchIndices array * RequestConsumption> =
let rec aux (index, lastEvaluated) = taskSeq {
yield index, t, Array.map Batch.ofSchema res.Records, rm.Consumed
lastEvaluated <- res.LastEvaluatedKey
more <- Option.isSome lastEvaluated
index <- index + 1 }
member internal _.QueryIAndNOrderByNAscending(stream, maxItems, ct) : IAsyncEnumerable<int * StopwatchInterval * BatchIndices array * RequestConsumption> = taskSeq {
let mutable index, lastEvaluated, more = 0, None, true
while more do
let rm = Metrics()
let context = createContext rm.Add
let keyCond = <@ fun (b : Batch.Schema) -> b.p = stream @>
let proj = <@ fun (b : Batch.Schema) -> b.i, b.c, b.n @> // TOCONSIDER want len of c, but b.e.Length explodes in empty array case, so no choice but to return the full thing
let! t, res = context.QueryProjectedPaginatedAsync(keyCond, proj, ?exclusiveStartKey = lastEvaluated, scanIndexForward = true, limit = maxItems)
|> Stopwatch.timeAsync ct
yield index, t, [| for i, c, n in res -> { isTip = Batch.isTip i; index = n - int64 c.Length; n = n } |], rm.Consumed
match res.LastEvaluatedKey with
| None -> ()
| le -> yield! aux (index + 1, le) }
aux (0, None)
lastEvaluated <- res.LastEvaluatedKey
more <- Option.isSome lastEvaluated
index <- index + 1 }
member x.DeleteItem(stream : string, i, ct) : Task<RequestConsumption> = task {
let rm = Metrics()
let context = createContext rm.Add
Expand Down

0 comments on commit f9dfe73

Please sign in to comment.