]> gitweb.factorcode.org Git - factor.git/blob - extra/bson/reader/reader.factor
Switch to https urls
[factor.git] / extra / bson / reader / reader.factor
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 ;
7 IN: bson.reader
8
9 SYMBOL: state
10
11 DEFER: stream>assoc
12
13 ERROR: unknown-bson-type type msg ;
14
15 <PRIVATE
16
17 DEFER: read-elements
18
19 : read-int32 ( -- int32 )
20     4 read signed-le> ; inline
21
22 : read-longlong ( -- longlong )
23     8 read signed-le> ; inline
24
25 : read-double ( -- double )
26     8 read le> bits>double ; inline
27
28 : read-byte-raw ( -- byte-raw )
29     1 read ; inline
30
31 : read-byte ( -- byte )
32     read-byte-raw first ; inline
33
34 : read-cstring ( -- string )
35     input-stream get utf8 <decoder>
36     "\0" swap stream-read-until drop ; inline
37
38 : read-sized-string ( length -- string )
39     read binary [ read-cstring ] with-byte-reader ; inline
40
41 : read-timestamp ( -- timestamp )
42     8 read [ 4 head signed-le> ] [ 4 tail signed-le> ] bi <mongo-timestamp> ;
43
44 : object-result ( quot -- object )
45     [
46         state get clone
47         [ clear-assoc ] [ ] [ ] tri state
48     ] dip with-variable ; inline
49
50 : bson-object-data-read ( -- ? )
51     read-int32 [ f ] [ drop read-elements t ] if-zero ; inline recursive
52
53 : bson-binary-read ( -- binary )
54     read-int32 read-byte
55     {
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 ]
63     } case ; inline
64
65 TYPED: bson-regexp-read ( -- mdbregexp: mdbregexp )
66     mdbregexp new
67     read-cstring >>regexp read-cstring >>options ; inline
68
69 TYPED: bson-oid-read ( -- oid: oid )
70     read-longlong read-int32 oid boa ; inline
71
72 : check-object ( assoc -- object )
73     dup dbref-assoc? [ assoc>dbref ] when ; inline
74
75 TYPED: element-data-read ( type: integer -- object )
76     {
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> ] }
91         { T_NULL        [ f ] }
92         [ "type unknown" unknown-bson-type ]
93     } case ; inline recursive
94
95 TYPED: (read-object) ( type: integer name: string -- )
96     [ element-data-read ] dip state get set-at ; inline recursive
97
98 TYPED: (element-read) ( type: integer -- cont?: boolean )
99     dup T_EOO >
100     [ read-cstring (read-object) t ]
101     [ drop f ] if ; inline recursive
102
103 : read-elements ( -- )
104     read-byte (element-read)
105     [ read-elements ] when ; inline recursive
106
107 PRIVATE>
108
109 : stream>assoc ( exemplar -- assoc/f )
110     clone [
111         state [ bson-object-data-read ] with-variable
112     ] keep swap [ drop f ] unless ;
113
114 : path>bson-sequence ( path -- assoc )
115     binary [
116         [ H{ } stream>assoc ] loop>array
117     ] with-file-reader ;