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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
|
//# tMemoryStMan.cc: Test program for the MemoryStMan storage manager
//# Copyright (C) 2003,2004
//# Associated Universities, Inc. Washington DC, USA.
//#
//# 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 of the License, or (at your option)
//# any later version.
//#
//# 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.
//#
//# You should have received a copy of the GNU General Public License along
//# with this program; if not, write to the Free Software Foundation, Inc.,
//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: aips2-request@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
//#
//# $Id$
#include <casacore/tables/Tables/TableDesc.h>
#include <casacore/tables/Tables/SetupNewTab.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/ScaColDesc.h>
#include <casacore/tables/Tables/ArrColDesc.h>
#include <casacore/tables/Tables/ScalarColumn.h>
#include <casacore/tables/Tables/ArrayColumn.h>
#include <casacore/tables/DataMan/MemoryStMan.h>
#include <casacore/casa/BasicSL/Complex.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/IO/ArrayIO.h>
#include <casacore/casa/Arrays/Cube.h>
#include <casacore/casa/Arrays/ArrayMath.h>
#include <casacore/casa/Arrays/ArrayLogical.h>
#include <casacore/casa/IO/ArrayIO.h>
#include <casacore/casa/Containers/Record.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Exceptions/Error.h>
#include <casacore/casa/iostream.h>
#include <casacore/casa/namespace.h>
// <summary>
// Test program for the MemoryStMan storage manager
// </summary>
// This program tests the MemoryStMan storage manager, especially the
// get and put functions.
// The results are written to stdout. The script executing this program,
// compares the results with the reference output file.
// Have a global area to keep the data to be inserted when
// the table is reopened.
Record theData;
// (re) open and write a few rows
// aMode == 0 open new
// aMode == 1 reopen existing
void init (uInt aMode);
// reopen table, and throw away a row
void deleteRow(const uInt aRow);
// reopen table, and throw away a few rows
void deleteRows(const Vector<rownr_t>& aNrRows);
// delete a Column
void deleteColumn(const String& aColumn);
// delete a column && put it back again
void deleteAndRestore();
// add aColumn
void addColumn(DataType aDataType);
//add a few direct arrays
void addDirectArrays();
//add an indirect string array
void addIndStringArray();
//add an indirect array
void addIndArray();
// show table info
void info(const Table aTable);
void saveData(const Table aTable);
void restoreData(const Table aTable);
// put/putColumn cache test
void putColumnTest();
int main ()
{
try {
init (0);
init (1);
deleteRow (0);
deleteRow (17);
// delete and restore Column 1 (should use perfect fit)
deleteAndRestore();
//
putColumnTest();
// delete middle Column
deleteColumn ("Col-2");
// add a Bool Column Should fit in freed space
addColumn (TpBool);
// add a DComplex Column Should use new index && space
addColumn (TpDComplex);
// delete first Column
deleteColumn ("Col-1");
// delete last Column
deleteColumn ("Col-3");
addDirectArrays ();
addIndStringArray();
addIndArray ();
Vector<rownr_t> aNrRows(3);
for (uInt i=0; i< 3; i++) {
aNrRows(i) = i+3;
}
deleteRows (aNrRows);
deleteColumn ("Col-7");
addColumn(TpString);
// remove all remaining rows to check freebucket performance
Vector<rownr_t> aNewNrRows(15);
for (uInt i=0; i< 15; i++) {
aNewNrRows(i) = i;
}
deleteRows (aNewNrRows);
} catch (std::exception& x) {
cout << "Caught an exception: " << x.what() << endl;
return 1;
}
return 0; // exit with success status
}
void initArrays(Cube<Float>& arrf, Vector<DComplex>& arrdc, Cube<Bool>& arrb)
{
// The static_cast is a workaround for an SGI compiler bug
indgen (static_cast< Cube<Float> &>(arrf));
arrdc(0) = DComplex(1.2, 3.4);
arrdc(1) = DComplex(-2.3, 5.6);
IPosition shape(arrb.shape());
uInt n = 0;
for (Int i=0; i<shape(2); i++) {
for (Int j=0; j<shape(1); j++) {
for (Int k=0; k<shape(0); k++) {
if (n++%3 == 2) {
arrb(k,j,i) = True;
}else{
arrb(k,j,i) = False;
}
}
}
}
}
void info( const Table aTable)
{
TableDesc tdesc = aTable.tableDesc();
for (uInt i=0; i<tdesc.ncolumn(); i++) {
const ColumnDesc& cdesc = tdesc.columnDesc(i);
cout << cdesc.name() << ": "
<< cdesc.dataType() << endl;
switch (cdesc.dataType()) {
case TpInt:
if (cdesc.isArray()) {
ArrayColumn<Int> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
} else {
ScalarColumn<Int> aa(aTable,cdesc.name());
cout << aa.getColumn() << endl;
}
break;
case TpBool:
if (cdesc.isArray()) {
ArrayColumn<Bool> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
} else {
ScalarColumn<Bool> ab(aTable,cdesc.name());
cout << ab.getColumn() << endl;
}
break;
case TpDComplex:
if (cdesc.isArray()) {
ArrayColumn<DComplex> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
} else {
ScalarColumn<DComplex> ac(aTable,cdesc.name());
cout << ac.getColumn() << endl;
}
break;
case TpFloat:
if (cdesc.isArray()) {
ArrayColumn<float> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
} else {
ScalarColumn<float> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
}
break;
case TpString:
if (cdesc.isArray()) {
ArrayColumn<String> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
} else {
ScalarColumn<String> ad(aTable,cdesc.name());
cout << ad.getColumn() << endl;
}
break;
default:
cout << "Sorry, datatype not implemented yet." << endl;
}
}
}
void saveData( const Table aTable)
{
theData = Record();
TableDesc tdesc = aTable.tableDesc();
for (uInt i=0; i<tdesc.ncolumn(); i++) {
const ColumnDesc& cdesc = tdesc.columnDesc(i);
if (cdesc.dataType() == TpInt) {
if (cdesc.isArray()) {
ArrayColumn<Int> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
} else {
ScalarColumn<Int> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
}
} else if (cdesc.dataType() == TpBool) {
if (cdesc.isArray()) {
ArrayColumn<Bool> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
} else {
ScalarColumn<Bool> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
}
} else if (cdesc.dataType() == TpDComplex) {
if (cdesc.isArray()) {
ArrayColumn<DComplex> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
} else {
ScalarColumn<DComplex> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
}
} else if (cdesc.dataType() == TpFloat) {
if (cdesc.isArray()) {
ArrayColumn<Float> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
} else {
ScalarColumn<Float> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
}
} else if (cdesc.dataType() == TpString) {
if (cdesc.isArray()) {
ArrayColumn<String> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
} else {
ScalarColumn<String> col(aTable, cdesc.name());
theData.define (cdesc.name(), col.getColumn());
}
} else {
cout << "Sorry, datatype not implemented yet." << endl;
}
}
}
void restoreData(const Table aTable)
{
for (uInt i=0; i<theData.nfields(); i++) {
String name = theData.name(i);
switch (theData.dataType(i)) {
case TpArrayInt:
{
const Array<Int>& arr = theData.asArrayInt(i);
if (arr.ndim() > 1) {
ArrayColumn<Int> col(aTable, name);
col.putColumn (arr);
} else {
ScalarColumn<Int> col(aTable, name);
col.putColumn (arr);
}
break;
}
case TpArrayBool:
{
const Array<Bool>& arr = theData.asArrayBool(i);
if (arr.ndim() > 1) {
ArrayColumn<Bool> col(aTable, name);
col.putColumn (arr);
} else {
ScalarColumn<Bool> col(aTable, name);
col.putColumn (arr);
}
break;
}
case TpArrayDComplex:
{
const Array<DComplex>& arr = theData.asArrayDComplex(i);
if (arr.ndim() > 1) {
ArrayColumn<DComplex> col(aTable, name);
col.putColumn (arr);
} else {
ScalarColumn<DComplex> col(aTable, name);
col.putColumn (arr);
}
break;
}
case TpArrayFloat:
{
const Array<Float>& arr = theData.asArrayFloat(i);
if (arr.ndim() > 1) {
ArrayColumn<Float> col(aTable, name);
col.putColumn (arr);
} else {
ScalarColumn<Float> col(aTable, name);
col.putColumn (arr);
}
break;
}
case TpArrayString:
{
const Array<String>& arr = theData.asArrayString(i);
if (arr.ndim() > 1) {
ArrayColumn<String> col(aTable, name);
col.putColumn (arr);
} else {
ScalarColumn<String> col(aTable, name);
col.putColumn (arr);
}
break;
}
default:
cout << "Sorry, datatype not implemented yet." << endl;
}
}
}
// First build a description.
void init (uInt aMode)
{
Table aTable;
if (aMode == 0) {
DataManager::registerCtor ("MemoryStMan",
MemoryStMan::makeObject);
// Build the table description.
TableDesc td("", "1", TableDesc::Scratch);
td.comment() = "A test of class TableDesc";
td.addColumn (ScalarColumnDesc<DComplex>("Col-1"));
td.addColumn (ScalarColumnDesc<Int>("Col-2"));
td.addColumn (ScalarColumnDesc<Bool>("Col-3"));
// Now create a new table from the description.
SetupNewTable aNewTab("tMemoryStMan_tmp.data", td, Table::New);
// Create a storage manager for it.
MemoryStMan aSm1 ("MSM");
aNewTab.bindAll (aSm1);
aTable = Table (aNewTab, 10);
} else {
aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
}
ScalarColumn<DComplex> aa(aTable,"Col-1");
ScalarColumn<Int> ab(aTable,"Col-2");
ScalarColumn<Bool> ac(aTable,"Col-3");
// fill columns with data
uInt i;
uInt j = 0;
for (i=0; i<10; i++) {
if (aMode == 1) {
aTable.addRow();
j=10;
}
DComplex a(i+j,(i+j)*2);
aa.put(i+j,a);
ab.put(i+j,i+j);
Bool b;
if ((i+j)%2 == 0) {
b=True;
} else {
b=False;
}
ac.put(i+j,b);
}
if (aMode == 0) {
cout << "after creation" << endl;
} else {
cout << "after reopening and adding 10 rows" << endl;
}
// Print column data
info(aTable);
saveData(aTable);
}
void deleteRow(const uInt aRow)
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ScalarColumn<DComplex> aa(aTable,"Col-1");
// Make sure ColumnCache is filled.
aa(0);
aTable.removeRow(aRow);
cout << "after removing row: " << aRow << endl;
// Print column data
info(aTable);
saveData(aTable);
}
void deleteRows(const Vector<rownr_t>& aNrRows)
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ScalarColumn<DComplex> ae(aTable,"Col-5");
// Make sure ColumnCache is filled.
ae(0);
aTable.removeRow(aNrRows);
cout << "after removing several rows at once: " << endl;
// Print column data
info(aTable);
saveData(aTable);
}
void deleteColumn(const String& aColumn)
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
cout << "Try to remove Column:" << aColumn << endl;
aTable.removeColumn(aColumn);
cout << "After removing Column: " << aColumn << endl;
// Print column data
info(aTable);
saveData(aTable);
}
void deleteAndRestore()
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ScalarColumn<DComplex> ac(aTable,"Col-1");
cout << "Try to remove Column 1 after saving the contents" << endl;
Vector<DComplex> save = ac.getColumn();
aTable.removeColumn("Col-1");
cout << "After removing Column 1" << endl;
// Print column data
info(aTable);
cout << "Try to restore Column 1" << endl;
aTable.addColumn(ScalarColumnDesc<DComplex>("Col-1"));
if (aTable.tableDesc().isColumn("Col-1")) {
ac.attach(aTable,"Col-1");
}
// refill again
ac.putColumn(save);
// Print column data
cout << "After restoring Column1:" << endl;
info(aTable);
saveData(aTable);
}
void addColumn(DataType aDataType)
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ScalarColumn<Bool> ad;
ScalarColumn<DComplex> ae;
ScalarColumn<String> aj;
switch (aDataType) {
case TpBool:
cout << "Try to add Column: Col-4 and fill it. "<< endl <<
"It Should be using space just freed up" << endl;
aTable.addColumn(ScalarColumnDesc<Bool>("Col-4"));
if (aTable.tableDesc().isColumn("Col-4")) {
ad.attach(aTable,"Col-4");
}
// fill new column with data
uInt i;
Bool b;
for (i=0; i<aTable.nrow(); i++) {
if (i < 10) {
b=True;
} else {
b=False;
}
ad.put(i,b);
}
break;
case TpDComplex:
cout << "Try to add Column: Col-5 and fill it. "<< endl <<
"It should make a new index because there's not enough free space" << endl;
aTable.addColumn(ScalarColumnDesc<DComplex>("Col-5"));
if (aTable.tableDesc().isColumn("Col-5")) {
ae.attach(aTable,"Col-5");
}
// fill new column with data
for (uInt i=0; i<aTable.nrow(); i++) {
DComplex a(aTable.nrow()-i,i);
ae.put(i,a);
}
break;
case TpString:
{
cout << "Try to add a string Column: Col-10 and fill it. "<< endl;
aTable.addColumn(ScalarColumnDesc<String>("Col-10"));
if (aTable.tableDesc().isColumn("Col-10")) {
aj.attach(aTable,"Col-10");
}
String aString("String-1");
// fill new column with data
for (uInt i=0; i<aTable.nrow(); i++) {
aj.put(i,aString);
aString += " "+String::toString(i);
}
}
break;
default:
cout << "Not implemented yet" << endl;
break;
}
info(aTable);
saveData(aTable);
}
void addDirectArrays()
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ArrayColumn<float> af;
ArrayColumn<DComplex> ag;
ArrayColumn<Bool> ah;
cout << "Trying to add a few Direct Array Columns." << endl;
aTable.addColumn(ArrayColumnDesc<float>("Col-6", IPosition(3,2,3,1),
ColumnDesc::Direct));
aTable.addColumn(ArrayColumnDesc<DComplex>("Col-7", IPosition(1,2),
ColumnDesc::Direct));
aTable.addColumn(ArrayColumnDesc<Bool>("Col-8", IPosition(3,5,7,1),
ColumnDesc::Direct));
Cube<float> arrf(IPosition(3,2,3,1));
Vector<DComplex> arrdc(2);
Cube<Bool> arrb(IPosition(3,5,7,1));
initArrays (arrf, arrdc, arrb);
if (aTable.tableDesc().isColumn("Col-6")) {
af.attach(aTable,"Col-6");
}
if (aTable.tableDesc().isColumn("Col-7")) {
ag.attach(aTable,"Col-7");
}
if (aTable.tableDesc().isColumn("Col-8")) {
ah.attach(aTable,"Col-8");
}
for (uInt i=0; i<aTable.nrow(); i++) {
af.put(i,arrf);
ag.put(i,arrdc);
ah.put(i,arrb);
arrf += (float)(arrf.nelements());
arrdc += DComplex(2, 3);
}
info(aTable);
saveData(aTable);
}
void addIndStringArray()
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ArrayColumn<String> ai;
cout << "Trying to add an indirect String Array Column." << endl;
aTable.addColumn(ArrayColumnDesc<String>("Col-9"));
Vector<String> arrs(5);
arrs(0)="Start-1";
arrs(1)="Start-2";
arrs(2)="Start-3";
arrs(3)="Start-4";
arrs(4)="Start-5";
if (aTable.tableDesc().isColumn("Col-9")) {
ai.attach(aTable,"Col-9");
}
for (uInt i=0; i<aTable.nrow(); i++) {
ai.put(i,arrs);
for (uInt j=0; j< 5;j++) {
arrs(j) += " "+String::toString(i);
}
}
info(aTable);
saveData(aTable);
}
void addIndArray()
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ArrayColumn<Int> ak;
cout << "Trying to add an indirect Array Column." << endl;
aTable.addColumn(ArrayColumnDesc<Int>("Col-11"));
Vector<Int> arrs(5);
arrs(0)=1;
arrs(1)=2;
arrs(2)=3;
arrs(3)=4;
arrs(4)=5;
if (aTable.tableDesc().isColumn("Col-11")) {
ak.attach(aTable,"Col-11");
}
for (uInt i=0; i<aTable.nrow(); i++) {
ak.put(i,arrs);
for (uInt j=0; j< 5;j++) {
arrs(j) += i;
}
}
info(aTable);
saveData(aTable);
}
void putColumnTest()
{
Table aTable = Table("tMemoryStMan_tmp.data", Table::Update);
restoreData(aTable);
ScalarColumn<Int> ab(aTable,"Col-2");
// put value 3 in rownr 5
ab.put(5,3);
AlwaysAssertExit(ab(5) == 3);
// put value 4 in column
ab.putColumn(ab.getColumn() + 1);
AlwaysAssertExit (ab(5) == 4);
saveData(aTable);
}
|