Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polydb:method find_one in the database #492

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
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 @@
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)

Check warning on line 201 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L199-L201

Added lines #L199 - L201 were not covered by tests
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

Check warning on line 206 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L204-L206

Added lines #L204 - L206 were not covered by tests
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)

Check warning on line 235 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L233-L235

Added lines #L233 - L235 were not covered by tests
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

Check warning on line 240 in src/polydb.jl

View check run for this annotation

Codecov / codecov/patch

src/polydb.jl#L238-L240

Added lines #L238 - L240 were not covered by tests
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