Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
abe0073
Memory Leak Fixes and Associted Options for the driver
washu Sep 26, 2019
60d40ac
Memory Leak Fixes and Associted Options for the driver
washu Sep 26, 2019
ca8a359
Memory Leak Fixes and Associted Options for the driver
washu Sep 26, 2019
553f311
Memory Leak Fixes and Associted Options for the driver
washu Sep 26, 2019
1ee0654
crystal 0.31 fixes
washu Sep 26, 2019
894eb87
crystal 0.31 fixes
washu Sep 26, 2019
dffe729
crystal 0.31 fixes
washu Sep 26, 2019
9e13316
crystal 0.31 fixes
washu Sep 26, 2019
7f66a40
memory leak fixes
washu Sep 27, 2019
a7dbdc9
memory leak fixes
washu Sep 27, 2019
2a0a3ed
emory leak work
washu Sep 27, 2019
879f771
emory leak work
washu Sep 27, 2019
7e3fc0f
emory leak work
washu Sep 27, 2019
0e2e894
emory leak work
washu Sep 27, 2019
5618599
memory leak fixes
washu Sep 27, 2019
1ec0781
memory leak fixes
washu Sep 27, 2019
9c546b6
memory leak fixes
washu Sep 27, 2019
a729641
memory leak fixes
washu Sep 27, 2019
bf71522
added invalidte to the pool
washu Sep 30, 2019
5224e65
Merge pull request #51 from washu/master
datanoise Sep 30, 2019
ede07ed
fixes custom stream initiator
elbywan Dec 15, 2019
78eba94
crystal 0.32.0 compatibility fixes
elbywan Dec 15, 2019
c9956a9
prevents freeing bson pointers twice
elbywan Dec 15, 2019
8f86086
Merge pull request #54 from elbywan/master
datanoise Dec 15, 2019
f6c76da
add missing Mongo::Stream definitions and properly handle reconnection
elbywan Dec 16, 2019
14036db
Merge pull request #55 from elbywan/master
datanoise Dec 16, 2019
6a5b6d9
fixes collection_command_simple call
elbywan Dec 30, 2019
6bb2882
streams: fix socket connection when in pooled mode (no polling)
elbywan Jan 2, 2020
38c8714
Merge pull request #56 from elbywan/master
datanoise Jan 2, 2020
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
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mongo
version: 0.1.0
version: 0.1.3

authors:
- Jerome Gravel-Niquet <[email protected]>
Expand All @@ -8,6 +8,6 @@ description: |
Bindings for MongoDB C Driver.

libraries:
libmongoc: 1.1.0
libmongoc: 1.1.15

license: MIT
18 changes: 9 additions & 9 deletions spec/bson_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe BSON::ObjectId do

it "should be able to get a time" do
oid = BSON::ObjectId.new
(oid.time - Time.utc_now).should be < 1.seconds
(oid.time - Time.utc).should be < 1.seconds
end

it "should be able to compare ObjectIds" do
Expand All @@ -43,9 +43,9 @@ end

describe BSON::Timestamp do
it "should be comparable" do
t = Time.now
t1 = BSON::Timestamp.new(t.epoch_ms, 1)
t2 = BSON::Timestamp.new(t.epoch_ms, 2)
t = Time.utc
t1 = BSON::Timestamp.new(t.to_unix.to_u32, 1)
t2 = BSON::Timestamp.new(t.to_unix.to_u32, 2)
t2.should be > t1
end
end
Expand Down Expand Up @@ -184,22 +184,22 @@ describe BSON do
end

it "should be able to append time" do
t = Time.now
t = Time.utc
bson = BSON.new
bson["time"] = t
bson_t = bson["time"]
if bson_t.is_a?(Time)
bson_t.epoch.should eq(t.to_utc.epoch)
bson_t.to_unix.should eq(t.to_utc.to_unix)
else
fail "expected Time"
end
end

it "should be able to append timestamp" do
t = Time.now
t = Time.utc
bson = BSON.new
bson["ts"] = BSON::Timestamp.new(t.epoch_ms, 1)
bson["ts"].should eq(BSON::Timestamp.new(t.epoch_ms, 1))
bson["ts"] = BSON::Timestamp.new(t.to_unix.to_u32, 1)
bson["ts"].should eq(BSON::Timestamp.new(t.to_unix.to_u32, 1))
end

it "should be able to append regex" do
Expand Down
6 changes: 3 additions & 3 deletions spec/collection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ describe Mongo::Collection do
it "should be able to drop a collection" do
with_collection do |col|
col.insert({"name" => "Bob"})
col.database.collection_names.includes?(col.name).should be_true
col.database.try &.collection_names.includes?(col.name).should be_true
col.drop
col.database.collection_names.includes?(col.name).should be_false
col.database.try &.collection_names.includes?(col.name).should be_false
end
end

Expand Down Expand Up @@ -142,7 +142,7 @@ describe Mongo::Collection do
it "should be able to rename a collection" do
with_collection do |col|
col.insert({"name" => "Bob"})
col.rename(col.database.name, "new_name")
col.rename(col.database.try &.name || "", "new_name")
col.name.should eq("new_name")
end
end
Expand Down
11 changes: 6 additions & 5 deletions spec/database_spec.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require "../src/mongo"
require "spec"

