Skip to content

Commit

Permalink
Merge pull request #29 from mashingan/gcsafedevelop
Browse files Browse the repository at this point in the history
adding gcsafe pragmas for findOne, update to work
  • Loading branch information
Samuel authored Oct 4, 2023
2 parents fd14232 + 850576f commit 5d7c61c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/anonimongo/collections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import multisock
## .. _items: #items.i,Cursor
## .. _getMore: dbops/crud.html#getMore,Database,int64,string,int

proc one*(q: Query[AsyncSocket]): Future[BsonDocument] {.multisock.} =
proc one*(q: Query[AsyncSocket]): Future[BsonDocument] {.multisock, gcsafe.} =
let doc = await q.collection.db.find(q.collection.name, q.query, q.sort,
q.projection, skip = q.skip, limit = 1, singleBatch = true)
let batch = doc["cursor"]["firstBatch"].ofArray
Expand Down Expand Up @@ -106,7 +106,7 @@ proc find*(c: Collection[AsyncSocket], query = bson(), projection = bsonNull()):
result.projection = projection

proc findOne*(c: Collection[AsyncSocket], query = bson(), projection = bsonNull(),
sort = bsonNull()): Future[BsonDocument] {.multisock.} =
sort = bsonNull()): Future[BsonDocument] {.multisock, gcsafe.} =
var q = await c.find(query, projection)
q.sort = sort
result = await q.one
Expand Down
8 changes: 4 additions & 4 deletions src/anonimongo/core/bson.nim
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ proc writeKey(s: var Streamable, key: string, kind: BsonKind): int32 =
s.write 0x00.byte
result = int32(1 + key.len + 1)

proc encode*(doc: BsonDocument): (int, string)
proc encode*(doc: BsonDocument): (int, string) {.gcsafe.}

proc encode(s: var Streamable, key: string, doc: BsonInt32): int =
result = s.writeKey(key, bkInt32) + doc.value.sizeof
Expand Down Expand Up @@ -742,7 +742,7 @@ proc encode(s: var Streamable, key: string, doc: BsonBinary): int =
for b in doc.value:
s.writeLE b

proc encode(s: var Streamable, key: string, doc: BsonTimestamp): int =
proc encode(s: var Streamable, key: string, doc: BsonTimestamp): int {.gcsafe.} =
result = s.writeKey(key, bkTimestamp) + uint64.sizeof
s.writeLE doc.value[0]
s.writeLE doc.value[1]
Expand Down Expand Up @@ -904,7 +904,7 @@ proc decodeKey(s: var Streamable): (string, BsonKind) =
buff &= achar
result = (buff, kind)

proc decode*(strbytes: string): BsonDocument
proc decode*(strbytes: string): BsonDocument {.gcsafe.}

proc decodeArray(s: var Streamable): seq[BsonBase] =
let length = s.peekInt32LE
Expand Down Expand Up @@ -954,7 +954,7 @@ proc decodeBinary(s: var Streamable): (BsonSubtype, seq[byte]) =
thebytes.add s.readChar.byte
result = (subtype, thebytes)

proc decode(s: var Streamable): (string, BsonBase) =
proc decode(s: var Streamable): (string, BsonBase) {.gcsafe.} =
let (key, kind) = s.decodeKey
var val: BsonBase
case kind
Expand Down
4 changes: 2 additions & 2 deletions src/anonimongo/core/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func flags*(d: Database): int32 = d.db.flags as int32

proc sendOps*(q: BsonDocument, db: Database[AsyncSocket], name = "", cmd = ckRead,
compression = cidNoop):
Future[ReplyFormat]{.multisock.} =
Future[ReplyFormat]{.multisock, gcsafe.} =
## A helper utility which greatly simplify actual Database command
## queries. Any new command implementation usually use this
## helper proc. Cmd argument is needed to recognize what kind
Expand Down Expand Up @@ -93,7 +93,7 @@ proc proceed*(db: Database[AsyncSocket], q: BsonDocument, dbname = "", cmd = ckR

#template crudops(db: Database, q: BsonDocument): untyped {.multisock.} =
proc crudops*(db: Database[AsyncSocket], q: BsonDocument, dbname = "", cmd = ckRead):
Future[BsonDocument]{.multisock.} =
Future[BsonDocument]{.multisock, gcsafe.} =
## About the same as ``proceed`` but this will return a BsonDocument
## compared to ``proceed`` that return ``WriteResult``.
let compressions = db.db.compressions
Expand Down
8 changes: 4 additions & 4 deletions src/anonimongo/core/wire.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type

const msgDefaultFlags = 0

proc serialize(s: var Streamable, doc: BsonDocument): int =
proc serialize(s: var Streamable, doc: BsonDocument): int {.gcsafe.} =
let (doclen, docstr) = encode doc
result = doclen
s.write docstr
Expand Down Expand Up @@ -108,7 +108,7 @@ proc replyParse*(s: var Streamable): ReplyFormat =
result.documents[i] = s.readStr(doclen).decode
if s.atEnd or s.peekChar.byte == 0: break

proc msgParse*(s: var Streamable, rest = 0): ReplyFormat =
proc msgParse*(s: var Streamable, rest = 0): ReplyFormat {.gcsafe.} =
## Get the message in the ReplyFormat from given data stream.
## This is adapted to older type ReplyFormat from newer wire
## protocol OP_MSG.
Expand All @@ -130,7 +130,7 @@ proc msgParse*(s: var Streamable, rest = 0): ReplyFormat =

proc prepareQuery*(s: var Streamable, reqId, target, opcode, flags: int32,
collname: string, nskip, nreturn: int32,
query = newbson(), selector = newbson(), compression = cidNoop): int =
query = newbson(), selector = newbson(), compression = cidNoop): int {.gcsafe.} =
## Convert and encode the query into stream to be ready for sending
## onto TCP wire socket.
var query = query
Expand Down Expand Up @@ -244,7 +244,7 @@ proc look*(reply: ReplyFormat) =
for d in reply.documents:
dump d

proc getReply*(socket: AsyncSocket): Future[ReplyFormat] {.multisock.} =
proc getReply*(socket: AsyncSocket): Future[ReplyFormat] {.multisock, gcsafe.} =
## Get data from socket and apply the replyParse into the result.
var bstrhead = newStringStream(await socket.recv(size = 16))
let msghdr = msgHeaderFetch bstrhead
Expand Down
2 changes: 1 addition & 1 deletion src/anonimongo/dbops/crud.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ proc find*(db: Database[AsyncSocket], coll: string,query = bson(),
max = bsonNull(), min = bsonNull(), returnKey = false, showRecordId = false,
tailable = false, awaitData = false, oplogReplay = false,
noCursorTimeout = false, partial = false,
collation = bsonNull(), explain = ""): Future[BsonDocument]{.multisock.} =
collation = bsonNull(), explain = ""): Future[BsonDocument]{.multisock, gcsafe.} =
var q = bson({ find: coll, filter: query })
for field, val in {
"sort": sort,
Expand Down

0 comments on commit 5d7c61c

Please sign in to comment.