Skip to content

Commit

Permalink
Merge pull request #492 from apaffenholz/polydb_find_one
Browse files Browse the repository at this point in the history
polydb:method find_one in the database
  • Loading branch information
benlorenz authored Jul 16, 2024
2 parents e22a1f9 + e72ff4b commit b5b08dd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
76 changes: 75 additions & 1 deletion src/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit b5b08dd

Please sign in to comment.