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
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================
// Memory map a whole file and use it as a string.
// ==========================================================================
// NOTE(esiragusa): this file is deprecated.
#ifndef SEQAN_HEADER_STRING_MMAP_H
#define SEQAN_HEADER_STRING_MMAP_H
/* IOREV
* _tested_
* _windows_
*
*
* tested in library/demos/howto/efficiently_import_sequences.cpp and stellar
*
* relation to file_format_mmap.h unclear
*
* relation to string_external unclear, what benefit does string_mmap provide?
*
*/
//////////////////////////////////////////////////////////////////////////////
namespace seqan2
{
/*!
* @class MMapConfig
* @headerfile <seqan/file.h>
* @brief Configuration for @link MMapString MMapStrings @endlink.
*
* @signature template <[typename TFile, [typename TSize]]>
* class MMapConfig;
*
* @tparam TFile The file type to use for the memory mapped string. Defaults to <tt>File<></tt>
* @tparam TSize The size type to use. Defaults to <tt>size_t</tt>.
*
*
* @typedef MMapConfig::TFile;
* @signature typedef (..) MMapConfig::TFile;
* @brief The type to use for files.
*
* @typedef MMapConfig::TSize;
* @signature typedef (..) MMapConfig::TSize;
* @brief The type to use for sizes.
*/
template < typename TFile_ = File<>, // default file type
typename TSize_ = size_t > // size type
struct MMapConfig {
//IOREV _nodoc_ doc says using MMap<MMapConfig> is correct, whats this for?
typedef TFile_ TFile;
typedef TSize_ TSize;
};
template < typename TConfig = MMapConfig<> >
struct MMap {};
//IOREV
//////////////////////////////////////////////////////////////////////////////
// Memory Mapped String
//////////////////////////////////////////////////////////////////////////////
/*!
* @class MMapString MMap String
* @headerfile <seqan/file.h>
* @extends String
* @brief String that is stored in external memory using direct memory mapping.
*
* @signature template <typename TValue[, typename TConfig]>
* class String<TValue, MMap<TConfig> >;
*
* @tparam TValue The value type to use for the items/characters.
* @tparam TConfig The configuration to use for the underlying file. Default: @link MMapConfig @endlink.
*
* The MMap String enables to access sequences larger than the available physical memory (RAM) by using external memory
* (e.g. Hard disk, Network storage, ...) mapped into memory. The size of the string is limited by external memory and
* the logical address space (4GB on a 32bit OS). See the String constructor for more details.
*
* This string also supports fast appending and removing of values at the end.
*/
/*!
* @fn MMapString::String
* @brief Constructor.
*
* @signature String::String();
* @signature String::String(file);
* @signature String::String(fileName[, openMode]);
*
* @param[in,out] file The @link File @endlink to use for reading and writing. You must ensture that
* <tt>file</tt> is open as the string will not call <tt>open</tt> and <tt>close</tt>
* on the file.
* @param[in] fileName The path to open. Type: <tt>char const *</tt>
* @param[in] openMode The open mode.
*/
template < typename TValue,
typename TConfig >
class String<TValue, MMap<TConfig> >
{
//IOREV
public:
typedef typename TConfig::TFile TFile;
typedef typename TConfig::TSize TSize;
TValue *data_begin;
TValue *data_end;
FileMapping<> mapping;
FileMappingAdvise advise;
explicit
String(TSize size = 0):
data_begin(NULL),
data_end(NULL),
advise(MAP_NORMAL)
{
resize(*this, size);
}
// associate a file
explicit
String(TFile &_file):
data_begin(NULL),
data_end(NULL),
advise(MAP_NORMAL)
{
open(*this, _file);
}
// associate a file given filename [and open mode]
explicit
String(const char *fileName, int openMode = DefaultOpenMode<TFile>::VALUE):
data_begin(NULL),
data_end(NULL),
advise(MAP_NORMAL)
{
open(*this, fileName, openMode);
}
// copy the contents from another string
String(String const & source):
data_begin(NULL),
data_end(NULL),
advise(MAP_NORMAL)
{
assign(*this, source);
}
template <typename TSource>
String(TSource const & source):
data_begin(NULL),
data_end(NULL),
advise(MAP_NORMAL)
{
assign(*this, source);
}
template <typename TSource>
String & operator =(TSource const & source)
{
assign(*this, source);
return *this;
}
String & operator =(String const & source)
{
assign(*this, source);
return *this;
}
~String()
{
close(*this);
}
//____________________________________________________________________________
template <typename TPos>
inline typename Reference<String>::Type
operator [] (TPos pos)
{
return value(*this, pos);
}
template <typename TPos>
inline typename Reference<String const>::Type
operator [] (TPos pos) const
{
return value(*this, pos);
}
//____________________________________________________________________________
inline operator bool()
{
return mapping;
}
//____________________________________________________________________________
};
template < typename TValue, typename TConfig >
inline typename Iterator<String<TValue, MMap<TConfig> > , Standard>::Type
begin(String<TValue, MMap<TConfig> > & me,
Standard)
{
//IOREV
return me.data_begin;
}
template < typename TValue, typename TConfig >
inline typename Iterator<String<TValue, MMap<TConfig> > const, Standard>::Type
begin(String<TValue, MMap<TConfig> > const & me,
Standard)
{
//IOREV
return me.data_begin;
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline typename Iterator<String<TValue, MMap<TConfig> > , Standard>::Type
end(String<TValue, MMap<TConfig> > & me,
Standard)
{
//IOREV
return me.data_end;
}
template < typename TValue, typename TConfig >
inline typename Iterator<String<TValue, MMap<TConfig> > const, Standard>::Type
end(String<TValue, MMap<TConfig> > const & me,
Standard)
{
//IOREV
return me.data_end;
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline typename Size<String<TValue, MMap<TConfig> > >::Type
capacity(String<TValue, MMap<TConfig> > const & me)
{
//IOREV
return length(me.mapping) / sizeof(TValue);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline void
_setLength(
String<TValue, MMap<TConfig> > & me,
size_t new_length)
{
//IOREV
me.data_end = me.data_begin + new_length;
}
//////////////////////////////////////////////////////////////////////////////
// meta-function interface
template < typename TValue, typename TConfig >
struct Size< String<TValue, MMap<TConfig> > >
{
//IOREV
typedef typename TConfig::TSize Type;
};
template < typename TValue, typename TConfig >
struct Difference< String<TValue, MMap<TConfig> > >
{
//IOREV
typedef typename MakeSigned_<typename TConfig::TSize>::Type Type;
};
//____________________________________________________________________________
template < typename TValue, typename TConfig >
struct DefaultOverflowExplicit<String<TValue, MMap<TConfig> > >
{
//IOREV
typedef Generous Type;
};
template < typename TValue, typename TConfig >
struct DefaultOverflowImplicit<String<TValue, MMap<TConfig> > >
{
//IOREV
typedef Generous Type;
};
//____________________________________________________________________________
template < typename TValue, typename TConfig >
struct IsContiguous< String<TValue, MMap<TConfig> > >
{
//IOREV
typedef True Type;
enum { VALUE = true };
};
template < typename TValue, typename TConfig >
struct AllowsFastRandomAccess< String<TValue, MMap<TConfig> > >
{
//IOREV
typedef False Type;
enum { VALUE = false };
};
//////////////////////////////////////////////////////////////////////////////
// global interface
//____________________________________________________________________________
/*!
* @fn MMapString#flush
* @brief Waits for all open read/write requests to complete.
*
* @signature void flush(str);
*
* @param[in,out] str The MMapString to flush.
*/
template < typename TValue, typename TConfig >
inline bool
flush(String<TValue, MMap<TConfig> > &me)
{
typedef typename Size<typename TConfig::TFile>::Type TFileSize;
return flushFileSegment(
me.mapping,
me.data_begin,
0,
(TFileSize)capacity(me) * (TFileSize)sizeof(TValue));
}
/*!
* @fn MMapString#mmapAdvise
* @brief Call advise function for memory mapped files.
*
* @signature bool mmapAdvise(str, scheme[, beginPos, size]);
*
* @param[in,out] str The MMapString to call advise in.
* @param[in] scheme The memory access scheme to use. Type: FileMappingAdvise.
* @param[in] beginPos Begin position in the string for the advise call.
* @param[in] size Size of the range used for the advise call.
*
* @return bool <tt>true</tt> if the advise was successful, <tt>false</tt> otherwise.
*/
template <typename TValue, typename TConfig, typename TPos, typename TSize>
inline bool
mmapAdvise(String<TValue, MMap<TConfig> > &me, FileMappingAdvise advise, TPos beginPos, TSize size)
{
typedef typename Size<typename TConfig::TFile>::Type TFileSize;
me.advise = advise;
return adviseFileSegment(
me.mapping,
advise,
me.data_begin,
(TFileSize)beginPos * (TFileSize)sizeof(TValue),
(TFileSize)size * (TFileSize)sizeof(TValue));
}
template <typename TValue, typename TConfig, typename TPos>
inline int
mmapAdvise(String<TValue, MMap<TConfig> > &me, FileMappingAdvise advise, TPos beginPos)
{
return mmapAdvise(me, advise, beginPos, capacity(me) - beginPos);
}
template <typename TValue, typename TConfig>
inline int
mmapAdvise(String<TValue, MMap<TConfig> > &me, FileMappingAdvise advise)
{
return mmapAdvise(me, advise, 0, capacity(me));
}
//____________________________________________________________________________
// cancel all transactions
template < typename TValue, typename TConfig >
inline void
cancel(String<TValue, MMap<TConfig> > &me)
{
typedef typename Size<typename TConfig::TFile>::Type TFileSize;
cancelFileSegment(
me.mapping,
me.data_begin,
0,
(TFileSize)capacity(me) * (TFileSize)sizeof(TValue));
}
//____________________________________________________________________________
// flush and free all allocated pages
template < typename TValue, typename TConfig >
inline bool
flushAndFree(String<TValue, MMap<TConfig> > &me)
{
return flush(me) && mmapAdvise(me, MAP_DONTNEED);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline bool
_map(String<TValue, MMap<TConfig> > &me, size_t new_capacity)
{
typedef typename Size<typename TConfig::TFile>::Type TFileSize;
if (new_capacity > 0)
{
_ensureFileIsOpen(me);
if (capacity(me) < new_capacity)
resize(me.mapping, (TFileSize)new_capacity * (TFileSize)sizeof(TValue));
me.data_begin = static_cast<TValue*>(mapFileSegment(me.mapping, 0, length(me.mapping)));
if (me.data_begin == NULL)
{
me.data_end = NULL;
return false;
}
adviseFileSegment(me.mapping, me.advise, me.data_begin, 0, length(me.mapping));
}
else
resize(me.mapping, 0);
_setLength(me, new_capacity);
return true;
}
template < typename TValue, typename TConfig >
inline bool
_unmap(String<TValue, MMap<TConfig> > &me)
{
bool result = true;
if (me.data_begin != NULL)
{
result = unmapFileSegment(me.mapping, me.data_begin, length(me.mapping));
me.data_begin = NULL;
}
me.data_end = NULL;
return result;
}
template < typename TValue, typename TConfig, typename TCapSize >
inline bool
_remap(String<TValue, MMap<TConfig> > &me, TCapSize new_capacity)
{
typedef typename Size<String<TValue, MMap<TConfig> > >::Type TSize SEQAN_UNUSED_TYPEDEF;
typedef typename Size<typename TConfig::TFile>::Type TFileSize SEQAN_UNUSED_TYPEDEF;
bool result = true;
#ifndef STDLIB_VS
// Windows doesn't allow to resize the file while having a mapped file segment
// Thus, the following part is only supported on Linux/BSD/Mac OS
TSize old_capacity = capacity(me);
if (me.data_begin && new_capacity > 0)
{
// if file gets bigger, resize first
if (old_capacity < new_capacity)
resize(me.mapping, (TFileSize)new_capacity * (TFileSize)sizeof(TValue));
me.data_begin = static_cast<TValue*>(remapFileSegment(
me.mapping,
me.data_begin,
0,
(TFileSize)old_capacity * (TFileSize)sizeof(TValue),
(TFileSize)new_capacity * (TFileSize)sizeof(TValue)));
// if file gets smaller, resize at last
if (old_capacity > new_capacity)
resize(me.mapping, (TFileSize)new_capacity * (TFileSize)sizeof(TValue));
if (me.data_begin == NULL)
{
me.data_end = NULL;
return false;
}
return true;
}
#endif
result &= _unmap(me);
result &= _map(me, new_capacity);
return result;
}
template < typename TValue, typename TConfig >
inline void
clear(String<TValue, MMap<TConfig> > &me)
{
//IOREV
cancel(me);
_unmap(me);
resize(me.mapping, 0);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig, typename TSize >
inline typename Value<String<TValue, MMap<TConfig> > >::Type *
_allocateStorage(String<TValue, MMap<TConfig> > &me, TSize new_capacity)
{
//IOREV
_map(me, _computeSizeForCapacity(me, new_capacity));
return NULL;
}
template < typename TValue, typename TConfig, typename TSize >
inline typename Value<String<TValue, MMap<TConfig> > >::Type *
_reallocateStorage(
String<TValue, MMap<TConfig> > &me,
TSize new_capacity)
{
//IOREV
TSize size = _computeSizeForCapacity(me, new_capacity);
_remap(me, size);
return NULL;
}
template < typename TValue, typename TConfig, typename TSize >
inline void
_deallocateStorage(String<TValue, MMap<TConfig> > &/*me*/, TValue * /*ptr*/, TSize /*capacity*/)
{
//IOREV
}
//____________________________________________________________________________
/*!
* @fn MMapString#open
* @brief Open the MMapString's underlying file from a path.
*
* @signature bool open(str, fileName[, openMode]);
*
* @param[in,out] str The MMapString to open.
* @param[in] fileName Path to the file to open. Type: <tt>char const *</tt>.
* @param[in] openMode The open mode. Type: <tt>int</tt>.
*
* @return bool <tt>true</tt> if the operation succeeded and <tt>false</tt> otherwise.
*/
template < typename TValue, typename TConfig >
inline bool
open(String<TValue, MMap<TConfig> > &me, const char *fileName, int openMode)
{
//IOREV
close(me);
if (open(me.mapping, fileName, openMode))
return _map(me, capacity(me));
return false;
}
template < typename TValue, typename TConfig >
inline bool
open(String<TValue, MMap<TConfig> > &me, const char *fileName)
{
//IOREV
typedef typename String<TValue, MMap<TConfig> >::TFile TFile;
return open(me, fileName, DefaultOpenMode<TFile>::VALUE);
}
template < typename TValue, typename TConfig >
inline bool
open(String<TValue, MMap<TConfig> > &me, typename TConfig::TFile file)
{
//IOREV
close(me);
if (open(me.mapping, file))
return _map(me, capacity(me));
return false;
}
/*!
* @fn MMapString#openTemp
* @brief Open an MMapString using an temporary file.
*
* @signature bool openTemp(str);
*
* @param[in,out] str The MMapString to open using temporary file.
*
* @return bool <tt>true</tt> if opening succeeded, <tt>false</tt> otherwise.
*/
template < typename TValue, typename TConfig >
inline bool
openTemp(String<TValue, MMap<TConfig> > &me)
{
//IOREV
close(me);
return openTemp(me.mapping);
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline void
_ensureFileIsOpen(String<TValue, MMap<TConfig> > &me)
{
//IOREV
if (!me)
{
if (!openTemp(me.mapping))
SEQAN_FAIL("Memory Mapped String couldn't open temporary file");
}
}
//____________________________________________________________________________
template < typename TValue, typename TConfig >
inline bool
save(String<TValue, MMap<TConfig> > const &/*me*/, const char * /*fileName*/, int /*openMode*/) {
//IOREV _nodoc_ shouldn't we flush here? in case of abnormal termination...
// Memory Mapped Strings are persistent, thus there is no need to save them
//MMapStringsDontNeedToBeSaved error;
return true;
}
template < typename TValue, typename TConfig >
inline bool
save(String<TValue, MMap<TConfig> > const &/*me*/, const char * /*fileName*/) {
//IOREV _nodoc_ shouldn't we flush here? in case of abnormal termination...
// Memory Mapped Strings are persistent, thus there is no need to save them
//MMapStringsDontNeedToBeSaved error;
return true;
}
template < typename TValue, typename TConfig >
inline bool
save(String<TValue, MMap<TConfig> > const &/*me*/, typename TConfig::TFile /*file*/) {
//IOREV _nodoc_ shouldn't we flush here? in case of abnormal termination...
// Memory Mapped Strings are persistent, thus there is no need to save them
//MMapStringsDontNeedToBeSaved error;
return true;
}
//____________________________________________________________________________
/*!
* @fn MMapString#close
* @brief Close the MMapString.
*
* @signature bool close(str);
*
* @param[in] str The MMapString to close the file of.
*
* @return bool <tt>true</tt> if the closing succeeded, <tt>false</tt> otherwise.
*/
template < typename TValue, typename TConfig >
inline bool
close(String<TValue, MMap<TConfig> > &me)
{
typedef typename Size<typename TConfig::TFile>::Type TFileSize;
if (me)
{
TFileSize finalLen = (TFileSize)length(me) * (TFileSize)sizeof(TValue);
// close associated file
if (me.mapping.temporary)
cancel(me);
_unmap(me);
closeAndResize(me.mapping, finalLen);
}
return true;
}
template < typename TValue, typename TConfig >
inline void
shrinkToFit(String<TValue, MMap<TConfig> > &me)
{
_remap(me, length(me));
}
//////////////////////////////////////////////////////////////////////////////
} //namespace seqan2
#endif //#ifndef SEQAN_HEADER_...
|