text
stringlengths
12
786k
let buildInfo a = wrap_unix send_cmd ( a , create_cmd " buildInfo " ) ; ;
let collStats a = wrap_unix send_cmd ( a , create_cmd " collStats " ) ; ;
let connPoolStats a = wrap_unix send_cmd ( a , create_cmd " connPoolStats " ) ; ;
let cursorInfo a = wrap_unix send_cmd ( a , create_cmd " cursorInfo " ) ; ;
let getCmdLineOpts a = wrap_unix send_cmd ( a , create_cmd " getCmdLineOpts " ) ; ;
let hostInfo a = wrap_unix send_cmd ( a , create_cmd " hostInfo " ) ; ;
let listCommands a = wrap_unix send_cmd ( a , create_cmd " listCommands " ) ; ;
let serverStatus a = wrap_unix send_cmd ( a , create_cmd " serverStatus " ) ; ;
type cmd = { name : string ; query : string } ; ;
let get_channel_pool = Mongo_lwt . get_channel_pool ; ;
let wrap_bson f arg = try ( f arg ) with | Bson . Invalid_objectId -> raise ( MongoAdmin_failed " Bson . Invalid_objectId " ) | Bson . Wrong_bson_type -> raise ( MongoAdmin_failed " Wrong_bson_type when encoding bson doc " ) | Bson . Malformed_bson -> raise ( MongoAdmin_failed " M...
let wrap_unix_lwt f arg = try_lwt ( f arg ) with | Unix . Unix_error ( e , _ , _ ) -> raise ( MongoAdmin_failed ( Unix . error_message e ) ) ; ;
let create ? max_connection ip port = Mongo_lwt . create ? max_connection ip port admin_db_name admin_collection_name ; ;
let create_local_default ( ) = create " 127 . 0 . 0 . 1 " 27017 ; ;
let destroy a = Mongo_lwt . destroy a ; ;
let create_cmd name = let e_1 = Bson . create_int32 ( 1l ) in let cmd_doc name = Bson . add_element name e_1 Bson . empty in { name = name ; query = let find_in ( flags , skip , return , q , s ) = wrap_bson find_in ( 0l , 0l , ( - 1l ) , ( cmd_doc name ) , Bson . ...
let send_cmd ( a , cmd ) = MongoSend_lwt . send_with_reply ( Mongo_lwt . get_channel_pool a ) cmd . query ; ;
let listDatabases a = wrap_unix_lwt send_cmd ( a , create_cmd " listDatabases " ) ; ;
let buildInfo a = wrap_unix_lwt send_cmd ( a , create_cmd " buildInfo " ) ; ;
let collStats a = wrap_unix_lwt send_cmd ( a , create_cmd " collStats " ) ; ;
let connPoolStats a = wrap_unix_lwt send_cmd ( a , create_cmd " connPoolStats " ) ; ;
let cursorInfo a = wrap_unix_lwt send_cmd ( a , create_cmd " cursorInfo " ) ; ;
let getCmdLineOpts a = wrap_unix_lwt send_cmd ( a , create_cmd " getCmdLineOpts " ) ; ;
let hostInfo a = wrap_unix_lwt send_cmd ( a , create_cmd " hostInfo " ) ; ;
let listCommands a = wrap_unix_lwt send_cmd ( a , create_cmd " listCommands " ) ; ;
let serverStatus a = wrap_unix_lwt send_cmd ( a , create_cmd " serverStatus " ) ; ;
type t = { message_len : int32 ; request_id : int32 ; response_to : int32 ; op : MongoOperation . t } ; ;
let create_header body_len request_id response_to op = { message_len = Int32 . of_int ( body_len + 4 * 4 ) ; request_id = request_id ; response_to = response_to ; op = op } ; ;
let create_request_header body_len request_id op = { message_len = Int32 . of_int ( body_len + 4 * 4 ) ; request_id = request_id ; response_to = 0l ; op = op } ; ;
let get_message_len h = h . message_len ; ;
let get_request_id h = h . request_id ; ;
let get_response_to h = h . response_to ; ;
let get_op h = h . op ; ;
let encode_header h = let buf = Buffer . create 8 in encode_int32 buf h . message_len ; encode_int32 buf h . request_id ; encode_int32 buf h . response_to ; encode_int32 buf ( MongoOperation . to_code ( h . op ) ) ; Buffer . contents buf ; ;
let decode_header str = let ( message_len , next ) = decode_int32 str 0 in let ( request_id , next ) = decode_int32 str next in let ( response_to , next ) = decode_int32 str next in let ( op_code , next ) = decode_int32 str next in { message_len = message_len ; request_id = request_i...
let to_string h = let buf = Buffer . create 64 in Buffer . add_string buf " message_len = " ; Buffer . add_string buf ( Int32 . to_string h . message_len ) ; Buffer . add_string buf " \ n " ; Buffer . add_string buf " request_id = " ; Buffer . add_string buf ( Int32 . t...
type meta_op = | Comment of string | MaxScan of int | Max of Bson . t | Min of Bson . t | OrderBy of Bson . t | Explain | Hint of Bson . t | ReturnKey | ShowDiskLoc | Snapshot
let meta_op query op = let r = if Bson . has_element " $ query " query then query else Bson . add_element " $ query " ( Bson . create_doc_element query ) Bson . empty in match op with | Comment c -> Bson . add_element " $ comment " ( Bson . create_string c ) r | MaxScan i -> Bso...
let comment c query = meta_op query ( Comment c )
let maxScan i query = meta_op query ( MaxScan i )
let min i query = meta_op query ( Min i )
let max i query = meta_op query ( Max i )
let orderBy o query = meta_op query ( OrderBy o )
let explain query = meta_op query Explain
let hint h query = meta_op query ( Hint h )
let returnKey query = meta_op query ReturnKey
let showDiskLoc query = meta_op query ShowDiskLoc
let snapshot query = meta_op query Snapshot
let or_op d_list = Bson . add_element " $ or " ( Bson . create_doc_element_list d_list ) Bson . empty
let and_op d_list = Bson . add_element " $ and " ( Bson . create_doc_element_list d_list ) Bson . empty
let not_op e = Bson . add_element " $ not " e Bson . empty
let nor_op d_list = Bson . add_element " $ nor " ( Bson . create_doc_element_list d_list ) Bson . empty
let all e_list = Bson . add_element " $ all " ( Bson . create_list e_list ) Bson . empty
let elemMatch d = Bson . add_element " $ elemMatch " ( Bson . create_doc_element d ) Bson . empty
let size i = Bson . add_element " $ size " ( Bson . create_int32 ( Int32 . of_int i ) ) Bson . empty
let inc d = Bson . add_element " $ inc " ( Bson . create_doc_element d ) Bson . empty
let rename d = Bson . add_element " $ rename " ( Bson . create_doc_element d ) Bson . empty
let setOnInsert d = Bson . add_element " $ setOnInsert " ( Bson . create_doc_element d ) Bson . empty
let set d = Bson . add_element " $ set " ( Bson . create_doc_element d ) Bson . empty
let unset d = Bson . add_element " $ unset " ( Bson . create_doc_element d ) Bson . empty
let addToSet d = Bson . add_element " $ addToSet " ( Bson . create_doc_element d ) Bson . empty
let pop d = Bson . add_element " $ pop " ( Bson . create_doc_element d ) Bson . empty
let pullAll d = Bson . add_element " $ pullAll " ( Bson . create_doc_element d ) Bson . empty
let pull d = Bson . add_element " $ pull " ( Bson . create_doc_element d ) Bson . empty
let pushAll d = Bson . add_element " $ pushAll " ( Bson . create_doc_element d ) Bson . empty
let push d = Bson . add_element " $ push " ( Bson . create_doc_element d ) Bson . empty
let each el = Bson . add_element " $ each " ( Bson . create_list el ) Bson . empty
let slice i = Bson . add_element " $ slice " ( Bson . create_int32 ( Int32 . of_int i ) ) Bson . empty
let sort d = Bson . add_element " $ sort " ( Bson . create_doc_element d ) Bson . empty
type t = { header : MongoHeader . t ; response_flags : int32 ; cursor_id : int64 ; starting_from : int32 ; num_returned : int32 ; document_list : Bson . t list } ; ;
let get_header r = r . header ; ;
let get_response_flags r = r . response_flags ; ;
let get_cursor r = r . cursor_id ; ;
let get_starting_from r = r . starting_from ; ;
let get_num_returned r = r . num_returned ; ;
let get_document_list r = r . document_list ; ;
let decode_reply_doc str = let rec decode_doc cur acc = if cur >= String . length str then acc else let ( len32 , _ ) = decode_int32 str cur in let len = Int32 . to_int len32 in decode_doc ( cur + len ) ( ( Bson . decode ( String . sub str cur len ) ) :: acc ) in List . rev ( d...
let decode_reply str = let header_str = String . sub str 0 ( 4 * 4 ) in let header = MongoHeader . decode_header header_str in let ( flags , next ) = decode_int32 str ( 4 * 4 ) in let ( cursor , next ) = decode_int64 str next in let ( from , next ) = decode_int32 str next in l...
let to_string r = let buf = Buffer . create 128 in Buffer . add_string buf ( MongoHeader . to_string r . header ) ; Buffer . add_string buf " response_flags = " ; Buffer . add_string buf ( Int32 . to_string r . response_flags ) ; Buffer . add_string buf " \ n " ; Buffer . ...
let combine_header_body request_id op body_buf = let body_len = Buffer . length body_buf in let whole_buf = Buffer . create ( 4 * 4 + body_len ) in let header_str = encode_header ( create_request_header body_len request_id op ) in Buffer . add_string whole_buf header_str ; Buffer . add_buffer ...
let create_insert ( db_name , collection_name ) ( request_id , flags ) insert_doc_list = let body_buf = Buffer . create 32 in encode_int32 body_buf flags ; encode_cstring body_buf ( db_name " . " ^^ collection_name ) ; let rec add_doc = function | [ ] -> ( ) | hd :: tl -> B...
let create_select_body_buf ( db_name , collection_name ) ( request_id , flags ) selector_doc = let body_buf = Buffer . create 32 in encode_int32 body_buf 0l ; encode_cstring body_buf ( db_name " . " ^^ collection_name ) ; encode_int32 body_buf flags ; Buffer . add_string body_buf ( ...
let create_update ( db_name , collection_name ) ( request_id , flags ) ( selector_doc , update_doc ) = let body_buf = create_select_body_buf ( db_name , collection_name ) ( request_id , flags ) selector_doc in Buffer . add_string body_buf ( encode update_doc ) ; combine_header_bo...
let create_delete ( db_name , collection_name ) ( request_id , flags ) selector_doc = let body_buf = create_select_body_buf ( db_name , collection_name ) ( request_id , flags ) selector_doc in combine_header_body request_id OP_DELETE body_buf ; ;
let create_query ( db_name , collection_name ) ( request_id , flags , skip , return ) ( query_doc , selector_doc ) = let body_buf = Buffer . create 32 in encode_int32 body_buf flags ; encode_cstring body_buf ( db_name " . " ^^ collection_name ) ; encode_int32 body_buf skip ; ...
let create_get_more ( db_name , collection_name ) ( request_id , return ) cursor = let body_buf = Buffer . create 32 in encode_int32 body_buf 0l ; encode_cstring body_buf ( db_name " . " ^^ collection_name ) ; encode_int32 body_buf return ; encode_int64 body_buf cursor ; combine_hea...
let create_kill_cursors request_id cursor_list = let body_buf = Buffer . create 32 in encode_int32 body_buf 0l ; let cursor_buf = Buffer . create 12 in let rec create_cursor_buf num = function | [ ] -> num | hd :: tl -> encode_int64 cursor_buf hd ; create_cursor_buf ( num + 1 ) tl in let n...
let print_buffer s = String . iter ( fun c -> let i = Char . code c in if i < 10 then Printf . printf " \\ x0 % X " i else Printf . printf " \\ x % X " i ) s ; print_endline " " ; ;
let cur_timestamp ( ) = Int32 . of_float ( Unix . time ( ) ) ; ;
let encode_int32 buf v = for i = 0 to 3 do let b = Int32 . logand 255l ( Int32 . shift_right v ( i * 8 ) ) in Buffer . add_char buf ( Char . chr ( Int32 . to_int b ) ) done ; ;
let encode_int64 buf v = for i = 0 to 7 do let b = Int64 . logand 255L ( Int64 . shift_right v ( i * 8 ) ) in Buffer . add_char buf ( Char . chr ( Int64 . to_int b ) ) done ; ;
let encode_cstring buf cs = Buffer . add_string buf cs ; Buffer . add_char buf ' \ x00 ' ; ;
let decode_int32 str cur = let rec decode i acc = if i < cur then acc else let high_byte = Char . code str . [ i ] in let high_int32 = Int32 . of_int high_byte in let shift_acc = Int32 . shift_left acc 8 in let new_acc = Int32 . logor high_int32 shift_acc in decode ( i - 1 ) new_acc in ( d...
let decode_int64 str cur = let rec decode i acc = if i < cur then acc else let high_byte = Char . code str . [ i ] in let high_int64 = Int64 . of_int high_byte in let shift_acc = Int64 . shift_left acc 8 in let new_acc = Int64 . logor high_int64 shift_acc in decode ( i - 1 ) new_acc in ( d...
let rec next_x00 str cur = String . index_from str cur ' \ x00 ' ; ;
let decode_cstring str cur = let x00 = next_x00 str cur in if x00 = - 1 then raise Bson . Malformed_bson else ( String . sub str cur ( x00 - cur ) , x00 + 1 ) ; ;
type t = { db_name : string ; collection_name : string ; ip : string ; port : int ; channel_pool : ( Lwt_io . input_channel * Lwt_io . output_channel ) Lwt_pool . t ; max_connection : int ; } ; ;
let get_db_name m = m . db_name ; ;
let get_collection_name m = m . collection_name ; ;
let get_ip m = m . ip ; ;