Skip to content

Commit

Permalink
Merge pull request hyrise#266 from bastih/master
Browse files Browse the repository at this point in the history
A small batch of improvements
  • Loading branch information
bastih committed Nov 20, 2013
2 parents a0fcca9 + c67ca2e commit 9fe3590
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 52 deletions.
13 changes: 13 additions & 0 deletions src/bin/units_storage/dict_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "gtest/gtest.h"

#include "storage/DictionaryFactory.h"
#include "storage/OrderPreservingDictionary.h"
#include "storage/OrderIndifferentDictionary.h"

namespace hyrise { namespace storage {

TEST(factory, creation) {
auto d = makeDictionary<OrderIndifferentDictionary>(IntegerType, 10);
}

}}
3 changes: 2 additions & 1 deletion src/lib/access/ExpressionScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "access/ExpressionScan.h"

#include "access/system/QueryParser.h"
#include "storage/DictionaryFactory.h"
#include "storage/OrderIndifferentDictionary.h"
#include "storage/MutableVerticalTable.h"
#include "storage/Table.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ void ExpressionScan::executePlanOperation() {
metadata.push_back(m);

std::vector<AbstractTable::SharedDictionaryPtr> dicts;
dicts.push_back(AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary>>(_expression->getType()));
dicts.push_back(storage::makeDictionary<OrderIndifferentDictionary>(_expression->getType()));

