Logo Search packages:      
Sourcecode: r-cran-genabel version File versions  Download package

void FileVector::initialize ( unsigned long  cachesizeMb )

!! nelements should actually be long to ensure !!!

Definition at line 43 of file FileVector.cpp.

                                                     {
      //Rprintf("initialize, open file %s\n",filename.c_str());
      dbg << "Opening FileVector '" << filename.c_str() <<"'."<< endl;

      if (!readOnly) {
            AbstractMatrix::checkOpenForWriting(filename);
      }

      indexFilename = extract_base_file_name(filename) + FILEVECTOR_INDEX_FILE_SUFFIX;
      dataFilename = extract_base_file_name(filename) + FILEVECTOR_DATA_FILE_SUFFIX;

      if(!file_exists(indexFilename)) {
            errorLog <<"Index file not exists: " <<  indexFilename << endl << errorExit;
      }

      dataFilename = extract_base_file_name(filename) + FILEVECTOR_DATA_FILE_SUFFIX;
      if(!file_exists(dataFilename))
            errorLog <<"Data file not exists: " <<  dataFilename.c_str() << endl <<errorExit;

      struct stat data_filestatus;
      stat(dataFilename.c_str(), &data_filestatus);

      struct stat index_filestatus;
      stat(indexFilename.c_str(), &index_filestatus);

    indexFile = ReusableFileHandle::getHandle(indexFilename, readOnly);

      if (!indexFile) {
            errorLog << "Opening file "<< indexFilename <<" for write & read failed\n" << errorExit;
      }

    dataFile = ReusableFileHandle::getHandle(dataFilename, readOnly);

      if (!dataFile) {
            errorLog << "Opening file "<< dataFilename << " for write & read failed\n" << errorExit;
      }

      indexFile.blockWriteOrRead(sizeof(fileHeader),(char*)&fileHeader, false);
      if (!indexFile){
            errorLog << "Failed to read datainfo from file:" << indexFilename << endl;
      }

      // some integrity checks
      if (getElementSize() != fileHeader.bytesPerRecord) {
            errorLog << "System data type size ("<<getElementSize();
            errorLog <<") and file data type size ("<<fileHeader.bytesPerRecord<<") do not match.\n";
      }

      //!!! nelements should actually be long to ensure !!!
      if (fileHeader.nelements != (fileHeader.numObservations*fileHeader.numVariables)) {
            errorLog << "Number of variables ("<<fileHeader.numVariables;
            errorLog << ") and observations ("<<fileHeader.numObservations<<") do not multiply to nelements";
            errorLog << "("<< fileHeader.nelements<<") (file integrity issue?)\n";
            errorLog << errorExit;
      }

      if ((fileHeader.bytesPerRecord != (fileHeader.bitsPerRecord/8)) ||
                  ((fileHeader.bitsPerRecord % 8) != 0) || (fileHeader.bitsPerRecord < 8)) {
            errorLog << "Size in bytes/bits do not match or bit-size of char !=8 or ";
            errorLog << "non-byte recods (file integrity issue?)" << errorExit;
      }

      unsigned long indexSize = sizeof(fileHeader) + sizeof(FixedChar)*(fileHeader.numVariables+fileHeader.numObservations);
      if(indexSize != (unsigned long) index_filestatus.st_size) {
            errorLog << "Index file "<<indexFilename<<" size(" << (int) index_filestatus.st_size << ") differs from the expected(";
            errorLog << indexSize <<")" << endl << errorExit;
      }

      // temp fix because nelements is not yet long ... !!!
      //    unsigned long estimated_size = data_type.bytes_per_record*data_type.nelements + headerSize;
      unsigned long estimated_size =
                  (unsigned long) fileHeader.bytesPerRecord *
                  (unsigned long) fileHeader.numVariables *
                  (unsigned long) fileHeader.numObservations;

      if (estimated_size != (unsigned long) data_filestatus.st_size) {
            errorLog << "Data file size (" << (int) data_filestatus.st_size;
            errorLog << ") differs from the expected ("<<estimated_size<<")"<<endl<<" [";
            errorLog << fileHeader.numVariables<<","<<fileHeader.numObservations<<"]" << endl;
            errorLog << errorExit;
      }

      variableNames = 0;
      observationNames = 0;

      setCacheSizeInMb(cachesizeMb);
      updateCache(0);
      dbg << "Filevector " << filename << " opened." << endl;
}

Generated by  Doxygen 1.6.0   Back to index