1 ! Copyright (C) 2010 Sascha Matzke.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs bson.constants calendar combinators
4 endian io io.encodings io.encodings.binary io.encodings.utf8
5 io.files io.streams.byte-array kernel math namespaces sequences
6 sequences.extras serialize strings typed ;
13 ERROR: unknown-bson-type type msg ;
19 : read-int32 ( -- int32 )
20 4 read signed-le> ; inline
22 : read-longlong ( -- longlong )
23 8 read signed-le> ; inline
25 : read-double ( -- double )
26 8 read le> bits>double ; inline
28 : read-byte-raw ( -- byte-raw )
31 : read-byte ( -- byte )
32 read-byte-raw first ; inline
34 : read-cstring ( -- string )
35 input-stream get utf8 <decoder>
36 "\0" swap stream-read-until drop ; inline
38 : read-sized-string ( length -- string )
39 read binary [ read-cstring ] with-byte-reader ; inline
41 : read-timestamp ( -- timestamp )
42 8 read [ 4 head signed-le> ] [ 4 tail signed-le> ] bi <mongo-timestamp> ;
44 : object-result ( quot -- object )
47 [ clear-assoc ] [ ] [ ] tri state
48 ] dip with-variable ; inline
50 : bson-object-data-read ( -- ? )
51 read-int32 [ f ] [ drop read-elements t ] if-zero ; inline recursive
53 : bson-binary-read ( -- binary )
56 { T_Binary_Default [ read ] }
57 { T_Binary_Bytes_Deprecated [ drop read-int32 read ] }
58 { T_Binary_Custom [ read bytes>object ] }
59 { T_Binary_Function [ read-sized-string ] }
60 { T_Binary_MD5 [ read >string ] }
61 { T_Binary_UUID [ read >string ] }
62 [ "unknown binary sub-type" unknown-bson-type ]
65 TYPED: bson-regexp-read ( -- mdbregexp: mdbregexp )
67 read-cstring >>regexp read-cstring >>options ; inline
69 TYPED: bson-oid-read ( -- oid: oid )
70 read-longlong read-int32 oid boa ; inline
72 : check-object ( assoc -- object )
73 dup dbref-assoc? [ assoc>dbref ] when ; inline
75 TYPED: element-data-read ( type: integer -- object )
77 { T_OID [ bson-oid-read ] }
78 { T_String [ read-int32 read-sized-string ] }
79 { T_Integer [ read-int32 ] }
80 { T_Integer64 [ read-longlong ] }
81 { T_Binary [ bson-binary-read ] }
82 { T_Object [ [ bson-object-data-read drop ] object-result check-object ] }
83 { T_Array [ [ bson-object-data-read drop ] object-result values ] }
84 { T_Double [ read-double ] }
85 { T_Boolean [ read-byte 1 = ] }
86 { T_Date [ read-longlong millis>timestamp ] }
87 { T_Regexp [ bson-regexp-read ] }
88 { T_Timestamp [ read-timestamp ] }
89 { T_Code [ read-int32 read-sized-string ] }
90 { T_ScopedCode [ read-int32 drop read-cstring H{ } clone stream>assoc <mongo-scoped-code> ] }
92 [ "type unknown" unknown-bson-type ]
93 } case ; inline recursive
95 TYPED: (read-object) ( type: integer name: string -- )
96 [ element-data-read ] dip state get set-at ; inline recursive
98 TYPED: (element-read) ( type: integer -- cont?: boolean )
100 [ read-cstring (read-object) t ]
101 [ drop f ] if ; inline recursive
103 : read-elements ( -- )
104 read-byte (element-read)
105 [ read-elements ] when ; inline recursive
109 : stream>assoc ( exemplar -- assoc/f )
111 state [ bson-object-data-read ] with-variable
112 ] keep swap [ drop f ] unless ;
114 : path>bson-sequence ( path -- assoc )
116 [ H{ } stream>assoc ] loop>array