Skip to content

Commit

Permalink
Adding wrapper for fstream files
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomestre2 committed Dec 5, 2018
1 parent ed60e92 commit 44667af
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ io_file

# Visual Studio
.vs
settings.json
.vscode

# QtCreator files
*.user
Expand Down
2 changes: 1 addition & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ add_subdirectory(types)
add_subdirectory(value_operations)
add_subdirectory(vector_operations)

add_library(duckdb_common OBJECT constants.cpp enums.cpp file_system.cpp limits.cpp printable.cpp serializer.cpp string_util.cpp symbols.cpp types.cpp)
add_library(duckdb_common OBJECT constants.cpp enums.cpp file_system.cpp limits.cpp printable.cpp serializer.cpp string_util.cpp symbols.cpp types.cpp fstream_util.cpp)
set(ALL_OBJECT_FILES ${ALL_OBJECT_FILES} $<TARGET_OBJECTS:duckdb_common> PARENT_SCOPE)
2 changes: 1 addition & 1 deletion src/common/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstdio>
#include <dirent.h>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Expand Down Expand Up @@ -115,5 +116,4 @@ void MoveFile(const string &source, const string &target) {
throw IOException("Could not rename file!");
}
}

} // namespace duckdb
28 changes: 28 additions & 0 deletions src/common/fstream_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "common/fstream_util.hpp"

using namespace std;
using namespace duckdb;

fstream FstreamUtil::OpenFile(const string &file_path) {
fstream new_file;
new_file.open(file_path, ios::in | ios::out | ios::binary);
if (!new_file.good()) {
throw IOException("Could not open File!");
}

return new_file;
}

size_t FstreamUtil::GetFileSize(fstream &file) {
file.seekg(0, ios::end);
return file.tellg();
}

unique_ptr<char[]> FstreamUtil::ReadBinary(fstream &file) {
size_t file_size = GetFileSize(file);
file.seekg(0, ios::beg);
auto result = unique_ptr<char[]>(new char[file_size]);
file.read(result.get(), file_size);

return result;
}
30 changes: 30 additions & 0 deletions src/include/common/fstream_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// common/fstream_util.hpp
//
//
//===----------------------------------------------------------------------===//

#pragma once

#include "common/constants.hpp"
#include "common/exception.hpp"

#include <fstream>
#include <iostream>

namespace duckdb {
/**
* Fstream Utility Functions
*/
class FstreamUtil {
public:
/**
* Returns true if the needle string exists in the haystack
*/
std::fstream OpenFile(const string &file_path);
size_t GetFileSize(std::fstream &file);
unique_ptr<char[]> ReadBinary(std::fstream &file);
};
} // namespace duckdb
4 changes: 3 additions & 1 deletion src/parser/transform/expression/transform_subquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ unique_ptr<Expression> Transformer::TransformSubquery(SubLink *root) {
case EXPR_SUBLINK: {
return subquery_expr;
}
default: { throw NotImplementedException("Subquery of type %d not implemented\n", (int)root->subLinkType); }
default: {
throw NotImplementedException("Subquery of type %d not implemented\n", (int)root->subLinkType);
}
}
}
4 changes: 3 additions & 1 deletion src/parser/transform/tableref/transform_join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ unique_ptr<TableRef> Transformer::TransformJoin(JoinExpr *root) {
result->type = duckdb::JoinType::SEMI;
break;
}
default: { throw NotImplementedException("Join type %d not supported yet...\n", root->jointype); }
default: {
throw NotImplementedException("Join type %d not supported yet...\n", root->jointype);
}
}

// Check the type of left arg and right arg before transform
Expand Down
31 changes: 31 additions & 0 deletions src/storage/storage
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@



[duckdb]
duckdb.wal
main.duck [VERSION_NUMBER, schemas]


[sys] <- schema name
tables.duck


[integers] <- table name
columns.duckdb

[i]
chunk-1.data
chunk-2.data




INSERT INTO integers VALUES (22);



[i.tmp]
chunk-1.data

mv i.tmp i

52 changes: 13 additions & 39 deletions src/storage/storage_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#include "catalog/catalog_entry/schema_catalog_entry.hpp"
#include "common/exception.hpp"
#include "common/file_system.hpp"
#include "common/fstream_util.hpp"
#include "common/serializer.hpp"
#include "function/function.hpp"
#include "main/client_context.hpp"
#include "main/database.hpp"
#include "transaction/transaction_manager.hpp"

#include <fstream>

