Implement relevant methods for DataIngestionLayer

This commit is contained in:
Leonetienne
2022-05-31 14:25:23 +02:00
parent 3823eb6cc7
commit 2f6df42696
2 changed files with 86 additions and 3 deletions

View File

@@ -2,6 +2,9 @@
#include "CommandlineInterface.h"
#include "Configuration.h"
#include <iostream>
#include <istream>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace IO;
@@ -50,6 +53,9 @@ void DataIngestionLayer::Init() {
break;
}
initialized = true;
reachedEof = false;
// Temporary
std::size_t n_last_bytes_read = 0;
char buf[64];
@@ -65,8 +71,62 @@ void DataIngestionLayer::Init() {
return;
}
void DataIngestionLayer::ReadBlock() {
if (!initialized) {
throw std::runtime_error("Attempted to read on uninitialized DataIngestionLayer!");
}
if (!reachedEof) {
// Create buffer to read into
char buf[Block::BLOCK_SIZE];
memset(buf, 0, sizeof(buf));
// Read
in->read(buf, sizeof(buf));
// Fetch how much we've read
const std::size_t n_bytes_read = in->gcount();
// Is this fewer bytes than we requested?
if (n_bytes_read < sizeof(buf)) {
// Yes: EOF reached.
reachedEof = true;
}
// Construct a Block from this buf
Block block;
block.FromByteString(std::string(buf));
// Enqueue it
blocks.emplace(block);
}
return;
}
bool DataIngestionLayer::ReachedEOF() {
return reachedEof;
}
bool DataIngestionLayer::IsBlockReady() {
return blocks.size() > 0;
}
Block DataIngestionLayer::GetNextBlock() {
if (!IsBlockReady()) {
throw std::runtime_error("Attempted to get the next block, but there are none left!");
}
// Why... why not just return a T in pop()???
const Block popped = blocks.front();
blocks.pop();
return popped;
}
std::istream* DataIngestionLayer::in;
std::ifstream DataIngestionLayer::ifs;
std::istringstream DataIngestionLayer::iss;
bool DataIngestionLayer::reachedEof = false;
bool DataIngestionLayer::initialized = false;
std::queue<Block> DataIngestionLayer::blocks;