storage::atable_ptr_t exp_result = std::make_shared<Table>(&metadata, &dicts, 0, false);
exp_result->resize(input_size);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/access/GroupByScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "access/system/QueryParser.h"
#include "storage/ColumnMetadata.h"
#include "storage/DictionaryFactory.h"
#include "storage/HashTable.h"
#include "storage/PointerCalculator.h"
#include "storage/OrderIndifferentDictionary.h"
Expand Down Expand Up @@ -133,7 +134,7 @@ storage::atable_ptr_t GroupByScan::createResultTableLayout() {
for (const auto & fun: _aggregate_functions) {
ColumnMetadata *m = new ColumnMetadata(fun->columnName(), fun->getType());
metadata.push_back(m);
dictionaries.push_back(AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary> >(fun->getType()));
dictionaries.push_back(storage::makeDictionary<OrderIndifferentDictionary>(fun->getType()));
}
storage::atable_ptr_t agg_tab = std::make_shared<Table>(&metadata, &dictionaries, 0, false);

Expand Down
14 changes: 7 additions & 7 deletions src/lib/access/Layouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "access/Layouter.h"

#include "access/system/QueryParser.h"

#include "storage/DictionaryFactory.h"
#include "storage/OrderIndifferentDictionary.h"
#include "storage/Table.h"

Expand Down Expand Up @@ -64,12 +64,12 @@ void LayoutSingleTable::executePlanOperation() {


std::vector<AbstractTable::SharedDictionaryPtr > vd;
vd.push_back(DictionaryFactory<OrderIndifferentDictionary>::build(StringType));
vd.push_back(DictionaryFactory<OrderIndifferentDictionary>::build(IntegerType));
vd.push_back(DictionaryFactory<OrderIndifferentDictionary>::build(FloatType));
vd.push_back(DictionaryFactory<OrderIndifferentDictionary>::build(IntegerType));
vd.push_back(DictionaryFactory<OrderIndifferentDictionary>::build(FloatType));
vd.push_back(DictionaryFactory<OrderIndifferentDictionary>::build(FloatType));
vd.push_back(storage::makeDictionary<OrderIndifferentDictionary>(StringType));
vd.push_back(storage::makeDictionary<OrderIndifferentDictionary>(IntegerType));
vd.push_back(storage::makeDictionary<OrderIndifferentDictionary>(FloatType));
vd.push_back(storage::makeDictionary<OrderIndifferentDictionary>(IntegerType));
vd.push_back(storage::makeDictionary<OrderIndifferentDictionary>(FloatType));
vd.push_back(storage::makeDictionary<OrderIndifferentDictionary>(FloatType));

// Allocate a new Table
result = std::make_shared<Table>(&vc, &vd, _maxResults, false);
Expand Down
31 changes: 1 addition & 30 deletions src/lib/storage/AbstractDictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,13 @@

#include "storage/storage_types.h"

class AbstractDictionary;

template < template<typename T> class D >
struct DictionaryFactory {
static std::shared_ptr<AbstractDictionary> build(DataType type, size_t size = 0) {
switch (type) {
case IntegerType:
return std::make_shared<D<hyrise_int_t>>(size);
break;

case FloatType:
return std::make_shared<D<hyrise_float_t>>(size);
break;

case StringType:
return std::make_shared<D<hyrise_string_t>>(size);
break;

default:
throw std::runtime_error("Type not supported for dictionary");
}
}
};


class AbstractDictionary {
public:
virtual ~AbstractDictionary() {}

virtual bool isOrdered() = 0;

template< class Factory >
static std::shared_ptr<AbstractDictionary> dictionaryWithType(DataType type, size_t size = 0) {
return Factory::build(type, size);
}

virtual void reserve(size_t size) = 0;

virtual std::shared_ptr<AbstractDictionary> copy() = 0;
Expand All @@ -54,5 +25,5 @@ class AbstractDictionary {

};

#endif // SRC_LIB_STORAGE_ABSTRACTDICTIONARY_H_

#endif // SRC_LIB_STORAGE_ABSTRACTDICTIONARY_H_
5 changes: 3 additions & 2 deletions src/lib/storage/AbstractTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <storage/MutableVerticalTable.h>
#include <storage/Table.h>
#include <storage/DictionaryFactory.h>
#include <storage/Store.h>
#include <storage/ColumnMetadata.h>
#include <storage/ColumnMetadata.h>
Expand Down Expand Up @@ -58,12 +59,12 @@ hyrise::storage::atable_ptr_t AbstractTable::copy_structure_modifiable(const fie
if (fields != nullptr) {
for (const field_t & field: *fields) {
metadata.push_back(metadataAt(field));
dictionaries->push_back(AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary> >(metadataAt(field)->getType()));
dictionaries->push_back(makeDictionary<OrderIndifferentDictionary>(metadataAt(field)->getType()));
}
} else {
for (size_t i = 0; i < columnCount(); ++i) {
metadata.push_back(metadataAt(i));
dictionaries->push_back(AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary> >(metadataAt(i)->getType()));
dictionaries->push_back(makeDictionary<OrderIndifferentDictionary>(metadataAt(i)->getType()));
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/lib/storage/BitCompressedVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <mutex>
#include <string>
#include <stdexcept>
#include <type_traits>

#include "memory/MallocStrategy.h"
#include "storage/BaseAttributeVector.h"
Expand All @@ -18,7 +19,14 @@
#define WORD_LENGTH 64
#endif


// Compute the maximum number representable for n-bit width integers.
template <typename T,
class = typename std::enable_if<std::is_integral<T>::value>::type>
T maxValueForBits(const std::size_t bits) {
assert(bits <= sizeof(T) * 8);
if (bits == sizeof(T) * 8) return static_cast<T>(-1);
else return (1ull << bits) -1;
}
/*
can only save positive numbers
*/
Expand Down Expand Up @@ -96,7 +104,8 @@ class BitCompressedVector : public BaseAttributeVector<T> {
void set(size_t column, size_t row, T value) {
checkAccess(column, row);
#ifdef EXPENSIVE_ASSERTIONS
if (value >= (1 << _bits[column])) throw std::out_of_range("trying to insert value larger than can be stored");
if (value > maxValueForBits<T>(_bits[column]))
throw std::out_of_range("trying to insert value larger than can be stored");
#endif
auto offset = _blockOffset(row);
auto colOffset = _offsetForColumn(column);
Expand Down
36 changes: 36 additions & 0 deletions src/lib/storage/DictionaryFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef STORAGE_DICTIONARYFACTORY_H_
#define STORAGE_DICTIONARYFACTORY_H_

#include <memory>

#include "storage/AbstractDictionary.h"
#include "storage/meta_storage.h"

namespace hyrise { namespace storage { namespace detail {

template <template <typename T> class D>
struct DictionaryCreator {
using value_type = std::shared_ptr<AbstractDictionary>;
std::size_t size;

template <typename R>
std::shared_ptr<AbstractDictionary> operator()() {
return std::make_shared<D<R>>(size);
}
};

} // ns detail

// Create dictionary for type and size,
// Usage: makeDictionary<OrderIndifferentDictionary>(IntegerType, 10),
// which returns shared_ptr<OrderIndifferentDictionary<hyrise_int_t>> (init-ed
// for 10 elements)
template <template <typename T> class D>
std::shared_ptr<AbstractDictionary> makeDictionary(DataType type, size_t size=0) {
detail::DictionaryCreator<D> dc {size};
return type_switch<hyrise_basic_types>()(type, dc);
}

}}

#endif
27 changes: 21 additions & 6 deletions src/lib/storage/Table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <cmath>

#include "storage/AttributeVectorFactory.h"
#include "storage/DictionaryFactory.h"
#include "storage/ValueIdMap.hpp"

using namespace hyrise::storage;

Table::Table(
std::vector<const ColumnMetadata *> *m,
std::vector<SharedDictionary> *d,
Expand Down Expand Up @@ -35,9 +38,7 @@ Table::Table(
// for empty tables is not useful
if (!sorted) {
for (size_t i = 0; i < width; i++) {
_dictionaries[i] = AbstractDictionary::dictionaryWithType <
DictionaryFactory<OrderIndifferentDictionary> > (
_metadata[i]->getType(), initial_size);
_dictionaries[i] = makeDictionary<OrderIndifferentDictionary>(_metadata[i]->getType(), initial_size);
}
}
}
Expand All @@ -57,6 +58,19 @@ Table::Table(
}


Table::Table(std::vector<ColumnMetadata> m,
SharedAttributeVector av,
std::vector<SharedDictionary> dicts) :
tuples(av),
_metadata(m.size()),
_dictionaries(dicts),
width(m.size()) {
assert(m.size() == dicts.size() && "Metadata size and dictionaries must match");
for (size_t i = 0; i < width; i++) {
_metadata[i] = new ColumnMetadata(m.at(i));
}
}


hyrise::storage::atable_ptr_t Table::copy_structure(const field_list_t *fields, const bool reuse_dict, const size_t initial_size, const bool with_containers, const bool compressed) const {

Expand Down Expand Up @@ -98,17 +112,18 @@ hyrise::storage::atable_ptr_t Table::copy_structure_modifiable(const field_list_
std::vector<AbstractTable::SharedDictionaryPtr > *dictionaries = new std::vector<AbstractTable::SharedDictionaryPtr >;

if (fields != nullptr) {
for (const field_t & field: *fields) {
for (const field_t & field: *fields) {
metadata.push_back(metadataAt(field));
dictionaries->push_back(AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary> >(metadataAt(field)->getType()));
}
} else {
for (size_t i = 0; i < columnCount(); ++i) {
metadata.push_back(metadataAt(i));
dictionaries->push_back(AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary> >(metadataAt(i)->getType()));
}
}

for (const auto& field: metadata) {
dictionaries->push_back(makeDictionary<OrderIndifferentDictionary>(field->getType()));
}

auto result = std::make_shared<Table>(&metadata, dictionaries, initial_size, false, _compressed);
delete dictionaries;
Expand Down
7 changes: 7 additions & 0 deletions src/lib/storage/Table.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class Table : public AbstractTable {
bool sorted = true,
bool compressed = true);

// Construct table from vector of metadata,
// a storage vector and dictionaries
// Expects: m.size() == dicts.size()
Table(std::vector<ColumnMetadata> m,
SharedAttributeVector av,
std::vector<SharedDictionary> dicts);

~Table();

size_t size() const;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/storage/TableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "storage/TableBuilder.h"

#include "storage/AbstractTable.h"
#include "storage/DictionaryFactory.h"
#include "storage/OrderIndifferentDictionary.h"
#include "storage/Table.h"
#include "storage/MutableVerticalTable.h"
Expand Down Expand Up @@ -31,7 +32,7 @@ hyrise::storage::atable_ptr_t TableBuilder::createTable(param_list::param_list_t
for (; begin != end; ++begin) {
vc.push_back(ColumnMetadata::metadataFromString((*begin).type, (*begin).name));
vd.push_back(
DictionaryFactory<OrderIndifferentDictionary>::build(vc.back()->getType()));
makeDictionary<OrderIndifferentDictionary>(vc.back()->getType()));
}

auto tmp = std::make_shared<Table>(&vc, &vd, 0, 0, compressed);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/storage/TableGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <helper/types.h>

#include <storage/AbstractTable.h>
#include <storage/DictionaryFactory.h>
#include <storage/MutableVerticalTable.h>
#include <storage/AbstractMergeStrategy.h>
#include <storage/SequentialHeapMerger.h>
Expand Down Expand Up @@ -363,7 +364,7 @@ hyrise::storage::atable_ptr_t TableGenerator::create_empty_table(size_t rows, st
md.push_back(m);

auto d = new std::vector<AbstractTable::SharedDictionaryPtr>;
auto new_dict = AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderPreservingDictionary> >(IntegerType);
auto new_dict = makeDictionary<OrderPreservingDictionary>(IntegerType);
d->push_back(new_dict);
dicts.push_back(d);
}
Expand Down Expand Up @@ -433,7 +434,7 @@ hyrise::storage::atable_ptr_t TableGenerator::create_empty_table_modifiable(size
md.push_back(m);

auto d = new std::vector<AbstractTable::SharedDictionaryPtr>;
auto new_dict = AbstractDictionary::dictionaryWithType<DictionaryFactory<OrderIndifferentDictionary> >(IntegerType);
auto new_dict = makeDictionary<OrderIndifferentDictionary>(IntegerType);
d->push_back(new_dict);
dicts.push_back(d);
}
Expand Down

0 comments on commit 9fe3590

Please sign in to comment.