| threaddb 2.0 A file mapped memory container extension

threaddbStream.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2019 by The ThreadDB Project
3 All Rights Reserved.
4
5 ThreadDB undergoes the BSD License 2.0. You should have received a copy along with this program; if not, write to the ThreadDB Project.
6 To obtain a full unlimited version contact thethreaddbproject(at)gmail.com.
7
8 threaddbStream.h - Stream implementation for reading/writing package data
9 */
10
11 #pragma once
12
20 #include "threaddbCPP.h"
21
22 #include <cstring>
23 #include <algorithm>
24 #include <ios>
25
26 namespace tdb
27 {
32 class istreambuf : public std::streambuf
33 {
34 public:
35 istreambuf(uint64_t PackageID_p, tdb::database& rDatabase_p) :
36 buff_sz(rDatabase_p.GetPackageSize()),
37 put_back_(size_t(1)),
38 buffer_(0),
39 m_rDatabase(rDatabase_p),
40 m_ReadHandle(rDatabase_p.Open(PackageID_p))
41 {
42 buffer_ = new char[std::max(buff_sz, put_back_) + put_back_];
43 char *end = buffer_ + buff_sz;
44 setg(end, end, end);
45 }
46
47 virtual ~istreambuf()
48 {
49 sync();
50 delete[] buffer_;
51 }
52
53 std::streambuf::int_type underflow()
54 {
55 if (gptr() < egptr()) // buffer not exhausted
56 return traits_type::to_int_type(*gptr());
57
58 char *base = buffer_;
59 char *start = base;
60
61 if (eback() == base)
62 {
63 std::memmove(base, egptr() - put_back_, put_back_);
64 start += put_back_;
65 }
66
67 size_t n = m_rDatabase.Recover(buff_sz, start, m_ReadHandle);
68 if (n == 0)
69 return traits_type::eof();
70
71 setg(base, start, start + n);
72
73 return traits_type::to_int_type(*gptr());
74 }
75
76 private:
77 size_t buff_sz;
78 const std::size_t put_back_;
79 char *buffer_;
80
81 tdb::database& m_rDatabase;
82 tdb::ReadInfo m_ReadHandle;
83 };
84
89 class ostreambuf : public std::streambuf
90 {
91 public:
92 ostreambuf(uint64_t PackageID_p, tdb::database& rDatabase_p) :
93 buff_sz(rDatabase_p.GetPackageSize()),
94 buffer_(0),
95 m_rDatabase(rDatabase_p),
96 m_PackageID(PackageID_p)
97 {
98 buffer_ = new char[buff_sz + 1];
99 memset(buffer_, 0, buff_sz);
100
101 setp(buffer_, buffer_ + buff_sz);
102 }
103
104 virtual ~ostreambuf()
105 {
106 sync();
107 delete[] buffer_;
108 }
109
110 virtual std::streambuf::int_type overflow(std::streambuf::int_type value)
111 {
112 size_t write = pptr() - pbase();
113 if (write)
114 {
115 m_rDatabase.Store(m_PackageID, write, buffer_);
116
117 // Write line to original buffer
118 std::streamsize written = write;
119
120 if (written != write) return traits_type::eof();
121 }
122
123 setp(buffer_, buffer_ + buff_sz);
124 if (!traits_type::eq_int_type(value, traits_type::eof())) sputc(value);
125
126 return traits_type::not_eof(value);
127 };
128
129 virtual int sync()
130 {
131 std::streambuf::int_type result = this->overflow(traits_type::eof());
132 m_rDatabase.Synchronize();
133 return traits_type::eq_int_type(result, traits_type::eof()) ? -1 : 0;
134 }
135
136 private:
137 size_t buff_sz;
138 char *buffer_;
139
140 uint64_t m_PackageID;
141 tdb::database& m_rDatabase;
142 };
143
148 class istream : public std::istream
149 {
150 public:
151 istream(uint64_t PackageID_p, tdb::database& rDatabase_p) :
152 std::istream(new istreambuf(PackageID_p, rDatabase_p)) {}
153
154 virtual ~istream()
155 {
156 delete rdbuf();
157 }
158 };
159
164 class ostream : public std::ostream
165 {
166 public:
167 ostream(uint64_t PackageID_p, tdb::database& rDatabase_p) :
168 std::ostream(new ostreambuf(PackageID_p, rDatabase_p)) {}
169
170 virtual ~ostream()
171 {
172 delete rdbuf();
173 }
174 };
175
176 }
177
tdb::ostreambuf::ostreambuf
ostreambuf(uint64_t PackageID_p, tdb::database &rDatabase_p)
Definition: threaddbStream.h:92
tdb::ostreambuf::~ostreambuf
virtual ~ostreambuf()
Definition: threaddbStream.h:104
tdb::istreambuf::~istreambuf
virtual ~istreambuf()
Definition: threaddbStream.h:47
tdb::ReadInfo
C++ wrapper class to the threadDB_ReadInfo control structure.
Definition: threaddbCPP.h:70
tdb::ostreambuf::sync
virtual int sync()
Definition: threaddbStream.h:129
tdb::ostream
Output stream implementation.
Definition: threaddbStream.h:164
tdb::istreambuf::istreambuf
istreambuf(uint64_t PackageID_p, tdb::database &rDatabase_p)
Definition: threaddbStream.h:35
threaddbCPP.h
Interface C++ wrapper classes.
tdb::ostreambuf::overflow
virtual std::streambuf::int_type overflow(std::streambuf::int_type value)
Definition: threaddbStream.h:110
tdb::ostream::ostream
ostream(uint64_t PackageID_p, tdb::database &rDatabase_p)
Definition: threaddbStream.h:167
tdb::database::Store
void Store(uint64_t Package_p, size_t Size_p, const char pData_p[], threadDB_ItemInfo *pItemHandle_p=0)
C++ wrapper to store a data item in the selected package see ThreadDB_Store.
Definition: threaddbCPP.h:234
tdb::istream::~istream
virtual ~istream()
Definition: threaddbStream.h:154
tdb
Definition: threaddbCPP.h:28
tdb::istreambuf
Input stream buffer implementation.
Definition: threaddbStream.h:32
tdb::database::Synchronize
void Synchronize(uint64_t Package_p)
C++ wrapper to synchronize the package buffers prior reading see ThreadDB_Synchronize.
Definition: threaddbCPP.h:243
tdb::ostreambuf
Output stream buffer implementation.
Definition: threaddbStream.h:89
tdb::istream
Input stream implementation.
Definition: threaddbStream.h:148
tdb::database
C++ wrapper class of the threadDB file mapped memory container extension.
Definition: threaddbCPP.h:97
tdb::database::Recover
size_t Recover(size_t Size_p, char pData_p[], tdb::ReadInfo &rReadInfo_p)
C++ wrapper for stream reading of data items see ThreadDB_RecoverContinous.
Definition: threaddbCPP.h:289
tdb::ostream::~ostream
virtual ~ostream()
Definition: threaddbStream.h:170
tdb::istreambuf::underflow
std::streambuf::int_type underflow()
Definition: threaddbStream.h:53
tdb::istream::istream
istream(uint64_t PackageID_p, tdb::database &rDatabase_p)
Definition: threaddbStream.h:151