Akumuli
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
volume.h
1 #pragma once
2 // stdlib
3 #include <memory>
4 #include <cstdint>
5 #include <future>
6 
7 // libraries
8 #include <apr.h>
9 #include <apr_general.h>
10 #include <apr_file_io.h>
11 
12 // project
13 #include "akumuli.h"
14 #include "util.h"
15 
16 namespace Akumuli {
17 namespace StorageEngine {
18 
20 typedef uint32_t BlockAddr;
21 enum {
22  AKU_BLOCK_SIZE = 4096
23 };
24 
25 typedef std::unique_ptr<apr_pool_t, void (*)(apr_pool_t*)> AprPoolPtr;
26 typedef std::unique_ptr<apr_file_t, void (*)(apr_file_t*)> AprFilePtr;
27 
28 
38 class MetaVolume {
39  MemoryMappedFile mmap_;
40  size_t file_size_;
41  uint8_t* mmap_ptr_;
42  mutable std::vector<uint8_t> double_write_buffer_;
43 
44  MetaVolume(const char* path);
45 public:
46 
53  static void create_new(const char* path, size_t capacity, uint32_t const* vol_capacities);
54 
60  static std::unique_ptr<MetaVolume> open_existing(const char* path);
61 
62  // Accessors
63 
65  std::tuple<aku_Status, uint32_t> get_nblocks(uint32_t id) const;
66 
68  std::tuple<aku_Status, uint32_t> get_capacity(uint32_t id) const;
69 
71  std::tuple<aku_Status, uint32_t> get_generation(uint32_t id) const;
72 
73  size_t get_nvolumes() const;
74 
75  // Mutators
76 
77  aku_Status update(uint32_t id, uint32_t nblocks, uint32_t capacity, uint32_t gen);
78 
80  aku_Status set_nblocks(uint32_t id, uint32_t nblocks);
81 
83  aku_Status set_capacity(uint32_t id, uint32_t nblocks);
84 
86  aku_Status set_generation(uint32_t id, uint32_t nblocks);
87 
89  void flush();
90 
92  aku_Status flush(uint32_t id);
93 };
94 
95 
96 class Volume
97 {
98  AprPoolPtr apr_pool_;
99  AprFilePtr apr_file_handle_;
100  uint32_t file_size_;
101  uint32_t write_pos_;
102 
103  Volume(const char* path, size_t write_pos);
104 public:
105 
111  static void create_new(const char* path, size_t capacity);
112 
119  static std::unique_ptr<Volume> open_existing(const char* path, size_t pos);
120 
121  // Mutators
122 
123  void reset();
124 
126  std::tuple<aku_Status, BlockAddr> append_block(const uint8_t* source);
127 
129  void flush();
130 
131  // Accessors
132 
134  aku_Status read_block(uint32_t ix, uint8_t* dest) const;
135 
137  uint32_t get_size() const;
138 };
139 
140 } // namespace V2
141 } // namespace Akumuli
std::tuple< aku_Status, uint32_t > get_capacity(uint32_t id) const
Get total capacity of the volume.
Definition: volume.cpp:130
Definition: volume.h:96
static std::unique_ptr< Volume > open_existing(const char *path, size_t pos)
Definition: volume.cpp:223
static std::unique_ptr< MetaVolume > open_existing(const char *path)
Definition: volume.cpp:108
uint32_t get_size() const
Return size in blocks.
Definition: volume.cpp:263
std::tuple< aku_Status, uint32_t > get_generation(uint32_t id) const
Get volume's generation.
Definition: volume.cpp:139
std::tuple< aku_Status, BlockAddr > append_block(const uint8_t *source)
Append block to file (source size should be 4 at least BLOCK_SIZE)
Definition: volume.cpp:230
aku_Status set_generation(uint32_t id, uint32_t nblocks)
Set generation.
Definition: volume.cpp:177
void flush()
Flush volume.
Definition: volume.cpp:258
std::tuple< aku_Status, uint32_t > get_nblocks(uint32_t id) const
Get number of blocks in the volume.
Definition: volume.cpp:121
aku_Status set_capacity(uint32_t id, uint32_t nblocks)
Set volume capacity.
Definition: volume.cpp:168
void flush()
Flush entire file.
Definition: volume.cpp:186
aku_Status read_block(uint32_t ix, uint8_t *dest) const
Read filxed size block from file.
Definition: volume.cpp:245
static void create_new(const char *path, size_t capacity, uint32_t const *vol_capacities)
Definition: volume.cpp:86
aku_Status set_nblocks(uint32_t id, uint32_t nblocks)
Set number of used blocks for the volume.
Definition: volume.cpp:159
Definition: volume.h:38
Definition: util.h:72
static void create_new(const char *path, size_t capacity)
Definition: volume.cpp:218