Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Abstract_File.h
Go to the documentation of this file.
1#ifndef joedb_Abstract_File_declared
2#define joedb_Abstract_File_declared
3
5#include "joedb/Blob.h"
6
7#include <stddef.h>
8#include <stdint.h>
9#include <string>
10
11namespace joedb
12{
13 /// @ingroup journal
15 {
16 private:
17 Open_Mode mode;
18 bool locked_tail;
19
20 protected:
23 static constexpr int64_t last_position = (1ULL << 63) - 1;
24
25 public:
27 mode(mode),
28 locked_tail
29 (
30 mode != Open_Mode::shared_write &&
32 )
33 {
34 }
35
36 bool is_shared() const noexcept
37 {
38 return mode == Open_Mode::shared_write;
39 }
40
41 bool is_readonly() const noexcept
42 {
43 return mode == Open_Mode::read_existing;
44 }
45
46 Open_Mode get_mode() const noexcept
47 {
48 return mode;
49 }
50
51 /// Get the size of the file, or -1 if it is unknown
52 virtual int64_t get_size() const {return -1;}
53
54 /// Read a range of bytes
55 ///
56 /// The returned value may be less than size, even if the end of the file
57 /// is not reached. 0 is returned if the end of the file is reached.
58 virtual size_t pread(char *data, size_t size, int64_t offset) const {return 0;}
59
60 /// Write a range of bytes. Extend file size if necessary.
61 virtual void pwrite(const char *data, size_t size, int64_t offset) {}
62
63 /// Write data durably (including file-size change)
64 virtual void sync() {}
65
66 /// Write data durably (no file-size change)
67 virtual void datasync() {sync();}
68
69 /// Lock a range of bytes for reading (prevents writes, not reads)
70 virtual void shared_lock(int64_t start, int64_t size) {}
71
72 /// Lock a range of bytes for writing (prevents both writes and reads)
73 virtual void exclusive_lock(int64_t start, int64_t size) {}
74
75 /// Remove a lock. The range should match the range of a corresponding lock
76 virtual void unlock(int64_t start, int64_t size) noexcept {}
77
79 {
81 locked_tail = true;
82 }
83
84 void unlock_tail() noexcept
85 {
87 locked_tail = false;
88 }
89
90 bool tail_is_locked() const noexcept
91 {
92 return locked_tail;
93 }
94
97 void unlock_head() noexcept {unlock(0, 1);}
98
100 {
101 private:
102 Abstract_File &file;
103
104 public:
106 {
107 file.shared_lock_head();
108 }
109
111 {
112 file.unlock_head();
113 }
114 };
115
117 {
118 private:
119 Abstract_File &file;
120
121 public:
123 {
124 file.exclusive_lock_head();
125 }
127 {
128 file.unlock_head();
129 }
130 };
131
132 std::string read_blob(Blob blob) const;
133
134 static void reading_past_end_of_file();
135
136 virtual void copy_to
137 (
138 Abstract_File &destination,
139 int64_t start,
140 int64_t size
141 ) const;
142
143 virtual bool equal_to
144 (
145 const Abstract_File &destination,
146 int64_t from,
147 int64_t until
148 ) const;
149
150 void copy_to(Abstract_File &destination) const
151 {
152 copy_to(destination, 0, get_size());
153 }
154
155 virtual ~Abstract_File() = default;
156 };
157}
158
159#endif
static constexpr int64_t last_position
virtual void copy_to(Abstract_File &destination, int64_t start, int64_t size) const
virtual void shared_lock(int64_t start, int64_t size)
Lock a range of bytes for reading (prevents writes, not reads)
virtual int64_t get_size() const
Get the size of the file, or -1 if it is unknown.
Abstract_File(Open_Mode mode)
Open_Mode get_mode() const noexcept
void copy_to(Abstract_File &destination) const
virtual void datasync()
Write data durably (no file-size change)
virtual void unlock(int64_t start, int64_t size) noexcept
Remove a lock. The range should match the range of a corresponding lock.
bool is_readonly() const noexcept
static void reading_past_end_of_file()
bool tail_is_locked() const noexcept
virtual void sync()
Write data durably (including file-size change)
virtual ~Abstract_File()=default
void unlock_head() noexcept
std::string read_blob(Blob blob) const
bool is_shared() const noexcept
void unlock_tail() noexcept
virtual void pwrite(const char *data, size_t size, int64_t offset)
Write a range of bytes. Extend file size if necessary.
virtual bool equal_to(const Abstract_File &destination, int64_t from, int64_t until) const
virtual void exclusive_lock(int64_t start, int64_t size)
Lock a range of bytes for writing (prevents both writes and reads)
virtual size_t pread(char *data, size_t size, int64_t offset) const
Read a range of bytes.
Open_Mode
Definition Open_Mode.h:8
@ write_existing
fails if does not exist or locked, locks the file for writing
@ shared_write
like write_existing_or_create_new, but does not lock the file, and does not fail if locked
@ read_existing
fails if does not exist