Commit 13ec7cdb authored by Philipp Götze's avatar Philipp Götze
Browse files

Added submitted solution from group1 - task1

parent 69691f2b
# This code depends on make tool being used
DEPFILES=$(wildcard $(addsuffix .d, ${OBJECTFILES} ${TESTOBJECTFILES}))
ifneq (${DEPFILES},)
include ${DEPFILES}
endif
/**
* Datenbank Implementierungstechniken
*/
#include "Block.h"
std::vector<uint8_t> Block::getData() {
// evtl aus Datei auslesen?
return Block_Data;
}
void Block::writeData(std::vector<uint8_t> data) {
// check size
if (data.size() == (long) Block_Size)
Block_Data = data;
// evtl in Datei schreiben?
}
/**
* Konstruktor
*/
Block::Block(uint8_t number, uint8_t size) : Block_Number(number), Block_Size(size) {
Block_Data.reserve((long)size);
// evtl in Datei schreiben?
}
/**
* Destruktor
*/
Block::~Block() {
// evtl aus Datei löschen?
}
/**
* Datenbank Implementierungstechniken
*/
/*
* File: Block.h
* Author: vanessa & daniel
*
* Created on 18. April 2017, 12:38
*/
#ifndef BLOCK_H //BLOCK_H ist Guard und muss dann in den sourcefiles definiert werden
#define BLOCK_H //Guards dienen zum Schutz gegen doppelte Kompilierung
#include <vector>
#include <cstdint>
class Block
{
private:
std::vector<uint8_t> Block_Data;
public:
uint8_t Block_Number; // Key
uint8_t Block_Size;
std::vector<uint8_t> // Data
getData();
void
writeData(std::vector<uint8_t> data);
// Konstruktor
Block(uint8_t number, uint8_t size);
// Destruktor
~Block();
};
#endif /* BLOCK_H */
\ No newline at end of file
/**
* Datenbank Implementierungstechniken
*/
#include <iostream> // cout
#include <string.h> // strncpy
#include "BlockFile.h"
Block BlockFile::allocate(uint8_t number, uint8_t size) {
Block b (number, size);
retMap = bitmap.insert(std::make_pair(number, &b));
if (!retMap.second)
std::cout << "Fehler: Konnte Block nicht in bitmap aufnehmen.";
block_count++;
return b;
}
void BlockFile::dispose(Block b) {
bitmap.erase(b.Block_Number);
block_count--;
}
void BlockFile::dispose(uint8_t number) {
bitmap.erase(number);
block_count--;
}
Block BlockFile::read(uint8_t number) {
return *bitmap[number];
}
void BlockFile::write(Block b) {
bitmap[b.Block_Number] = &b;
}
void BlockFile::initBlockFile(std::vector<uint8_t>) {
// @TODO: Datenstruktur auslesen und in der bitmap speichern
}
/**
* Konstruktor
*/
BlockFile::BlockFile(char* filename) {
// @TODO: File einlesen, bitmap erstellen
file_name = new char[sizeof(filename) + 1];
strncpy(file_name, filename, sizeof(filename)-1);
std::cout << "BlockFile created! Filename: " << file_name << std::endl;
}
/**
* Destruktor
*/
BlockFile::~BlockFile() {
delete[] file_name;
}
/**
* Datenbank Implementierungstechniken
*/
/*
* File: BlockFile.h
* Author: vanessa & daniel
*
* Created on 18. April 2017, 12:38
*/
#ifndef BLOCKFILE_H
#define BLOCKFILE_H
#include <map> // Hashmap
#include <cstdint> // uint8_t
#include <vector>
#include "Block.h"
class BlockFile
{
private:
char* file_name;
uint8_t block_count = 0; // Anzahl Bloecke in BlockFile
std::map<uint8_t, Block*> bitmap; // <Key, Value>
std::pair< std::map<uint8_t, Block*>::iterator, bool > retMap; // zum checken
public:
Block
allocate(uint8_t number, uint8_t size);
void
dispose(Block b); // delete by Block
void
dispose(uint8_t number); // delete by Key
Block
read(uint8_t number);
void
write(Block b);
void
initBlockFile(std::vector<uint8_t>);
// Konstruktor
BlockFile(char* filename);
// Destruktor
~BlockFile();
};
#endif /* BLOCKFILE_H */
/**
* Datenbank Implementierungstechniken
*/
/*
* File: iohandler.cpp
* Author: vanessa
*
* Created on 25. April 2017, 18:15
*/
#include <stdio.h> // delete
#include <fstream> // file i/o
#include "IoHandler.h"
using namespace std;
void IoHandler::createFile() {
ofstream file (file_name, ios::out|ios::binary);
file.close();
}
bool IoHandler::deleteFile() {
return remove(file_name) == 0; // true, falls erfolgreich
}
vector<uint8_t> IoHandler::readFile() {
vector<uint8_t> buffer;
ifstream file (file_name, ios::in|ios::binary|ios::ate); // starte am Ende
if (file.is_open()){
size_t size = file.tellg(); // Ende des Files = size
file.seekg(0, ios::beg); // springe an Anfang
buffer.resize(size/sizeof(uint8_t));
file.read((char*) buffer.data(), size);
file.close();
}
return buffer;
}
bool IoHandler::writeFile(vector<uint8_t> block) {
ofstream file (file_name, ios::out|ios::binary);
if (file.is_open()){
file.write((char*) &block[0], block.size()*sizeof(uint8_t));
file.close();
return true;
} else {
return false;
}
}
IoHandler::IoHandler(char* filename) : file_name(filename) {
}
IoHandler::~IoHandler() {
}
/**
* Datenbank Implementierungstechniken
*/
/*
* File: iohandler.h
* Author: vanessa
*
* Created on 25. April 2017, 18:15
*/
#ifndef IOHANDLER_H
#define IOHANDLER_H
#include <vector>
#include <cstdint>
class IoHandler
{
private:
char* file_name;
public:
void
createFile();
std::vector<uint8_t>
readFile();
bool
writeFile(std::vector<uint8_t> block);
bool
deleteFile();
IoHandler(char* filename);
~IoHandler();
};
#endif /* IOHANDLER_H */
#
# There exist several targets which are by default empty and which can be
# used for execution of your targets. These targets are usually executed
# before and after some main targets. They are:
#
# .build-pre: called before 'build' target
# .build-post: called after 'build' target
# .clean-pre: called before 'clean' target
# .clean-post: called after 'clean' target
# .clobber-pre: called before 'clobber' target
# .clobber-post: called after 'clobber' target
# .all-pre: called before 'all' target
# .all-post: called after 'all' target
# .help-pre: called before 'help' target
# .help-post: called after 'help' target
#
# Targets beginning with '.' are not intended to be called on their own.
#
# Main targets can be executed directly, and they are:
#
# build build a specific configuration
# clean remove built files from a configuration
# clobber remove all built files
# all build all configurations
# help print help mesage
#
# Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and
# .help-impl are implemented in nbproject/makefile-impl.mk.
#
# Available make variables:
#
# CND_BASEDIR base directory for relative paths
# CND_DISTDIR default top distribution directory (build artifacts)
# CND_BUILDDIR default top build directory (object files, ...)
# CONF name of current configuration
# CND_PLATFORM_${CONF} platform name (current configuration)
# CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration)
# CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration)
# CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration)
# CND_PACKAGE_DIR_${CONF} directory of package (current configuration)
# CND_PACKAGE_NAME_${CONF} name of package (current configuration)
# CND_PACKAGE_PATH_${CONF} path to package (current configuration)
#
# NOCDDL
# Environment
MKDIR=mkdir
CP=cp
CCADMIN=CCadmin
# build
build: .build-post
.build-pre:
# Add your pre 'build' code here...
.build-post: .build-impl
# Add your post 'build' code here...
# clean
clean: .clean-post
.clean-pre:
# Add your pre 'clean' code here...
.clean-post: .clean-impl
# Add your post 'clean' code here...
# clobber
clobber: .clobber-post
.clobber-pre:
# Add your pre 'clobber' code here...
.clobber-post: .clobber-impl
# Add your post 'clobber' code here...
# all
all: .all-post
.all-pre:
# Add your pre 'all' code here...
.all-post: .all-impl
# Add your post 'all' code here...
# build tests
build-tests: .build-tests-post
.build-tests-pre:
# Add your pre 'build-tests' code here...
.build-tests-post: .build-tests-impl
# Add your post 'build-tests' code here...
# run tests
test: .test-post
.test-pre: build-tests
# Add your pre 'test' code here...
.test-post: .test-impl
# Add your post 'test' code here...
# help
help: .help-post
.help-pre:
# Add your pre 'help' code here...
.help-post: .help-impl
# Add your post 'help' code here...
# include project implementation makefile
include nbproject/Makefile-impl.mk
# include project make variables
include nbproject/Makefile-variables.mk
/**
* Datenbank Implementierungstechniken
*
* Dieses Projekt basiert auf dem einfachen key-value Model, da dieses auch
* für die spätere Implementierung eines LMS-Tree's notwendig ist.
*
* Von: Vanessa Gombisch & Daniel Contu
*/
//#include <cstdlib>
#include <stdio.h> // fopen
#include <iostream> // cout
#include "BlockFile.h"
#include "IoHandler.h"
using namespace std;
int main(int argc, char** argv) {
char filename[128];
cout << "Bitte Filename fuer Datafile angeben\n";
cin >> filename;
BlockFile database(filename);
IoHandler handler(filename);
// check if file exists
if(FILE * file = fopen(filename, "r")) {
fclose(file);
// @TODO: Read File
cout << "Found File! :)\n";
// Lese Rohen Vektor aus dem File und initialisiere BlockFile
database.initBlockFile(handler.readFile());
} else {
// @TODO: Create File
cout << "Didn't found File! :(\n";
cout << "Creating new one\n";
handler.createFile();
}
return EXIT_SUCCESS;
}
#
# Generated Makefile - do not edit!
#
# Edit the Makefile in the project folder instead (../Makefile). Each target
# has a -pre and a -post target defined where you can add customized code.
#
# This makefile implements configuration specific macros and targets.
# Environment
MKDIR=mkdir
CP=cp
GREP=grep
NM=nm
CCADMIN=CCadmin
RANLIB=ranlib
CC=gcc
CCC=g++-5
CXX=g++-5
FC=gfortran
AS=as
# Macros
CND_PLATFORM=GNU-Linux
CND_DLIB_EXT=so
CND_CONF=Debug
CND_DISTDIR=dist
CND_BUILDDIR=build
# Include project Makefile
include Makefile
# Object Directory
OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/Block.o \
${OBJECTDIR}/BlockFile.o \
${OBJECTDIR}/IoHandler.o \
${OBJECTDIR}/main.o
# C Compiler Flags
CFLAGS=
# CC Compiler Flags
CCFLAGS=
CXXFLAGS=
# Fortran Compiler Flags
FFLAGS=
# Assembler Flags
ASFLAGS=
# Link Libraries and Options
LDLIBSOPTIONS=
# Build Targets
.build-conf: ${BUILD_SUBPROJECTS}
"${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/db_it
${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/db_it: ${OBJECTFILES}
${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/db_it ${OBJECTFILES} ${LDLIBSOPTIONS}
${OBJECTDIR}/Block.o: Block.cpp
${MKDIR} -p ${OBJECTDIR}
${RM} "$@.d"
$(COMPILE.cc) -g -Wall -std=c++14 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Block.o Block.cpp
${OBJECTDIR}/BlockFile.o: BlockFile.cpp
${MKDIR} -p ${OBJECTDIR}
${RM} "$@.d"
$(COMPILE.cc) -g -Wall -std=c++14 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/BlockFile.o BlockFile.cpp
${OBJECTDIR}/IoHandler.o: IoHandler.cpp
${MKDIR} -p ${OBJECTDIR}
${RM} "$@.d"
$(COMPILE.cc) -g -Wall -std=c++14 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/IoHandler.o IoHandler.cpp
${OBJECTDIR}/main.o: main.cpp
${MKDIR} -p ${OBJECTDIR}
${RM} "$@.d"
$(COMPILE.cc) -g -Wall -std=c++14 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp
# Subprojects
.build-subprojects:
# Clean Targets
.clean-conf: ${CLEAN_SUBPROJECTS}
${RM} -r ${CND_BUILDDIR}/${CND_CONF}
${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/db_it
# Subprojects
.clean-subprojects:
# Enable dependency checking
.dep.inc: .depcheck-impl
include .dep.inc
#
# Generated Makefile - do not edit!
#
# Edit the Makefile in the project folder instead (../Makefile). Each target
# has a -pre and a -post target defined where you can add customized code.
#
# This makefile implements configuration specific macros and targets.
# Environment
MKDIR=mkdir
CP=cp
GREP=grep
NM=nm
CCADMIN=CCadmin
RANLIB=ranlib
CC=gcc
CCC=g++-5
CXX=g++-5
FC=gfortran
AS=as
# Macros
CND_PLATFORM=GNU-Linux
CND_DLIB_EXT=so
CND_CONF=Release
CND_DISTDIR=dist
CND_BUILDDIR=build
# Include project Makefile
include Makefile
# Object Directory
OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/Block.o \
${OBJECTDIR}/BlockFile.o \
${OBJECTDIR}/IoHandler.o \
${OBJECTDIR}/main.o
# C Compiler Flags
CFLAGS=
# CC Compiler Flags
CCFLAGS=
CXXFLAGS=
# Fortran Compiler Flags
FFLAGS=
# Assembler Flags
ASFLAGS=
# Link Libraries and Options
LDLIBSOPTIONS=