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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
# Copyright (c) 1997-2018
# Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
# http://www.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://solros.de
# http://www.mathematik.tu-darmstadt.de/~paffenholz
#
use Term::ANSIColor;
use namespaces qw(+ PolyDB);
CREDIT polyDB
sub db_print_name_with_info {
my ($name) = @_;
my $ret = " ".$name->{name};
if ( defined($name->{affiliation}) ) {
$ret .= ", ".$name->{affiliation};
}
if ( defined($name->{email}) ) {
$ret .= ", ".$name->{email};
}
if ( defined($name->{remark}) ) {
$ret .= " (".$name->{remark}.")";
}
return $ret;
}
sub db_print_list_of_names {
my $names = shift;
my $title = shift;
if ( ref($names) eq "ARRAY" ) {
print $title.":\n";
foreach my $name (@{$names}) {
print db_print_name_with_info($name);
print "\n";
}
} else {
print $title.": ".$names."\n";
}
}
# @category Database
# Print information about available databases and collections.
# @option String db name of the database, default: all available databases
# @option String collection name of the collection, default: all available collections
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String username necessary if reading a database with access control
user_function db_info ({db => undef, collection => undef, local => 0, username => $PolyDB::default::db_user}) {
my $options = shift;
my $client = Client::get_client($options->{local},"","");
my $idb = $client->get_database($PolyDB::default::db_info_db);
my $icol = $idb->get_collection($PolyDB::default::db_info_collection);
my %query;
my %db_query = ('$or' => [{"users"=>$options->{username}}, {"users"=>"polymake"}] );
$db_query{"col"} = "db_info";
if (defined($options->{db})) {
$db_query{"db"} = $options->{db};
}
if (defined($options->{collection})) {
$query{"col"} = $options->{collection};
}
my $dbs = $icol->find(\%db_query)->sort({"db"=>1});
while (my $db_cursor = $dbs->next) {
my $current_db = $db_cursor->{db};
$query{"db"} = $current_db;
my $search = $icol->find(\%query)->sort({"col"=>1});
if (!$search->has_next) {next;}
print colored ['red'], "DATABASE: ".$current_db."\n";
print $db_cursor->{description}."\n\n";
while (my $col_cursor = $search->next) {
if ($col_cursor->{col} eq "db_info") {next;}
print colored ['blue'], "Collection: ".$col_cursor->{col}."\n";
print $col_cursor->{description}."\n\n";
if ( defined ($col_cursor->{author} ) ) {
db_print_list_of_names($col_cursor->{author},"Author(s)");
}
if ( defined ($col_cursor->{contributor} ) ) {
db_print_list_of_names($col_cursor->{contributor},"Contributor(s)");
}
if ( defined ($col_cursor->{maintainer} ) ) {
db_print_list_of_names($col_cursor->{maintainer},"Maintainer(s)");
}
if ( defined($col_cursor->{references}) ) {
foreach my $ref (@{$col_cursor->{references}} ) {
print "Cite: \n";
if ( ref($ref) eq "HASH" ) {
print " ".$ref->{authors}.", ".$ref->{title}.", ".$ref->{bib}."\n";
if ( defined($ref->{links}) ) {
print " obtain at: \n";
foreach my $link (@{$ref->{links}}) {
print " ".$link->{type}.": ".$link->{link}."\n";
}
}
} else {
print " ",$ref."\n";
}
}
}
if ( defined($col_cursor->{webpage}) ) {
foreach my $ref (@{$col_cursor->{webpage}} ) {
print "Online Resources: \n";
if ( ref($ref) eq "HASH" ) {
print " ".$ref->{description}.": ".$ref->{address}."\n";
} else {
print " ",$ref."\n";
}
}
}
print "\n";
}
print "__________________\n\n";
}
undef $client;
}
# @category Database
# Returns a list of available databases and collections (in the form db.collection).
# @option String db name of the database, default: all available databases
# @option String collection name of the collection, default: all available collections
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String username necessary if reading a database with access control
# @return Array
user_function db_get_list_db_col ({db => undef, collection => undef, local => 0, username => $PolyDB::default::db_user}) {
my $options = shift;
my $client = Client::get_client($options->{local},"","");
my @ret;
my $idb = $client->get_database($PolyDB::default::db_info_db);
my $icol = $idb->get_collection($PolyDB::default::db_info_collection);
my %query;
my %db_query = ('$or' => [{"users"=>$options->{username}}, {"users"=>"polymake"}] );
$db_query{"col"} = "db_info";
if (defined($options->{db})) {
$db_query{"db"} = $options->{db};
}
if (defined($options->{collection})) {
$query{"col"} = $options->{collection};
}
my $dbs = $icol->find(\%db_query)->sort({"db"=>1});
while (my $db_cursor = $dbs->next) {
my $current_db = $db_cursor->{db};
$query{"db"} = $current_db;
my $search = $icol->find(\%query)->sort({"col"=>1});
if (!$search->has_next) { print "nothing found\n"; next;}
while (my $col_cursor = $search->next) {
if ($col_cursor->{col} eq "db_info") {next;}
push @ret, $current_db.".".$col_cursor->{col};
}
}
undef $client;
return \@ret;
}
# @category Database
# Returns a list of available collections in a database.
# @option String db name of the database
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String username necessary if reading a database with access control
# @return Array
user_function db_get_list_col_for_db ({db=> undef, local => 0, username => $PolyDB::default::db_user}) {
my ($options) = @_;
if ( !defined($options->{"db"}) ) {
die "db must be given\n";
}
my $client = Client::get_client($options->{local},"","");
my @ret;
my $idb = $client->get_database($PolyDB::default::db_info_db);
my $icol = $idb->get_collection($PolyDB::default::db_info_collection);
my %query;
$query{"db"} = $options->{db};
my $search = $icol->find(\%query)->sort({"col"=>1});
while (my $col_cursor = $search->next) {
if ($col_cursor->{col} eq "db_info") {next;}
push @ret, $col_cursor->{col};
}
undef $client;
return \@ret;
}
# @category Database
# Returns a list of available databases.
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String username necessary if reading a database with access control
# @return Array
user_function db_get_list_db ({local => 0, username => $PolyDB::default::db_user}) {
my $options = shift;
my $client = Client::get_client($options->{local},"","");
my @ret;
my $idb = $client->get_database($PolyDB::default::db_info_db);
my $icol = $idb->get_collection($PolyDB::default::db_info_collection);
my %query;
my %db_query = ('$or' => [{"users"=>$options->{username}}, {"users"=>"polymake"}] );
$db_query{"col"} = "db_info";
my $dbs = $icol->find(\%db_query)->sort({"db"=>1});
while (my $db_cursor = $dbs->next) {
push @ret, $db_cursor->{db};
}
undef $client;
return \@ret;
}
# @category Database Admin
# Add a user to database //db//. Note that this only affects the user's output for [[db_info]], [[db_list]] etc and the tab completion. It does not create the user.
# You need write access for this.
# @param String db name of the database
# @param String user
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String username
# @option String password
function add_user_to_db ($, $, {local => 0, username => $PolyDB::default::db_user, password => $PolyDB::default::db_pwd}){
my ($db, $user, $options) = @_;
my $client = Client::get_client($options);
my $updated_one = 0;
my $idb = $client->get_database($PolyDB::default::db_info_db);
my $icol = $idb->get_collection($PolyDB::default::db_info_collection);
my $search = $icol->find_one({'db'=>$db, 'col'=>"db_info"});
if ($search) {
my $users = $search->{users};
push @$users, $user;
my $output = $icol->update({'db'=>$db, 'col'=>"db_info"}, { '$set' => {'users'=>$users} });
if ($output->{ok}) {
print "Successfully added user $user to database $db.\n";
print "Users with read access: ".join(", ", @{$users})."\n\n";
} else {
print "an error occurred when trying to user $user to database $db\n";
}
$updated_one = 1;
}
if ( !$updated_one ) {
die "There is no type information for database $db\n";
}
}
# @category Database
# Add a db documentation.
# You need write access for this.
# @param String db name of the database the description applies to
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option Bool replace whether an existing documentation should be updated or replaced
# @option String file a file with the documentation in json format, only one of file and documentation is allowed
# @option HASH documentation a perl hash with the documentation, only one of documentation and file is allowed
# @option String username
# @option String password
# @option String polydb_version version number
# @option Bool verbose verbose mode
user_function db_write_db_info ({db=> undef, \
local => 0, \
id => undef, \
replace => undef, \
file => undef, \
documentation => undef, \
username => undef, \
password => undef, \
polydb_version => undef, \
verbose => 0 } ) \
{
my ($options) = @_;
my $min_version = "2.0"; # this function is for the new format
# read the documentation either from a file or from a perl hash
# exactly one must be given, otherwise we exit
my $documentation;
if ( defined($options->{'file'}) ) {
if ( defined($options->{'documentation'}) ) {
die "only one of the options file and documentation are allowed\n";
}
$documentation = DatabaseHelpers::read_json($options->{'file'});
} elsif ( $options->{'documentation'}) {
$documentation = $options->{'documentation'};
} else {
die "at least one of the options file or documentation must be given\n";
}
# get the version of polyDB the documentation applies to
my $version = $options->{'polydb_version'} // $documentation->{'polydb_version'};
if ( !defined($version) ) {
die "version not defined or different version in options and file\n";
}
if ( !Client::check_polydb_version($version,$min_version) ) {
die "this function is for polydb files of version $min_version and higher\n";
}
# database can be given in two places: either it is contained in the documentation or set via options
# missing database in the documentation is, however, only allowed when we do (partial) updates of already given documentation
my $db = $options->{'db'} // $documentation->{'db'};
if ( !defined($db) ) {
if ( $PolyDB::default::db_database_name ne "" ) {
$db = $PolyDB::default::db_database_name;
} else {
die "missing database in options and documentation\n";
}
}
# now we can connect to the database
# username/password are either taken from options or from the custom variables
my $client = Client::get_client($options);
my $tdb = $client->get_database("$PolyDB::default::db_info_db");
my $tcol = $tdb->get_collection($PolyDB::default::db_info_collection);
# determine whether we update an already existing documentation
# for this we check whether the given db/version combination already exists in the Info db, if so, we extract the id for further use
my $update = 0;
my $id = "";
if ( my $res=$tcol->find_one({'db' => $db, 'version' => $version})) {
$update = 1;
$id = $res->{"_id"};
} else {
if ( $options->{'replace'} ) {
die "option replace given but no existing documentation found\n";
}
if ( defined($documentation->{'_id}'}) ) {
$id = $documentation->{'_id}'};
} else {
$id = $db.".".$version;
}
if (my $res2 = $tcol->find_one({'_id' => $id})) {
die "given doc already exists in database\n";
}
$documentation->{'_id'} = $id;
$documentation->{'db'} = $db;
}
# FIXME do some sanity checks for new documentation
if ( !$update or $options->{'replace'} ) {
if ( !defined($documentation->{'maintainer'})
or !defined($documentation->{'polydb_version'})
or !defined($documentation->{'users'})
or !defined($documentation->{'description'}) ) {
die "new or replacement documentation must define maintainer, description, and version\n";
}
}
# if update then update, otherwise insert
if ($update) {
my $output;
if ( $options->{'replace'} ) {
$output = $tcol->replace_one({'_id' => $id, 'db' => $db, 'version' => $version}, $documentation);
} else {
$output = $tcol->update_one({'_id' => $id, 'db' => $db, 'version' => $version}, {'$set' => $documentation});
}
if ($options->{verbose}) {
if ($output->acknowledged) {
print "successfully updated documentation for $db\n"
} else {
print "an error occurred when trying to update the documentation for $db:\n$output\n";
}
}
} else {
my $output = $tcol->insert_one($documentation);
if ($options->{verbose}) {
if ($output->acknowledged) {
print "successfully saved documentation for $db\n";
} else {
print "an error occurred when trying to save the documentation for $db:\n$output\n";
}
}
}
undef $client;
}
# @category Database
# Add documentation for a collection
# You need write access for this.
# @option String db name of the database the description applies to
# @option String collection name of the collection the description applies to
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option Bool replace whether an existing documentation should be updated or replaced
# @option String file a file with the documentation in json format, only one of file and documentation is allowed
# @option HASH documentation a perl hash with the documentation, only one of documentation and file is allowed
# @option String username
# @option String password
# @option String polydb_version version number
# @option Bool verbose verbose mode
user_function db_write_collection_info ({db=> undef, \
collection => undef, \
local => 0, \
replace => undef, \
file => undef, \
documentation => undef, \
username => undef, \
password => undef, \
polydb_version => undef, \
verbose => 0 } ) \
{
my ($options) = @_;
my $min_version = "2.0"; # this function is for the new format
# read the documentation either from a file or from a perl hash
# exactly one must be given, otherwise we exit
my $documentation;
if ( defined($options->{'file'}) ) {
if ( defined($options->{'documentation'}) ) {
die "only one of the options file and documentation are allowed\n";
}
$documentation = DatabaseHelpers::read_json($options->{'file'});
} elsif ( $options->{'documentation'}) {
$documentation = $options->{'documentation'};
} else {
die "at least one of the options file or documentation must be given\n";
}
# get the version of polyDB the documentation applies to
my $version = $options->{'polydb_version'} // $documentation->{'polydb_version'};
if ( !defined($version) ) {
die "version not defined or different versions in options and file\n";
}
if ( !Client::check_polydb_version($version,$min_version) ) {
die "this function is for polydb files of version $min_version and higher";
}
# database/collection can be given in two places: either it is contained in the type_information or set via options
# missing databse/collection in the documentation is, however, only allowed when we do (partial) updates of already given documentation
my $db = $options->{'db'} // $documentation->{'db'};
if ( !defined($db) ) {
if ( $PolyDB::default::db_database_name ne "" ) {
$db = $PolyDB::default::db_database_name;
} else {
die "missing database in options and documentation\n";
}
}
my $collection = $options->{'col'} // $documentation->{'col'};
if ( !defined($collection) ) {
if ( $PolyDB::default::db_collection_name ne "" ) {
$collection = $PolyDB::default::db_collection_name;
} else {
die "missing collection in options and documentation\n";
}
}
# now we can connect to the database
# username/password are either taken from options or from the custom variables
my $client = Client::get_client($options);
my $tdb = $client->get_database("$PolyDB::default::db_info_db");
my $tcol = $tdb->get_collection($PolyDB::default::db_info_collection);
# determine whether we update an already existing documentation
# for this we check whether the given db/collection/version combination already exists in the Info db, if so, we extract the id for further use
my $update = 0;
my $id = "";
if ( my $res=$tcol->find_one({'db' => $db, 'col' => $collection, 'version' => $version})) {
$update = 1;
$id = $res->{"_id"};
} else {
if ( $options->{'replace'} ) {
die "option replace given but no existing documentation found\n";
}
if ( defined($documentation->{'_id}'}) ) {
$id = $documentation->{'_id}'};
} else {
$id = $db.".".$collection.".".$version;
}
if (my $res2 = $tcol->find_one({'_id' => $id})) {
die "given doc already exists in database\n";
}
$documentation->{'_id'} = $id;
$documentation->{'db'} = $db;
$documentation->{'col'} = $collection;
}
# FIXME do some sanity checks for new documentation
if ( !$update or $options->{'replace'} ) {
if ( !defined($documentation->{'author'})
or !defined($documentation->{'contributor'})
or !defined($documentation->{'maintainer'})
or !defined($documentation->{'db'})
or !defined($documentation->{'col'})
or !defined($documentation->{'polydb_version'})
or !defined($documentation->{'description'}) ) {
die"new or replacement documentation must define author, maintainer, contributor, db, collection, description, and version\n";
}
}
# if update then update, otherwise insert
if ($update) {
my $output;
if ( $options->{'replace'} ) {
$output = $tcol->replace_one({'_id' => $id, 'db' => $db, 'col' => $collection, 'version' => $version}, $documentation);
} else {
$output = $tcol->update_one({'_id' => $id, 'db' => $db, 'col' => $collection, 'version' => $version}, {'$set' => $documentation});
}
if ($options->{verbose}) {
if ($output->acknowledged) {
print "successfully updated documentation for $db.$collection\n"
} else {
print "an error occurred when trying to update the documentation for $db.$collection:\n$output\n";
}
}
} else {
my $output = $tcol->insert_one($documentation);
if ($options->{verbose}) {
if ($output->acknowledged) {
print "successfully saved documentation for $db.$collection\n";
} else {
print "an error occurred when trying to save the documentation for $db.$collection:\n$output\n";
}
}
}
undef $client;
}
# @category Database
# Retrieve the metadata of an object
# @param Object p the polyDB object
# @return HASH metadata the metadata of the database object
user_function db_metadata ($ ) {
my ($p) = @_;
return $p->get_attachment("polyDB") // die "object contains no polyDB metadata\n";
}
# @category Database
# print the metadata of an object
# @param Object p the polyDB object
user_function db_print_metadata ($ ) {
my ($p) = @_;
my $metadata = db_metadata($p);
foreach (keys %$metadata) {
print colored['red'], $_;
print ": ", $metadata->{$_}, "\n";
}
}
# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|