File: db_insert.rules

package info (click to toggle)
polymake 4.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,288 kB
  • sloc: cpp: 156,233; perl: 42,962; javascript: 30,726; ansic: 2,907; java: 2,654; python: 641; sh: 244; xml: 117; makefile: 61
file content (148 lines) | stat: -rw-r--r-- 5,438 bytes parent folder | download
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
#  Copyright (c) 1997-2021
#  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 = (
   # 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 verbose if true provides some status output for the insert
   verbose => false
);

# @category Database Collection Administration
# Additional options if objects are added one by one
options %insert_one_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 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 noinsert if true the object is only prepared according to the schema, 
   # but not written into the database
   # useful for testing
   noinsert => false 
);

sub prepare_schema_and_info {
   my ($self, $options) = @_;

   $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};
   }
}

# @category Database Collection Administration
# Insert one or more objects into a given collection
# @param Core::BigObject one or more objects to be inserted 
# @options %insert_one_options
# @return Int the number of objects written
user_method insert(Core::BigObject+, %insert_one_options) {
   my ($self, $obj, $options) = @_;

   croak "no object given\n" if scalar(@$obj) == 0;

   prepare_schema_and_info($self,$options);
   
   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
# Insert array of objects into a given collection, basic operation, use only for new collections
# @param Array objects to be inserted 
# @options %insert_options
user_method insert_array(Core::BigObject+, %insert_options) {
   my ($self, $obj, $options) = @_;

   croak "no object given\n" if scalar(@$obj) == 0;
   prepare_schema_and_info($self,$options);
   $self->insert_array_impl($obj, $options);
}


# @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: