From 2e641216f3dd088b02dc3464504e16a87cc4b664 Mon Sep 17 00:00:00 2001 From: Sascha Matzke Date: Mon, 9 Feb 2009 08:31:47 +0100 Subject: [PATCH] changed type > byte-array conversion; now using io.binary (>le, le>) updated USING:s --- bson/constants/constants.factor | 3 +-- bson/reader/reader.factor | 20 ++++++++++---------- bson/writer/writer.factor | 28 +++++++++++++--------------- mongodb/mongodb.factor | 8 ++++++-- mongodb/msg/msg.factor | 23 ++++++++++++----------- 5 files changed, 42 insertions(+), 40 deletions(-) diff --git a/bson/constants/constants.factor b/bson/constants/constants.factor index fc54f62927..29144ded86 100644 --- a/bson/constants/constants.factor +++ b/bson/constants/constants.factor @@ -1,5 +1,4 @@ -USING: alien.c-types accessors kernel calendar random math.bitwise math unix -constructors uuid ; +USING: accessors constructors uuid ; IN: bson.constants diff --git a/bson/reader/reader.factor b/bson/reader/reader.factor index f697f16691..0f699ca499 100644 --- a/bson/reader/reader.factor +++ b/bson/reader/reader.factor @@ -1,7 +1,6 @@ -USING: mirrors io io.encodings.utf8 io.encodings.binary math kernel sequences - splitting accessors io.streams.byte-array namespaces prettyprint - bson.constants assocs alien.c-types alien.strings fry words - serialize byte-arrays byte-vectors ; +USING: accessors assocs bson.constants byte-arrays byte-vectors fry io +io.binary io.encodings.string io.encodings.utf8 kernel math namespaces +sequences serialize ; IN: bson.reader @@ -48,19 +47,19 @@ GENERIC: element-binary-read ( length type -- object ) [ get-state ] dip '[ _ + ] change-read drop ; inline : read-int32 ( -- int32 ) - 4 [ read *int ] [ count-bytes ] bi ; inline + 4 [ read le> ] [ count-bytes ] bi ; inline : read-longlong ( -- longlong ) - 8 [ read *longlong ] [ count-bytes ] bi ; inline + 8 [ read le> ] [ count-bytes ] bi ; inline : read-double ( -- double ) - 8 [ read *double ] [ count-bytes ] bi ; inline + 8 [ read le> bits>double ] [ count-bytes ] bi ; inline : read-byte-raw ( -- byte-raw ) 1 [ read ] [ count-bytes ] bi ; inline : read-byte ( -- byte ) - read-byte-raw *char ; inline + read-byte-raw first ; inline : (read-cstring) ( acc -- ) [ read-byte-raw first ] dip ! b acc @@ -71,11 +70,12 @@ GENERIC: element-binary-read ( length type -- object ) : read-cstring ( -- string ) BV{ } clone [ (read-cstring) ] keep - >byte-array utf8 alien>string ; inline + [ zero? ] trim-tail + >byte-array utf8 decode ; inline : read-sized-string ( length -- string ) [ read ] [ count-bytes ] bi - utf8 alien>string ; inline + [ zero? ] trim-tail utf8 decode ; inline : read-element-type ( -- type ) read-byte ; inline diff --git a/bson/writer/writer.factor b/bson/writer/writer.factor index 4e07e3ab2f..a850c86e32 100644 --- a/bson/writer/writer.factor +++ b/bson/writer/writer.factor @@ -1,11 +1,9 @@ ! Copyright (C) 2008 Sascha Matzke. ! See http://factorcode.org/license.txt for BSD license. -USING: bson bson.constants accessors kernel io.streams.string - io.encodings.binary classes byte-arrays quotations serialize - io.encodings.utf8 strings splitting math.parser - sequences math assocs classes words make fry - prettyprint hashtables mirrors alien.strings alien.c-types - io.streams.byte-array io alien.strings ; +USING: accessors assocs bson.constants byte-arrays fry io io.binary +io.encodings.binary io.encodings.string io.encodings.utf8 +io.streams.byte-array kernel math math.parser quotations sequences +serialize strings words ; IN: bson.writer @@ -32,11 +30,11 @@ M: objref bson-type? ( objref -- type ) drop T_Binary ; M: quotation bson-type? ( quotation -- type ) drop T_Binary ; M: byte-array bson-type? ( byte-array -- type ) drop T_Binary ; -: write-byte ( byte -- ) write ; inline -: write-int32 ( int -- ) write ; inline -: write-double ( real -- ) write ; inline -: write-cstring ( string -- ) utf8 string>alien write ; inline -: write-longlong ( object -- ) write ; inline +: write-byte ( byte -- ) 1 >le write ; inline +: write-int32 ( int -- ) 4 >le write ; inline +: write-double ( real -- ) double>bits 8 >le write ; inline +: write-cstring ( string -- ) utf8 encode B{ 0 } append write ; inline +: write-longlong ( object -- ) 8 >le write ; inline : write-eoo ( -- ) T_EOO write-byte ; inline : write-type ( obj -- obj ) [ bson-type? write-byte ] keep ; inline @@ -50,7 +48,7 @@ M: t bson-write ( t -- ) drop 1 write-byte ; M: string bson-write ( obj -- ) - utf8 string>alien + utf8 encode B{ 0 } append [ length write-int32 ] keep write ; @@ -71,14 +69,14 @@ M: quotation bson-write ( quotation -- ) write ; M: objid bson-write ( oid -- ) - id>> utf8 string>alien + id>> utf8 encode [ length write-int32 ] keep T_Binary_UUID write-byte write ; M: objref bson-write ( objref -- ) - [ ns>> utf8 string>alien ] - [ objid>> id>> utf8 string>alien ] bi + [ ns>> utf8 encode ] + [ objid>> id>> utf8 encode ] bi append [ length write-int32 ] keep T_Binary_Custom write-byte diff --git a/mongodb/mongodb.factor b/mongodb/mongodb.factor index 1d5d7f3693..69c2809a1e 100644 --- a/mongodb/mongodb.factor +++ b/mongodb/mongodb.factor @@ -9,7 +9,8 @@ IN: mongodb GENERIC: store ( tuple/ht -- ) GENERIC: find ( example -- tuple/ht ) GENERIC# nfind 1 ( example n -- tuple/ht ) -GENERIC: load ( object -- object ) +GENERIC: load ( object -- object ) +GENERIC: explain ( object -- object ) > master>> ] dip (find) build-result ; - +M: mdb-persistent explain ( example -- result ) + prepare-find [ query>> [ t "$explain" ] dip set-at ] keep + [ mdb>> master>> ] dip (find-one) + build-result ; diff --git a/mongodb/msg/msg.factor b/mongodb/msg/msg.factor index 3b0db0a08f..1df971b229 100644 --- a/mongodb/msg/msg.factor +++ b/mongodb/msg/msg.factor @@ -1,5 +1,5 @@ -USING: accessors alien.c-types alien.strings assocs bson.reader -bson.writer byte-arrays byte-vectors constructors fry io +USING: accessors io.encodings.string assocs bson.reader +bson.writer byte-arrays byte-vectors constructors fry io io.binary io.encodings.binary io.encodings.utf8 io.streams.byte-array kernel linked-assocs math namespaces sequences strings ; @@ -126,15 +126,15 @@ SYMBOL: msg-bytes-read : change-bytes-read ( integer -- ) bytes-read> [ 0 ] unless* + >bytes-read ; inline -: write-byte ( byte -- ) write ; inline -: write-int32 ( int -- ) write ; inline -: write-double ( real -- ) write ; inline -: write-cstring ( string -- ) utf8 string>alien write ; inline -: write-longlong ( object -- ) write ; inline +: write-byte ( byte -- ) 1 >le write ; inline +: write-int32 ( int -- ) 4 >le write ; inline +: write-double ( real -- ) double>bits 8 >le write ; inline +: write-cstring ( string -- ) utf8 encode B{ 0 } append write ; inline +: write-longlong ( object -- ) 8 >le write ; inline -: read-int32 ( -- int32 ) 4 [ read *int ] [ change-bytes-read ] bi ; inline -: read-longlong ( -- longlong ) 8 [ read *longlong ] [ change-bytes-read ] bi ; inline -: read-byte-raw ( -- byte-raw ) 1 [ read ] [ change-bytes-read ] bi ; inline +: read-int32 ( -- int32 ) 4 [ read le> ] [ change-bytes-read ] bi ; inline +: read-longlong ( -- longlong ) 8 [ read le> ] [ change-bytes-read ] bi ; inline +: read-byte-raw ( -- byte-raw ) 1 [ read le> ] [ change-bytes-read ] bi ; inline : read-byte ( -- byte ) read-byte-raw first ; inline : (read-cstring) ( acc -- ) @@ -146,7 +146,8 @@ SYMBOL: msg-bytes-read : read-cstring ( -- string ) BV{ } clone [ (read-cstring) ] keep - >byte-array utf8 alien>string ; inline + [ zero? ] trim-tail + >byte-array utf8 decode ; inline GENERIC: (read-message) ( message opcode -- message )