Skip to content

Commit

Permalink
Merge pull request #454 from oscar-system/bl/dblength
Browse files Browse the repository at this point in the history
  • Loading branch information
benlorenz authored Sep 7, 2023
2 parents 53e85f3 + 276ef43 commit 6a8e38c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,45 @@ Polymake.Polydb.Collection{Polymake.BigObject}: Matroids.Small
"""
Base.getindex(db::Database, name::AbstractString) = Collection{Polymake.BigObject}(db.mdb[name])

"""
length(c::Collection{T}, d::Dict=Dict())
Count documents in a collection `c` matching the criteria given by `d`.
# Examples
```julia-repl
julia> db = Polymake.Polydb.get_db();
julia> collection = db["Polytopes.Lattice.SmoothReflexive"];
julia> query = Dict("DIM"=>3, "N_FACETS"=>5);
julia> length(collection, query)
4
```
"""
function Base.length(c::Collection{T}, d::Dict=Dict()) where T
return Base.length(c.mcol, Mongoc.BSON(d))
end

"""
length(c::Collection{T}, d::Pair...)
Count documents in a collection `c` matching the criteria given by `d`.
# Examples
```julia-repl
julia> db = Polymake.Polydb.get_db();
julia> collection = db["Polytopes.Lattice.SmoothReflexive"];
julia> length(collection, "DIM"=>3, "N_FACETS"=>5)
4
```
"""
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
6 changes: 6 additions & 0 deletions test/polydb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ using Polymake.Polydb.Mongoc
results_bson = Polymake.Polydb.find(collection_bson, constraints...)
@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 length(collection_bo, constraints...) == 7
@test length(collection_bo, query) == 7
@test length(collection_bson, constraints...) == 7
@test length(collection_bson, query) == 7

@testset verbose=true "Iterator (Cursor)" begin
@test iterate(results_bo) isa Tuple{Polymake.BigObject, Nothing}
@test iterate(results_bson) isa Tuple{Mongoc.BSON, Nothing}
Expand Down

0 comments on commit 6a8e38c

Please sign in to comment.