jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

Reusing the same connection for several db/connections #117

Closed mredaelli closed 6 years ago

mredaelli commented 6 years ago

Regarding #84, this pull request tries to introduce the behaviour while maintaining compatibility with existing code. One new function, $mongo_client(), creates a client object without any reference to a db or collection. Is has a function $use(), which opens a new collection backed by the same connection.

Each mongo object also has a $use() function, with the same behaviour.

mredaelli commented 6 years ago

@jeroen I'm not sure if the errors in appveyor and travis are my fault or not

This:

Connect to mongolabs

con <- mongo("mtcars", url = "mongodb://readwrite:test@ds043942.mongolab.com:43942/jeroen_test") [1] "opened collection mtcars in db test in 249.218018 milliseconds" if(con$count() > 0) con$drop() Error: not authorized on test to execute command { count: "mtcars", query: {} }

seems not to be, and besides there are other pull requests that also failed but were merged. Can you advise if there's something else I should do?

codecov-io commented 6 years ago

Codecov Report

Merging #117 into master will decrease coverage by 8.81%. The diff coverage is 55.43%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #117      +/-   ##
==========================================
- Coverage   35.65%   26.84%   -8.82%     
==========================================
  Files         125      126       +1     
  Lines       20118    20150      +32     
==========================================
- Hits         7174     5409    -1765     
- Misses      12944    14741    +1797
Impacted Files Coverage Δ
R/client.R 39.56% <ø> (+0.85%) :arrow_up:
R/mongo.R 48.36% <36.95%> (-0.05%) :arrow_down:
R/mongoclient.R 73.91% <73.91%> (ø)
R/gridfs.R 0% <0%> (-95.24%) :arrow_down:
src/mongoc/mongoc-stream-tls-openssl-bio.c 0% <0%> (-86.37%) :arrow_down:
src/gridfs.c 0% <0%> (-79.72%) :arrow_down:
src/mongoc/mongoc-gridfs-file-page.c 0% <0%> (-75.81%) :arrow_down:
src/mongoc/mongoc-stream-tls-openssl.c 0% <0%> (-72.61%) :arrow_down:
src/mongoc/mongoc-stream-file.c 0% <0%> (-71.93%) :arrow_down:
src/mongoc/mongoc-gridfs-file.c 0% <0%> (-66.93%) :arrow_down:
... and 41 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update b21a3ea...a7474b8. Read the comment docs.

jeroen commented 6 years ago

Thanks for your pull request. I have implemented a slightly different approach, which automatically uses existing connections when possible, but also automatically disconnects when they are no longer used. You can try it in master (soon on cran).