This file is indexed.

/usr/lib/x86_64-linux-gnu/perl5/5.22/MongoDB/IndexView.pm is in libmongodb-perl 1.2.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#
#  Copyright 2015 MongoDB, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

package MongoDB::IndexView;

# ABSTRACT: Index management for a collection

use version;
our $VERSION = 'v1.2.2';

use Moo;
use MongoDB::Error;
use MongoDB::WriteConcern;
use MongoDB::_Types qw(
    BSONCodec
    IxHash
    is_IndexModelList
    is_OrderedDoc
);
use Types::Standard qw(
    InstanceOf
    Str
    is_Str
);
use boolean;
use namespace::clean -except => 'meta';

#pod =attr collection
#pod
#pod The L<MongoDB::Collection> for which indexes are being created or viewed.
#pod
#pod =cut

#--------------------------------------------------------------------------#
# constructor attributes
#--------------------------------------------------------------------------#

has collection => (
    is       => 'ro',
    isa      => InstanceOf( ['MongoDB::Collection'] ),
    required => 1,
);

#--------------------------------------------------------------------------#
# private attributes
#--------------------------------------------------------------------------#

has _bson_codec => (
    is      => 'lazy',
    isa     => BSONCodec,
    builder => '_build__bson_codec',
);

sub _build__bson_codec {
    my ($self) = @_;
    return $self->collection->bson_codec;
}

has _client => (
    is      => 'lazy',
    isa     => InstanceOf( ['MongoDB::MongoClient'] ),
    builder => '_build__client',
);

sub _build__client {
    my ($self) = @_;
    return $self->collection->client;
}

has _coll_name => (
    is      => 'lazy',
    isa     => Str,
    builder => '_build__coll_name',
);

sub _build__coll_name {
    my ($self) = @_;
    return $self->collection->name;
}

has _db_name => (
    is      => 'lazy',
    isa     => Str,
    builder => '_build__db_name',
);

sub _build__db_name {
    my ($self) = @_;
    return $self->collection->database->name;
}

#--------------------------------------------------------------------------#
# public methods
#--------------------------------------------------------------------------#

#pod =method list
#pod
#pod     $result = $indexes->list;
#pod
#pod     while ( my $index = $result->next ) {
#pod         ...
#pod     }
#pod
#pod     for my $index ( $result->all ) {
#pod         ...
#pod     }
#pod
#pod This method returns a L<MongoDB::QueryResult> which can be used to
#pod retrieve index information either one at a time (with C<next>) or
#pod all at once (with C<all>).
#pod
#pod If the list can't be retrieved, an exception will be thrown.
#pod
#pod =cut

my $list_args;

sub list {
    my ($self) = @_;

    my $op = MongoDB::Op::_ListIndexes->_new(
        client     => $self->_client,
        db_name    => $self->_db_name,
        coll_name  => $self->_coll_name,
        bson_codec => $self->_bson_codec,
    );

    return $self->_client->send_read_op($op);
}

#pod =method create_one
#pod
#pod     $name = $indexes->create_one( [ x => 1 ] );
#pod     $name = $indexes->create_one( [ x => 1, y => 1 ] );
#pod     $name = $indexes->create_one( [ z => 1 ], { unique => 1 } );
#pod
#pod This method takes an ordered index specification document and an optional
#pod hash reference of index options and returns the name of the index created.
#pod It will throw an exception on error.
#pod
#pod The index specification document is an ordered document (array reference,
#pod L<Tie::IxHash> object, or single-key hash reference) with index keys and
#pod direction/type.
#pod
#pod See L</create_many> for important information about index specifications
#pod and options.
#pod
#pod =cut

my $create_one_args;

sub create_one {
    my ( $self, $keys, $opts ) = @_;

    MongoDB::UsageError->throw("Argument to create_one must be an ordered document")
      unless is_OrderedDoc($keys);

    my ($name) =
      $self->create_many( { keys => $keys, ( $opts ? ( options => $opts ) : () ) } );
    return $name;
}

