Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Async_Reader.cpp
Go to the documentation of this file.
3
4namespace joedb
5{
6 //////////////////////////////////////////////////////////////////////////
8 //////////////////////////////////////////////////////////////////////////
9 (
10 const Abstract_File &file,
11 int64_t start,
12 int64_t end
13 ):
14 file(file),
15 end(end),
16 current(start),
17 end_of_file(false)
18 {
19 if (current > end)
20 current = end;
21 }
22
23 //////////////////////////////////////////////////////////////////////////
25 //////////////////////////////////////////////////////////////////////////
26 (
27 const Abstract_File &file,
28 Blob blob
29 ):
30 file(file)
31 {
32 Buffer<3> buffer;
33 buffer.index = 0;
34 buffer.write<int64_t>(0); // avoid uninitialized data if eof
35 const size_t read = file.pread(buffer.data, 8, blob.get_position());
36 buffer.index = 0;
37 int64_t size = buffer.compact_read<int64_t>();
38 end_of_file = read < buffer.index;
39 current = blob.get_position() + int64_t(buffer.index);
40 end = current + size;
41
42 if (current > end)
43 current = end;
44 }
45
46 //////////////////////////////////////////////////////////////////////////
47 size_t Async_Reader::read(char *buffer, size_t capacity)
48 //////////////////////////////////////////////////////////////////////////
49 {
50 size_t size = size_t(end - current);
51 if (size > capacity)
52 size = capacity;
53
54 size_t total_read = 0;
55
56 while (size > 0)
57 {
58 const size_t actually_read = file.pread
59 (
60 buffer + total_read,
61 size,
62 current
63 );
64
65 if (actually_read == 0)
66 {
67 end_of_file = true;
68 break;
69 }
70 else
71 end_of_file = false;
72
73 current += int64_t(actually_read);
75 size -= actually_read;
76 }
77
78 return total_read;
79 }
80}
virtual size_t pread(char *data, size_t size, int64_t offset) const
Async_Reader(const Abstract_File &file, int64_t start, int64_t end)
size_t read(char *buffer, size_t capacity)
T compact_read()
Definition Buffer.h:70
size_t index
Definition Buffer.h:21
void write(T x)
Definition Buffer.h:24
char data[size+extra_size]
Definition Buffer.h:20
Definition Blob.h:7