| 1 | package Mango::BSON; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use Mojo::Base -strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use re 'regexp_pattern'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Mango::BSON::Binary; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Mango::BSON::Code; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Mango::BSON::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Mango::BSON::Number; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Mango::BSON::ObjectID; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Mango::BSON::Time; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Mango::BSON::Timestamp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use Mojo::JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | my @BSON = ( | |||||||||||||||||||||||||||
| 18 | qw(bson_bin bson_code bson_dbref bson_decode bson_doc bson_double), | ||||||||||||||||||||||||||||
| 19 | qw(bson_encode bson_false bson_int32 bson_int64 bson_length bson_max), | ||||||||||||||||||||||||||||
| 20 | qw(bson_min bson_oid bson_raw bson_time bson_true bson_ts) | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | 11 | our @EXPORT_OK = (@BSON, 'encode_cstring'); | |||||||||||||||||||||||||||
| 23 | 11 | our %EXPORT_TAGS = (bson => \@BSON); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Types | ||||||||||||||||||||||||||||
| 26 | 11 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 11 | DOUBLE => "\x01", | |||||||||||||||||||||||||||
| 28 | STRING => "\x02", | ||||||||||||||||||||||||||||
| 29 | DOCUMENT => "\x03", | ||||||||||||||||||||||||||||
| 30 | ARRAY => "\x04", | ||||||||||||||||||||||||||||
| 31 | BINARY => "\x05", | ||||||||||||||||||||||||||||
| 32 | UNDEFINED => "\x06", | ||||||||||||||||||||||||||||
| 33 | OBJECT_ID => "\x07", | ||||||||||||||||||||||||||||
| 34 | BOOL => "\x08", | ||||||||||||||||||||||||||||
| 35 | DATETIME => "\x09", | ||||||||||||||||||||||||||||
| 36 | NULL => "\x0a", | ||||||||||||||||||||||||||||
| 37 | REGEX => "\x0b", | ||||||||||||||||||||||||||||
| 38 | CODE => "\x0d", | ||||||||||||||||||||||||||||
| 39 | CODE_SCOPE => "\x0f", | ||||||||||||||||||||||||||||
| 40 | INT32 => "\x10", | ||||||||||||||||||||||||||||
| 41 | TIMESTAMP => "\x11", | ||||||||||||||||||||||||||||
| 42 | INT64 => "\x12", | ||||||||||||||||||||||||||||
| 43 | MIN_KEY => "\xff", | ||||||||||||||||||||||||||||
| 44 | MAX_KEY => "\x7f" | ||||||||||||||||||||||||||||
| 45 | 11 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Binary subtypes | ||||||||||||||||||||||||||||
| 48 | 11 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 11 | BINARY_GENERIC => "\x00", | |||||||||||||||||||||||||||
| 50 | BINARY_FUNCTION => "\x01", | ||||||||||||||||||||||||||||
| 51 | BINARY_UUID => "\x04", | ||||||||||||||||||||||||||||
| 52 | BINARY_MD5 => "\x05", | ||||||||||||||||||||||||||||
| 53 | BINARY_USER_DEFINED => "\x80" | ||||||||||||||||||||||||||||
| 54 | 11 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # The pack() format to use for each numeric type | ||||||||||||||||||||||||||||
| 57 | 11 | my %num_pack_fmt = ( | |||||||||||||||||||||||||||
| 58 | DOUBLE() => 'd<', | ||||||||||||||||||||||||||||
| 59 | INT32() => 'l<', | ||||||||||||||||||||||||||||
| 60 | INT64() => 'q<' | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Reuse boolean singletons | ||||||||||||||||||||||||||||
| 64 | 11 | my $FALSE = Mojo::JSON->false; | |||||||||||||||||||||||||||
| 65 | 11 | my $TRUE = Mojo::JSON->true; | |||||||||||||||||||||||||||
| 66 | 11 | my $BOOL = blessed $TRUE; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 11 | my $MAXKEY = bless {}, 'Mango::BSON::_MaxKey'; | |||||||||||||||||||||||||||
| 69 | 11 | my $MINKEY = bless {}, 'Mango::BSON::_MinKey'; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 14 | ▶ | sub bson_bin { Mango::BSON::Binary->new(data => shift) } | ||||||||||||||||||||||||||
|
Subroutine
bson_bin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | sub bson_code { Mango::BSON::Code->new(code => shift) } | ||||||||||||||||||||||||||
|
Subroutine
bson_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub bson_dbref { bson_doc('$ref' => shift, '$id' => shift) } | ||||||||||||||||||||||||||
|
Subroutine
bson_dbref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 62 | ▶ | sub bson_decode { | ||||||||||||||||||||||||||
|
Subroutine
bson_decode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 62 | my $bson = shift; | |||||||||||||||||||||||||||
| 79 | 62 | ▶ | return undef unless my $len = bson_length($bson); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 59 | ▶ | return length $bson == $len ? _decode_doc(\$bson) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 91 | ▶ | sub bson_doc { | ||||||||||||||||||||||||||
|
Subroutine
bson_doc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 91 | tie my %hash, 'Mango::BSON::Document', @_; | |||||||||||||||||||||||||||
| 85 | 91 | return \%hash; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | sub bson_double { Mango::BSON::Number->new(shift, DOUBLE) } | ||||||||||||||||||||||||||
|
Subroutine
bson_double: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 113 | ▶ | sub bson_encode { | ||||||||||||||||||||||||||
|
Subroutine
bson_encode: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 113 | my $doc = shift; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Embedded BSON | ||||||||||||||||||||||||||||
| 94 | 113 | ▶ | return $doc->{'$bson'} if exists $doc->{'$bson'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | my $bson = join '', | ||||||||||||||||||||||||||||
| 97 | 110 | map { _encode_value(encode_cstring($_), $doc->{$_}) } keys %$doc; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Document ends with null byte | ||||||||||||||||||||||||||||
| 100 | 110 | return pack('l<', length($bson) + 5) . $bson . "\x00"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 10 | ▶ | sub bson_false {$FALSE} | ||||||||||||||||||||||||||
|
Subroutine
bson_false: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 7 | ▶ | sub bson_int32 { Mango::BSON::Number->new(shift, INT32) } | ||||||||||||||||||||||||||
|
Subroutine
bson_int32: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 7 | ▶ | sub bson_int64 { Mango::BSON::Number->new(shift, INT64) } | ||||||||||||||||||||||||||
|
Subroutine
bson_int64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 73 | ▶ | sub bson_length { length $_[0] < 4 ? undef : unpack 'l<', substr($_[0], 0, 4) } | ||||||||||||||||||||||||||
|
Subroutine
bson_length: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | ▶ | sub bson_max {$MAXKEY} | ||||||||||||||||||||||||||
|
Subroutine
bson_max: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | sub bson_min {$MINKEY} | ||||||||||||||||||||||||||
|
Subroutine
bson_min: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 13 | ▶ | sub bson_oid { Mango::BSON::ObjectID->new(@_) } | ||||||||||||||||||||||||||
|
Subroutine
bson_oid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub bson_raw { bson_doc('$bson' => shift) } | ||||||||||||||||||||||||||
|
Subroutine
bson_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 14 | ▶ | sub bson_time { Mango::BSON::Time->new(@_) } | ||||||||||||||||||||||||||
|
Subroutine
bson_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub bson_ts { | ||||||||||||||||||||||||||
|
Subroutine
bson_ts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 1 | Mango::BSON::Timestamp->new(seconds => shift, increment => shift); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 10 | ▶ | sub bson_true {$TRUE} | ||||||||||||||||||||||||||
|
Subroutine
bson_true: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 120 | ▶ | sub encode_cstring { | ||||||||||||||||||||||||||
|
Subroutine
encode_cstring: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 120 | my $str = shift; | |||||||||||||||||||||||||||
| 129 | 120 | utf8::encode $str; | |||||||||||||||||||||||||||
| 130 | 120 | return pack 'Z*', $str; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | ▶ | sub _decode_binary { | ||||||||||||||||||||||||||
|
Subroutine
_decode_binary: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 134 | 6 | my $bsonref = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 6 | my $len = unpack 'l<', substr($$bsonref, 0, 4, ''); | |||||||||||||||||||||||||||
| 137 | 6 | my $subtype = substr $$bsonref, 0, 1, ''; | |||||||||||||||||||||||||||
| 138 | 6 | my $binary = substr $$bsonref, 0, $len, ''; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 6 | ▶ | return bson_bin($binary)->type('function') if $subtype eq BINARY_FUNCTION; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 5 | ▶ | return bson_bin($binary)->type('md5') if $subtype eq BINARY_MD5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 4 | ▶ | return bson_bin($binary)->type('uuid') if $subtype eq BINARY_UUID; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 3 | ▶ | return bson_bin($binary)->type('user_defined') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | if $subtype eq BINARY_USER_DEFINED; | ||||||||||||||||||||||||||||
| 145 | 2 | return bson_bin($binary)->type('generic'); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 77 | ▶ | sub _decode_cstring { | ||||||||||||||||||||||||||
|
Subroutine
_decode_cstring: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | 77 | my $bsonref = shift; | |||||||||||||||||||||||||||
| 150 | 77 | my $str = substr $$bsonref, 0, index($$bsonref, "\x00"), ''; | |||||||||||||||||||||||||||
| 151 | 77 | utf8::decode $str; | |||||||||||||||||||||||||||
| 152 | 77 | substr $$bsonref, 0, 1, ''; | |||||||||||||||||||||||||||
| 153 | 77 | return $str; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 75 | ▶ | sub _decode_doc { | ||||||||||||||||||||||||||
|
Subroutine
_decode_doc: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 157 | 75 | my $bsonref = shift; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Every element starts with a type | ||||||||||||||||||||||||||||
| 160 | 75 | my @doc; | |||||||||||||||||||||||||||
| 161 | 75 | substr $$bsonref, 0, 4, ''; | |||||||||||||||||||||||||||
| 162 | 75 | ▶ | while (my $type = substr $$bsonref, 0, 1, '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Null byte (end of document) | ||||||||||||||||||||||||||||
| 165 | 150 | ▶ | last if $type eq "\x00"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 75 | push @doc, _decode_cstring($bsonref), _decode_value($type, $bsonref); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 75 | return bson_doc(@doc); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 17 | ▶ | sub _decode_string { | ||||||||||||||||||||||||||
|
Subroutine
_decode_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 17 | my $bsonref = shift; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 17 | my $len = unpack 'l<', substr($$bsonref, 0, 4, ''); | |||||||||||||||||||||||||||
| 177 | 17 | my $str = substr $$bsonref, 0, $len - 1, ''; | |||||||||||||||||||||||||||
| 178 | 17 | utf8::decode $str; | |||||||||||||||||||||||||||
| 179 | 17 | substr $$bsonref, 0, 1, ''; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 17 | return $str; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 75 | ▶ | sub _decode_value { | ||||||||||||||||||||||||||
|
Subroutine
_decode_value: called CC=22 SCAR=31.2
| |||||||||||||||||||||||||||||
| 185 | 75 | my ($type, $bsonref) = @_; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # String | ||||||||||||||||||||||||||||
| 188 | 75 | ▶ | return _decode_string($bsonref) if $type eq STRING; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Object ID | ||||||||||||||||||||||||||||
| 191 | 60 | ▶ | return bson_oid(unpack 'H*', substr $$bsonref, 0, 12, '') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | if $type eq OBJECT_ID; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Double/Int32/Int64 | ||||||||||||||||||||||||||||
| 195 | 58 | ▶ | return unpack 'd<', substr $$bsonref, 0, 8, '' if $type eq DOUBLE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 51 | ▶ | return unpack 'l<', substr($$bsonref, 0, 4, '') if $type eq INT32; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 43 | ▶ | return unpack 'q<', substr($$bsonref, 0, 8, '') if $type eq INT64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # Document | ||||||||||||||||||||||||||||
| 200 | 41 | ▶ | return _decode_doc($bsonref) if $type eq DOCUMENT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Array | ||||||||||||||||||||||||||||
| 203 | 31 | ▶ | return [values %{_decode_doc($bsonref)}] if $type eq ARRAY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # Booleans and Null | ||||||||||||||||||||||||||||
| 206 | 25 | ▶ | return substr($$bsonref, 0, 1, '') eq "\x00" ? bson_false() : bson_true() | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 207 | if $type eq BOOL; | ||||||||||||||||||||||||||||
| 208 | 15 | ▶ | return undef if $type eq NULL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # Time | ||||||||||||||||||||||||||||
| 211 | 14 | ▶ | return bson_time(unpack 'q<', substr($$bsonref, 0, 8, '')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | if $type eq DATETIME; | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # Regex | ||||||||||||||||||||||||||||
| 215 | 12 | ▶ | if ($type eq REGEX) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 1 | my ($p, $m) = (_decode_cstring($bsonref), _decode_cstring($bsonref)); | |||||||||||||||||||||||||||
| 217 | 1 | ▶ | croak "invalid regex modifier(s) in 'qr/$p/$m'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $m and not $m =~ /^[msixpadlun]+\z/u
MC/DC0%0/2
length $m and not $m =~ /^[msixpadlun]+\z/u
length $m
not $m =~ /^[msixpadlun]+\z/u
Truth table33%1/3
A and not(B)
A= length $mB= not $m =~ /^[msixpadlun]+\z/u
| |||||||||||||||||||||||||||||
| 218 | if length($m) and $m !~ /^[msixpadlun]+\z/; | ||||||||||||||||||||||||||||
| 219 | # escape $pat to avoid code injection | ||||||||||||||||||||||||||||
| 220 | 1 | return eval "qr/\$p/$m"; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # Binary (with subtypes) | ||||||||||||||||||||||||||||
| 224 | 11 | ▶ | return _decode_binary($bsonref) if $type eq BINARY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # Min/Max | ||||||||||||||||||||||||||||
| 227 | 5 | ▶ | return bson_min() if $type eq MIN_KEY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 4 | ▶ | return bson_max() if $type eq MAX_KEY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # Code (with and without scope) | ||||||||||||||||||||||||||||
| 231 | 3 | ▶ | return bson_code(_decode_string($bsonref)) if $type eq CODE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 2 | ▶ | if ($type eq CODE_SCOPE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 1 | substr $$bsonref, 0, 4, ''; | |||||||||||||||||||||||||||
| 234 | 1 | return bson_code(_decode_string($bsonref))->scope(_decode_doc($bsonref)); | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # Timestamp | ||||||||||||||||||||||||||||
| 238 | return bson_ts( | ||||||||||||||||||||||||||||
| 239 | 1 | ▶ | reverse map({ unpack 'l<', substr($$_, 0, 4, '') } $bsonref, $bsonref)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | if $type eq TIMESTAMP; | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # Undefined - a deprecated type which should not exist anymore | ||||||||||||||||||||||||||||
| 243 | # but apparently still does: https://github.com/oliwer/mango/issues/1 | ||||||||||||||||||||||||||||
| 244 | 0 | ▶ | return undef if $type eq UNDEFINED; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # Unknown | ||||||||||||||||||||||||||||
| 247 | 0 | croak 'Unknown BSON type'; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 6 | ▶ | sub _encode_binary { | ||||||||||||||||||||||||||
|
Subroutine
_encode_binary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 251 | 6 | my ($e, $subtype, $value) = @_; | |||||||||||||||||||||||||||
| 252 | 6 | return BINARY . $e . pack('l<', length $value) . $subtype . $value; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 48 | ▶ | sub _encode_object { | ||||||||||||||||||||||||||
|
Subroutine
_encode_object: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||
| 256 | 48 | my ($e, $value, $class) = @_; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # ObjectID | ||||||||||||||||||||||||||||
| 259 | 48 | ▶ | return OBJECT_ID . $e . $value->to_bytes | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | if $class eq 'Mango::BSON::ObjectID'; | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # Boolean | ||||||||||||||||||||||||||||
| 263 | 46 | ▶ | return BOOL . $e . ($value ? "\x01" : "\x00") if $class eq $BOOL; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # Time | ||||||||||||||||||||||||||||
| 266 | 36 | ▶ | return DATETIME . $e . pack('q<', $value) if $class eq 'Mango::BSON::Time'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # Max | ||||||||||||||||||||||||||||
| 269 | 34 | ▶ | return MAX_KEY . $e if $value eq $MAXKEY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # Min | ||||||||||||||||||||||||||||
| 272 | 33 | ▶ | return MIN_KEY . $e if $value eq $MINKEY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # Regex | ||||||||||||||||||||||||||||
| 275 | 32 | ▶ | if ($class eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 1 | my ($p, $m) = regexp_pattern($value); | |||||||||||||||||||||||||||
| 277 | 1 | return REGEX . $e . encode_cstring($p) . encode_cstring($m); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # Binary | ||||||||||||||||||||||||||||
| 281 | 31 | ▶ | if ($class eq 'Mango::BSON::Binary') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 6 | ▶ | my $type = $value->type // 'generic'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$value->type // "generic"
MC/DC0%0/1
$value->type // "generic"
$value->type
Truth table50%1/2
A
A= $value->type
| |||||||||||||||||||||||||||||
| 283 | 6 | my $data = $value->data; | |||||||||||||||||||||||||||
| 284 | 6 | ▶ | return _encode_binary($e, BINARY_FUNCTION, $data) if $type eq 'function'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 5 | ▶ | return _encode_binary($e, BINARY_MD5, $data) if $type eq 'md5'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 4 | ▶ | return _encode_binary($e, BINARY_USER_DEFINED, $data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | if $type eq 'user_defined'; | ||||||||||||||||||||||||||||
| 288 | 3 | ▶ | return _encode_binary($e, BINARY_UUID, $data) if $type eq 'uuid'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 2 | return _encode_binary($e, BINARY_GENERIC, $data); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | # Code | ||||||||||||||||||||||||||||
| 293 | 25 | ▶ | if ($class eq 'Mango::BSON::Code') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # With scope | ||||||||||||||||||||||||||||
| 296 | 2 | ▶ | if (my $scope = $value->scope) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | 1 | my $code = _encode_string($value->code) . bson_encode($scope); | |||||||||||||||||||||||||||
| 298 | 1 | return CODE_SCOPE . $e . pack('l<', length $code) . $code; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # Without scope | ||||||||||||||||||||||||||||
| 302 | 1 | return CODE . $e . _encode_string($value->code); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # Timestamp | ||||||||||||||||||||||||||||
| 306 | 23 | ▶ | return TIMESTAMP, $e, map { pack 'l<', $_ } $value->increment, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | $value->seconds | ||||||||||||||||||||||||||||
| 308 | if $class eq 'Mango::BSON::Timestamp'; | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # Number | ||||||||||||||||||||||||||||
| 311 | 22 | ▶ | if ($class eq 'Mango::BSON::Number') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 17 | my $t = $value->type; | |||||||||||||||||||||||||||
| 313 | 17 | return $t . $e . pack($num_pack_fmt{$t}, $value->value); | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # Blessed reference with TO_JSON method | ||||||||||||||||||||||||||||
| 317 | 5 | ▶ | if (my $sub = $value->can('TO_BSON') // $value->can('TO_JSON')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value->can("TO_BSON") // $value->can("TO_JSON")
MC/DC100%2/2
$value->can("TO_BSON") // $value->can("TO_JSON")
$value->can("TO_BSON")
$value->can("TO_JSON")
Truth table100%3/3
A // B
A= $value->can("TO_BSON")B= $value->can("TO_JSON")
| |||||||||||||||||||||||||||||
| 318 | 4 | return _encode_value($e, $value->$sub); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # Stringify | ||||||||||||||||||||||||||||
| 322 | 1 | return STRING . $e . _encode_string($value); | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 27 | ▶ | sub _encode_string { | ||||||||||||||||||||||||||
|
Subroutine
_encode_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 326 | 27 | my $str = shift; | |||||||||||||||||||||||||||
| 327 | 27 | utf8::encode $str; | |||||||||||||||||||||||||||
| 328 | 27 | return pack('l<', length($str) + 1) . "$str\x00"; | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 118 | ▶ | sub _encode_value { | ||||||||||||||||||||||||||
|
Subroutine
_encode_value: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 332 | 118 | my ($e, $value) = @_; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Null | ||||||||||||||||||||||||||||
| 335 | 118 | ▶ | return NULL . $e unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # Reference | ||||||||||||||||||||||||||||
| 338 | 117 | ▶ | if (my $ref = ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # Blessed | ||||||||||||||||||||||||||||
| 341 | 70 | ▶ | return _encode_object($e, $value, $ref) if blessed $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # Hash (Document) | ||||||||||||||||||||||||||||
| 344 | 28 | ▶ | return DOCUMENT . $e . bson_encode($value) if $ref eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # Array | ||||||||||||||||||||||||||||
| 347 | 17 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 11 | my $i = 0; | |||||||||||||||||||||||||||
| 349 | 11 | return ARRAY . $e . bson_encode(bson_doc(map { $i++ => $_ } @$value)); | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | # Scalar (boolean shortcut) | ||||||||||||||||||||||||||||
| 353 | 6 | ▶ | return _encode_object($e, !!$$value, $BOOL) if $ref eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # Numeric | ||||||||||||||||||||||||||||
| 357 | 47 | ▶ | if (my $type = Mango::BSON::Number::guess_type($value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 23 | return $type . $e . pack($num_pack_fmt{$type}, $value); | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | # String | ||||||||||||||||||||||||||||
| 362 | 24 | return STRING . $e . _encode_string("$value"); | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | # Constants | ||||||||||||||||||||||||||||
| 366 | package Mango::BSON::_MaxKey; | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | package Mango::BSON::_MinKey; | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 11 | 1; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =encoding utf8 | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head1 NAME | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | Mango::BSON - BSON | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | use Mango::BSON ':bson'; | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | my $bson = bson_encode { | ||||||||||||||||||||||||||||
| 383 | foo => 'bar', | ||||||||||||||||||||||||||||
| 384 | baz => 0.42, | ||||||||||||||||||||||||||||
| 385 | unordered => {one => [1, 2, 3], two => bson_time}, | ||||||||||||||||||||||||||||
| 386 | ordered => bson_doc(one => qr/test/i, two => bson_true) | ||||||||||||||||||||||||||||
| 387 | }; | ||||||||||||||||||||||||||||
| 388 | my $doc = bson_decode $bson; | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | L<Mango::BSON> is a minimalistic implementation of L<http://bsonspec.org>. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | In addition to a bunch of custom BSON data types it supports normal Perl data | ||||||||||||||||||||||||||||
| 395 | types like scalar, regular expression, C<undef>, array reference, hash | ||||||||||||||||||||||||||||
| 396 | reference and will try to call the C<TO_BSON> and C<TO_JSON> methods on | ||||||||||||||||||||||||||||
| 397 | blessed references, or stringify them if it doesn't exist. Scalar references | ||||||||||||||||||||||||||||
| 398 | will be used to generate booleans, based on if their values are true or false. | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | L<Mango::BSON> implements the following functions, which can be imported | ||||||||||||||||||||||||||||
| 403 | individually or at once with the C<:bson> flag. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head2 bson_bin | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | my $bin = bson_bin $bytes; | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Create new BSON element of the binary type with L<Mango::BSON::Binary>, | ||||||||||||||||||||||||||||
| 410 | defaults to the C<generic> binary subtype. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | # Function | ||||||||||||||||||||||||||||
| 413 | bson_bin($bytes)->type('function'); | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | # MD5 | ||||||||||||||||||||||||||||
| 416 | bson_bin($bytes)->type('md5'); | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # UUID | ||||||||||||||||||||||||||||
| 419 | bson_bin($bytes)->type('uuid'); | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # User defined | ||||||||||||||||||||||||||||
| 422 | bson_bin($bytes)->type('user_defined'); | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head2 bson_code | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | my $code = bson_code 'function () {}'; | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Create new BSON element of the code type with L<Mango::BSON::Code>. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | # With scope | ||||||||||||||||||||||||||||
| 431 | bson_code('function () {}')->scope({foo => 'bar'}); | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head2 bson_dbref | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | my $dbref = bson_dbref 'test', $oid; | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | Create a new database reference. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # Longer version | ||||||||||||||||||||||||||||
| 440 | my $dbref = {'$ref' => 'test', '$id' => $oid}; | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head2 bson_decode | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | my $doc = bson_decode $bson; | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | Decode BSON into Perl data structures. | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =head2 bson_doc | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | my $doc = bson_doc; | ||||||||||||||||||||||||||||
| 451 | my $doc = bson_doc foo => 'bar', baz => 0.42, yada => {yada => [1, 2, 3]}; | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Create new BSON document with L<Mango::BSON::Document>, which can also be used | ||||||||||||||||||||||||||||
| 454 | as a generic ordered hash. | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | # Order is preserved | ||||||||||||||||||||||||||||
| 457 | my $hash = bson_doc one => 1, two => 2, three => 3; | ||||||||||||||||||||||||||||
| 458 | $hash->{four} = 4; | ||||||||||||||||||||||||||||
| 459 | delete $hash->{two}; | ||||||||||||||||||||||||||||
| 460 | say for keys %$hash; | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head2 bson_double | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | my $doc = { foo => bson_double(13.0) }; | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Force a scalar value to be encoded as a double in MongoDB. Croaks if the | ||||||||||||||||||||||||||||
| 467 | value is incompatible with the double type. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =head2 bson_encode | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | my $bson = bson_encode $doc; | ||||||||||||||||||||||||||||
| 472 | my $bson = bson_encode {}; | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | Encode Perl data structures into BSON. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =head2 bson_false | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | my $false = bson_false; | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | Create new BSON element of the boolean type false. | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =head2 bson_int32 | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | my $doc = { foo => bson_int32(13) }; | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | # This will die (integer is too big) | ||||||||||||||||||||||||||||
| 487 | my $doc = { foo => bson_int32(2147483648) }; | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | Force a scalar value to be encoded as a 32 bit integer in MongoDB. Croaks if | ||||||||||||||||||||||||||||
| 490 | the value is incompatible with the int32 type. | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =head2 bson_int64 | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | my $doc = { foo => bson_int64(666) }; | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | Force a scalar value to be encoded as a 64 bit integer in MongoDB. Croaks if | ||||||||||||||||||||||||||||
| 497 | the value is incompatible with the int64 type. | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | =head2 bson_length | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | my $len = bson_length $bson; | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | Check BSON length prefix. | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =head2 bson_max | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | my $max_key = bson_max; | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | Create new BSON element of the max key type. | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | =head2 bson_min | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | my $min_key = bson_min; | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | Create new BSON element of the min key type. | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =head2 bson_oid | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | my $oid = bson_oid; | ||||||||||||||||||||||||||||
| 520 | my $oid = bson_oid '1a2b3c4e5f60718293a4b5c6'; | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | Create new BSON element of the object id type with L<Mango::BSON::ObjectID>, | ||||||||||||||||||||||||||||
| 523 | defaults to generating a new unique object id. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | # Generate object id with specific epoch time | ||||||||||||||||||||||||||||
| 526 | my $oid = bson_oid->from_epoch(1359840145); | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | =head2 bson_raw | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | my $raw = bson_raw $bson; | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | Pre-encoded BSON document. | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | # Longer version | ||||||||||||||||||||||||||||
| 535 | my $raw = {'$bson' => $bson}; | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | # Embed pre-encoded BSON document | ||||||||||||||||||||||||||||
| 538 | my $first = bson_encode {foo => 'bar'}; | ||||||||||||||||||||||||||||
| 539 | my $second = bson_encode {test => bson_raw $first}; | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | =head2 bson_time | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | my $now = bson_time; | ||||||||||||||||||||||||||||
| 544 | my $time = bson_time time * 1000; | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | Create new BSON element of the UTC datetime type with L<Mango::BSON::Time>, | ||||||||||||||||||||||||||||
| 547 | defaults to milliseconds since the UNIX epoch. | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | # "1360626536.748" | ||||||||||||||||||||||||||||
| 550 | bson_time(1360626536748)->to_epoch; | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | # "2013-02-11T23:48:56.748Z" | ||||||||||||||||||||||||||||
| 553 | bson_time(1360626536748)->to_datetime; | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | =head2 bson_true | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | my $true = bson_true; | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | Create new BSON element of the boolean type true. | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | =head2 bson_ts | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | my $timestamp = bson_ts 23, 24; | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | Create new BSON element of the timestamp type with L<Mango::BSON::Timestamp>. | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | =head2 encode_cstring | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | my $bytes = encode_cstring $cstring; | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | Encode cstring. | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | =cut | ||||||||||||||||||||||||||||