factor/mongodb/mongodb.factor

43 lines
1.1 KiB
Factor
Raw Normal View History

2009-01-23 00:53:08 -05:00
USING: accessors assocs fry io.encodings.binary io.sockets kernel math
2009-01-27 13:42:29 -05:00
math.parser namespaces sequences splitting
mongodb.connection mongodb.persistent mongodb.msg mongodb.query
mongodb.tuple ;
IN: mongodb
! generic methods
GENERIC: store ( tuple/ht -- )
GENERIC: find ( example -- tuple/ht )
GENERIC: findOne ( exampe -- tuple/ht )
GENERIC: load ( object -- object )
<PRIVATE
2009-01-23 00:53:08 -05:00
: prepare-find ( example -- query )
[ mdb-collection>> get-collection-fqn ] keep
H{ } tuple>query <mdb-query-msg> ; inline
2009-01-23 00:53:08 -05:00
PRIVATE>
: <mdb> ( db host port -- mdb )
(<mdb>) ;
M: mdb-persistent store ( tuple -- )
2009-01-23 00:53:08 -05:00
prepare-store ! H { collection { ... values ... }
2009-01-27 13:42:29 -05:00
[ [ get-collection-fqn ] dip
values <mdb-insert-msg>
[ mdb>> master>> binary ] dip '[ _ write-message ] with-client
2009-01-23 00:53:08 -05:00
] assoc-each ;
M: mdb-persistent find ( example -- result )
2009-01-27 13:42:29 -05:00
prepare-find [ mdb>> master>> ] dip (find)
2009-01-23 00:53:08 -05:00
build-result ;
M: mdb-persistent findOne ( example -- result )
2009-01-27 13:42:29 -05:00
prepare-find [ mdb>> master>> ] dip (find-one)
2009-01-23 00:53:08 -05:00
dup returned#>> 1 =
[ objects>> first ]
[ drop f ] if ;