#pod =method create_many
#pod
#pod     @names = $indexes->create_many(
#pod         { keys => [ x => 1, y => 1 ] },
#pod         { keys => [ z => 1 ], options => { unique => 1 } }
#pod     );
#pod
#pod This method takes a list of index models (given as hash references)
#pod and returns a list of index names created.  It will throw an exception
#pod on error.
#pod
#pod Each index module is described by the following fields:
#pod
#pod =for :list
#pod * C<keys> (required) — an index specification as an ordered document (array
#pod   reference, L<Tie::IxHash> object, or single-key hash reference)
#pod   with index keys and direction/type.  See below for more.
#pod * C<options> — an optional hash reference of index options.
#pod
#pod The C<keys> document needs to be ordered.  You are B<STRONGLY> encouraged
#pod to get in the habit of specifying index keys with an array reference.
#pod Because Perl randomizes the order of hash keys, you may B<ONLY> use a hash
#pod reference if it contains a single key.
#pod
#pod The form of the C<keys> document differs based on the type of index (e.g.
#pod single-key, multi-key, text, geospatial, etc.).
#pod
#pod For single and multi-key indexes, the value is "1" for an ascending index
#pod and "-1" for a descending index.
#pod
#pod     [ name => 1, votes => -1 ] # ascending on name, descending on votes
#pod
#pod See L<Index Types|http://docs.mongodb.org/manual/core/index-types/> in the
#pod MongoDB Manual for instructions for other index types.
#pod
#pod The C<options> hash reference may have a mix of general-purpose and
#pod index-type-specific options.  See L<Index
#pod Options|http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#options>
#pod in the MongoDB Manual for specifics.
#pod
#pod Some of the more commonly used options include:
#pod
#pod =for :list
#pod * C<background> — when true, index creation won't block but will run in the
#pod   background; this is strongly recommended to avoid blocking other
#pod   operations on the database.
#pod * C<unique> — enforce uniqueness when true; inserting a duplicate document
#pod   (or creating one with update modifiers) will raise an error.
#pod * C<name> — a name (string) for the index; one will be generated if this is
#pod   omitted.
#pod
#pod =cut

my $create_many_args;

sub create_many {
    my ( $self, @models ) = @_;

    MongoDB::UsageError->throw("Argument to create_many must be a list of index models")
      unless is_IndexModelList(\@models);

    my $indexes = [ map __flatten_index_model($_), @models ];
    my $op = MongoDB::Op::_CreateIndexes->_new(
        db_name       => $self->_db_name,
        coll_name     => $self->_coll_name,
        bson_codec    => $self->_bson_codec,
        indexes       => $indexes,
        write_concern => MongoDB::WriteConcern->new,
    );

    # succeed or die; we don't care about response document
    $self->_client->send_write_op($op);

    return map $_->{name}, @$indexes;
}

#pod =method drop_one
#pod
#pod     $output = $indexes->drop_one( $name );
#pod
#pod This method takes the name of an index and drops it.  It returns the output
#pod of the dropIndexes command (a hash reference) on success or throws a
#pod exception if the command fails.
#pod
#pod =cut

my $drop_one_args;

sub drop_one {
    my ( $self, $name ) = @_;

    MongoDB::UsageError->throw("Argument to drop_one must be a string")
      unless is_Str($name);

    if ( $name eq '*' ) {
        MongoDB::UsageError->throw("Can't use '*' as an argument to drop_one");
    }

    return $self->collection->_run_command(
        [
            dropIndexes => $self->_coll_name,
            index       => $name,
        ]
    );
}

#pod =method drop_all
#pod
#pod     $output = $indexes->drop_all;
#pod
#pod This method drops all indexes (except the one on the C<_id> field).  It
#pod returns the output of the dropIndexes command (a hash reference) on success
#pod or throws a exception if the command fails.
#pod
#pod =cut

my $drop_all_args;

sub drop_all {
    my ($self) = @_;

    return $self->collection->_run_command(
        [
            dropIndexes => $self->_coll_name,
            index       => '*',
        ]
    );
}

#--------------------------------------------------------------------------#
# private functions
#--------------------------------------------------------------------------#

sub __flatten_index_model {
    my ($model) = @_;

    my ( $keys, $orig ) = @{$model}{qw/keys options/};

    $keys = IxHash->coerce($keys);

    # copy the original so we don't modify it
    my $opts = { $orig ? %$orig : () };

    for my $k (qw/keys key/) {
        MongoDB::UsageError->throw("Can't specify '$k' in options to index creation")
          if exists $opts->{$k};
    }

    # add name if not provided
    $opts->{name} = __to_index_string($keys)
      unless defined $opts->{name};

    # convert some things to booleans
    for my $k (qw/unique background sparse dropDups/) {
        next unless exists $opts->{$k};
        $opts->{$k} = boolean( $opts->{$k} );
    }

    # return is document ready for the createIndexes command
    return { key => $keys, %$opts };
}

# utility function to generate an index name by concatenating key/value pairs
sub __to_index_string {
    my $keys = shift;

    if ( ref $keys eq 'Tie::IxHash' ) {
        my @name;
        my @ks = $keys->Keys;
        my @vs = $keys->Values;

        for ( my $i = 0; $i < $keys->Length; $i++ ) {
            push @name, $ks[$i];
            push @name, $vs[$i];
        }

        return join( "_", @name );
    }
    else {
        MongoDB::InternalError->throw("expected Tie::IxHash for __to_index_string");
    }

}


