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