Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLLargeObject.cpp @ 384:e236be67e5f9 db-protobuf
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 03 Apr 2023 10:46:01 +0200 |
parents | 16aac0287485 |
children | 3d6886f3e5b3 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
359
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
248
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
248
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 // http://www.postgresql.org/docs/9.1/static/lo-interfaces.html#AEN33102 | |
24 | |
107
5765cc5fd268
reverted fix for OS X that breaks other targets
Sebastien Jodogne <s.jodogne@orthanc-labs.com>
parents:
105
diff
changeset
|
25 #include "PostgreSQLIncludes.h" // Must be the first |
105 | 26 #include "PostgreSQLLargeObject.h" |
0 | 27 |
152 | 28 #include <Logging.h> |
29 #include <OrthancException.h> | |
0 | 30 |
31 #include <boost/lexical_cast.hpp> | |
32 #include <libpq/libpq-fs.h> | |
33 | |
34 | |
35 namespace OrthancDatabases | |
36 { | |
37 void PostgreSQLLargeObject::Create() | |
38 { | |
39 PGconn* pg = reinterpret_cast<PGconn*>(database_.pg_); | |
40 | |
41 oid_ = lo_creat(pg, INV_WRITE); | |
42 if (oid_ == 0) | |
43 { | |
44 LOG(ERROR) << "PostgreSQL: Cannot create a large object"; | |
45 database_.ThrowException(false); | |
46 } | |
47 } | |
48 | |
49 | |
50 void PostgreSQLLargeObject::Write(const void* data, | |
51 size_t size) | |
52 { | |
53 static int MAX_CHUNK_SIZE = 16 * 1024 * 1024; | |
54 | |
55 PGconn* pg = reinterpret_cast<PGconn*>(database_.pg_); | |
56 | |
57 int fd = lo_open(pg, oid_, INV_WRITE); | |
58 if (fd < 0) | |
59 { | |
60 database_.ThrowException(true); | |
61 } | |
62 | |
63 const char* position = reinterpret_cast<const char*>(data); | |
64 while (size > 0) | |
65 { | |
66 int chunk = (size > static_cast<size_t>(MAX_CHUNK_SIZE) ? | |
67 MAX_CHUNK_SIZE : static_cast<int>(size)); | |
68 int nbytes = lo_write(pg, fd, position, chunk); | |
69 if (nbytes <= 0) | |
70 { | |
71 lo_close(pg, fd); | |
72 database_.ThrowException(true); | |
73 } | |
74 | |
75 size -= nbytes; | |
76 position += nbytes; | |
77 } | |
78 | |
79 lo_close(pg, fd); | |
80 } | |
81 | |
82 | |
83 PostgreSQLLargeObject::PostgreSQLLargeObject(PostgreSQLDatabase& database, | |
84 const std::string& s) : | |
85 database_(database) | |
86 { | |
87 Create(); | |
88 | |
89 if (s.size() != 0) | |
90 { | |
91 Write(s.c_str(), s.size()); | |
92 } | |
93 else | |
94 { | |
95 Write(NULL, 0); | |
96 } | |
97 } | |
98 | |
99 | |
100 class PostgreSQLLargeObject::Reader | |
101 { | |
102 private: | |
103 PostgreSQLDatabase& database_; | |
104 int fd_; | |
105 size_t size_; | |
106 | |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
107 void ReadInternal(PGconn* pg, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
108 std::string& target) |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
109 { |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
110 for (size_t position = 0; position < target.size(); ) |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
111 { |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
112 size_t remaining = target.size() - position; |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
113 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
114 int nbytes = lo_read(pg, fd_, &target[position], remaining); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
115 if (nbytes < 0) |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
116 { |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
117 LOG(ERROR) << "PostgreSQL: Unable to read the large object in the database"; |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
118 database_.ThrowException(false); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
119 } |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
120 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
121 position += static_cast<size_t>(nbytes); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
122 } |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
123 } |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
124 |
0 | 125 public: |
126 Reader(PostgreSQLDatabase& database, | |
127 const std::string& oid) : | |
128 database_(database) | |
129 { | |
130 PGconn* pg = reinterpret_cast<PGconn*>(database.pg_); | |
131 Oid id = boost::lexical_cast<Oid>(oid); | |
132 | |
133 fd_ = lo_open(pg, id, INV_READ); | |
134 | |
135 if (fd_ < 0 || | |
136 lo_lseek(pg, fd_, 0, SEEK_END) < 0) | |
137 { | |
138 LOG(ERROR) << "PostgreSQL: No such large object in the database; " | |
139 << "Make sure you use a transaction"; | |
140 database.ThrowException(false); | |
141 } | |
142 | |
143 // Get the size of the large object | |
144 int size = lo_tell(pg, fd_); | |
145 if (size < 0) | |
146 { | |
147 database.ThrowException(true); | |
148 } | |
149 size_ = static_cast<size_t>(size); | |
150 } | |
151 | |
152 ~Reader() | |
153 { | |
154 lo_close(reinterpret_cast<PGconn*>(database_.pg_), fd_); | |
155 } | |
156 | |
157 size_t GetSize() const | |
158 { | |
159 return size_; | |
160 } | |
161 | |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
162 void ReadWhole(std::string& target) |
0 | 163 { |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
164 if (target.size() != size_) |
0 | 165 { |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
166 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
167 } |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
168 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
169 PGconn* pg = reinterpret_cast<PGconn*>(database_.pg_); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
170 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
171 // Go to the first byte of the object |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
172 lo_lseek(pg, fd_, 0, SEEK_SET); |
0 | 173 |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
174 ReadInternal(pg, target); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
175 } |
0 | 176 |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
177 void ReadRange(std::string& target, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
178 uint64_t start) |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
179 { |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
180 PGconn* pg = reinterpret_cast<PGconn*>(database_.pg_); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
181 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
182 // Go to the first byte of the object |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
183 lo_lseek(pg, fd_, start, SEEK_SET); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
184 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
185 ReadInternal(pg, target); |
0 | 186 } |
187 }; | |
188 | |
189 | |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
190 void PostgreSQLLargeObject::ReadWhole(std::string& target, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
191 PostgreSQLDatabase& database, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
192 const std::string& oid) |
0 | 193 { |
194 Reader reader(database, oid); | |
195 target.resize(reader.GetSize()); | |
196 | |
197 if (target.size() > 0) | |
198 { | |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
199 reader.ReadWhole(target); |
0 | 200 } |
201 } | |
202 | |
203 | |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
204 void PostgreSQLLargeObject::ReadRange(std::string& target, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
205 PostgreSQLDatabase& database, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
206 const std::string& oid, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
207 uint64_t start, |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
208 size_t length) |
0 | 209 { |
210 Reader reader(database, oid); | |
211 | |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
212 if (start >= reader.GetSize() || |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
213 start + length > reader.GetSize()) |
0 | 214 { |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
215 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRange); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
216 } |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
217 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
218 target.resize(length); |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
219 |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
220 if (target.size() > 0) |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
221 { |
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
222 reader.ReadRange(target, start); |
0 | 223 } |
224 } | |
225 | |
226 | |
227 std::string PostgreSQLLargeObject::GetOid() const | |
228 { | |
229 return boost::lexical_cast<std::string>(oid_); | |
230 } | |
231 | |
232 | |
233 void PostgreSQLLargeObject::Delete(PostgreSQLDatabase& database, | |
234 const std::string& oid) | |
235 { | |
236 PGconn* pg = reinterpret_cast<PGconn*>(database.pg_); | |
237 Oid id = boost::lexical_cast<Oid>(oid); | |
238 | |
239 if (lo_unlink(pg, id) < 0) | |
240 { | |
241 LOG(ERROR) << "PostgreSQL: Unable to delete the large object from the database"; | |
242 database.ThrowException(false); | |
243 } | |
244 } | |
245 } |