1;


# vim: set ts=4 sts=4 sw=4 et tw=75:

__END__

=pod

=encoding UTF-8

=head1 NAME

MongoDB::IndexView - Index management for a collection

=head1 VERSION

version v1.2.2

=head1 SYNOPSIS

    my $indexes = $collection->indexes;

    # listing indexes

    @names = map { $_->{name} } $indexes->list->all;

    my $result = $indexes->list;

    while ( my $index_doc = $result->next ) {
        # do stuff with each $index_doc
    }

    # creating indexes

    $name = $indexes->create_one( [ x => 1, y => -1 ], { unique => 1 } );

    @names = $indexes->create_many(
        { keys => [ x => 1, y => -1 ], options => { unique => 1 } },
        { keys => [ z => 1 ] },
    );

    # dropping indexes

    $indexes->drop_one( "x_1_y_-1" );

    $indexes->drop_all;

=head1 DESCRIPTION

This class models the indexes on a L<MongoDB::Collection> so you can
create, list or drop them.

For more on MongoDB indexes, see the L<MongoDB Manual pages on
indexing|http://docs.mongodb.org/manual/core/indexes/>

=head1 ATTRIBUTES

=head2 collection

The L<MongoDB::Collection> for which indexes are being created or viewed.

=head1 METHODS

=head2 list

    $result = $indexes->list;

    while ( my $index = $result->next ) {
        ...
    }

    for my $index ( $result->all ) {
        ...
    }

This method returns a L<MongoDB::QueryResult> which can be used to
retrieve index information either one at a time (with C<next>) or
all at once (with C<all>).

If the list can't be retrieved, an exception will be thrown.

=head2 create_one

    $name = $indexes->create_one( [ x => 1 ] );
    $name = $indexes->create_one( [ x => 1, y => 1 ] );
    $name = $indexes->create_one( [ z => 1 ], { unique => 1 } );

This method takes an ordered index specification document and an optional
hash reference of index options and returns the name of the index created.
It will throw an exception on error.

The index specification document is an ordered document (array reference,
L<Tie::IxHash> object, or single-key hash reference) with index keys and
direction/type.

See L</create_many> for important information about index specifications
and options.

=head2 create_many

    @names = $indexes->create_many(
        { keys => [ x => 1, y => 1 ] },
        { keys => [ z => 1 ], options => { unique => 1 } }
    );

This method takes a list of index models (given as hash references)
and returns a list of index names created.  It will throw an exception
on error.

Each index module is described by the following fields:

=over 4

=item *

C<keys> (required) — an index specification as an ordered document (array reference, L<Tie::IxHash> object, or single-key hash reference) with index keys and direction/type.  See below for more.

=item *

C<options> — an optional hash reference of index options.

=back

The C<keys> document needs to be ordered.  You are B<STRONGLY> encouraged
to get in the habit of specifying index keys with an array reference.
Because Perl randomizes the order of hash keys, you may B<ONLY> use a hash
reference if it contains a single key.

The form of the C<keys> document differs based on the type of index (e.g.
single-key, multi-key, text, geospatial, etc.).

For single and multi-key indexes, the value is "1" for an ascending index
and "-1" for a descending index.

    [ name => 1, votes => -1 ] # ascending on name, descending on votes

See L<Index Types|http://docs.mongodb.org/manual/core/index-types/> in the
MongoDB Manual for instructions for other index types.

The C<options> hash reference may have a mix of general-purpose and
index-type-specific options.  See L<Index
Options|http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#options>
in the MongoDB Manual for specifics.

Some of the more commonly used options include:

=over 4

=item *

C<background> — when true, index creation won't block but will run in the background; this is strongly recommended to avoid blocking other operations on the database.

=item *

C<unique> — enforce uniqueness when true; inserting a duplicate document (or creating one with update modifiers) will raise an error.

=item *

C<name> — a name (string) for the index; one will be generated if this is omitted.

=back

=head2 drop_one

    $output = $indexes->drop_one( $name );

This method takes the name of an index and drops it.  It returns the output
of the dropIndexes command (a hash reference) on success or throws a
exception if the command fails.

=head2 drop_all

    $output = $indexes->drop_all;

This method drops all indexes (except the one on the C<_id> field).  It
returns the output of the dropIndexes command (a hash reference) on success
or throws a exception if the command fails.

=head1 AUTHORS

=over 4

=item *

David Golden <david@mongodb.com>

=item *

Mike Friedman <friedo@friedo.com>

=item *

Kristina Chodorow <k.chodorow@gmail.com>

=item *

Florian Ragwitz <rafl@debian.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2016 by MongoDB, Inc..

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut