Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the cl-mongo-id Reference Manual, version 0.1.0, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Aug 15 03:50:12 2022 GMT+0.
Next: Systems, Previous: The cl-mongo-id Reference Manual, Up: The cl-mongo-id Reference Manual [Contents][Index]
This is a simple library for creating/handling MongoDB's ObjectIDs. It works by grabbing key pieces of info (unix timestamp, hostname, process pid, global "inc" value) and creating a new ObjectID as a 12-byte array #(79 151 129 ...)
It aims to be as cross-platform/implementation as possible. If you find you're unable to run it on your specific platform, please let me know and I'll do whatever I can to fix it!
Install
(ql:quickload :cl-mongo-id)
Create byte array from string id:
(mongoid:oid "4f9638d834322b9531000005") ->
#(79 150 56 216 52 50 43 149 49 0 0 5)
Create new id using MongoDB ObjectID specification:
(mongoid:oid) ->
#(79 150 62 92 61 196 44 20 228 0 0 0)
Grab a string representation of an ObjectID:
(mongoid:oid-str (mongoid:oid)) ->
"4F97001C3DC42C14E4000050"
The cl-mongo-id library can be used with cl-mongo.
There is just one caveat: You must import the make-bson-oid
function from the
cl-mongo package before you can use the two together.
(import 'cl-mongo::make-bson-oid)
(with-mongo-connection (:host "127.0.0.1" :db "test")
;; querying a document of a known id
(db.find "mycoll" (kv "_id" (make-bson-oid :oid (mongoid:oid "4f9638d834322b9531000005"))))
;; creating a new document with an oid
(let ((doc (make-document :oid (make-bson-oid :oid (mongoid:oid)))))
(db.save "mycoll" doc)))
cl-mongo-id is built to be thread-safe (specifically the inc value). This means
you can create ID's via (oid)
from several different threads without problems,
and your inc value will be unique between all of them.
cl-mongo-id has some helper functions that may be useful to you for either debugging or grabbing values from your IDs.
Note that all of the following helper functions take a keyword argument :bytes
,
which if set to T will return the raw bytes of that portion of the ID and not
convert them to an integer.
Get the unix timestamp from a Mongo Object ID:
(mongoid:get-timestamp (oid "4f96f9fa3dc42c14e400004e")) ->
1335294458
Get the PID the ID was created under:
(mongoid:get-pid (oid "4f96f9fa3dc42c14e400004e")) ->
5348
Get the int value corresponding to the first three bytes of the MD5 of the hostname the ID was created on:
(mongoid:get-hostname (oid "4f96f9fa3dc42c14e400004e")) ->
4047916
(mongoid:get-hostname (oid "4f96f9fa3dc42c14e400004e") :bytes t) ->
#(61 196 44)
Get the ID's "inc" value:
(mongoid:get-inc (oid "4f96f9fa3dc42c14e400004e")) ->
78
Next: Files, Previous: Introduction, Up: The cl-mongo-id Reference Manual [Contents][Index]
The main system appears first, followed by any subsystem dependency.
A library for the creation/parsing of MongoDB Object IDs
Andrew Lyon <orthecreedence@gmail.com>
MIT
0.1.0
mongo-id.lisp (file).
Next: Packages, Previous: Systems, Up: The cl-mongo-id Reference Manual [Contents][Index]
Files are sorted by type and then listed depth-first from the systems components trees.
Next: cl-mongo-id/mongo-id.lisp, Previous: Lisp, Up: Lisp [Contents][Index]
cl-mongo-id (system).
Previous: cl-mongo-id/cl-mongo-id.asd, Up: Lisp [Contents][Index]
cl-mongo-id (system).
Next: Definitions, Previous: Files, Up: The cl-mongo-id Reference Manual [Contents][Index]
Packages are listed by definition order.
mongoid
common-lisp.
Next: Indexes, Previous: Packages, Up: The cl-mongo-id Reference Manual [Contents][Index]
Definitions are sorted by export status, category, package, and then by lexicographic order.
Next: Internals, Previous: Definitions, Up: Definitions [Contents][Index]
Previous: Public Interface, Up: Public Interface [Contents][Index]
Grab the inc value out of a vector oid. Passing :bytes t will return an array of bytes corresponding to the inc value of the ID instead of parsing it as an integer.
Grab the timestamp out of a vector oid. Passing :bytes t will return an array of bytes corresponding to the timestamp part of the ID instead of parsing it as an integer.
Generate a mongo id, in byte vector format.
Given a vector ID, convert it to a string.
Previous: Public Interface, Up: Definitions [Contents][Index]
Next: Ordinary functions, Previous: Internals, Up: Internals [Contents][Index]
Previous: Special variables, Up: Internals [Contents][Index]
Takes a hex string, IE 4f2b8096 and converts it into a byte array:
4f2b8096 -> #(79 43 128 150)
Hex string *must* have even number of bytes.
Convert a byte array to an integer: #(79 150 243 81) -> 1335292753
Create a brand-spankin-new ObjectId using the current timestamp/inc values, along with hostname and process pid.
Get the current process’ PID. This function does it’s best to be cross- implementation. If it isn’t able to grab the PID from the system, it defaults to returning whatever value is passed into the :if-not-exists-return key.
Get current unix timestamp.
Thread-safe method to get current ObjectId inc value. Takes an optional timestamp value to calculate inc for.
Previous: Definitions, Up: The cl-mongo-id Reference Manual [Contents][Index]
Jump to: | C F G O |
---|
Jump to: | C F G O |
---|
Next: Data types, Previous: Functions, Up: Indexes [Contents][Index]
Jump to: | *
S |
---|
Jump to: | *
S |
---|
Jump to: | C F M P S |
---|
Jump to: | C F M P S |
---|