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