From e72ff4bd5713fd88db49f3f57245e07e741c74a0 Mon Sep 17 00:00:00 2001 From: Andreas Paffenholz Date: Mon, 15 Jul 2024 16:30:31 +0200 Subject: [PATCH] polydb: return either Polymake.BigObject or Mongoc.BSON, tests for both options --- src/polydb.jl | 32 +++++++++++++++++++++++++++++--- test/polydb.jl | 12 ++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/polydb.jl b/src/polydb.jl index f29e7ddb..286395a2 100644 --- a/src/polydb.jl +++ b/src/polydb.jl @@ -171,6 +171,7 @@ end find_one(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict}) Return one document from a collection `c` matching the criteria given by `d`. +`T` can be chosen from `Polymake.BigObject` and `Mongoc.BSON`. Apply search options `opts`. # Examples ```julia-repl @@ -186,17 +187,30 @@ julia> pm_object = Polymake.Polydb.find_one(collection, query, opts=opts); julia> typeof(pm_object) Polymake.LibPolymake.BigObjectAllocated + +julia> collection_bson = Polymake.Polydb.Collection{Mongoc.BSON}(collection); + +julia> pm_object = Polymake.Polydb.find_one(collection_bson, query, opts=opts); + +julia> typeof(pm_object) +Mongoc.BSON ``` """ -function Mongoc.find_one(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing) where T +function Mongoc.find_one(c::Collection{Polymake.BigObject}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing) p = Mongoc.find_one(c.mcol, Mongoc.BSON(d); options=(isnothing(opts) ? nothing : Mongoc.BSON(opts))) return isnothing(p) ? nothing : parse_document(p) end +function Mongoc.find_one(c::Collection{Mongoc.BSON}, d::Dict=Dict(); opts::Union{Nothing, Dict}=nothing) + p = Mongoc.find_one(c.mcol, Mongoc.BSON(d); options=(isnothing(opts) ? nothing : Mongoc.BSON(opts))) + return isnothing(p) ? nothing : p +end + """ find_one(c::Collection{T}, d::Pair...) -Search a collection `c` for documents matching the criteria given by `d`. +Return one document from a collection `c` matching the criteria given by `d`. +`T` can be chosen from `Polymake.BigObject` and `Mongoc.BSON`. # Examples ```julia-repl julia> db = Polymake.Polydb.get_db(); @@ -207,13 +221,25 @@ julia> pm_object = Polymake.Polydb.find_one(collection, "DIM"=>3, "N_FACETS"=>5) julia> typeof(pm_object) Polymake.LibPolymake.BigObjectAllocated + +julia> collection_bson = Polymake.Polydb.Collection{Mongoc.BSON}(collection); + +julia> pm_object = Polymake.Polydb.find_one(collection_bson, "DIM"=>3, "N_FACETS"=>5); + +julia> typeof(pm_object) +Mongoc.BSON ``` """ -function Mongoc.find_one(c::Collection{T}, d::Pair...) where T +function Mongoc.find_one(c::Collection{Polymake.BigObject}, d::Pair...) p = Mongoc.find_one(c.mcol, Mongoc.BSON(d...)) return isnothing(p) ? nothing : parse_document(p) end +function Mongoc.find_one(c::Collection{Mongoc.BSON}, d::Pair...) + p = Mongoc.find_one(c.mcol, Mongoc.BSON(d...)) + return isnothing(p) ? nothing : p +end + """ Collection{T}(c::Collection) diff --git a/test/polydb.jl b/test/polydb.jl index cc9ae368..6a36f624 100644 --- a/test/polydb.jl +++ b/test/polydb.jl @@ -30,6 +30,8 @@ using Polymake.Polydb.Mongoc @test Polymake.Polydb.Collection{Polymake.BigObject}(collection_bson) isa Polymake.Polydb.Collection{Polymake.BigObject} constraints = _acp(["DIM" => 3, "N_VERTICES" => 8]) query = Dict(constraints...) + opts_success = Dict("skip"=>3) + opts_nothing = Dict("skip"=>13) # Queries @test Polymake.Polydb.find(collection_bo, query) isa Polymake.Polydb.Cursor @test Polymake.Polydb.find(collection_bo, query) isa Polymake.Polydb.Cursor{Polymake.BigObject} @@ -46,6 +48,16 @@ using Polymake.Polydb.Mongoc @test Polymake.Polydb.Cursor{Polymake.BigObject}(results_bson) isa Polymake.Polydb.Cursor @test Polymake.Polydb.Cursor{Polymake.BigObject}(results_bson) isa Polymake.Polydb.Cursor{Polymake.BigObject} + @test Polymake.Polydb.find_one(collection_bo, query) isa Polymake.BigObject + @test Polymake.Polydb.find_one(collection_bo, query, opts=opts_success) isa Polymake.BigObject + @test isnothing(Polymake.Polydb.find_one(collection_bo, query, opts=opts_nothing)) + @test Polymake.Polydb.find_one(collection_bo, constraints...) isa Polymake.BigObject + + @test Polymake.Polydb.find_one(collection_bson, query) isa Mongoc.BSON + @test Polymake.Polydb.find_one(collection_bson, query, opts=opts_success) isa Mongoc.BSON + @test isnothing(Polymake.Polydb.find_one(collection_bson, query, opts=opts_nothing)) + @test Polymake.Polydb.find_one(collection_bson, constraints...) isa Mongoc.BSON + @test length(collection_bo, constraints...) == 7 @test length(collection_bo, query) == 7 @test length(collection_bson, constraints...) == 7