OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileBuffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
24 
25 #include <future>
26 #include <map>
27 #include <thread>
28 #include <utility> // std::pair
29 
31 #include "Shared/File.h"
32 #include "Shared/checked_alloc.h"
33 
34 using namespace std;
35 
36 namespace File_Namespace {
37 size_t FileBuffer::headerBufferOffset_ = 32;
38 
39 FileBuffer::FileBuffer(FileMgr* fm,
40  const size_t pageSize,
41  const ChunkKey& chunkKey,
42  const size_t initialSize)
43  : AbstractBuffer(fm->getDeviceId())
44  , fm_(fm)
45  , metadataPages_(METADATA_PAGE_SIZE)
46  , pageSize_(pageSize)
47  , chunkKey_(chunkKey) {
48  // Create a new FileBuffer
49  CHECK(fm_);
52  //@todo reintroduce initialSize - need to develop easy way of
53  // differentiating these pre-allocated pages from "written-to" pages
54  /*
55  if (initalSize > 0) {
56  // should expand to initialSize bytes
57  size_t initialNumPages = (initalSize + pageSize_ -1) / pageSize_;
58  int32_t epoch = fm_->epoch();
59  for (size_t pageNum = 0; pageNum < initialNumPages; ++pageNum) {
60  Page page = addNewMultiPage(epoch);
61  writeHeader(page,pageNum,epoch);
62  }
63  }
64  */
65 }
66 
68  const size_t pageSize,
69  const ChunkKey& chunkKey,
70  const SQLTypeInfo sqlType,
71  const size_t initialSize)
72  : AbstractBuffer(fm->getDeviceId(), sqlType)
73  , fm_(fm)
74  , metadataPages_(METADATA_PAGE_SIZE)
75  , pageSize_(pageSize)
76  , chunkKey_(chunkKey) {
77  CHECK(fm_);
80 }
81 
83  /* const size_t pageSize,*/ const ChunkKey& chunkKey,
84  const std::vector<HeaderInfo>::const_iterator& headerStartIt,
85  const std::vector<HeaderInfo>::const_iterator& headerEndIt)
86  : AbstractBuffer(fm->getDeviceId())
87  , fm_(fm)
88  , metadataPages_(METADATA_PAGE_SIZE)
89  , pageSize_(0)
90  , chunkKey_(chunkKey) {
91  // We are being assigned an existing FileBuffer on disk
92 
93  CHECK(fm_);
95  int32_t lastPageId = -1;
96  int32_t curPageId = 0;
97  // Page lastMetadataPage;
98  for (auto vecIt = headerStartIt; vecIt != headerEndIt; ++vecIt) {
99  curPageId = vecIt->pageId;
100 
101  // We only want to read last metadata page
102  if (curPageId == -1) { // stats page
103  metadataPages_.push(vecIt->page, vecIt->versionEpoch);
104  } else {
105  if (curPageId != lastPageId) {
106  // protect from bad data on disk, and give diagnostics
107  if (curPageId != lastPageId + 1) {
108  LOG(FATAL) << "Failure reading DB file " << show_chunk(chunkKey)
109  << " Current page " << curPageId << " last page " << lastPageId
110  << " epoch " << vecIt->versionEpoch;
111  }
112  if (lastPageId == -1) {
113  // If we are on first real page
114  CHECK(metadataPages_.current().page.fileId != -1); // was initialized
117  }
118  MultiPage multiPage(pageSize_);
119  multiPages_.push_back(multiPage);
120  lastPageId = curPageId;
121  }
122  multiPages_.back().push(vecIt->page, vecIt->versionEpoch);
123  }
124  }
125  if (curPageId == -1) { // meaning there was only a metadata page
128  }
129 }
130 
132  // need to free pages
133  // NOP
134 }
135 
136 void FileBuffer::reserve(const size_t numBytes) {
137  size_t numPagesRequested = (numBytes + pageSize_ - 1) / pageSize_;
138  size_t numCurrentPages = multiPages_.size();
139  int32_t epoch = fm_->epoch();
140 
141  for (size_t pageNum = numCurrentPages; pageNum < numPagesRequested; ++pageNum) {
142  Page page = addNewMultiPage(epoch);
143  writeHeader(page, pageNum, epoch);
144  }
145 }
146 
148  // 3 * sizeof(int32_t) is for headerSize, for pageId and versionEpoch
149  // sizeof(size_t) is for chunkSize
150  reservedHeaderSize_ = (chunkKey_.size() + 3) * sizeof(int32_t);
151  size_t headerMod = reservedHeaderSize_ % headerBufferOffset_;
152  if (headerMod > 0) {
154  }
155 }
156 
157 void FileBuffer::freePage(const Page& page, const bool isRolloff) {
158  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
159  fileInfo->freePage(page.pageNum, isRolloff);
160 }
161 
163  for (auto metaPageIt = metadataPages_.pageVersions.begin();
164  metaPageIt != metadataPages_.pageVersions.end();
165  ++metaPageIt) {
166  freePage(metaPageIt->page, false /* isRolloff */);
167  }
168  while (metadataPages_.pageVersions.size() > 0) {
170  }
171 }
172 
174  size_t num_pages_freed = multiPages_.size();
175  for (auto multiPageIt = multiPages_.begin(); multiPageIt != multiPages_.end();
176  ++multiPageIt) {
177  for (auto pageIt = multiPageIt->pageVersions.begin();
178  pageIt != multiPageIt->pageVersions.end();
179  ++pageIt) {
180  freePage(pageIt->page, false /* isRolloff */);
181  }
182  }
183  multiPages_.clear();
184  return num_pages_freed;
185 }
186 
189  freeChunkPages();
190 }
191 
193  const int32_t targetEpoch,
194  const int32_t currentEpoch) {
195  std::vector<EpochedPage> epochedPagesToFree =
196  multiPage.freePagesBeforeEpoch(targetEpoch, currentEpoch);
197  for (const auto& epochedPageToFree : epochedPagesToFree) {
198  freePage(epochedPageToFree.page, true /* isRolloff */);
199  }
200 }
201 
202 void FileBuffer::freePagesBeforeEpoch(const int32_t targetEpoch) {
203  // This method is only safe to be called within a checkpoint, after the sync and epoch
204  // increment where a failure at any point32_t in the process would lead to a safe
205  // rollback
206  const int32_t currentEpoch = fm_->epoch();
207  CHECK_LE(targetEpoch, currentEpoch);
208  freePagesBeforeEpochForMultiPage(metadataPages_, targetEpoch, currentEpoch);
209  for (auto& multiPage : multiPages_) {
210  freePagesBeforeEpochForMultiPage(multiPage, targetEpoch, currentEpoch);
211  }
212 
213  // Check if all buffer pages can be freed
214  if (size_ == 0) {
215  size_t max_historical_buffer_size{0};
216  for (auto& epoch_page : metadataPages_.pageVersions) {
217  // Create buffer that is used to get the buffer size at the epoch version
218  FileBuffer buffer{fm_, pageSize_, chunkKey_};
219  buffer.readMetadata(epoch_page.page);
220  max_historical_buffer_size = std::max(max_historical_buffer_size, buffer.size());
221  }
222 
223  // Free all chunk pages, if none of the old chunk versions has any data
224  if (max_historical_buffer_size == 0) {
225  freeChunkPages();
226  }
227  }
228 }
229 
230 struct readThreadDS {
231  FileMgr* t_fm; // ptr to FileMgr
232  size_t t_startPage; // start page for the thread
233  size_t t_endPage; // last page for the thread
234  int8_t* t_curPtr; // pointer to the current location of the target for the thread
235  size_t t_bytesLeft; // number of bytes to be read in the thread
236  size_t t_startPageOffset; // offset - used for the first page of the buffer
237  bool t_isFirstPage; // true - for first page of the buffer, false - otherwise
238  std::vector<MultiPage> multiPages; // MultiPages of the FileBuffer passed to the thread
239 };
240 
241 static size_t readForThread(FileBuffer* fileBuffer, const readThreadDS threadDS) {
242  size_t startPage = threadDS.t_startPage; // start reading at startPage, including it
243  size_t endPage = threadDS.t_endPage; // stop reading at endPage, not including it
244  int8_t* curPtr = threadDS.t_curPtr;
245  size_t bytesLeft = threadDS.t_bytesLeft;
246  size_t totalBytesRead = 0;
247  bool isFirstPage = threadDS.t_isFirstPage;
248 
249  // Traverse the logical pages
250  for (size_t pageNum = startPage; pageNum < endPage; ++pageNum) {
251  CHECK(threadDS.multiPages[pageNum].pageSize == fileBuffer->pageSize());
252  Page page = threadDS.multiPages[pageNum].current().page;
253 
254  FileInfo* fileInfo = threadDS.t_fm->getFileInfoForFileId(page.fileId);
255  CHECK(fileInfo);
256 
257  // Read the page into the destination (dst) buffer at its
258  // current (cur) location
259  size_t bytesRead = 0;
260  if (isFirstPage) {
261  bytesRead = fileInfo->read(
262  page.pageNum * fileBuffer->pageSize() + threadDS.t_startPageOffset +
263  fileBuffer->reservedHeaderSize(),
264  min(fileBuffer->pageDataSize() - threadDS.t_startPageOffset, bytesLeft),
265  curPtr);
266  isFirstPage = false;
267  } else {
268  bytesRead = fileInfo->read(
269  page.pageNum * fileBuffer->pageSize() + fileBuffer->reservedHeaderSize(),
270  min(fileBuffer->pageDataSize(), bytesLeft),
271  curPtr);
272  }
273  curPtr += bytesRead;
274  bytesLeft -= bytesRead;
275  totalBytesRead += bytesRead;
276  }
277  CHECK(bytesLeft == 0);
278 
279  return (totalBytesRead);
280 }
281 
282 void FileBuffer::read(int8_t* const dst,
283  const size_t numBytes,
284  const size_t offset,
285  const MemoryLevel dstBufferType,
286  const int32_t deviceId) {
287  if (dstBufferType != CPU_LEVEL) {
288  LOG(FATAL) << "Unsupported Buffer type";
289  }
290 
291  // variable declarations
292  size_t startPage = offset / pageDataSize_;
293  size_t startPageOffset = offset % pageDataSize_;
294  size_t numPagesToRead =
295  (numBytes + startPageOffset + pageDataSize_ - 1) / pageDataSize_;
296  /*
297  if (startPage + numPagesToRead > multiPages_.size()) {
298  cout << "Start page: " << startPage << endl;
299  cout << "Num pages to read: " << numPagesToRead << endl;
300  cout << "Num multipages: " << multiPages_.size() << endl;
301  cout << "Offset: " << offset << endl;
302  cout << "Num bytes: " << numBytes << endl;
303  }
304  */
305 
306  CHECK(startPage + numPagesToRead <= multiPages_.size());
307 
308  size_t numPagesPerThread = 0;
309  size_t numBytesCurrent = numBytes; // total number of bytes still to be read
310  size_t bytesRead = 0; // total number of bytes already being read
311  size_t bytesLeftForThread = 0; // number of bytes to be read in the thread
312  size_t numExtraPages = 0; // extra pages to be assigned one per thread as needed
313  size_t numThreads = fm_->getNumReaderThreads();
314  std::vector<readThreadDS>
315  threadDSArr; // array of threadDS, needed to avoid racing conditions
316 
317  if (numPagesToRead > numThreads) {
318  numPagesPerThread = numPagesToRead / numThreads;
319  numExtraPages = numPagesToRead - (numThreads * numPagesPerThread);
320  } else {
321  numThreads = numPagesToRead;
322  numPagesPerThread = 1;
323  }
324 
325  /* set threadDS for the first thread */
326  readThreadDS threadDS;
327  threadDS.t_fm = fm_;
328  threadDS.t_startPage = offset / pageDataSize_;
329  if (numExtraPages > 0) {
330  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread + 1;
331  numExtraPages--;
332  } else {
333  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread;
334  }
335  threadDS.t_curPtr = dst;
336  threadDS.t_startPageOffset = offset % pageDataSize_;
337  threadDS.t_isFirstPage = true;
338 
339  bytesLeftForThread = min(((threadDS.t_endPage - threadDS.t_startPage) * pageDataSize_ -
340  threadDS.t_startPageOffset),
341  numBytesCurrent);
342  threadDS.t_bytesLeft = bytesLeftForThread;
343  threadDS.multiPages = getMultiPage();
344 
345  if (numThreads == 1) {
346  bytesRead += readForThread(this, threadDS);
347  } else {
348  std::vector<std::future<size_t>> threads;
349 
350  for (size_t i = 0; i < numThreads; i++) {
351  threadDSArr.push_back(threadDS);
352  threads.push_back(
353  std::async(std::launch::async, readForThread, this, threadDSArr[i]));
354 
355  // calculate elements of threadDS
356  threadDS.t_fm = fm_;
357  threadDS.t_isFirstPage = false;
358  threadDS.t_curPtr += bytesLeftForThread;
359  threadDS.t_startPage +=
360  threadDS.t_endPage -
361  threadDS.t_startPage; // based on # of pages read on previous iteration
362  if (numExtraPages > 0) {
363  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread + 1;
364  numExtraPages--;
365  } else {
366  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread;
367  }
368  numBytesCurrent -= bytesLeftForThread;
369  bytesLeftForThread = min(
370  ((threadDS.t_endPage - threadDS.t_startPage) * pageDataSize_), numBytesCurrent);
371  threadDS.t_bytesLeft = bytesLeftForThread;
372  threadDS.multiPages = getMultiPage();
373  }
374 
375  for (auto& p : threads) {
376  p.wait();
377  }
378  for (auto& p : threads) {
379  bytesRead += p.get();
380  }
381  }
382  CHECK(bytesRead == numBytes);
383 }
384 
386  Page& destPage,
387  const size_t numBytes,
388  const size_t offset) {
389  // FILE *srcFile = fm_->files_[srcPage.fileId]->f;
390  // FILE *destFile = fm_->files_[destPage.fileId]->f;
391  CHECK_LE(offset + numBytes, pageDataSize_);
392  FileInfo* srcFileInfo = fm_->getFileInfoForFileId(srcPage.fileId);
393  FileInfo* destFileInfo = fm_->getFileInfoForFileId(destPage.fileId);
394 
395  int8_t* buffer = reinterpret_cast<int8_t*>(checked_malloc(numBytes));
396  size_t bytesRead = srcFileInfo->read(
397  srcPage.pageNum * pageSize_ + offset + reservedHeaderSize_, numBytes, buffer);
398  CHECK(bytesRead == numBytes);
399  size_t bytesWritten = destFileInfo->write(
400  destPage.pageNum * pageSize_ + offset + reservedHeaderSize_, numBytes, buffer);
401  CHECK(bytesWritten == numBytes);
402  free(buffer);
403 }
404 
405 Page FileBuffer::addNewMultiPage(const int32_t epoch) {
406  Page page = fm_->requestFreePage(pageSize_, false);
407  MultiPage multiPage(pageSize_);
408  multiPage.push(page, epoch);
409  multiPages_.emplace_back(multiPage);
410  return page;
411 }
412 
414  const int32_t pageId,
415  const int32_t epoch,
416  const bool writeMetadata) {
417  int32_t intHeaderSize = chunkKey_.size() + 3; // does not include chunkSize
418  vector<int32_t> header(intHeaderSize);
419  // in addition to chunkkey we need size of header, pageId, version
420  header[0] =
421  (intHeaderSize - 1) * sizeof(int32_t); // don't need to include size of headerSize
422  // value - sizeof(size_t) is for chunkSize
423  std::copy(chunkKey_.begin(), chunkKey_.end(), header.begin() + 1);
424  header[intHeaderSize - 2] = pageId;
425  header[intHeaderSize - 1] = epoch;
426  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
427  size_t pageSize = writeMetadata ? METADATA_PAGE_SIZE : pageSize_;
428  fileInfo->write(
429  page.pageNum * pageSize, (intHeaderSize) * sizeof(int32_t), (int8_t*)&header[0]);
430 }
431 
432 void FileBuffer::readMetadata(const Page& page) {
433  FILE* f = fm_->getFileForFileId(page.fileId);
434  fseek(f, page.pageNum * METADATA_PAGE_SIZE + reservedHeaderSize_, SEEK_SET);
435  fread((int8_t*)&pageSize_, sizeof(size_t), 1, f);
436  fread((int8_t*)&size_, sizeof(size_t), 1, f);
437  vector<int32_t> typeData(
438  NUM_METADATA); // assumes we will encode hasEncoder, bufferType,
439  // encodingType, encodingBits all as int
440  fread((int8_t*)&(typeData[0]), sizeof(int32_t), typeData.size(), f);
441  int32_t version = typeData[0];
442  CHECK(version == METADATA_VERSION); // add backward compatibility code here
443  bool has_encoder = static_cast<bool>(typeData[1]);
444  if (has_encoder) {
445  sql_type_.set_type(static_cast<SQLTypes>(typeData[2]));
446  sql_type_.set_subtype(static_cast<SQLTypes>(typeData[3]));
447  sql_type_.set_dimension(typeData[4]);
448  sql_type_.set_scale(typeData[5]);
449  sql_type_.set_notnull(static_cast<bool>(typeData[6]));
450  sql_type_.set_compression(static_cast<EncodingType>(typeData[7]));
451  sql_type_.set_comp_param(typeData[8]);
452  sql_type_.set_size(typeData[9]);
454  encoder_->readMetadata(f);
455  }
456 }
457 
458 void FileBuffer::writeMetadata(const int32_t epoch) {
459  // Right now stats page is size_ (in bytes), bufferType, encodingType,
460  // encodingDataType, numElements
462  writeHeader(page, -1, epoch, true);
463  FILE* f = fm_->getFileForFileId(page.fileId);
464  fseek(f, page.pageNum * METADATA_PAGE_SIZE + reservedHeaderSize_, SEEK_SET);
465  fwrite((int8_t*)&pageSize_, sizeof(size_t), 1, f);
466  fwrite((int8_t*)&size_, sizeof(size_t), 1, f);
467  vector<int32_t> typeData(
468  NUM_METADATA); // assumes we will encode hasEncoder, bufferType,
469  // encodingType, encodingBits all as int32_t
470  typeData[0] = METADATA_VERSION;
471  typeData[1] = static_cast<int32_t>(hasEncoder());
472  if (hasEncoder()) {
473  typeData[2] = static_cast<int32_t>(sql_type_.get_type());
474  typeData[3] = static_cast<int32_t>(sql_type_.get_subtype());
475  typeData[4] = sql_type_.get_dimension();
476  typeData[5] = sql_type_.get_scale();
477  typeData[6] = static_cast<int32_t>(sql_type_.get_notnull());
478  typeData[7] = static_cast<int32_t>(sql_type_.get_compression());
479  typeData[8] = sql_type_.get_comp_param();
480  typeData[9] = sql_type_.get_size();
481  }
482  fwrite((int8_t*)&(typeData[0]), sizeof(int32_t), typeData.size(), f);
483  if (hasEncoder()) { // redundant
484  encoder_->writeMetadata(f);
485  }
486  metadataPages_.push(page, epoch);
487 }
488 
489 void FileBuffer::append(int8_t* src,
490  const size_t numBytes,
491  const MemoryLevel srcBufferType,
492  const int32_t deviceId) {
493  setAppended();
494 
495  size_t startPage = size_ / pageDataSize_;
496  size_t startPageOffset = size_ % pageDataSize_;
497  size_t numPagesToWrite =
498  (numBytes + startPageOffset + pageDataSize_ - 1) / pageDataSize_;
499  size_t bytesLeft = numBytes;
500  int8_t* curPtr = src; // a pointer to the current location in dst being written to
501  size_t initialNumPages = multiPages_.size();
502  size_ = size_ + numBytes;
503  int32_t epoch = fm_->epoch();
504  for (size_t pageNum = startPage; pageNum < startPage + numPagesToWrite; ++pageNum) {
505  Page page;
506  if (pageNum >= initialNumPages) {
507  page = addNewMultiPage(epoch);
508  writeHeader(page, pageNum, epoch);
509  } else {
510  // we already have a new page at current
511  // epoch for this page - just grab this page
512  page = multiPages_[pageNum].current().page;
513  }
514  CHECK(page.fileId >= 0); // make sure page was initialized
515  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
516  size_t bytesWritten;
517  if (pageNum == startPage) {
518  bytesWritten = fileInfo->write(
519  page.pageNum * pageSize_ + startPageOffset + reservedHeaderSize_,
520  min(pageDataSize_ - startPageOffset, bytesLeft),
521  curPtr);
522  } else {
523  bytesWritten = fileInfo->write(page.pageNum * pageSize_ + reservedHeaderSize_,
524  min(pageDataSize_, bytesLeft),
525  curPtr);
526  }
527  curPtr += bytesWritten;
528  bytesLeft -= bytesWritten;
529  }
530  CHECK(bytesLeft == 0);
531 }
532 
533 void FileBuffer::write(int8_t* src,
534  const size_t numBytes,
535  const size_t offset,
536  const MemoryLevel srcBufferType,
537  const int32_t deviceId) {
538  if (srcBufferType != CPU_LEVEL) {
539  LOG(FATAL) << "Unsupported Buffer type";
540  }
541 
542  bool tempIsAppended = false;
543  setDirty();
544  if (offset < size_) {
545  setUpdated();
546  }
547  if (offset + numBytes > size_) {
548  tempIsAppended = true; // because is_appended_ could have already been true - to
549  // avoid rewriting header
550  setAppended();
551  size_ = offset + numBytes;
552  }
553 
554  size_t startPage = offset / pageDataSize_;
555  size_t startPageOffset = offset % pageDataSize_;
556  size_t numPagesToWrite =
557  (numBytes + startPageOffset + pageDataSize_ - 1) / pageDataSize_;
558  size_t bytesLeft = numBytes;
559  int8_t* curPtr = src; // a pointer to the current location in dst being written to
560  size_t initialNumPages = multiPages_.size();
561  int32_t epoch = fm_->epoch();
562 
563  if (startPage >
564  initialNumPages) { // means there is a gap we need to allocate pages for
565  for (size_t pageNum = initialNumPages; pageNum < startPage; ++pageNum) {
566  Page page = addNewMultiPage(epoch);
567  writeHeader(page, pageNum, epoch);
568  }
569  }
570  for (size_t pageNum = startPage; pageNum < startPage + numPagesToWrite; ++pageNum) {
571  Page page;
572  if (pageNum >= initialNumPages) {
573  page = addNewMultiPage(epoch);
574  writeHeader(page, pageNum, epoch);
575  } else if (multiPages_[pageNum].current().epoch <
576  epoch) { // need to create new page b/c this current one lags epoch and we
577  // can't overwrite it also need to copy if we are on first or
578  // last page
579  Page lastPage = multiPages_[pageNum].current().page;
580  page = fm_->requestFreePage(pageSize_, false);
581  multiPages_[pageNum].push(page, epoch);
582  if (pageNum == startPage && startPageOffset > 0) {
583  // copyPage takes care of header offset so don't worry
584  // about it
585  copyPage(lastPage, page, startPageOffset, 0);
586  }
587  if (pageNum == (startPage + numPagesToWrite - 1) &&
588  bytesLeft > 0) { // bytesLeft should always > 0
589  copyPage(lastPage,
590  page,
591  pageDataSize_ - bytesLeft,
592  bytesLeft); // these would be empty if we're appending but we won't
593  // worry about it right now
594  }
595  writeHeader(page, pageNum, epoch);
596  } else {
597  // we already have a new page at current
598  // epoch for this page - just grab this page
599  page = multiPages_[pageNum].current().page;
600  }
601  CHECK(page.fileId >= 0); // make sure page was initialized
602  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
603  size_t bytesWritten;
604  if (pageNum == startPage) {
605  bytesWritten = fileInfo->write(
606  page.pageNum * pageSize_ + startPageOffset + reservedHeaderSize_,
607  min(pageDataSize_ - startPageOffset, bytesLeft),
608  curPtr);
609  } else {
610  bytesWritten = fileInfo->write(page.pageNum * pageSize_ + reservedHeaderSize_,
611  min(pageDataSize_, bytesLeft),
612  curPtr);
613  }
614  curPtr += bytesWritten;
615  bytesLeft -= bytesWritten;
616  if (tempIsAppended && pageNum == startPage + numPagesToWrite - 1) { // if last page
617  //@todo below can lead to undefined - we're overwriting num
618  // bytes valid at checkpoint
619  writeHeader(page, 0, multiPages_[0].current().epoch, true);
620  }
621  }
622  CHECK(bytesLeft == 0);
623 }
624 
625 std::string FileBuffer::dump() const {
626  std::stringstream ss;
627  ss << "chunk_key = " << show_chunk(chunkKey_) << "\n";
628  ss << "has_encoder = " << (hasEncoder() ? "true\n" : "false\n");
629  ss << "size_ = " << size_ << "\n";
630  return ss.str();
631 }
632 
633 } // namespace File_Namespace
virtual std::vector< MultiPage > getMultiPage() const
Returns vector of MultiPages in the FileBuffer.
Definition: FileBuffer.h:142
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:315
void set_compression(EncodingType c)
Definition: sqltypes.h:414
void set_size(int s)
Definition: sqltypes.h:412
#define METADATA_PAGE_SIZE
Definition: FileBuffer.h:37
std::vector< int > ChunkKey
Definition: types.h:37
void freePagesBeforeEpochForMultiPage(MultiPage &multiPage, const int32_t targetEpoch, const int32_t currentEpoch)
Definition: FileBuffer.cpp:192
void freePage(const Page &page, const bool isRolloff)
Definition: FileBuffer.cpp:157
HOST DEVICE int get_size() const
Definition: sqltypes.h:324
Page requestFreePage(size_t pagesize, const bool isMetadata)
Definition: FileMgr.cpp:909
void freePagesBeforeEpoch(const int32_t targetEpoch)
Definition: FileBuffer.cpp:202
static size_t headerBufferOffset_
Definition: FileBuffer.h:177
static size_t readForThread(FileBuffer *fileBuffer, const readThreadDS threadDS)
Definition: FileBuffer.cpp:241
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
void pop()
Purges the oldest Page.
Definition: Page.h:110
#define LOG(tag)
Definition: Logger.h:188
Page addNewMultiPage(const int32_t epoch)
Definition: FileBuffer.cpp:405
HOST DEVICE int get_scale() const
Definition: sqltypes.h:319
void writeMetadata(const int32_t epoch)
Definition: FileBuffer.cpp:458
void write(int8_t *src, const size_t numBytes, const size_t offset=0, const MemoryLevel srcMemoryLevel=CPU_LEVEL, const int32_t deviceId=-1) override
Writes the contents of source (src) into new versions of the affected logical pages.
Definition: FileBuffer.cpp:533
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:405
std::vector< MultiPage > multiPages_
Definition: FileBuffer.h:179
std::vector< MultiPage > multiPages
Definition: FileBuffer.cpp:238
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:314
Represents/provides access to contiguous data stored in the file system.
Definition: FileBuffer.h:57
void initEncoder(const SQLTypeInfo &tmp_sql_type)
std::string show_chunk(const ChunkKey &key)
Definition: types.h:85
size_t write(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:60
std::deque< EpochedPage > pageVersions
Definition: Page.h:81
void * checked_malloc(const size_t size)
Definition: checked_alloc.h:45
DEVICE auto copy(ARGS &&...args)
Definition: gpu_enabled.h:51
int32_t fileId
Definition: Page.h:47
size_t pageSize() const override
Returns the size in bytes of each page in the FileBuffer.
Definition: FileBuffer.h:132
void set_scale(int s)
Definition: sqltypes.h:409
std::string dump() const
Definition: FileBuffer.cpp:625
FILE * getFileForFileId(const int32_t fileId)
Returns FILE pointer associated with requested fileId.
Definition: FileMgr.cpp:1022
An AbstractBuffer is a unit of data management for a data manager.
size_t pageNum
unique identifier of the owning file
Definition: Page.h:48
void append(int8_t *src, const size_t numBytes, const MemoryLevel srcMemoryLevel=CPU_LEVEL, const int32_t deviceId=-1) override
Definition: FileBuffer.cpp:489
void freePage(int32_t pageId, const bool isRolloff)
Definition: FileInfo.cpp:206
void set_comp_param(int p)
Definition: sqltypes.h:415
FileInfo * getFileInfoForFileId(const int32_t fileId)
Definition: FileMgr.h:216
string version
Definition: setup.in.py:63
void writeHeader(Page &page, const int32_t pageId, const int32_t epoch, const bool writeMetadata=false)
Write header writes header at top of page in format.
Definition: FileBuffer.cpp:413
virtual size_t reservedHeaderSize() const
Definition: FileBuffer.h:139
#define CHECK_LE(x, y)
Definition: Logger.h:208
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:322
#define NUM_METADATA
Definition: FileBuffer.h:35
void set_dimension(int d)
Definition: sqltypes.h:406
void push(const Page &page, const int epoch)
Pushes a new page with epoch value.
Definition: Page.h:102
~FileBuffer() override
Destructor.
Definition: FileBuffer.cpp:131
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:66
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:316
std::unique_ptr< Encoder > encoder_
int32_t epoch()
Returns current value of epoch - should be one greater than recorded at last checkpoint.
Definition: FileMgr.h:273
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:323
FileBuffer(FileMgr *fm, const size_t pageSize, const ChunkKey &chunkKey, const size_t initialSize=0)
Constructs a FileBuffer object.
Definition: FileBuffer.cpp:39
void set_notnull(bool n)
Definition: sqltypes.h:411
#define CHECK(condition)
Definition: Logger.h:197
char * f
void reserve(const size_t numBytes) override
Definition: FileBuffer.cpp:136
std::vector< EpochedPage > freePagesBeforeEpoch(const int32_t target_epoch, const int32_t current_epoch)
Definition: Page.h:117
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:321
void read(int8_t *const dst, const size_t numBytes=0, const size_t offset=0, const MemoryLevel dstMemoryLevel=CPU_LEVEL, const int32_t deviceId=-1) override
Definition: FileBuffer.cpp:282
void readMetadata(const Page &page)
Definition: FileBuffer.cpp:432
void copyPage(Page &srcPage, Page &destPage, const size_t numBytes, const size_t offset=0)
Definition: FileBuffer.cpp:385
EpochedPage current() const
Returns a reference to the most recent version of the page.
Definition: Page.h:94
virtual size_t pageDataSize() const
Returns the size in bytes of the data portion of each page in the FileBuffer.
Definition: FileBuffer.h:135
#define METADATA_VERSION
Definition: FileBuffer.h:36
The MultiPage stores versions of the same logical page in a deque.
Definition: Page.h:79
A selection of helper methods for File I/O.
size_t getNumReaderThreads()
Returns number of threads defined by parameter num-reader-threads which should be used during initial...
Definition: FileMgr.h:306
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:404