using namespace duckdb;
using namespace std;

Expand Down Expand Up @@ -70,6 +69,7 @@ void StorageManager::LoadDatabase() {

int StorageManager::LoadFromStorage() {
ClientContext context(database);
FstreamUtil file_util;

auto meta_info_path = JoinPath(path, DATABASE_INFO_FILE);
// read the meta information, if there is any
Expand All @@ -80,11 +80,7 @@ int StorageManager::LoadFromStorage() {

context.transaction.BeginTransaction();
// first read the meta information
ifstream meta_info;
meta_info.open(meta_info_path);
if (!meta_info.good()) {
throw IOException("Could not open meta file for writing!");
}
auto meta_info = file_util.OpenFile(meta_info_path);
int64_t storage_version;
int iteration;

Expand All @@ -96,13 +92,9 @@ int StorageManager::LoadFromStorage() {
auto schema_path = JoinPath(storage_path_base, SCHEMA_FILE);

// read the list of schemas
// FIXME: turn into function Open()
ifstream schema_file;
schema_file.open(schema_path);
if (!schema_file.good()) {
throw IOException("Could not open schema for writing!");
}
// END OF FIXME

auto schema_file = file_util.OpenFile(schema_path);

string schema_name;
while (getline(schema_file, schema_name)) {
// create the schema in the catalog
Expand All @@ -117,11 +109,7 @@ int StorageManager::LoadFromStorage() {

// read the list of schemas
// FIXME: turn into function Open()
ifstream table_list_file;
table_list_file.open(table_list_path);
if (!table_list_file.good()) {
throw IOException("Could not open schema for writing!");
}
auto table_list_file = file_util.OpenFile(table_list_path);
// END OF FIXME
string table_name;
while (getline(table_list_file, table_name)) {
Expand All @@ -130,21 +118,14 @@ int StorageManager::LoadFromStorage() {
auto table_meta_name = JoinPath(table_directory_path, TABLE_FILE);

// FIXME: turn into function Open()
ifstream table_file;
table_file.open(table_meta_name, ifstream::binary);
if (!table_file.good()) {
throw IOException("Could not open table file for writing!");
}
auto table_file = file_util.OpenFile(table_meta_name);
// END OF FIXME
// FIXME: turn into function ReadBinary()
table_file.seekg(0, ios::end);
size_t table_file_size = table_file.tellg();
auto result = unique_ptr<char[]>(new char[table_file_size]);
table_file.seekg(0, ios::beg);
table_file.read(result.get(), table_file_size);
auto result = file_util.ReadBinary(table_file);
// END OF FIXME

// deserialize the CreateTableInformation
auto table_file_size = file_util.GetFileSize(table_file);
Deserializer source((uint8_t *)result.get(), table_file_size);
auto info = TableCatalogEntry::Deserialize(source);
// create the table inside the catalog
Expand All @@ -164,21 +145,14 @@ int StorageManager::LoadFromStorage() {
}

// FIXME: turn into function Open()
ifstream chunk_file;
chunk_file.open(chunk_name, ifstream::binary);
if (!chunk_file.good()) {
throw IOException("Could not open table file for writing!");
}
auto chunk_file = file_util.OpenFile(chunk_name);
// END OF FIXME
// FIXME: turn into function ReadBinary()
chunk_file.seekg(0, ios::end);
auto chunk_file_size = chunk_file.tellg();
auto result = unique_ptr<char[]>(new char[chunk_file_size]);
chunk_file.seekg(0, ios::beg);
chunk_file.read(result.get(), chunk_file_size);
auto result = file_util.ReadBinary(chunk_file);
// END OF FIXME
// deserialize the chunk
DataChunk insert_chunk;
auto chunk_file_size = file_util.GetFileSize(chunk_file);
Deserializer source((uint8_t *)result.get(), chunk_file_size);
insert_chunk.Deserialize(source);
// insert the chunk into the table
Expand Down
4 changes: 3 additions & 1 deletion test/sqlite/slt_duckdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ static int duckdbQuery(void *pConn, /* Connection created by xConnect */
snprintf(buffer, BUFSIZ, "%s", str ? (str == 0 ? "(empty)" : str) : "NULL");
break;
}
default: { fprintf(stderr, "%s\n", "UNKNOWN"); }
default: {
fprintf(stderr, "%s\n", "UNKNOWN");
}
}
(*pazResult)[r * result.column_count + c] = buffer;
}
Expand Down

0 comments on commit 44667af

Please sign in to comment.