| 
									
										
										
										
											2009-07-05 07:28:41 -04:00
										 |  |  | USING: accessors arrays assocs bson.constants combinators | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  | combinators.smart constructors destructors fry hashtables io | 
					
						
							|  |  |  | io.pools io.sockets kernel linked-assocs locals math | 
					
						
							|  |  |  | mongodb.cmd mongodb.connection mongodb.msg namespaces parser | 
					
						
							|  |  |  | prettyprint prettyprint.custom prettyprint.sections sequences | 
					
						
							|  |  |  | sets splitting strings ;
 | 
					
						
							|  |  |  | FROM: ascii => ascii? ;
 | 
					
						
							| 
									
										
										
										
											2010-07-31 04:48:49 -04:00
										 |  |  | FROM: math.bitwise => set-bit ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | IN: mongodb.driver | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-22 10:09:03 -04:00
										 |  |  | TUPLE: mdb-pool < pool mdb ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | TUPLE: mdb-cursor id query ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: mdb-collection | 
					
						
							|  |  |  | { name string } | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  | { capped boolean } | 
					
						
							|  |  |  | { size integer } | 
					
						
							|  |  |  | { max integer } ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | CONSTRUCTOR: mdb-collection ( name -- collection ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: index-spec | 
					
						
							|  |  |  | { ns string } { name string } { key hashtable } { unique? boolean initial: f } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CONSTRUCTOR: index-spec ( ns name key -- index-spec ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-14 09:14:43 -04:00
										 |  |  | M: mdb-pool make-connection | 
					
						
							|  |  |  |     mdb>> mdb-open ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : <mdb-pool> ( mdb -- pool ) [ mdb-pool <pool> ] dip >>mdb ; inline
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | CONSTANT: PARTIAL? "partial?" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | ERROR: mdb-error msg ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-20 13:14:40 -04:00
										 |  |  | M: mdb-error pprint* ( obj -- )
 | 
					
						
							|  |  |  |     msg>> text ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : >pwd-digest ( user password -- digest )
 | 
					
						
							|  |  |  |     "mongo" swap 3array ":" join md5-checksum ;  | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | GENERIC: <mdb-cursor> ( id mdb-query-msg/mdb-getmore-msg -- mdb-cursor )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | M: mdb-query-msg <mdb-cursor> | 
					
						
							|  |  |  |     mdb-cursor boa ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | M: mdb-getmore-msg <mdb-cursor> | 
					
						
							|  |  |  |     query>> mdb-cursor boa ;
 | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-17 12:50:46 -04:00
										 |  |  | : >mdbregexp ( value -- regexp )
 | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  |    first <mdbregexp> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | GENERIC: update-query ( mdb-result-msg mdb-query-msg/mdb-getmore-msg -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | M: mdb-query-msg update-query  | 
					
						
							|  |  |  |     swap [ start#>> ] [ returned#>> ] bi + >>skip# drop ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | M: mdb-getmore-msg update-query | 
					
						
							|  |  |  |     query>> update-query ;  | 
					
						
							|  |  |  |        | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : make-cursor ( mdb-result-msg mdb-query-msg/mdb-getmore-msg -- mdb-cursor/f )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     over cursor>> 0 >
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  |     [ [ update-query ] | 
					
						
							|  |  |  |       [ [ cursor>> ] dip <mdb-cursor> ] 2bi
 | 
					
						
							|  |  |  |     ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEFER: send-query | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: verify-query-result ( mdb-result-msg mdb-query-msg/mdb-getmore-msg -- mdb-result-msg mdb-query-msg/mdb-getmore-msg )  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | M: mdb-query-msg verify-query-result ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  | M: mdb-getmore-msg verify-query-result | 
					
						
							|  |  |  |     over flags>> ResultFlag_CursorNotFound =
 | 
					
						
							|  |  |  |     [ nip query>> [ send-query-plain ] keep ] when ;
 | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : send-query ( mdb-query-msg/mdb-getmore-msg -- mdb-cursor/f seq )
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     [ send-query-plain ] keep
 | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  |     verify-query-result  | 
					
						
							| 
									
										
										
										
											2009-04-14 09:14:43 -04:00
										 |  |  |     [ collection>> >>collection drop ] | 
					
						
							| 
									
										
										
										
											2009-04-28 16:03:39 -04:00
										 |  |  |     [ return#>> >>requested# ]  | 
					
						
							|  |  |  |     [ make-cursor ] 2tri
 | 
					
						
							|  |  |  |     swap objects>> ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-05 07:28:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-18 23:03:05 -04:00
										 |  |  | SYNTAX: r/ | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  |     \ / [ >mdbregexp ] parse-literal ;  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  | : with-db ( mdb quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  |     '[ _ mdb-open &dispose _ with-connection ] with-destructors ; inline
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : with-mdb ( mdb quot -- )
 | 
					
						
							|  |  |  |     [ <mdb-pool> ] dip
 | 
					
						
							|  |  |  |     [ mdb-pool swap with-variable ] curry with-disposal ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-10 13:42:43 -04:00
										 |  |  | : with-mdb-pool ( ..a mdb-pool quot -- ..b )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     '[ _ with-connection ] with-pooled-connection ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-10 13:42:43 -04:00
										 |  |  | : with-mdb-connection ( quot -- )
 | 
					
						
							|  |  |  |     [ mdb-pool get ] dip with-mdb-pool ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : >id-selector ( assoc -- selector )
 | 
					
						
							|  |  |  |     [ MDB_OID_FIELD swap at ] keep
 | 
					
						
							|  |  |  |     H{ } clone [ set-at ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | : <mdb> ( db host port -- mdb )
 | 
					
						
							| 
									
										
										
										
											2009-04-22 10:09:03 -04:00
										 |  |  |    <inet> t [ <mdb-node> ] keep
 | 
					
						
							|  |  |  |    H{ } clone [ set-at ] keep <mdb-db> | 
					
						
							|  |  |  |    [ verify-nodes ] keep ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-05 07:28:41 -04:00
										 |  |  | GENERIC: create-collection ( name/collection -- )
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-03 16:44:24 -05:00
										 |  |  | M: string create-collection | 
					
						
							|  |  |  |     <mdb-collection> create-collection ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  | M: mdb-collection create-collection ( collection -- )
 | 
					
						
							|  |  |  |     create-cmd make-cmd over
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ name>> "create" set-cmd-opt ] | 
					
						
							|  |  |  |         [ capped>> [ "capped" set-cmd-opt ] when* ] | 
					
						
							|  |  |  |         [ max>> [ "max" set-cmd-opt ] when* ] | 
					
						
							|  |  |  |         [ size>> [ "size" set-cmd-opt ] when* ] | 
					
						
							|  |  |  |     } cleave send-cmd check-ok | 
					
						
							|  |  |  |     [ drop [ ] [ name>> ] bi mdb-instance collections>> set-at ] | 
					
						
							|  |  |  |     [ throw ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-07-05 07:28:41 -04:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | : load-collection-list ( -- collection-list )
 | 
					
						
							|  |  |  |     namespaces-collection | 
					
						
							|  |  |  |     H{ } clone <mdb-query-msg> send-query-plain objects>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ensure-valid-collection-name ( collection -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ ";$." intersect length 0 > ] keep
 | 
					
						
							|  |  |  |         '[ _ "contains invalid characters ( . $ ; )" ":" glue throw ] when
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ ascii? ] all? [ "collection names must only contain ascii characters" throw ] unless
 | 
					
						
							|  |  |  |     ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2009-07-05 07:28:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : build-collection-map ( -- assoc )
 | 
					
						
							|  |  |  |     H{ } clone load-collection-list       | 
					
						
							|  |  |  |     [ [ "name" ] dip at "." split second <mdb-collection> ] map
 | 
					
						
							|  |  |  |     over '[ [ ] [ name>> ] bi _ set-at ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ensure-collection-map ( mdb-instance -- assoc )
 | 
					
						
							|  |  |  |     dup collections>> dup keys length 0 =  | 
					
						
							|  |  |  |     [ drop build-collection-map [ >>collections drop ] keep ] | 
					
						
							|  |  |  |     [ nip ] if ;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (ensure-collection) ( collection mdb-instance -- collection )
 | 
					
						
							|  |  |  |     ensure-collection-map [ dup ] dip key?
 | 
					
						
							|  |  |  |     [ ] [ [ ensure-valid-collection-name ] | 
					
						
							|  |  |  |           [ create-collection ] | 
					
						
							|  |  |  |           [ ] tri ] if ;  | 
					
						
							|  |  |  |        | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : reserved-namespace? ( name -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     [ "$cmd" = ] [ "system" head? ] bi or ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : check-collection ( collection -- fq-collection )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 14:56:15 -04:00
										 |  |  |     [let | 
					
						
							|  |  |  |         mdb-instance :> instance | 
					
						
							|  |  |  |         instance name>> :> instance-name | 
					
						
							|  |  |  |         dup mdb-collection? [ name>> ] when
 | 
					
						
							|  |  |  |         "." split1 over instance-name =
 | 
					
						
							|  |  |  |         [ nip ] [ drop ] if
 | 
					
						
							|  |  |  |         [ ] [ reserved-namespace? ] bi
 | 
					
						
							|  |  |  |         [ instance (ensure-collection) ] unless
 | 
					
						
							|  |  |  |         [ instance-name ] dip "." glue
 | 
					
						
							|  |  |  |     ] ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-24 03:32:00 -04:00
										 |  |  | : fix-query-collection ( mdb-query -- mdb-query )
 | 
					
						
							|  |  |  |     [ check-collection ] change-collection ; inline
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : get-more ( mdb-cursor -- mdb-cursor seq )
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  |     [ [ query>> dup [ collection>> ] [ return#>> ] bi ] | 
					
						
							|  |  |  |       [ id>> ] bi <mdb-getmore-msg> swap >>query send-query ]  | 
					
						
							|  |  |  |     [ f f ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-17 02:04:49 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : <query> ( collection assoc -- mdb-query-msg )
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     <mdb-query-msg> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 05:41:07 -04:00
										 |  |  | : >slave-ok ( mdb-query-msg -- mdb-query-msg )
 | 
					
						
							|  |  |  |     [ 2 set-bit ] change-flags ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >await-data ( mdb-query-msg -- mdb-query-msg )
 | 
					
						
							|  |  |  |     [ 5 set-bit ] change-flags ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >tailable ( mdb-query-msg -- mdb-query-msg )
 | 
					
						
							|  |  |  |     [ 1 set-bit ] change-flags ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : limit ( mdb-query-msg limit# -- mdb-query-msg )
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     >>return# ; inline
 | 
					
						
							| 
									
										
										
										
											2009-03-09 17:58:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : skip ( mdb-query-msg skip# -- mdb-query-msg )
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     >>skip# ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : asc ( key -- spec ) 1 2array ; inline
 | 
					
						
							|  |  |  | : desc ( key -- spec ) -1 2array ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 08:42:29 -04:00
										 |  |  | : sort ( mdb-query-msg sort-quot -- mdb-query-msg )
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  |     output>array >hashtable >>orderby ; inline
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 07:29:24 -05:00
										 |  |  | : filter-fields ( mdb-query-msg filterseq -- mdb-query-msg )
 | 
					
						
							|  |  |  |     [ asc ] map >hashtable >>returnfields ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | : key-spec ( spec-quot -- spec-assoc )
 | 
					
						
							|  |  |  |     output>array >hashtable ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC# hint 1 ( mdb-query-msg index-hint -- mdb-query-msg )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: mdb-query-msg hint  | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     >>hint ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 12:31:19 -04:00
										 |  |  | GENERIC: find ( selector -- mdb-cursor/f seq )
 | 
					
						
							| 
									
										
										
										
											2009-03-11 09:40:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-03 16:44:24 -05:00
										 |  |  | M: mdb-query-msg find | 
					
						
							| 
									
										
										
										
											2009-04-24 03:32:00 -04:00
										 |  |  |     fix-query-collection send-query ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-11 09:40:07 -04:00
										 |  |  | M: mdb-cursor find | 
					
						
							|  |  |  |     get-more ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-09 09:22:51 -04:00
										 |  |  | : each-chunk ( selector quot: ( seq -- ) -- )
 | 
					
						
							|  |  |  |     swap find
 | 
					
						
							|  |  |  |     [ pick call( seq -- ) ] when*
 | 
					
						
							|  |  |  |     [ swap each-chunk ] [ drop ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : find-all ( selector -- seq )
 | 
					
						
							|  |  |  |     [ V{ } clone ] dip
 | 
					
						
							|  |  |  |     over '[ _ push-all ] each-chunk >array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : explain. ( mdb-query-msg -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  |     t >>explain find nip . ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : find-one ( mdb-query-msg -- result/f )
 | 
					
						
							| 
									
										
										
										
											2011-10-13 15:53:46 -04:00
										 |  |  |     fix-query-collection 1 >>return# | 
					
						
							|  |  |  |     send-query-plain objects>> ?first ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : count ( mdb-query-msg -- result )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     [ count-cmd make-cmd ] dip
 | 
					
						
							|  |  |  |     [ collection>> "count" set-cmd-opt ] | 
					
						
							|  |  |  |     [ query>> "query" set-cmd-opt ] bi send-cmd  | 
					
						
							| 
									
										
										
										
											2009-04-24 02:24:12 -04:00
										 |  |  |     [ check-ok nip ] keep '[ "n" _ at >fixnum ] [ f ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-04-17 02:04:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | : lasterror ( -- error )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     getlasterror-cmd make-cmd send-cmd | 
					
						
							|  |  |  |     [ "err" ] dip at ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  | GENERIC: validate. ( collection -- )
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  | M: string validate. | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     [ validate-cmd make-cmd ] dip
 | 
					
						
							|  |  |  |     "validate" set-cmd-opt send-cmd | 
					
						
							|  |  |  |     [ check-ok nip ] keep
 | 
					
						
							| 
									
										
										
										
											2009-04-24 02:24:12 -04:00
										 |  |  |     '[ "result" _ at print ] [  ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  | M: mdb-collection validate. | 
					
						
							|  |  |  |     name>> validate. ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : send-message-check-error ( message -- )
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  |     send-message lasterror [ mdb-error ] when* ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : save ( collection assoc -- )
 | 
					
						
							| 
									
										
										
										
											2009-04-17 02:04:49 -04:00
										 |  |  |     [ check-collection ] dip
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     <mdb-insert-msg> send-message-check-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : save-unsafe ( collection assoc -- )
 | 
					
						
							| 
									
										
										
										
											2009-04-17 02:04:49 -04:00
										 |  |  |     [ check-collection ] dip
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  |     <mdb-insert-msg> send-message ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : ensure-index ( index-spec -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     <linked-hash> [ [ <oid> "_id" ] dip set-at ] keep
 | 
					
						
							| 
									
										
										
										
											2009-05-01 08:04:25 -04:00
										 |  |  |     [ { [ [ name>> "name" ] dip set-at ] | 
					
						
							|  |  |  |         [ [ ns>> index-ns "ns" ] dip set-at ] | 
					
						
							|  |  |  |         [ [ key>> "key" ] dip set-at ] | 
					
						
							|  |  |  |         [ swap unique?>> | 
					
						
							|  |  |  |           [ swap [ "unique" ] dip set-at ] [ drop ] if* ] } 2cleave
 | 
					
						
							|  |  |  |     ] keep
 | 
					
						
							|  |  |  |     [ index-collection ] dip save ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : drop-index ( collection name -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     [ delete-index-cmd make-cmd ] 2dip
 | 
					
						
							|  |  |  |     [ "deleteIndexes" set-cmd-opt ] | 
					
						
							|  |  |  |     [ "index" set-cmd-opt ] bi* send-cmd drop ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-01 12:31:19 -04:00
										 |  |  | : <update> ( collection selector object -- mdb-update-msg )
 | 
					
						
							| 
									
										
										
										
											2009-04-17 02:04:49 -04:00
										 |  |  |     [ check-collection ] 2dip <mdb-update-msg> ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-11 09:40:07 -04:00
										 |  |  | : >upsert ( mdb-update-msg -- mdb-update-msg )
 | 
					
						
							| 
									
										
										
										
											2010-07-31 04:48:49 -04:00
										 |  |  |     [ 0 set-bit ] change-update-flags ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >multi ( mdb-update-msg -- mdb-update-msg )
 | 
					
						
							|  |  |  |     [ 1 set-bit ] change-update-flags ;
 | 
					
						
							| 
									
										
										
										
											2009-03-11 09:40:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : update ( mdb-update-msg -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-11 09:40:07 -04:00
										 |  |  |     send-message-check-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : update-unsafe ( mdb-update-msg -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-11 09:40:07 -04:00
										 |  |  |     send-message ;
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : find-and-modify ( collection selector modifier -- mongodb-cmd )
 | 
					
						
							|  |  |  |     [ findandmodify-cmd make-cmd ] 3dip
 | 
					
						
							|  |  |  |     [ "findandmodify" set-cmd-opt ] | 
					
						
							|  |  |  |     [ "query" set-cmd-opt ] | 
					
						
							|  |  |  |     [ "update" set-cmd-opt ] tri* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : run-cmd ( cmd -- result )
 | 
					
						
							|  |  |  |     send-cmd ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 05:41:07 -04:00
										 |  |  | : <delete> ( collection selector -- mdb-delete-msg )
 | 
					
						
							|  |  |  |     [ check-collection ] dip <mdb-delete-msg> ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 05:41:07 -04:00
										 |  |  | : >single-remove ( mdb-delete-msg -- mdb-delete-msg )
 | 
					
						
							|  |  |  |     [ 0 set-bit ] change-delete-flags ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete ( mdb-delete-msg -- )
 | 
					
						
							|  |  |  |     send-message-check-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-unsafe ( mdb-delete-msg -- )
 | 
					
						
							|  |  |  |     send-message ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 09:56:38 -05:00
										 |  |  | : kill-cursor ( mdb-cursor -- )
 | 
					
						
							|  |  |  |     id>> <mdb-killcursors-msg> send-message ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | : load-index-list ( -- index-list )
 | 
					
						
							|  |  |  |     index-collection | 
					
						
							| 
									
										
										
										
											2009-03-25 13:22:28 -04:00
										 |  |  |     H{ } clone <mdb-query-msg> find nip ;
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-17 02:04:49 -04:00
										 |  |  | : ensure-collection ( name -- )
 | 
					
						
							|  |  |  |     check-collection drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-01 16:45:38 -05:00
										 |  |  | : drop-collection ( name -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-12 14:40:57 -05:00
										 |  |  |     [ drop-cmd make-cmd ] dip
 | 
					
						
							|  |  |  |     "drop" set-cmd-opt send-cmd drop ;
 | 
					
						
							| 
									
										
										
										
											2009-04-22 10:09:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |