ztsdb
allocator_factory.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef ALLOCATOR_FACTORY_HPP
20 #define ALLOCATOR_FACTORY_HPP
21 
22 
23 #include <memory>
24 #include <string>
25 #include <boost/filesystem.hpp>
26 #include "allocator.hpp"
27 
28 
29 namespace fsys = boost::filesystem;
30 
31 
32 namespace arr {
33 
34  struct AllocFactory {
35  virtual std::unique_ptr<baseallocator> get(const std::string& name) const = 0;
36  virtual std::unique_ptr<baseallocator> get(size_t nb) const = 0;
37  virtual ~AllocFactory() { }
38  virtual std::string to_string() const = 0;
39  inline virtual bool isPersistent() const { return false; }
40  inline virtual fsys::path getDirname() const {
41  throw std::range_error("no file mapping");
42  }
43  };
44 
45  struct MemAllocFactory : public AllocFactory {
46  inline std::unique_ptr<baseallocator> get(const std::string&) const {
47  return std::make_unique<memallocator>();
48  }
49  inline std::unique_ptr<baseallocator> get(size_t) const {
50  return std::make_unique<memallocator>();
51  }
52  inline std::string to_string() const {
53  return "malloc-based"s;
54  }
55  };
56 
57  struct FlexAllocFactory : public AllocFactory {
58  inline std::unique_ptr<baseallocator> get(const std::string&) const {
59  return std::make_unique<flexallocator>();
60  }
61  inline std::unique_ptr<baseallocator> get(size_t) const {
62  return std::make_unique<flexallocator>();
63  }
64  inline std::string to_string() const {
65  return "mmap-based"s;
66  }
67  };
68 
69  struct MmapAllocFactory : public AllocFactory {
70  MmapAllocFactory(const fsys::path& dirname_p, bool expectExists) : dirname(dirname_p) {
71  struct stat st = {0};
72  if (stat(dirname.c_str(), &st) == 0) {
73  if (!expectExists) {
74  throw std::range_error(dirname.c_str() + " already exists"s);
75  }
76  }
77  else {
78  if (expectExists) {
79  throw std::range_error(dirname.c_str() + " does not exist"s);
80  }
81  else {
82  if (mkdir(dirname.c_str(), 0700) == -1) {
83  throw std::system_error(std::error_code(errno, std::system_category()),
84  "cannot create "s + dirname.c_str());
85  }
86  }
87  }
88  }
89 
90  inline std::unique_ptr<baseallocator> get(const std::string& name) const {
91  return std::make_unique<mmapallocator>(dirname / fsys::path(name));
92  }
93  inline std::unique_ptr<baseallocator> get(size_t nb) const {
94  return std::make_unique<mmapallocator>(dirname / fsys::path(std::to_string(nb)));
95  }
96 
97  inline std::string to_string() const {
98  return "mmap file = "s + dirname.c_str();
99  }
100 
101  inline virtual fsys::path getDirname() const { return dirname; }
102 
103  inline virtual bool isPersistent() const { return true; }
104 
105  private:
106  const fsys::path dirname;
107  };
108 
109 } // end namespace arr
110 
111 #endif
arr::AllocFactory
Definition: allocator_factory.hpp:34
arr::MemAllocFactory
Definition: allocator_factory.hpp:45
arr::MmapAllocFactory
Definition: allocator_factory.hpp:69
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29
arr::FlexAllocFactory
Definition: allocator_factory.hpp:57