diff --git a/.gitignore b/.gitignore index 717fcea..60eef93 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,9 @@ -*.cov +*.jl.cov +*.jl.*.cov +*.jl.mem +/deps/build.log +/deps/deps.jl +/deps/usr/ +/docs/build/ +/docs/site/ +/docs/out.xml diff --git a/.travis.yml b/.travis.yml index 0001291..3426333 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,16 +2,12 @@ language: julia os: - linux julia: - - 0.5 - 0.6 - # - nightly services: - mongodb notifications: email: false -sudo: false -script: - - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi - - julia -e 'Pkg.clone(pwd()); Pkg.build("Mongo"); Pkg.test("Mongo", coverage=true)' +before_script: + - julia -e 'Pkg.clone("LibBSON"); Pkg.build()' after_success: - - if [ $JULIAVERSION = "juliareleases" ]; then julia -e 'cd(Pkg.dir("Mongo")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'; fi + - julia -e 'VERSION < v"0.7.0-DEV" || (using Pkg); cd(Pkg.dir("Mongo")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder()); Codecov.submit(Codecov.process_folder())' diff --git a/README.md b/README.md index d3917b4..65a6980 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,9 @@ -IMPORTANT: Looking for New Maintainer -------------------------------------- - -Note that we are looking for a new maintainer for the Mongo.jl package. If you are interested in taking over maintenance of Mongo.jl please contact peter dot zion at gmail dot com. Mongo.jl =========== [![Build Status](https://api.travis-ci.org/pzion/Mongo.jl.svg?branch=master)](https://travis-ci.org/pzion/Mongo.jl) -======= [![0.6 Status](http://pkg.julialang.org/badges/Mongo_0.6.svg)](http://pkg.julialang.org/?pkg=Mongo) -[![0.5 Status](http://pkg.julialang.org/badges/Mongo_0.5.svg)](http://pkg.julialang.org/?pkg=Mongo) [![Coverage Status](https://img.shields.io/coveralls/pzion/Mongo.jl.svg)](https://coveralls.io/r/pzion/Mongo.jl?branch=master) [MongoDB](http://www.mongodb.org) bindings for [The Julia Language](http://julialang.org/) diff --git a/REQUIRE b/REQUIRE index 7e795d4..3591dd8 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,5 +1,3 @@ -julia 0.5 -LibBSON 0.2.8 -BinDeps 0.4 -Compat 0.9 -@osx Homebrew +julia 0.6 0.7 +LibBSON +BinaryProvider diff --git a/deps/.gitignore b/deps/.gitignore deleted file mode 100644 index 9e1af8e..0000000 --- a/deps/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -deps.jl -builds/ -downloads/ -src/ -usr/ diff --git a/deps/build.jl b/deps/build.jl index 963acc6..4d7b6f9 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -1,26 +1,40 @@ -using BinDeps -@BinDeps.setup +using BinaryProvider # requires BinaryProvider 0.3.0 or later -libmongoc = library_dependency( - "libmongoc", - aliases = ["libmongoc", "libmongoc-1.0"] - ) +# Parse some basic command-line arguments +const verbose = "--verbose" in ARGS +const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr"))) +products = [ + LibraryProduct(prefix, String["libmongoc"], :libmongoc), +] -provides(Sources, Dict( - URI("http://github.com/mongodb/mongo-c-driver/releases/download/1.0.0/mongo-c-driver-1.0.0.tar.gz") => libmongoc - )) +# Download binaries from hosted location +bin_prefix = "https://github.com/felipenoris/mongo-c-driver-builder/releases/download/v1.9.5" -provides( - BuildProcess, - Autotools(libtarget = "libmongoc-1.0.la"), - libmongoc, - os = :Unix - ) - -@static if is_apple() - using Homebrew - provides(Homebrew.HB, Dict("mongo-c-driver" => libmongoc)) +# Listing of files generated by BinaryBuilder: +download_info = Dict( + Linux(:aarch64, :glibc) => ("$bin_prefix/libmongoc.v1.9.5.aarch64-linux-gnu.tar.gz", "3bcad8b9004e7ca498db6ef58e7c12819b47b316b1bbfc30c017968d6a086c94"), + Linux(:armv7l, :glibc, :eabihf) => ("$bin_prefix/libmongoc.v1.9.5.arm-linux-gnueabihf.tar.gz", "5c329679e4206a10c43a4b447c29e1fe9601ad4023d48c805ebaa4cc75ec1e06"), + Linux(:powerpc64le, :glibc) => ("$bin_prefix/libmongoc.v1.9.5.powerpc64le-linux-gnu.tar.gz", "9f786c54e33986eddf2132b2ea35f967416246e4d5e098ee468d8cc2e98e7111"), + MacOS(:x86_64) => ("$bin_prefix/libmongoc.v1.9.5.x86_64-apple-darwin14.tar.gz", "465a4628d18f0485772e79214ec916af0c934eea5534e0d2ebac9b6e78e2f3a0"), + Linux(:x86_64, :glibc) => ("$bin_prefix/libmongoc.v1.9.5.x86_64-linux-gnu.tar.gz", "3b9a4464c2eac6b7a98dd295b42f3a7793238d11b9e9f6ce5b4e8b3b0859aa86"), + FreeBSD(:x86_64) => ("$bin_prefix/libmongoc.v1.9.5.x86_64-unknown-freebsd11.1.tar.gz", "d4737348d786f094710dc7e94ca6e9363252c2d002a9c502aaab85a77177c1d4"), +) + +# Install unsatisfied or updated dependencies: +unsatisfied = any(!satisfied(p; verbose=verbose) for p in products) +if haskey(download_info, platform_key()) + url, tarball_hash = download_info[platform_key()] + if unsatisfied || !isinstalled(url, tarball_hash; prefix=prefix) + # Download and install binaries + install(url, tarball_hash; prefix=prefix, force=true, verbose=verbose) + end +elseif unsatisfied + # If we don't have a BinaryProvider-compatible .tar.gz to download, complain. + # Alternatively, you could attempt to install from a separate provider, + # build from source or something even more ambitious here. + error("Your platform $(triplet(platform_key())) is not supported by this package!") end -@BinDeps.install Dict(:libmongoc => :libmongoc) +# Write out a deps.jl file that will contain mappings for our products +write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose) diff --git a/src/Mongo.jl b/src/Mongo.jl index 4c3c191..8628b1f 100644 --- a/src/Mongo.jl +++ b/src/Mongo.jl @@ -1,21 +1,21 @@ + +__precompile__(true) module Mongo -include("../deps/deps.jl") +const deps_script = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl") +if !isfile(deps_script) + error("Mongo.jl is not installed properly, run Pkg.build(\"Mongo\") and restart Julia.") +end +include(deps_script) +check_deps() using LibBSON -import Base.show, - Base.convert, - Base.start, - Base.next, - Base.count, - Base.find, - Base.done - ccall( (:mongoc_init, libmongoc), Void, () ) + atexit() do ccall((:mongoc_cleanup, libmongoc), Void, ()) end diff --git a/src/MongoClient.jl b/src/MongoClient.jl index 853b04e..54c95a2 100644 --- a/src/MongoClient.jl +++ b/src/MongoClient.jl @@ -1,6 +1,5 @@ -using Compat -type MongoClient +mutable struct MongoClient uri::AbstractString _wrap_::Ptr{Void} @@ -26,19 +25,19 @@ MongoClient(host::AbstractString, user::AbstractString, password::AbstractString end export MongoClient -show(io::IO, client::MongoClient) = print(io, "MongoClient($(client.uri))") -export show +Base.show(io::IO, client::MongoClient) = print(io, "MongoClient($(client.uri))") """ Issues a command to MongoDB client through `mongoc_client_command_simple`. Possible commands: https://docs.mongodb.org/manual/reference/command/ """ -command_simple( +function command_simple( client::MongoClient, # mongoc_client_t db_name::AbstractString, # const char command::BSONObject#, # const bson_t - ) = begin + ) + dbCStr = String(db_name) reply = BSONObject() # bson_t bsonError = BSONError() # bson_error_t @@ -54,6 +53,7 @@ command_simple( ) || error("update: $(string(bsonError))") return reply end + command_simple( client::MongoClient, db_name::AbstractString, @@ -63,6 +63,7 @@ command_simple( db_name, BSONObject(command) ) + command_simple( client::MongoClient, db_name::AbstractString, diff --git a/src/MongoCollection.jl b/src/MongoCollection.jl index abe790d..1b9f06f 100644 --- a/src/MongoCollection.jl +++ b/src/MongoCollection.jl @@ -1,6 +1,5 @@ -using Compat -type MongoCollection +mutable struct MongoCollection _wrap_::Ptr{Void} client::MongoClient db::AbstractString @@ -25,7 +24,7 @@ type MongoCollection end export MongoCollection -show(io::IO, collection::MongoCollection) = begin +function Base.show(io::IO, collection::MongoCollection) nameCStr = ccall( (:mongoc_collection_get_name, libmongoc), Ptr{UInt8}, (Ptr{Void},), @@ -34,7 +33,6 @@ show(io::IO, collection::MongoCollection) = begin name = unsafe_string(nameCStr) print(io, "MongoCollection($name)") end -export show baremodule MongoInsertFlags const None = 0 @@ -42,11 +40,12 @@ baremodule MongoInsertFlags end export MongoInsertFlags -insert( +function insert( collection::MongoCollection, document::BSONObject; flags::Int = MongoInsertFlags.None - ) = begin + ) + oid = get!(document, "_id", BSONOID()) bsonError = BSONError() @@ -62,11 +61,13 @@ insert( return oid end + insert( collection::MongoCollection, dict::Associative; flags::Int = MongoInsertFlags.None ) = insert(collection, BSONObject(dict), flags=flags) + insert( collection::MongoCollection, dict::NakedDict; @@ -81,12 +82,13 @@ baremodule MongoUpdateFlags end export MongoUpdateFlags -update( +function update( collection::MongoCollection, selector::BSONObject, change::BSONObject; flags::Int = MongoUpdateFlags.None - ) = begin + ) + bsonError = BSONError() ccall( (:mongoc_collection_update, libmongoc), @@ -99,6 +101,7 @@ update( bsonError._wrap_ ) || error("update: $(string(bsonError))") end + update( collection::MongoCollection, selector::Associative, @@ -110,6 +113,7 @@ update( BSONObject(change), flags = flags ) + update(c::MongoCollection, s::NakedDict, chg::NakedDict; kwargs...) = update(c, BSONObject(Dict(s)), BSONObject(Dict(chg)); kwargs...) export update @@ -126,7 +130,7 @@ baremodule MongoQueryFlags end export MongoQueryFlags -Base.find( +function Base.find( collection::MongoCollection, selector::BSONObject, fields::BSONObject; @@ -134,7 +138,8 @@ Base.find( skip::Int = 0, limit::Int = 0, batch_size::Int = 0 - ) = begin + ) + result = ccall( (:mongoc_collection_find, libmongoc), Ptr{Void}, (Ptr{Void}, Cint, UInt32, UInt32, UInt32, Ptr{Void}, Ptr{Void}, Ptr{Void}), @@ -150,6 +155,7 @@ Base.find( result == C_NULL && error("mongoc_collection_find: failure") return MongoCursor( result ) end + Base.find( collection::MongoCollection, selector::Associative, @@ -167,14 +173,16 @@ Base.find( batch_size = batch_size, flags = flags ) -Base.find( + +function Base.find( collection::MongoCollection, selector::BSONObject; flags::Int = MongoQueryFlags.None, skip::Int = 0, limit::Int = 0, batch_size::Int = 0 - ) = begin + ) + result = ccall( (:mongoc_collection_find, libmongoc), Ptr{Void}, (Ptr{Void}, Cint, UInt32, UInt32, UInt32, Ptr{Void}, Ptr{Void}, Ptr{Void}), @@ -190,6 +198,7 @@ Base.find( result == C_NULL && error("mongoc_collection_find: failure") return MongoCursor( result ) end + Base.find( collection::MongoCollection, selector::Associative; @@ -205,19 +214,21 @@ Base.find( batch_size = batch_size, flags = flags ) + Base.find(c::MongoCollection, s::NakedDict; kwargs...) = find(c, BSONObject(Dict(s)); kwargs...) + Base.find(c::MongoCollection, s::NakedDict, f::NakedDict; kwargs...) = find(c, BSONObject(Dict(s)), BSONObject(Dict(f)); kwargs...) -export find -Base.count( +function Base.count( collection::MongoCollection, queryBSON::BSONObject; skip::Int64 = 0, limit::Int64 = 0, flags::Int = MongoQueryFlags.None - ) = begin + ) + bsonError = BSONError() result = ccall( (:mongoc_collection_count, libmongoc), @@ -233,6 +244,7 @@ Base.count( result < 0 && error("count: $(string(bsonError))") return result end + Base.count( collection::MongoCollection, query::Associative; @@ -246,10 +258,11 @@ Base.count( limit = limit, flags = flags ) + Base.count(c::MongoCollection, s::NakedDict; kwargs...) = count(c, BSONObject(Dict(s)); kwargs...) + Base.count(c::MongoCollection) = count(c, BSONObject()) -export count baremodule MongoDeleteFlags const None = 0 @@ -275,6 +288,7 @@ delete( result < 0 && error("delete: $(string(bsonError))") return result end + delete( collection::MongoCollection, selector::Associative; @@ -284,6 +298,7 @@ delete( BSONObject(selector), flags = flags ) + delete(c::MongoCollection, s::NakedDict; kwargs...) = delete(c, BSONObject(Dict(s)); kwargs...) export delete diff --git a/src/MongoCursor.jl b/src/MongoCursor.jl index c2a7db3..0574574 100644 --- a/src/MongoCursor.jl +++ b/src/MongoCursor.jl @@ -1,4 +1,5 @@ -type MongoCursor + +mutable struct MongoCursor _wrap_::Ptr{Void} MongoCursor(_wrap_::Ptr{Void}) = begin @@ -11,18 +12,16 @@ export MongoCursor # Iterator -start(cursor::MongoCursor) = nothing -export start +Base.start(cursor::MongoCursor) = nothing -next(cursor::MongoCursor, state::Void) = +Base.next(cursor::MongoCursor, state::Void) = (BSONObject(ccall( (:mongoc_cursor_current, libmongoc), Ptr{Void}, (Ptr{Void},), cursor._wrap_ ), Union{}), state) -export next -done(cursor::MongoCursor, state::Void) = begin +function Base.done(cursor::MongoCursor, state::Void) return !ccall( (:mongoc_cursor_next, libmongoc), Bool, (Ptr{Void}, Ptr{Ptr{Void}}), @@ -30,11 +29,8 @@ done(cursor::MongoCursor, state::Void) = begin Array{Ptr{Void}}(1) ) end -export done -if Base.VERSION > v"0.5.0-" Base.iteratorsize(::Type{MongoCursor}) = Base.SizeUnknown() -end Base.eltype(::Type{MongoCursor}) = BSONObject destroy(collection::MongoCursor) = diff --git a/test/.gitkeep b/test/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/test/REQUIRE b/test/REQUIRE deleted file mode 100644 index 1019439..0000000 --- a/test/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -FactCheck -DataStructures 0.4.2 diff --git a/test/runtests.jl b/test/runtests.jl index f1faff1..a6116e8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,90 +1,98 @@ -using FactCheck, LibBSON, Mongo, DataStructures -facts("Mongo") do +using LibBSON, Mongo, Base.Test + +@testset "Mongo" begin client = MongoClient() collection = MongoCollection(client, "foo", "bar") oid = BSONOID() - context("variables") do - @fact collection.client --> client - @fact collection.db --> "foo" - @fact collection.name --> "bar" + @testset "variables" begin + @test collection.client == client + @test collection.db == "foo" + @test collection.name == "bar" end - context("insert") do + @testset "insert" begin insert(collection, ("_id" => oid, "hello" => "before")) - @fact count(collection, ("_id" => oid)) --> 1 - @fact count(collection) --> 1 + @test count(collection, ("_id" => oid)) == 1 + @test count(collection) == 1 for item in find(collection, ("_id" => oid), ("_id" => false, "hello" => true)) - @fact dict(item) --> Dict("hello" => "before") + @test dict(item) == Dict("hello" => "before") end end - context("update") do + @testset "update" begin update( collection, ("_id" => oid), set("hello" => "after") ) - @fact count(collection, ("_id" => oid)) --> 1 + + @test count(collection, ("_id" => oid)) == 1 for item in find(collection, ("_id" => oid), ("_id" => false, "hello" => true)) - @fact dict(item) --> Dict("hello" => "after") + @test dict(item) == Dict("hello" => "after") end end - context("command_simple") do + @testset "command_simple" begin reply = command_simple( client, "foo", - OrderedDict( + Dict( "count" => "bar", "query" => Dict("_id" => oid)) ) - @fact reply["n"] --> 1 + @test reply["n"] == 1 end - context("delete") do + @testset "delete" begin delete( collection, ("_id" => oid) ) - @fact count(collection, ("_id" => oid)) --> 0 - @fact count(collection) --> 0 + @test count(collection, ("_id" => oid)) == 0 + @test count(collection) == 0 end end -facts("Mongo: bad host/port") do +@testset "Mongo: bad host/port" begin client = MongoClient("bad-host-name", 9999) collection = MongoCollection(client, "foo", "bar") - @fact_throws insert(collection, ("foo" => "bar")) + @test_throws ErrorException insert(collection, ("foo" => "bar")) end -facts("Query building helpers") do +@testset "Query building helpers" begin client = MongoClient() ppl = MongoCollection(client, "foo", "ppl") person(name, age) = insert(ppl, ("name" => name, "age" => age)) person("Tim", 25) person("Jason", 21) person("Jim", 87) - context("orderby") do - @fact first(find(ppl, (query(), orderby("age" => -1))))["name"] --> "Jim" - @fact first(find(ppl, (query(), orderby("age" => 1))))["name"] --> "Jason" + + @testset "orderby" begin + @test first(find(ppl, (query(), orderby("age" => -1))))["name"] == "Jim" + @test first(find(ppl, (query(), orderby("age" => 1))))["name"] == "Jason" end - context("gt and lt") do - @fact first(find(ppl, query("age" => lt(25))))["name"] --> "Jason" - @fact first(find(ppl, query("age" => gt(50))))["name"] --> "Jim" + + @testset "gt and lt" begin + @test first(find(ppl, query("age" => lt(25))))["name"] == "Jason" + @test first(find(ppl, query("age" => gt(50))))["name"] == "Jim" end - context("in and nin") do - @fact first(find(ppl, query("age" => in([21]))))["name"] --> "Jason" - @fact first(find(ppl, query("age" => nin([21,25]))))["name"] --> "Jim" + + @testset "in and nin" begin + @test first(find(ppl, query("age" => in([21]))))["name"] == "Jason" + @test first(find(ppl, query("age" => nin([21,25]))))["name"] == "Jim" end - context("eq and ne") do - @fact first(find(ppl, query("age" => eq(21))))["name"] --> "Jason" - @fact first(find(ppl, query("age" => ne(87))))["name"] == "Jim" --> false + + @testset "eq and ne" begin + @test first(find(ppl, query("age" => eq(21))))["name"] == "Jason" + @test first(find(ppl, query("age" => ne(87))))["name"] != "Jim" end - context("update with operator") do + + @testset "update with operator" begin update(ppl, ("age" => 87), set("age" => 88)) - @fact first(find(ppl, query("name" => "Jim")))["age"] --> 88 + @test first(find(ppl, query("name" => "Jim")))["age"] == 88 end + delete(ppl, ()) end