db_name = "my_db_#{Time.utc.to_unix_ms}"

describe Mongo::Database do
it "should be able to create a new database" do
client = Mongo::Client.new("mongodb://localhost")
db_name = "my_db_#{Time.now.epoch}"
db = client[db_name]
db.name.should eq(db_name)
end

it "should be able to creata a collection" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
db = client[db_name]
db.create_collection("my_col")

db.has_collection?("my_col").should be_true
Expand All @@ -26,7 +27,7 @@ describe Mongo::Database do

it "should be able to manage users" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
db = client[db_name]
db.add_user("new_user", "new_pass")
db["my_col"].insert(BSON.new)
user = db.users.not_nil!["0"]
Expand All @@ -42,7 +43,7 @@ describe Mongo::Database do

it "should be able to modify write_concern" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
db = client[db_name]
db.write_concern.fsync.should be_false
db.write_concern.fsync = true
db.write_concern.fsync.should be_true
Expand All @@ -54,7 +55,7 @@ describe Mongo::Database do

it "should be able to modify read preferences" do
client = Mongo::Client.new("mongodb://localhost")
db = client["my_db_#{Time.now.epoch}"]
db = client[db_name]
db.read_prefs.mode.should eq(LibMongoC::ReadMode::PRIMARY)
tag = BSON.new
tag["name"] = "my_tag"
Expand Down
6 changes: 4 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
def create_client
Mongo::Client.new("mongodb://localhost")
client = Mongo::Client.new("mongodb://localhost")
client.setup_stream
client
end

def create_database
client = create_client
client["my_db_#{Time.now.epoch}"]
client["my_db_#{Time.utc.to_unix_ms}"]
end

def create_collection
Expand Down
6 changes: 3 additions & 3 deletions src/bson.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class BSON
new(handle)
end

protected def invalidate
finalize
def invalidate
@valid = false
LibBSON.bson_destroy(@handle)
end

protected def handle
Expand Down Expand Up @@ -173,7 +173,7 @@ class BSON
end

def []=(key, value : Time)
LibBSON.bson_append_date_time(handle, key, key.bytesize, value.to_utc.epoch * 1000)
LibBSON.bson_append_date_time(handle, key, key.bytesize, value.to_utc.to_unix * 1000)
end

def []=(key, value : Timestamp)
Expand Down
71 changes: 71 additions & 0 deletions src/bson/lib_bson.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,77 @@ lib LibBSON
BSON_TYPE_MINKEY = 0xFF
end

enum ErrorDomain
MONGOC_ERROR_CLIENT = 1,
MONGOC_ERROR_STREAM,
MONGOC_ERROR_PROTOCOL,
MONGOC_ERROR_CURSOR,
MONGOC_ERROR_QUERY,
MONGOC_ERROR_INSERT,
MONGOC_ERROR_SASL,
MONGOC_ERROR_BSON,
MONGOC_ERROR_MATCHER,
MONGOC_ERROR_NAMESPACE,
MONGOC_ERROR_COMMAND,
MONGOC_ERROR_COLLECTION,
MONGOC_ERROR_GRIDFS,
MONGOC_ERROR_SCRAM,
MONGOC_ERROR_SERVER_SELECTION,
MONGOC_ERROR_WRITE_CONCERN,
MONGOC_ERROR_SERVER,
MONGOC_ERROR_TRANSACTION,
end

