From 5afca2989c6a0d2a3bc5a3bb15ee16815a7b166e Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Fri, 20 Jul 2012 09:37:54 -0700 Subject: [PATCH] mongodb/couchdb: use associate where possible. --- extra/couchdb/couchdb.factor | 2 +- extra/mongodb/benchmark/benchmark.factor | 21 ++++++++++--------- .../tuple/collection/collection.factor | 8 +++---- extra/mongodb/tuple/tuple.factor | 6 ++++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/extra/couchdb/couchdb.factor b/extra/couchdb/couchdb.factor index ed5dd1268f..9e1e49babd 100644 --- a/extra/couchdb/couchdb.factor +++ b/extra/couchdb/couchdb.factor @@ -156,7 +156,7 @@ C: db couch get db-url "_temp_view" append couch-post ; : temp-view-map ( map -- results ) - "map" H{ } clone [ set-at ] keep temp-view ; + "map" associate temp-view ; : save-doc-as ( assoc id -- ) [ dup ] dip id-url couch-put response-ok diff --git a/extra/mongodb/benchmark/benchmark.factor b/extra/mongodb/benchmark/benchmark.factor index 9826923df0..dac4fcbeb8 100644 --- a/extra/mongodb/benchmark/benchmark.factor +++ b/extra/mongodb/benchmark/benchmark.factor @@ -1,6 +1,7 @@ USING: calendar math fry kernel assocs math.ranges bson.reader io.streams.byte-array sequences formatting combinators namespaces io tools.time prettyprint io.encodings.binary -accessors words mongodb.driver strings math.parser bson.writer ; +accessors words mongodb.driver strings math.parser bson.writer +hashtables ; FROM: mongodb.driver => find ; FROM: memory => gc ; @@ -183,7 +184,7 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" } : (check-find-result) ( result -- ) "x" check-for-key ; inline - + : (find) ( cursor -- ) [ find [ (check-find-result) ] each (find) ] when* ; inline recursive @@ -191,15 +192,15 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" } drop [ trial-size collection-name - trial-size 2 / "x" H{ } clone [ set-at ] keep + trial-size 2 / "x" associate '[ _ _ 1 limit (find) ] times ] ; - + : find-all ( quot -- quot: ( -- ) ) drop collection-name H{ } clone '[ _ _ (find) ] ; - + : find-range ( quot -- quot: ( -- ) ) drop [ trial-size batch-size /i @@ -211,7 +212,7 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" } : batch ( -- ) result [ t >>batch ] change ; inline - + : index ( -- ) result [ t >>index ] change ; inline @@ -260,7 +261,7 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" } "Deserialization Tests" print print-separator-bold \ deserialize [bench-quot] '[ _ call( doc-word -- ) ] each ; - + : run-insert-bench ( doc-word-seq feat-seq -- ) "Insert Tests" print print-separator-bold @@ -281,7 +282,7 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" } print-separator-bold \ find-range [bench-quot] '[ _ call( doc-word -- ) ] each ; - + : run-benchmarks ( -- ) "db" "db" get* "host" "127.0.0.1" get* "port" 27017 get* ensure-number [ print-header @@ -307,8 +308,8 @@ CONSTANT: DOC-LARGE H{ { "base_url" "http://www.example.com/test-me" } { { } { index } } run-find-all-bench ! find-range { small-doc medium-doc large-doc } - { { } { index } } run-find-range-bench + { { } { index } } run-find-range-bench ] with-db ; - + MAIN: run-benchmarks diff --git a/extra/mongodb/tuple/collection/collection.factor b/extra/mongodb/tuple/collection/collection.factor index 272fc3df1b..c3a8ef7681 100644 --- a/extra/mongodb/tuple/collection/collection.factor +++ b/extra/mongodb/tuple/collection/collection.factor @@ -1,7 +1,7 @@ - USING: accessors arrays assocs bson.constants classes classes.tuple combinators continuations fry kernel mongodb.driver sequences strings -vectors words combinators.smart literals memoize slots constructors ; +vectors words combinators.smart literals memoize slots constructors +hashtables ; IN: mongodb.tuple @@ -88,8 +88,8 @@ GENERIC: mdb-index-map ( tuple -- sequence ) : user-defined-key-index ( class -- assoc ) mdb-slot-map user-defined-key [ drop [ "user-defined-key-index" 1 ] dip - H{ } clone [ set-at ] keep t >>unique? - [ ] [ name>> ] bi H{ } clone [ set-at ] keep + associate t >>unique? + [ ] [ name>> ] bi associate ] [ 2drop H{ } clone ] if ; diff --git a/extra/mongodb/tuple/tuple.factor b/extra/mongodb/tuple/tuple.factor index 1568572bfb..428e5f7bdb 100644 --- a/extra/mongodb/tuple/tuple.factor +++ b/extra/mongodb/tuple/tuple.factor @@ -1,5 +1,7 @@ USING: accessors assocs classes.mixin classes.tuple -classes.tuple.parser compiler.units fry kernel sequences mongodb.driver +classes.tuple.parser compiler.units fry kernel sequences +hashtables +mongodb.driver mongodb.msg mongodb.tuple.collection mongodb.tuple.persistent mongodb.tuple.state strings ; FROM: mongodb.driver => update delete find count ; @@ -45,7 +47,7 @@ DEFER: tuple>query GENERIC: id-selector ( object -- selector ) M: toid id-selector - [ value>> ] [ key>> ] bi H{ } clone [ set-at ] keep ; inline + [ value>> ] [ key>> ] bi associate ; inline M: mdb-persistent id-selector >toid id-selector ;