| 1 | 71 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 71 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 71 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package BSON::Timestamp; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: BSON type wrapper for timestamps | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 71 | our $VERSION = 'v1.12.2'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 71 | ▶ | use Tie::IxHash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 71 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #pod =attr seconds | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod A value representing seconds since the Unix epoch. The default is | ||||||||||||||||||||||||||||
| 19 | #pod current value of C<time()>. | ||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||
| 21 | #pod =attr increment | ||||||||||||||||||||||||||||
| 22 | #pod | ||||||||||||||||||||||||||||
| 23 | #pod A numeric value to disambiguate timestamps in the same second. The | ||||||||||||||||||||||||||||
| 24 | #pod default is 0. | ||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||
| 26 | #pod =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 71 | has [qw/seconds increment/] => ( | |||||||||||||||||||||||||||
| 29 | is => 'ro' | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 71 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 71 | my $max_int32 = 2147483647; | |||||||||||||||||||||||||||
| 35 | 71 | my $max_uint32 = 4_294_967_295; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Support back-compat 'sec' and inc' and legacy constructor shortcut | ||||||||||||||||||||||||||||
| 38 | 17617 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=11 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 17617 | my ($class) = shift; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 17617 | my %args; | |||||||||||||||||||||||||||
| 42 | 17617 | ▶ | if ( @_ && $_[0] !~ /^[s|i]/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and not $_[0] =~ /^[s|i]/
MC/DC100%2/2
@_ and not $_[0] =~ /^[s|i]/
@_
not $_[0] =~ /^[s|i]/
Truth table100%3/3
A and not(B)
A= @_B= not $_[0] =~ /^[s|i]/
| |||||||||||||||||||||||||||||
| 43 | 17575 | $args{seconds} = $_[0]; | |||||||||||||||||||||||||||
| 44 | 17575 | $args{increment} = $_[1]; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||
| 47 | 42 | ▶ | Carp::croak( __PACKAGE__ . "::new called with an odd number of elements\n" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | unless @_ % 2 == 0; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 42 | %args = @_; | |||||||||||||||||||||||||||
| 51 | 42 | ▶ | $args{seconds} = $args{sec} if exists $args{sec} && !exists $args{seconds}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"sec"} and not exists $args{"seconds"}
MC/DC0%0/2
exists $args{"sec"} and not exists $args{"seconds"}
exists $args{"sec"}
not exists $args{"seconds"}
Truth table33%1/3
A and not(B)
A= exists $args{"sec"}B= not exists $args{"seconds"}
| |||||||||||||||||||||||||||||
| 52 | 42 | ▶ | $args{increment} = $args{inc} if exists $args{inc} && !exists $args{increment}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"inc"} and not exists $args{"increment"}
MC/DC0%0/2
exists $args{"inc"} and not exists $args{"increment"}
exists $args{"inc"}
not exists $args{"increment"}
Truth table33%1/3
A and not(B)
A= exists $args{"inc"}B= not exists $args{"increment"}
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 17617 | ▶ | $args{seconds} = time unless defined $args{seconds}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 17617 | ▶ | $args{increment} = 0 unless defined $args{increment}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 17617 | $args{$_} = int( $args{$_} ) for qw/seconds increment/; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 17617 | return \%args; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 17617 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 17617 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 17617 | for my $attr (qw/seconds increment/) { | |||||||||||||||||||||||||||
| 66 | 35232 | my $v = $self->$attr; | |||||||||||||||||||||||||||
| 67 | 35232 | ▶ | Carp::croak("BSON::Timestamp '$attr' must be uint32") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$v >= 0 and $v <= $max_uint32
MC/DC100%2/2
$v >= 0 and $v <= $max_uint32
$v >= 0
$v <= $max_uint32
Truth table100%3/3
A and B
A= $v >= 0B= $v <= $max_uint32
| |||||||||||||||||||||||||||||
| 68 | unless $v >= 0 && $v <= $max_uint32; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 17613 | return; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # For backwards compatibility | ||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 71 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 71 | *sec = \&seconds; | |||||||||||||||||||||||||||
| 78 | 71 | *inc = \&increment; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #pod =method TO_JSON | ||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||
| 83 | #pod If the C<BSON_EXTJSON> option is true, returns a hashref compatible with | ||||||||||||||||||||||||||||
| 84 | #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 85 | #pod format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 86 | #pod | ||||||||||||||||||||||||||||
| 87 | #pod {"$timestamp" : { "t":<seconds>, "i":<increment> }} | ||||||||||||||||||||||||||||
| 88 | #pod | ||||||||||||||||||||||||||||
| 89 | #pod If the C<BSON_EXTJSON> option is false, an error is thrown, as this value | ||||||||||||||||||||||||||||
| 90 | #pod can't otherwise be represented in JSON. | ||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||
| 92 | #pod =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 11 | ▶ | if ( $ENV{BSON_EXTJSON} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 10 | my %data; | |||||||||||||||||||||||||||
| 97 | 10 | tie %data, 'Tie::IxHash'; | |||||||||||||||||||||||||||
| 98 | 10 | $data{t} = int($_[0]->{seconds}); | |||||||||||||||||||||||||||
| 99 | 10 | $data{i} = int($_[0]->{increment}); | |||||||||||||||||||||||||||
| 100 | 10 | return { '$timestamp' => \%data }; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | Carp::croak( "The value '$_[0]' is illegal in JSON" ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 8 | ▶ | sub _cmp { | ||||||||||||||||||||||||||
|
Subroutine
_cmp: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 107 | 8 | my ( $l, $r, $swap ) = @_; | |||||||||||||||||||||||||||
| 108 | 8 | ▶ | if ( !defined($l) || !defined($r) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $l and defined $r
MC/DC50%1/2
defined $l and defined $r
defined $l
defined $r
Truth table66%2/3
A and B
A= defined $lB= defined $r
| |||||||||||||||||||||||||||||
| 109 | 3 | Carp::carp "Use of uninitialized value in BSON::Timestamp comparison (<=>); will be treated as 0,0"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 8 | ▶ | $l //= { seconds => 0, increment => 0 }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$l //= {"seconds", 0, "increment", 0}
MC/DC0%0/1
$l //= {"seconds", 0, "increment", 0}
$l
Truth table50%1/2
A
A= $l
| |||||||||||||||||||||||||||||
| 112 | 8 | ▶ | $r //= { seconds => 0, increment => 0 }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$r //= {"seconds", 0, "increment", 0}
MC/DC100%1/1
$r //= {"seconds", 0, "increment", 0}
$r
Truth table100%2/2
A
A= $r
| |||||||||||||||||||||||||||||
| 113 | 8 | ▶ | ($r, $l) = ($l, $r) if $swap; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | my $cmp = ( $l->{seconds} <=> $r->{seconds} ) | ||||||||||||||||||||||||||||
| 115 | 8 | ▶ | || ( $l->{increment} <=> $r->{increment} ); | ||||||||||||||||||||||||||
|
Condition100%3/3
$l->{'seconds'} <=> $r->{'seconds'} || $l->{'increment'} <=> $r->{'increment'}
MC/DC100%2/2
$l->{'seconds'} <=> $r->{'seconds'} || $l->{'increment'} <=> $r->{'increment'}
$l->{'seconds'} <=> $r->{'seconds'}
$l->{'increment'} <=> $r->{'increment'}
Truth table100%3/3
A || B
A= $l->{'seconds'} <=> $r->{'seconds'}B= $l->{'increment'} <=> $r->{'increment'}
| |||||||||||||||||||||||||||||
| 116 | 8 | return $cmp; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 71 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 71 | '<=>' => \&_cmp, | |||||||||||||||||||||||||||
| 121 | fallback => 1, | ||||||||||||||||||||||||||||
| 122 | 71 | ); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 71 | 1; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =pod | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 NAME | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | BSON::Timestamp - BSON type wrapper for timestamps | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 VERSION | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | version v1.12.2 | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | use BSON::Types ':all'; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | bson_timestamp( $seconds ); | ||||||||||||||||||||||||||||
| 143 | bson_timestamp( $seconds, $increment ); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | This module provides a BSON type wrapper for a BSON timestamp value. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Generally, it should not be used by end-users, but is provided for | ||||||||||||||||||||||||||||
| 150 | backwards compatibility. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head2 seconds | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | A value representing seconds since the Unix epoch. The default is | ||||||||||||||||||||||||||||
| 157 | current value of C<time()>. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 increment | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | A numeric value to disambiguate timestamps in the same second. The | ||||||||||||||||||||||||||||
| 162 | default is 0. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 METHODS | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 TO_JSON | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | If the C<BSON_EXTJSON> option is true, returns a hashref compatible with | ||||||||||||||||||||||||||||
| 169 | MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 170 | format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | {"$timestamp" : { "t":<seconds>, "i":<increment> }} | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | If the C<BSON_EXTJSON> option is false, an error is thrown, as this value | ||||||||||||||||||||||||||||
| 175 | can't otherwise be represented in JSON. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =for Pod::Coverage BUILD BUILDARGS sec inc | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =over 4 | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item * | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | David Golden <david@mongodb.com> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item * | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Stefan G. <minimalist@lavabit.com> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =back | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||