diff --git a/src/polydb.jl b/src/polydb.jl index b348c578..286395a2 100644 --- a/src/polydb.jl +++ b/src/polydb.jl @@ -7,7 +7,7 @@ import NetworkOptions import Mongoc -import Mongoc: find +import Mongoc: find, find_one #Polymake.Polydb's types store information via # a corresponding Mongoc type variable @@ -123,6 +123,7 @@ julia> length(collection, "DIM"=>3, "N_FACETS"=>5) function Base.length(c::Collection{T}, d::Pair...) where T return Base.length(c.mcol, Mongoc.BSON(d...)) end + """ find(c::Collection{T}, d::Dict=Dict(); opts::Union{Nothing, Dict}) @@ -166,6 +167,79 @@ function Mongoc.find(c::Collection{T}, d::Pair...) where T return Cursor{T}(Mongoc.find(c.mcol, Mongoc.BSON(d...))) 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 +julia> db = Polymake.Polydb.get_db(); + +julia> collection = db["Polytopes.Lattice.SmoothReflexive"]; + +julia> query = Dict("DIM"=>5, "N_FACETS"=>8); + +julia> opts = Dict("skip"=>13); + +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{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...) + +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(); + +julia> collection = db["Polytopes.Lattice.SmoothReflexive"]; + +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{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