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