Skip to content

Commit

Permalink
Merge pull request #15 from forki/master
Browse files Browse the repository at this point in the history
chore: Refactor executeAsync function in AzureTable module
  • Loading branch information
tforkmann authored Jun 17, 2024
2 parents 825c622 + 055306d commit 27d4743
Showing 1 changed file with 15 additions and 42 deletions.
57 changes: 15 additions & 42 deletions src/AzureTackle/AzureTackle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,6 @@ module Table =
)
}

let queryAsync<'a> (filter: string option) (table: TableClient) =
task {

match filter with
| Some f -> return table.QueryAsync(f, Nullable(1500), [||], CancellationToken.None)
| None -> return table.QueryAsync("", Nullable(1500), [||], CancellationToken.None)
}

[<RequireQualifiedAccess>]
module AzureTable =
open Table
Expand Down Expand Up @@ -450,17 +442,26 @@ module AzureTable =
return failwithf "Execute failed with exn: %s" exn.Message
}

let executeAsync (read: TableEntity -> 't) (props: TableProps) =
let executeAsync (mapF: TableEntity -> 't) (props: TableProps) =
task {
try
let azureTable = getTable props

let applyFilter =
let filter =
match props.Filter with
| Some filters -> filters |> toQuery
| None -> None
| Some filters -> filters |> toQuery |> Option.defaultValue ""
| None -> ""

let maxElements =
match props.MaxElements with
| Some x -> min x 1000
| None -> 1000

let pageAble =
match props.CancellationToken with
| Some t -> azureTable.QueryAsync(filter = filter, maxPerPage = maxElements, cancellationToken = t)
| None -> azureTable.QueryAsync(filter = filter, maxPerPage = maxElements)

let! pageAble = queryAsync applyFilter azureTable
let resultEnum = pageAble.AsPages().GetAsyncEnumerator()

let mutable hasValue = true
Expand All @@ -475,39 +476,11 @@ module AzureTable =

return
allValues.ToArray()
|> Array.map (fun v ->
read v)
|> Array.map mapF
with exn ->
return failwithf "ExecuteAsync failed with exn: %s" exn.Message
}

let executeAsyncWithMapper mapper (props: TableProps) =
task {
try
let azureTable = getTable props

let applyFilter =
match props.Filter with
| Some filters -> filters |> toQuery
| None -> None

let! pageAble = queryAsync applyFilter azureTable
let resultEnum = pageAble.AsPages().GetAsyncEnumerator()

let mutable hasValue = true
let mutable allValues = System.Collections.Generic.List()

while hasValue do
let! page = resultEnum.MoveNextAsync()

match page with
| true -> allValues.AddRange(resultEnum.Current.Values)
| false -> hasValue <- false

return allValues.ToArray() |> Array.map (fun v -> mapper v)
with exn ->
return failwithf "ExecuteAsync failed with exn: %s" exn.Message
}

let executeDirect (read: TableEntity -> 't) (props: TableProps) =
task {
Expand Down

0 comments on commit 27d4743

Please sign in to comment.