factor/mongodb/mongodb.factor

47 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
math.parser namespaces sequences splitting ;
IN: mongodb
! generic methods
GENERIC: store ( tuple/ht -- )
GENERIC: find ( example -- tuple/ht )
GENERIC: findOne ( exampe -- tuple/ht )
GENERIC: load ( object -- object )
USING: mongodb.msg mongodb.persistent mongodb.query mongodb.tuple
mongodb.collection mongodb.connection ;
<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 ... }
[ [ <mdb-insert-msg> ] 2dip
[ get-collection-fqn >>collection ] dip
objects>>
[ mdb>> master>> binary ] dip '[ _ write-request ] with-client
] assoc-each ;
M: mdb-persistent find ( example -- result )
2009-01-23 00:53:08 -05:00
prepare-find (find)
build-result ;
M: mdb-persistent findOne ( example -- result )
2009-01-23 00:53:08 -05:00
prepare-find (find-one)
dup returned#>> 1 =
[ objects>> first ]
[ drop f ] if ;