2009-01-27 03:21:04 -05:00
|
|
|
USING: accessors combinators fry io.encodings.binary io.sockets kernel
|
2009-01-27 13:42:29 -05:00
|
|
|
mongodb.msg mongodb.persistent mongodb.connection sequences math namespaces assocs
|
|
|
|
formatting splitting mongodb.tuple mongodb.index ;
|
2009-01-27 03:21:04 -05:00
|
|
|
|
|
|
|
IN: mongodb.query
|
|
|
|
|
|
|
|
TUPLE: mdb-result { cursor integer }
|
|
|
|
{ start# integer }
|
|
|
|
{ returned# integer }
|
|
|
|
{ objects sequence } ;
|
|
|
|
|
2009-01-27 13:42:29 -05:00
|
|
|
: namespaces-ns ( name -- ns )
|
|
|
|
"%s.system.namespaces" sprintf ; inline
|
2009-01-27 03:21:04 -05:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: (execute-query) ( inet quot -- result )
|
2009-01-27 13:42:29 -05:00
|
|
|
[ binary ] dip with-client ; inline
|
2009-01-27 03:21:04 -05:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-01-27 13:42:29 -05:00
|
|
|
: (find) ( inet query -- result )
|
2009-01-28 15:11:45 -05:00
|
|
|
'[ _ write-message read-message ] (execute-query) ; inline
|
2009-01-27 03:21:04 -05:00
|
|
|
|
2009-01-27 13:42:29 -05:00
|
|
|
: (find-one) ( inet query -- result )
|
2009-01-29 07:50:42 -05:00
|
|
|
1 >>return#
|
|
|
|
(find) ; inline
|
2009-01-27 03:21:04 -05:00
|
|
|
|
|
|
|
: build-result ( resultmsg -- mdb-result )
|
|
|
|
[ mdb-result new ] dip
|
|
|
|
{
|
|
|
|
[ cursor>> >>cursor ]
|
|
|
|
[ start#>> >>start# ]
|
|
|
|
[ returned#>> >>returned# ]
|
|
|
|
[ objects>> [ assoc>tuple ] map >>objects ]
|
|
|
|
} cleave ;
|
|
|
|
|
2009-01-27 13:42:29 -05:00
|
|
|
: load-collections ( -- collections )
|
|
|
|
mdb>> [ master>> ] [ name>> namespaces-ns ] bi
|
|
|
|
H{ } clone <mdb-query-msg> (find)
|
|
|
|
objects>> [ [ "name" ] dip at "." split second <mdb-collection> ] map
|
|
|
|
H{ } clone tuck
|
|
|
|
'[ [ ensure-indices ] [ ] [ name>> ] tri _ set-at ] each
|
|
|
|
[ mdb>> ] dip >>collections collections>> ;
|
|
|
|
|
|
|
|
: check-ok ( result -- ? )
|
|
|
|
[ "ok" ] dip key? ; inline
|
|
|
|
|
|
|
|
: create-collection ( mdb-collection -- )
|
|
|
|
dup name>> "create" H{ } clone [ set-at ] keep
|
|
|
|
[ mdb>> [ master>> ] [ name>> ] bi "%s.$cmd" sprintf ] dip
|
2009-01-29 07:50:42 -05:00
|
|
|
<mdb-query-msg> (find-one) objects>> first
|
2009-01-27 13:42:29 -05:00
|
|
|
check-ok
|
|
|
|
[ [ ensure-indices ] keep dup name>> mdb>> collections>> set-at ]
|
|
|
|
[ "could not create collection" throw ] if ;
|
|
|
|
|
|
|
|
: get-collection-fqn ( mdb-collection -- fqdn )
|
|
|
|
mdb>> collections>>
|
|
|
|
dup keys length 0 =
|
|
|
|
[ drop load-collections ]
|
|
|
|
[ ] if
|
|
|
|
[ dup name>> ] dip
|
|
|
|
key?
|
|
|
|
[ ]
|
|
|
|
[ dup create-collection ] if
|
|
|
|
name>> [ mdb>> name>> ] dip "%s.%s" sprintf ;
|
|
|
|
|
|
|
|
|