Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLLargeObject.cpp @ 348:47db0b70f705 OrthancPostgreSQL-3.3
closing OrthancPostgreSQL-3.3
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Aug 2021 16:09:37 +0200 |
parents | 063aa53b5917 |
children | 3236894320d6 |
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 | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
107
diff
changeset
|
5 * Copyright (C) 2017-2020 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 void* data, | |
84 size_t size) : | |
85 database_(database) | |
86 { | |
87 Create(); | |
88 Write(data, size); | |
89 } | |
90 | |
91 | |
92 PostgreSQLLargeObject::PostgreSQLLargeObject(PostgreSQLDatabase& database, | |
93 const std::string& s) : | |
94 database_(database) | |
95 { | |
96 Create(); | |
97 | |
98 if (s.size() != 0) | |
99 { | |
100 Write(s.c_str(), s.size()); | |
101 } | |
102 else | |
103 { | |
104 Write(NULL, 0); | |
105 } | |
106 } | |
107 | |
108 | |
109 class PostgreSQLLargeObject::Reader | |
110 { | |
111 private: | |
112 PostgreSQLDatabase& database_; | |
113 int fd_; | |
114 size_t size_; | |
115 | |
116 public: | |
117 Reader(PostgreSQLDatabase& database, | |
118 const std::string& oid) : | |
119 database_(database) | |
120 { | |
121 PGconn* pg = reinterpret_cast<PGconn*>(database.pg_); | |
122 Oid id = boost::lexical_cast<Oid>(oid); | |
123 | |
124 fd_ = lo_open(pg, id, INV_READ); | |
125 | |
126 if (fd_ < 0 || | |
127 lo_lseek(pg, fd_, 0, SEEK_END) < 0) | |
128 { | |
129 LOG(ERROR) << "PostgreSQL: No such large object in the database; " | |
130 << "Make sure you use a transaction"; | |
131 database.ThrowException(false); | |
132 } | |
133 | |
134 // Get the size of the large object | |
135 int size = lo_tell(pg, fd_); | |
136 if (size < 0) | |
137 { | |
138 database.ThrowException(true); | |
139 } | |
140 size_ = static_cast<size_t>(size); | |
141 | |
142 // Go to the first byte of the object | |
143 lo_lseek(pg, fd_, 0, SEEK_SET); | |
144 } | |
145 | |
146 ~Reader() | |
147 { | |
148 lo_close(reinterpret_cast<PGconn*>(database_.pg_), fd_); | |
149 } | |
150 | |
151 size_t GetSize() const | |
152 { | |
153 return size_; | |
154 } | |
155 | |
156 void Read(char* target) | |
157 { | |
158 for (size_t position = 0; position < size_; ) | |
159 { | |
160 size_t remaining = size_ - position; | |
161 | |
162 int nbytes = lo_read(reinterpret_cast<PGconn*>(database_.pg_), fd_, target + position, remaining); | |
163 if (nbytes < 0) | |
164 { | |
165 LOG(ERROR) << "PostgreSQL: Unable to read the large object in the database"; | |
166 database_.ThrowException(false); | |
167 } | |
168 | |
169 position += static_cast<size_t>(nbytes); | |
170 } | |
171 } | |
172 }; | |
173 | |
174 | |
175 void PostgreSQLLargeObject::Read(std::string& target, | |
176 PostgreSQLDatabase& database, | |
177 const std::string& oid) | |
178 { | |
179 Reader reader(database, oid); | |
180 target.resize(reader.GetSize()); | |
181 | |
182 if (target.size() > 0) | |
183 { | |
184 reader.Read(&target[0]); | |
185 } | |
186 } | |
187 | |
188 | |
189 void PostgreSQLLargeObject::Read(void*& target, | |
190 size_t& size, | |
191 PostgreSQLDatabase& database, | |
192 const std::string& oid) | |
193 { | |
194 Reader reader(database, oid); | |
195 size = reader.GetSize(); | |
196 | |
197 if (size == 0) | |
198 { | |
199 target = NULL; | |
200 } | |
201 else | |
202 { | |
203 target = malloc(size); | |
1
d17b2631bb67
starting StorageBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
204 if (target == NULL) |
d17b2631bb67
starting StorageBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
205 { |
d17b2631bb67
starting StorageBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
206 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory); |
d17b2631bb67
starting StorageBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
207 } |
d17b2631bb67
starting StorageBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
208 |
0 | 209 reader.Read(reinterpret_cast<char*>(target)); |
210 } | |
211 } | |
212 | |
213 | |
214 std::string PostgreSQLLargeObject::GetOid() const | |
215 { | |
216 return boost::lexical_cast<std::string>(oid_); | |
217 } | |
218 | |
219 | |
220 void PostgreSQLLargeObject::Delete(PostgreSQLDatabase& database, | |
221 const std::string& oid) | |
222 { | |
223 PGconn* pg = reinterpret_cast<PGconn*>(database.pg_); | |
224 Oid id = boost::lexical_cast<Oid>(oid); | |
225 | |
226 if (lo_unlink(pg, id) < 0) | |
227 { | |
228 LOG(ERROR) << "PostgreSQL: Unable to delete the large object from the database"; | |
229 database.ThrowException(false); | |
230 } | |
231 } | |
232 } |