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
|
# 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 MongoDB;
use MongoDB::OID;
use PolyDB;
use namespaces qw(+ PolyDB);
CREDIT polyDB
# @topic property_types/MongoClient
# @category Database
# A handler for the polyDB database internally controlling the connection to the MongoDB database
package PolyDB::default;
# @category Database
# polyDB: The hostname of the database, default 'polymake.org'
custom $db_host = "db.polymake.org";
#custom $db_host = "130.83.68.137";
# polyDB: The port of the database, default 27017
custom $db_port = "27027";
# polyDB: The database username, default 'polymake'
custom $db_user = "polymake";
# polyDB: The database password, default 'database'
custom $db_pwd = "database";
# polyDB: The name of the authentication database, default 'admin'
custom $db_auth_db = "admin";
# polyDB: The name of the type database, default 'pm'
custom $db_type_db = "pm";
# polyDB: The name of the info database, default 'pm'
custom $db_info_db_old = "pm";
# polyDB: The name of the info database, default 'pm'
custom $db_info_db = "polyDB";
# polyDB: The name of the type_information collection, default 'type_information'
custom $db_type_information = "type_information";
# polyDB: The name of the Info collection, default 'Info'
custom $db_info_collection = "Info";
# polyDB: The name of the database you want to access. Set this if you want to access a certain database all (or most of) the time.
custom $db_database_name = "";
# polyDB: The name of the collection you want to access. Set this if you want to access a certain collection all (or most of) the time.
custom $db_collection_name = "";
# polyDB: The timeout for waiting to get a socket connection to the db server
custom $db_socket_timeout = 120000;
package application;
# @category Database
# Returns all objects in the database //db// in //collection// that match the //query//. This is only recommended for a reasonably small number of matching objects. If you expect many such objects you should instead use a database cursor.
# @param HASH query
# @option String db name of the database, see [[http://www.polymake.org/doku.php/data|here]] or [[db_info]] for available databases
# @option String collection name of the collection, see [[http://www.polymake.org/doku.php/data|here]] or [[db_info]] for available collections
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option Int limit limit the number of objects that will be returned (default: no limit)
# @option String username Some databases might have access control.
# @option String password Some databases might have access control.
# @option MongoClient client
# @option Int skip skip the first elements, default: 0
# @option HASH sort_by specify a sorting order
# @return Array<Core::Object>
user_function db_query ($, \
{db => $PolyDB::default::db_database_name, \
collection => $PolyDB::default::db_collection_name, \
representative=>0, \
distinct=>undef, \
local => 0, \
limit => 0, \
username => $PolyDB::default::db_user, \
password => $PolyDB::default::db_pwd, \
client => undef, \
skip => 0, \
sort_by => {"_id" => 1}}) \
{
my ($query, $options) = @_;
Client::check_options($options);
my ($client, $collection) = Client::prepare_query({ db=>$options->{"db"},
collection=>$options->{"collection"},
client=>$options->{"client"} });
my $output;
my $construct_object_function;
if ( $options->{'representative'} ) {
$output = $collection->find_one($query)
// die "No such object in the database.\n";
if ( exists($output->{'polyDB'}->{'type_information_key'}) ) {
my $type_information_key = $output->{'polyDB'}->{'type_information_key'};
my $ti_query = {db => $options->{'db'},
collection => $options->{'collection'},
local => $options->{local},
client => $client,
type_information_key => $type_information_key};
my $type_information = db_get_type_information($ti_query)
// die "Type information not found\n";
if ( defined($type_information->{'construct_object'} ) ) {
$construct_object_function = $type_information->{'construct_object'};
}
}
} elsif( $options->{'distinct'}) {
$output = $client->get_database($options->{'db'})->run_command([
"distinct" => $options->{'collection'},
"key" => $options->{'distinct'},
"query" => $query
]);
} else {
unless ($collection->count($query)) { die "No such object in the database.\n"; }
# get type information keys
my $keys = $client->get_database($options->{'db'})->run_command([
"distinct" => $options->{'collection'},
"key" => "polyDB.type_information_key",
"query" => $query,
"limit" => $options->{'limit'},
"skip" => $options->{'skip'}
]);
$construct_object_function = {};
foreach my $key (@{$keys->{'values'}}) {
my $ti_query = {db => $options->{'db'},
collection => $options->{'collection'},
local => $options->{local},
client => $client,
type_information_key => $key};
my $type_information = db_get_type_information($ti_query)
// die "Type information for key $key not found\n";
if ( defined($type_information->{'construct_object'} ) ) {
$construct_object_function->{$key} = $type_information->{'construct_object'};
}
}
$output = $collection->find($query)->limit($options->{limit})->sort($options->{sort_by})->skip($options->{skip});
}
unless (defined($options->{client})){
undef $client;
}
if ( $options->{'representative'}) {
return PolymakeJsonConversion::perl2polymake($output, $options->{db}, $options->{collection}, $construct_object_function);
} elsif($options->{'distinct'}) {
return $output->{'values'};
} else {
return PolymakeJsonConversion::cursor2ObjectArray($output, $options->{db}, $options->{collection}, $construct_object_function);
}
}
# @category Database
# Returns the number of objects in the database //db// in //collection// that match the //query//.
# @param HASH query
# @option String db name of the database, see [[http://www.polymake.org/doku.php/data|here]] or [[db_info]] for available databases
# @option String collection name of the collection, see [[http://www.polymake.org/doku.php/data|here]] or [[db_info]] for available collections
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String username Some databases might have access control.
# @option String password Some databases might have access control.
# @option MongoClient client
# @return Int
user_function db_count($, \
{db => $PolyDB::default::db_database_name, \
collection => $PolyDB::default::db_collection_name, \
distinct=>undef, \
local => 0, \
username => $PolyDB::default::db_user, \
password => $PolyDB::default::db_pwd, \
client => undef}) \
{
my ($query, $options) = @_;
Client::check_options($options);
my ($client, $collection) = Client::prepare_query({ db=>$options->{"db"},
collection=>$options->{"collection"},
client=>$options->{"client"} });
my $output;
if ( $options->{'distinct'} ) {
$output = $client->get_database($options->{'db'})->run_command([
"distinct" => $options->{'collection'},
"key" => $options->{'distinct'},
"query" => $query
])
// die "No such object.\n";
} else {
$output = $collection->count($query);
}
unless (defined($options->{client})){
undef $client;
}
if ( $options->{'distinct'}) {
return scalar(@{$output->{'values'}});
} else {
return $output;
}
}
# @category Database
# Returns the IDs of all objects in the database //db// in //collection// that match the //query//. This is only recommended for a reasonably small number of matching objects. If you expect many such objects you should instead construct a [[DBCursor]].
# @param HASH query
# @option String db name of the database, see [[http://www.polymake.org/doku.php/data|here]] or [[db_info]] for available databases
# @option String collection name of the collection, see [[http://www.polymake.org/doku.php/data|here]] or [[db_info]] for available collections
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option Int limit limit the number of objects that will be returned (default: no limit)
# @option HASH sort_by specify a sorting order
# @option Int skip skip the first elements, default: 0
# @option String username Some databases might have access control.
# @option String password Some databases might have access control.
# @option MongoClient client
# @return Array<String>
user_function db_ids ($, \
{db => $PolyDB::default::db_database_name, \
collection => $PolyDB::default::db_collection_name, \
local => 0, \
limit => 0, \
username => $PolyDB::default::db_user, \
password => $PolyDB::default::db_pwd, \
client => undef, \
skip => 0, \
sort_by => {}})\
{
my ($query, $options) = @_;
Client::check_options($options);
my ($client, $collection) = Client::prepare_query({ db=>$options->{"db"},
collection=>$options->{"collection"},
client=>$options->{"client"} });
my $output = $collection->find($query)->fields({'_id'=>1})->limit($options->{limit})->sort($options->{sort_by})->skip($options->{skip})
// die "No such object.\n";
return new Array<String>(PolymakeJsonConversion::cursor2IdArray($output));
}
# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|