DataNode.hpp 3.74 KB
Newer Older
Philipp Götze's avatar
Philipp Götze committed
1
/*
Philipp Götze's avatar
Philipp Götze committed
2
 * Copyright (C) 2017 DBIS Group - TU Ilmenau, All Rights Reserved.
Philipp Götze's avatar
Philipp Götze committed
3
 *
Philipp Götze's avatar
Philipp Götze committed
4
 * This file is part of PTable.
Philipp Götze's avatar
Philipp Götze committed
5
 *
Philipp Götze's avatar
Philipp Götze committed
6 7 8 9
 * PTable is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Philipp Götze's avatar
Philipp Götze committed
10
 *
Philipp Götze's avatar
Philipp Götze committed
11
 * PTable is distributed in the hope that it will be useful,
Philipp Götze's avatar
Philipp Götze committed
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Philipp Götze's avatar
Philipp Götze committed
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Philipp Götze's avatar
Philipp Götze committed
14 15 16
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Philipp Götze's avatar
Philipp Götze committed
17
 * along with PTable.  If not, see <http://www.gnu.org/licenses/>.
Philipp Götze's avatar
Philipp Götze committed
18 19 20 21 22 23
 */

#ifndef DataNode_hpp_
#define DataNode_hpp_

#include <array>
24
#include <unordered_map>
Philipp Götze's avatar
Philipp Götze committed
25

26
#include "config.h"
27
#include <libpmemobj++/allocator.hpp>
Philipp Götze's avatar
Philipp Götze committed
28 29 30 31
#include <libpmemobj++/persistent_ptr.hpp>

namespace ptable {

32 33
using nvml::obj::allocator;
using nvml::obj::p;
Philipp Götze's avatar
Philipp Götze committed
34 35 36
using nvml::obj::persistent_ptr;

/** Positions in NVM_Block */
37 38 39 40 41 42
constexpr int gBDCCRangePos1 = 0;
constexpr int gBDCCRangePos2 = 4;
constexpr int gCountPos = 8;
constexpr int gFreeSpacePos = 12;
constexpr int gSmaOffsetPos = 14;
constexpr int gDataOffsetPos = 16;
Philipp Götze's avatar
Philipp Götze committed
43 44

/** Sizes/Lengths in NVM_Block */
45 46 47 48
constexpr int gFixedHeaderSize = 14;
constexpr int gBDCCValueSize = 4;
constexpr int gAttrOffsetSize = 4;
constexpr int gOffsetSize = 2;
Philipp Götze's avatar
Philipp Götze committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

/** The size of a single block in persistent memory */
static constexpr std::size_t gBlockSize = 1 << 12; // 12->4KB, 15->32KB, max 16 due to data types

/**
 * \brief This type represents a byte array used for persistent structures.
 *
 * A BDCC_Block is a PAX oriented data block with the following structure for 32KB:
 * <bdcc_range><bdcc_cnt><sma_offset_0><data_offset_0> ...<sma_offset_n><data_offset_n>
 * <sma_min_0><sma_max_0><data_vector_0> ... <sma_min_n><sma_max_n><data_vector_n>
 *  0 bdcc_range          -> long (x2) - 8 Byte
 *  8 bdcc_cnt            -> long - 4 Byte
 * 12 free_space         -> unsigned short
 * for each attribute:
 * 14 sma_offset_x       -> unsigned short - 2 Byte (depends on block size)
 * 16 data_offset_x      -> unsigned short
 * ...
 *
 * for each attribute (int, double):
 *  . sma_min_x          -> size of attributes data type
 *  . sma_max_x          -> size of attributes data type
 *  . data_vector        -> size of attributes data type * bdcc_cnt
 *  ...
 *
 * for each attribute (string - data starts at the end of the minipage):
 *  . sma_min_offset_x   -> unsigned short
 *  . sma_max_offset_x   -> unsigned short
 *  . data_offset_vector -> unsigned short * bdcc_cnt
 *  . ...
 *  . data               -> size of all strings + bdcc_cnt (Nul termination)
 */
using BDCC_Block = typename std::array<uint8_t, gBlockSize>;

template<typename KeyType>
struct DataNode {
84
  using DeletedVector = std::vector<uint16_t, allocator<uint16_t>>;
Philipp Götze's avatar
Philipp Götze committed
85 86 87 88 89
  using KeyVector = std::array<KeyType, 8192>;
  using HistogramType = std::unordered_map<uint32_t,
                                          std::size_t,
                                          std::hash<uint32_t>,
                                          std::equal_to<uint32_t>,
90
                                          allocator<uint32_t>>;
Philipp Götze's avatar
Philipp Götze committed
91 92 93 94 95 96 97

  DataNode() : next(nullptr) {}
  DataNode(BDCC_Block _block) : next(nullptr), block(_block) {}

  persistent_ptr<struct DataNode> next;
  p<BDCC_Block> block;
  p<KeyVector> keys;
98
  p<DeletedVector> deleted;
Philipp Götze's avatar
Philipp Götze committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
  p<HistogramType> histogram;

  const uint32_t calcAverageBDCC() const {
    auto sum = 0u;
    for(const auto &bdccValue : histogram.get_ro()) {
      sum += bdccValue.first * bdccValue.second;
    }
    return sum / reinterpret_cast<const uint32_t &>(block.get_ro()[gCountPos]);
  }
};

} /* end namespace ptable */

#endif /* DataNode_hpp_ */