Joedb 10.4.3
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
19 protected:
22 static constexpr int64_t last_position = (1ULL << 63) - 1;
23
24 public:
26 mode(mode)
27 {
28 }
29
30 bool is_shared() const noexcept
31 {
32 return mode == Open_Mode::shared_write;
33 }
34
35 bool is_readonly() const noexcept
36 {
37 return mode == Open_Mode::read_existing;
38 }
39
40 Open_Mode get_mode() const noexcept
41 {
42 return mode;
43 }
44
45 /// Get the size of the file, or -1 if it is unknown
46 virtual int64_t get_size() const {return -1;}
47
48 /// Read a range of bytes
49 ///
50 /// The returned value may be less than size, even if the end of the file
51 /// is not reached. 0 is returned if the end of the file is reached.
52 virtual size_t pread(char *data, size_t size, int64_t offset) const {return 0;}
53
54 /// Fill the buffer, or throw if the end of the file is reached.
55 void full_pread(char *data, size_t size, int64_t offset) const;
56
57 /// Write a range of bytes. Extend file size if necessary.
58 virtual void pwrite(const char *data, size_t size, int64_t offset) {}
59
60 /// Write data durably (including file-size change)
61 virtual void sync() {}
62
63 /// Write data durably (no file-size change)
64 virtual void datasync() {sync();}
65
66 /// Lock a range of bytes for reading (prevents writes, not reads)
67 virtual void shared_lock(int64_t start, int64_t size) {}
68
69 /// Lock a range of bytes for writing (prevents both writes and reads)
70 virtual void exclusive_lock(int64_t start, int64_t size) {}
71
72 /// Remove a lock. The range should match the range of a corresponding lock
73 virtual void unlock(int64_t start, int64_t size) noexcept {}
74
76 void unlock_tail() noexcept {unlock(last_position, 1);}
77
80 void unlock_head() noexcept {unlock(0, 1);}
81
82 std::string read_blob(Blob blob, int64_t max_size = -1) const;
83
84 static void reading_past_end_of_file();
85
86 virtual void copy_to
87 (
88 Abstract_File &destination,
89 int64_t start,
90 int64_t size
91 ) const;
92
93 virtual bool equal_to
94 (
95 const Abstract_File &destination,
96 int64_t from,
97 int64_t until
98 ) const;
99
100 void copy_to(Abstract_File &destination) const
101 {
102 copy_to(destination, 0, get_size());
103 }
104
105 virtual ~Abstract_File() = default;
106 };
107}
108
109#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.
void full_pread(char *data, size_t size, int64_t offset) const
Fill the buffer, or throw if the end of the file is reached.
bool is_readonly() const noexcept
static void reading_past_end_of_file()
virtual void sync()
Write data durably (including file-size change)
virtual ~Abstract_File()=default
void unlock_head() noexcept
bool is_shared() const noexcept
std::string read_blob(Blob blob, int64_t max_size=-1) const
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