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
|
# Copyright (c) 1997-2020
# Ewgenij Gawrilow, Michael Joswig, and the polymake team
# Technische Universität Berlin, Germany
# https://polymake.org
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version: http://www.gnu.org/licenses/gpl.txt.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------
#
# This file is part of the polymake database interface polyDB.
#
# @author Silke Horn, Andreas Paffenholz
# http://www.mathematik.tu-darmstadt.de/~paffenholz
#
use namespaces qw(+ PolyDB);
CREDIT polyDB
package PolyDB::Collection;
# @category Database Collection Administration
# Common options for database insertions
options %insert_options = (
# String id provide an id for the object to be written.
# Can only be used if a single object is written into the database.
# If not given, or for arrays of objects the name of the object is used as id
id => undef,
# Bool use_schema use the schema for to collection stored in the database
# to project the object so that it contains
# precisely the required properties
use_schema => true,
# HASH schema provide a schema for the object
schema => undef,
# HASH info provide all meta infmation for the object
info => undef,
# String creation_date specify a creation date, can also be set in info, object, or current date will be used
# precedence options > info > object > current date
creation_date => undef,
# String uri A uri with a description of the object and the collection it belongs to, can also be set in object or collection info
# precedence options > info > object
uri => undef,
# String polymake_version The minimal version needed to read the object from the database, can also be set in collection info
# precedence: options > info > current version
polymake_version => undef,
# Bool replace if true, an object with the same id in the database will be overwritten if it exists, otherwise it will be inserted
replace => false,
# Bool verbose if true provides some status output for the insert
verbose => false,
# Bool noinsert if true the object is only prepared according to the schema,
# but not written into the database
# useful for testing
noinsert => false
);
# @category Database Collection Administration
# Insert one or more objects into a given collection
# @param Core::BigObject one or more objects to be inserted
# to be inserted into the database, or a filename containing an object or an array of objects
# @options %insert_options
# @return Int the number of objects written
user_method insert(Core::BigObject+, %insert_options) {
my ($self, $obj, $options) = @_;
croak "no object given\n" if scalar(@$obj) == 0;
$options->{use_schema} = false if defined($options->{schema});
if ($options->{use_schema} ) {
croak "options use_schema and info cannot be used together\n" if defined($options->{info});
croak "options use_schema and schema cannot be used together\n" if defined($options->{schema});
$options->{info} = $self->get_own_info();
$options->{schema} = $self->get_own_schema();
$options->{creation_date} //= $options->{info}->{creation_date};
$options->{uri} //= $options->{info}->{uri};
$options->{polymake_version} //= $options->{info}->{polymake_version};
}
my $count = 0;
if ( scalar(@$obj) > 1 ) {
croak "option id cannot be used for addition of arrays\n" if defined($options->{id});
foreach my $object (@$obj) {
$count += $self->insert_or_replace($object, $options);
}
} else {
$count += $self->insert_or_replace($obj->[0], $options);
}
print "Inserted $count objects.\n\n" if ($options->{verbose});
return $count;
}
# @category Database Collection Administration
# Remove one object from a collection identified by its id
# @param String id the id of the object to remove
user_method remove($) {
my ($collection, $id) = @_;
my $output = $collection->delete_one({'_id' => $id});
return $output->deleted_count;
}
# @category Database Collection Administration
# Remove all objects from a collection
user_method remove_all() {
my ($collection) = @_;
my $output = $collection->delete_many({});
return $output->deleted_count;
}
# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|