Joedb 10.3.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
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 /// For very large reads, the returned value may be less than size, even
51 /// if the end of the file is not reached. It is assumed that small reads
52 /// (such as the 41 bytes of the joedb header) will not be truncated.
53 /// 0 is returned if the end of the file is reached.
54 virtual size_t pread(char *data, size_t size, int64_t offset) const {return 0;}
55
56 /// Write a range of bytes. Extend file size if necessary.
57 virtual void pwrite(const char *data, size_t size, int64_t offset) {}
58
59 /// Write data durably (including file-size change)
60 virtual void sync() {}
61
62 /// Write data durably (no file-size change)
63 virtual void datasync() {sync();}
64
65 /// Lock a range of bytes for reading (prevents writes, not reads)
66 virtual void shared_lock(int64_t start, int64_t size) {}
67
68 /// Lock a range of bytes for writing (prevents both writes and reads)
69 virtual void exclusive_lock(int64_t start, int64_t size) {}
70
71 /// Remove a lock. The range should match the range of a corresponding lock
72 virtual void unlock(int64_t start, int64_t size) noexcept {}
73
75 void unlock_tail() noexcept {unlock(last_position, 1);}
76
79 void unlock_head() noexcept {unlock(0, 1);}
80
81 std::string read_blob(Blob blob) const;
82
83 static void reading_past_end_of_file();
84
85 virtual void copy_to
86 (
87 Abstract_File &destination,
88 int64_t start,
89 int64_t size
90 ) const;
91
92 virtual bool equal_to
93 (
94 const Abstract_File &destination,
95 int64_t from,
96 int64_t until
97 ) const;
98
99 void copy_to(Abstract_File &destination) const
100 {
101 copy_to(destination, 0, get_size());
102 }
103
104 virtual ~Abstract_File() = default;
105 };
106}
107
108#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()
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