enum ErrorCode
MONGOC_ERROR_STREAM_INVALID_TYPE = 1,
MONGOC_ERROR_STREAM_INVALID_STATE,
MONGOC_ERROR_STREAM_NAME_RESOLUTION,
MONGOC_ERROR_STREAM_SOCKET,
MONGOC_ERROR_STREAM_CONNECT,
MONGOC_ERROR_STREAM_NOT_ESTABLISHED,
MONGOC_ERROR_CLIENT_NOT_READY,
MONGOC_ERROR_CLIENT_TOO_BIG,
MONGOC_ERROR_CLIENT_TOO_SMALL,
MONGOC_ERROR_CLIENT_GETNONCE,
MONGOC_ERROR_CLIENT_AUTHENTICATE,
MONGOC_ERROR_CLIENT_NO_ACCEPTABLE_PEER,
MONGOC_ERROR_CLIENT_IN_EXHAUST,
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
MONGOC_ERROR_PROTOCOL_BAD_WIRE_VERSION,
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
MONGOC_ERROR_QUERY_FAILURE,
MONGOC_ERROR_BSON_INVALID,
MONGOC_ERROR_MATCHER_INVALID,
MONGOC_ERROR_NAMESPACE_INVALID,
MONGOC_ERROR_NAMESPACE_INVALID_FILTER_TYPE,
MONGOC_ERROR_COMMAND_INVALID_ARG,
MONGOC_ERROR_COLLECTION_INSERT_FAILED,
MONGOC_ERROR_COLLECTION_UPDATE_FAILED,
MONGOC_ERROR_COLLECTION_DELETE_FAILED,
MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST = 26,
MONGOC_ERROR_GRIDFS_INVALID_FILENAME,
MONGOC_ERROR_SCRAM_NOT_DONE,
MONGOC_ERROR_SCRAM_PROTOCOL_ERROR,
MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND = 59,
MONGOC_ERROR_QUERY_NOT_TAILABLE = 13051,
MONGOC_ERROR_SERVER_SELECTION_BAD_WIRE_VERSION,
MONGOC_ERROR_SERVER_SELECTION_FAILURE,
MONGOC_ERROR_SERVER_SELECTION_INVALID_ID,
MONGOC_ERROR_GRIDFS_CHUNK_MISSING,
MONGOC_ERROR_GRIDFS_PROTOCOL_ERROR,
MONGOC_ERROR_PROTOCOL_ERROR = 17,
MONGOC_ERROR_WRITE_CONCERN_ERROR = 64,
MONGOC_ERROR_DUPLICATE_KEY = 11000,
MONGOC_ERROR_MAX_TIME_MS_EXPIRED = 50,
MONGOC_ERROR_CHANGE_STREAM_NO_RESUME_TOKEN,
MONGOC_ERROR_CLIENT_SESSION_FAILURE,
MONGOC_ERROR_TRANSACTION_INVALID_STATE,
MONGOC_ERROR_GRIDFS_CORRUPT,
MONGOC_ERROR_GRIDFS_BUCKET_FILE_NOT_FOUND,
MONGOC_ERROR_GRIDFS_BUCKET_STREAM
end


struct Oid
bytes: UInt8[12]
end
Expand Down
2 changes: 1 addition & 1 deletion src/bson/object_id.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BSON
t = LibBSON.bson_oid_get_time_t(@handle)
ts = LibC::Timespec.new
ts.tv_sec = t
Time.new(ts, Time::Kind::Utc)
Time.new(ts, Time::Location::UTC)
end
end
end
20 changes: 19 additions & 1 deletion src/bson/value.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "./lib_bson"

class BSON
alias ValueType = BSON | Binary | Code | MaxKey | MinKey | ObjectId | Symbol | Timestamp | Bool | Float64 | Int32 | Int64 | Regex | String | Time | Nil
class Value
@handle : LibBSON::Value

Expand All @@ -18,6 +19,23 @@ class BSON
def value
v = @handle.value
case @handle.v_type
when LibBSON::Type::BSON_TYPE_BINARY
bytes = Bytes.new(v.v_binary.data, v.v_binary.len.to_i32)
subtype = case v.v_binary.sub_type
when LibBSON::SubType::BSON_SUBTYPE_BINARY
Binary::SubType::Binary
when LibBSON::SubType::BSON_SUBTYPE_FUNCTION
Binary::SubType::Function
when LibBSON::SubType::BSON_SUBTYPE_UUID
Binary::SubType::UUID
when LibBSON::SubType::BSON_SUBTYPE_MD5
Binary::SubType::MD5
when LibBSON::SubType::BSON_SUBTYPE_USER
Binary::SubType::User
else
raise "Deprecated binary types should not be used"
end
Binary.new(subtype, bytes)
when LibBSON::Type::BSON_TYPE_EOD
nil
when LibBSON::Type::BSON_TYPE_DOUBLE
Expand All @@ -39,7 +57,7 @@ class BSON
when LibBSON::Type::BSON_TYPE_DATE_TIME
spec = LibC::Timespec.new
spec.tv_sec = v.v_datetime / 1000
Time.new(spec, Time::Kind::Utc)
Time.new(spec, Time::Location::UTC)
when LibBSON::Type::BSON_TYPE_NULL
nil
when LibBSON::Type::BSON_TYPE_REGEX
Expand Down
2 changes: 2 additions & 0 deletions src/mongo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ module Mongo
LibMongoC.log_set_handler ->(level, domain, msg, user_data) {
self.log(level, String.new(domain), String.new(msg))
}, nil
LibMongoC.mongo_init(nil)
at_exit { LibMongoC.mongo_cleanup(nil) }
end
12 changes: 10 additions & 2 deletions src/mongo/bulk_operation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ class Mongo::BulkOperation
def initialize(@handle : LibMongoC::BulkOperation)
raise "invalid handle" unless @handle
@executed = false
@valid = true
end

def finalize
def invalidate
@valid = false
LibMongoC.bulk_operation_destroy(@handle)
end

def finalize
LibMongoC.bulk_operation_destroy(@handle) if @valid
end

# Queue an insert of a single document into a bulk operation. The insert is
# not performed until `execute` is called.
def insert(document)
Expand Down Expand Up @@ -65,7 +71,9 @@ class Mongo::BulkOperation
if hint == 0
raise BSON::BSONError.new(pointerof(error))
end
BSON.copy_from pointerof(reply)
results = BSON.copy_from pointerof(reply)
LibBSON.bson_destroy(pointerof(reply))
results
end

def to_unsafe
Expand Down
Loading