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
|
//# SSMStringHandler.cc: Store Strings in the Standard Storage Manager
//# Copyright (C) 2000,2001,2002
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library 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 Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; 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: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#include <casacore/tables/DataMan/SSMStringHandler.h>
#include <casacore/tables/DataMan/SSMBase.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/OS/CanonicalConversion.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Utilities/ValType.h>
#include <casacore/casa/Exceptions/Error.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
SSMStringHandler::SSMStringHandler (SSMBase* aBase):
itsSSMPtr (aBase),
itsCurrentBucket (-1),
itsLength (0),
itsNDeleted (0),
itsUsedLength (0),
itsNextBucket (-1),
itsData (0),
itsIntBuf (0),
isChanged (False),
itsLastBucket (-1)
{
}
SSMStringHandler::~SSMStringHandler()
{
delete [] itsData;
delete [] itsIntBuf;
}
void SSMStringHandler::init()
{
delete [] itsData;
itsData = 0;
delete [] itsIntBuf;
itsIntBuf = 0;
itsIntSize = CanonicalConversion::canonicalSize(&itsUsedLength);
itsStart = 4*itsIntSize;
itsLength = itsSSMPtr->getBucketSize()-itsStart;
itsData = new char[itsLength];
itsIntBuf = new char[itsIntSize];
memset(itsData,0,itsLength);
memset(itsIntBuf,0,itsIntSize);
}
void SSMStringHandler::replace(Int bucketNr, Int offset, Int length,
const String& string)
{
// Check if current bucket is wanted bucket, else get wanted bucket.
if (bucketNr != itsCurrentBucket) {
getBucket(bucketNr);
}
replaceData(offset,string.length(),string.chars());
// remove additional space left (if any)
if (length - string.length() > 0) {
remove (itsCurrentBucket, offset, length-string.length());
}
}
void SSMStringHandler::replace(Int bucketNr, Int offset, Int length,
Int totalLength, const Array<String>& string,
Bool handleShape)
{
const IPosition& aShape = string.shape();
// Check if current bucket is wanted bucket, else get wanted bucket.
if (bucketNr != itsCurrentBucket) {
getBucket(bucketNr);
}
Bool deleteIt;
const String *aString = string.getStorage(deleteIt);
if (handleShape) {
CanonicalConversion::fromLocal (itsIntBuf, (uInt) aShape.nelements());
replaceData (offset,itsIntSize, itsIntBuf);
for (uInt i=0; i< aShape.nelements();i++) {
CanonicalConversion::fromLocal (itsIntBuf, Int(aShape(i)));
replaceData (offset,itsIntSize, itsIntBuf);
}
CanonicalConversion::fromLocal (itsIntBuf,1);
replaceData (offset,itsIntSize, itsIntBuf);
}
for (uInt i=0;i<string.nelements();i++) {
//
// Made it a uInt so the SGI compiler could figure out which overloaded
// function to use, since it seemed confused by string::size_t -> size_t
//
CanonicalConversion::fromLocal (itsIntBuf, uInt(aString[i].length()));
replaceData (offset,itsIntSize, itsIntBuf);
replaceData (offset,aString[i].length(), aString[i].chars());
}
string.freeStorage(aString,deleteIt);
// remove additional space left (if any)
if (length - totalLength > 0) {
remove (itsCurrentBucket, offset, length-totalLength);
}
}
void SSMStringHandler::replace(Int bucketNr, Int offset, Int length,
Int totalLength, const IPosition& aShape)
{
// Check if current bucket is wanted bucket, else get wanted bucket.
if (bucketNr != itsCurrentBucket) {
getBucket(bucketNr);
}
CanonicalConversion::fromLocal (itsIntBuf, uInt(aShape.nelements()));
replaceData (offset,itsIntSize, itsIntBuf);
for (uInt i=0; i< aShape.nelements();i++) {
CanonicalConversion::fromLocal (itsIntBuf, Int(aShape(i)));
replaceData (offset,itsIntSize, itsIntBuf);
}
// write 'empty' flag
CanonicalConversion::fromLocal (itsIntBuf,0);
replaceData (offset,itsIntSize, itsIntBuf);
// remove additional space left (if any)
if (length - totalLength > 0) {
remove (itsCurrentBucket, offset, length-totalLength);
}
}
void SSMStringHandler::replaceData (Int& offset,Int length,
const Char* data)
{
while (length > 0) {
Int nCopy = length;
if (length > itsLength-offset) {
nCopy = itsLength-offset;
}
length -= nCopy;
memcpy (itsData+offset, data, nCopy);
data += nCopy;
offset += nCopy;
isChanged=True;
if (length > 0) {
offset = 0;
getBucket (itsNextBucket);
}
}
}
void SSMStringHandler::put (Int& bucketNr, Int& offset, Int& length,
const String& string)
{
if (length > 0) {
if (static_cast<Int>(string.length()) > length || string.length() == 0) {
remove (bucketNr, offset, length);
bucketNr = 0;
offset = 0;
length = 0;
}
}
if (string.length() == 0) {
return;
}
if (length > 0) {
if (itsCurrentBucket != bucketNr) {
getBucket(bucketNr);
}
replace(bucketNr, offset, length, string);
length = string.length();
return;
}
if (itsLastBucket == -1) {
getNewBucket(False);
} else if (itsCurrentBucket != itsLastBucket) {
getBucket(itsLastBucket);
}
// if Bucket available but string doesn't fit and space < 50 get
// a new bucket anyway.
if (static_cast<Int>(string.length()) > itsLength-itsUsedLength &&
itsLength-itsUsedLength < 50 ) {
getNewBucket(False);
}
offset = itsUsedLength;
bucketNr = itsCurrentBucket;
length = string.length();
putData (length, string.chars());
}
void SSMStringHandler::put (Int& bucketNr, Int& offset, Int& length,
const Array<String>& string, Bool handleShape)
{
const IPosition& aShape = string.shape();
Int totalLength=0;
Bool deleteIt;
const String *aString = string.getStorage(deleteIt);
for (uInt i=0;i<string.nelements();i++) {
totalLength += aString[i].length()+itsIntSize;
}
if (handleShape) {
totalLength += (string.ndim()+2)*ValType::getCanonicalSize (TpInt);
}
if (length > 0) {
if (totalLength > length || totalLength == 0) {
remove (bucketNr, offset, length);
bucketNr = 0;
offset = 0;
length = 0;
}
}
if (totalLength == 0) {
string.freeStorage(aString,deleteIt);
return;
}
if (length > 0) {
if (itsCurrentBucket != bucketNr) {
getBucket(bucketNr);
}
replace(bucketNr, offset, length, totalLength,string,handleShape);
length = totalLength;
string.freeStorage(aString,deleteIt);
return;
}
if (itsLastBucket == -1) {
getNewBucket(False);
} else if (itsCurrentBucket != itsLastBucket) {
getBucket(itsLastBucket);
}
// if Bucket available but string doesn't fit and space < 50 get
// a new bucket anyway.
if (totalLength > itsLength-itsUsedLength &&
itsLength-itsUsedLength < 50 ) {
getNewBucket(False);
}
bucketNr=itsCurrentBucket;
offset= itsUsedLength;
length= totalLength;
if (handleShape) {
CanonicalConversion::fromLocal (itsIntBuf, uInt(aShape.nelements()));
putData (itsIntSize, itsIntBuf);
for (uInt i=0; i< string.ndim();i++) {
CanonicalConversion::fromLocal (itsIntBuf, Int(aShape(i)));
putData (itsIntSize, itsIntBuf);
}
CanonicalConversion::fromLocal (itsIntBuf,1);
putData (itsIntSize, itsIntBuf);
}
for (uInt i=0; i< string.nelements();i++) {
//
// Made it a uInt so the SGI compiler could figure out which overloaded
// function to use, since it seemed confused by string::size_t -> size_t
//
CanonicalConversion::fromLocal (itsIntBuf, uInt(aString[i].length()));
putData (itsIntSize, itsIntBuf);
putData (aString[i].length(), aString[i].chars());
}
string.freeStorage(aString,deleteIt);
}
void SSMStringHandler::putData (Int length, const Char* data)
{
while (length > 0) {
Int toDo = length;
if (toDo > itsLength-itsUsedLength ) {
toDo = itsLength-itsUsedLength;
}
length-=toDo;
memcpy (itsData+itsUsedLength, data, toDo);
data += toDo;
itsNDeleted -= toDo;
itsUsedLength += toDo;
isChanged=True;
if (length > 0) {
getNewBucket(True);
}
}
}
void SSMStringHandler::getData (Int length, Char* data,Int& offset)
{
while (length > 0) {
Int nCopy = itsUsedLength-offset;
if (length < nCopy) {
nCopy = length;
}
length -= nCopy;
memcpy (data,itsData+offset, nCopy);
data += nCopy;
offset += nCopy;
if (length > 0) {
getBucket(itsNextBucket);
offset=0;
}
}
}
void SSMStringHandler::remove (Int bucketNr, Int offset, Int length)
{
if (itsCurrentBucket != bucketNr) {
getBucket(bucketNr);
}
Int n = itsLength-offset;
if (length < n) {
n = length;
}
itsNDeleted+=n;
// If space was at the end of the bucket, reset used length.
if (offset+n == itsUsedLength) {
itsUsedLength = offset;
}
isChanged = True;
if (itsNDeleted == itsLength) {
itsSSMPtr->removeBucket(itsCurrentBucket);
if (itsCurrentBucket == itsLastBucket) {
itsLastBucket=-1;
}
itsCurrentBucket=-1;
isChanged = False;
}
// Check if continuation in next bucket
length -= n;
if (length > 0) {
Int next=itsNextBucket;
// We are deleting this concatenated string
itsNextBucket=-1;
offset=0;
remove(next,offset,length);
}
}
void SSMStringHandler::get (String& string, Int bucket, Int offset,
Int length)
{
if (itsCurrentBucket != static_cast<Int>(bucket)) {
getBucket(bucket);
}
string.resize (length); // resize storage which adds trailing 0
Char* data = &(string[0]); // get actual string
getData(length,data,offset);
// terminate string for old strings
#ifdef USE_OLD_STRING
data[length] = '\0';
#endif
}
void SSMStringHandler::get (Array<String>& string, Int bucket, Int offset,
Int length, Bool handleShape)
{
IPosition aShape;
uInt aFilledFlag=0;
String emptyString;
if (length >0) {
if (itsCurrentBucket != static_cast<Int>(bucket)) {
getBucket(bucket);
}
aFilledFlag=1;
if (handleShape) {
getShape(aShape,bucket,offset,length);
DebugAssert (aShape.isEqual (string.shape()),AipsError);
// get the 'filled' flag
getData (itsIntSize, itsIntBuf,offset);
CanonicalConversion::toLocal(aFilledFlag,itsIntBuf);
}
}
Bool deleteIt;
String* aString = string.getStorage(deleteIt);
for (uInt i=0; i< string.nelements();i++) {
if (aFilledFlag == 0) {
aString[i] = emptyString;
} else {
// get length of next string Beware, offset resetting will be done in
// getdata, so you don't need to do it here again...
getData (itsIntSize, itsIntBuf,offset);
Int aL=0;
CanonicalConversion::toLocal(aL,itsIntBuf);
aString[i].resize (aL); // resize storage which adds trailing 0
Char* aS = &(aString[i][0]); // get actual string
// get next string. Beware, offset resetting will be done in
// getdata, so you don't need to do it here again...
getData (aL, aS, offset);
// terminate string
#ifdef USE_OLD_STRING
aS[aL] = '\0';
#endif
}
}
string.putStorage(aString,deleteIt);
}
void SSMStringHandler::putShape (Int& bucketNr, Int& offset, Int& length,
const IPosition& aShape)
{
Int totalLength=0;
if (itsLastBucket == -1) {
getNewBucket(False);
} else if (itsCurrentBucket != itsLastBucket) {
getBucket(itsLastBucket);
}
totalLength = (aShape.nelements()+2)*ValType::getCanonicalSize (TpInt);
if (length > 0) {
if (totalLength > length || totalLength == 0) {
remove (bucketNr, offset, length);
bucketNr = 0;
offset = 0;
length = 0;
}
}
if (length > 0) {
if (itsCurrentBucket != bucketNr) {
getBucket(bucketNr);
}
replace(bucketNr, offset, length, totalLength,aShape);
length = totalLength;
return;
}
if (itsLastBucket == -1) {
getNewBucket(False);
} else if (itsCurrentBucket != itsLastBucket) {
getBucket(itsLastBucket);
}
// if Bucket available but shape doesn't fit and space < 50 get
// a new bucket anyway.
if (totalLength > itsLength-itsUsedLength &&
itsLength-itsUsedLength < 50 ) {
getNewBucket(False);
}
bucketNr=itsCurrentBucket;
offset= itsUsedLength;
length= totalLength;
CanonicalConversion::fromLocal (itsIntBuf, uInt(aShape.nelements()));
putData (itsIntSize, itsIntBuf);
for (uInt i=0; i< aShape.nelements();i++) {
CanonicalConversion::fromLocal (itsIntBuf, Int(aShape(i)));
putData (itsIntSize, itsIntBuf);
}
// write 'empty' flag
CanonicalConversion::fromLocal (itsIntBuf,0);
putData (itsIntSize, itsIntBuf);
}
void SSMStringHandler::getShape (IPosition& aShape, Int bucket, Int& offset,
Int)
{
if (itsCurrentBucket != static_cast<Int>(bucket)) {
getBucket(bucket);
}
getData (itsIntSize, itsIntBuf,offset);
Int nDim=0;
CanonicalConversion::toLocal(nDim,itsIntBuf);
aShape.resize(nDim);
Int tmp;
for (Int i=0; i< nDim; i++) {
getData (itsIntSize, itsIntBuf,offset);
CanonicalConversion::toLocal(tmp, itsIntBuf);
aShape(i) = tmp;
}
}
void SSMStringHandler::flush()
{
if (isChanged) {
AlwaysAssert (itsCurrentBucket != -1, AipsError);
//save old bucket
Char* aPtr = itsSSMPtr->getBucket(itsCurrentBucket);
CanonicalConversion::fromLocal (aPtr+itsIntSize, itsUsedLength);
CanonicalConversion::fromLocal (aPtr+itsIntSize*2, itsNDeleted);
CanonicalConversion::fromLocal (aPtr+itsIntSize*3, itsNextBucket);
memcpy (aPtr+itsStart, itsData, itsLength);
itsSSMPtr->setBucketDirty();
isChanged = False;
}
}
void SSMStringHandler::getBucket (uInt bucketNr,Bool isNew)
{
// check if itsCurrentBuffer is in use, if so save this one first
flush();
itsCurrentBucket = bucketNr;
if (! isNew) {
char* aPtr = itsSSMPtr->getBucket(itsCurrentBucket);
memcpy (itsData, aPtr+itsStart, itsLength);
CanonicalConversion::toLocal (itsUsedLength, aPtr+itsIntSize);
CanonicalConversion::toLocal (itsNDeleted, aPtr+2*itsIntSize);
CanonicalConversion::toLocal (itsNextBucket, aPtr+3*itsIntSize);
}
}
void SSMStringHandler::getNewBucket(Bool doConcat)
{
Int bucketNr = itsSSMPtr->getNewBucket();
if (doConcat) {
itsNextBucket = bucketNr;
// save nextbucket
isChanged=True;
}
getBucket(bucketNr,True);
// zero dataspace
itsUsedLength = 0;
itsNDeleted = itsLength;
itsNextBucket=-1;
itsLastBucket=itsCurrentBucket;
}
void SSMStringHandler::resync()
{
AlwaysAssert (!isChanged,AipsError);
itsCurrentBucket = -1;
}
} //# NAMESPACE CASACORE - END
|