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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * Affero General Public License for more details.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 // http://www.postgresql.org/docs/9.1/static/lo-interfaces.html#AEN33102
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
eefbcee7bd78 fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 103
diff changeset
26 #include "PostgreSQLLargeObject.h"
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
152
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
28 #include <Logging.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
29 #include <OrthancException.h>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <boost/lexical_cast.hpp>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include <libpq/libpq-fs.h>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 namespace OrthancDatabases
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 void PostgreSQLLargeObject::Create()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 PGconn* pg = reinterpret_cast<PGconn*>(database_.pg_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 oid_ = lo_creat(pg, INV_WRITE);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 if (oid_ == 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 LOG(ERROR) << "PostgreSQL: Cannot create a large object";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 database_.ThrowException(false);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 void PostgreSQLLargeObject::Write(const void* data,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 size_t size)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 static int MAX_CHUNK_SIZE = 16 * 1024 * 1024;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 PGconn* pg = reinterpret_cast<PGconn*>(database_.pg_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 int fd = lo_open(pg, oid_, INV_WRITE);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 if (fd < 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 database_.ThrowException(true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 const char* position = reinterpret_cast<const char*>(data);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 while (size > 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 int chunk = (size > static_cast<size_t>(MAX_CHUNK_SIZE) ?
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 MAX_CHUNK_SIZE : static_cast<int>(size));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 int nbytes = lo_write(pg, fd, position, chunk);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if (nbytes <= 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 lo_close(pg, fd);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 database_.ThrowException(true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 size -= nbytes;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 position += nbytes;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 lo_close(pg, fd);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 PostgreSQLLargeObject::PostgreSQLLargeObject(PostgreSQLDatabase& database,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 const std::string& s) :
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 database_(database)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 Create();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (s.size() != 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 Write(s.c_str(), s.size());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 Write(NULL, 0);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 class PostgreSQLLargeObject::Reader
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 private:
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 PostgreSQLDatabase& database_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 int fd_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 size_t size_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 public:
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 Reader(PostgreSQLDatabase& database,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 const std::string& oid) :
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 database_(database)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 PGconn* pg = reinterpret_cast<PGconn*>(database.pg_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 Oid id = boost::lexical_cast<Oid>(oid);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 fd_ = lo_open(pg, id, INV_READ);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 if (fd_ < 0 ||
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 lo_lseek(pg, fd_, 0, SEEK_END) < 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 LOG(ERROR) << "PostgreSQL: No such large object in the database; "
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 << "Make sure you use a transaction";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 database.ThrowException(false);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 // Get the size of the large object
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 int size = lo_tell(pg, fd_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 if (size < 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 database.ThrowException(true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 size_ = static_cast<size_t>(size);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 ~Reader()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 lo_close(reinterpret_cast<PGconn*>(database_.pg_), fd_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 size_t GetSize() const
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 return size_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 };
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 Reader reader(database, oid);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 target.resize(reader.GetSize());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 if (target.size() > 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 Reader reader(database, oid);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 std::string PostgreSQLLargeObject::GetOid() const
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 return boost::lexical_cast<std::string>(oid_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 void PostgreSQLLargeObject::Delete(PostgreSQLDatabase& database,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 const std::string& oid)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 PGconn* pg = reinterpret_cast<PGconn*>(database.pg_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 Oid id = boost::lexical_cast<Oid>(oid);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 if (lo_unlink(pg, id) < 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 LOG(ERROR) << "PostgreSQL: Unable to delete the large object from the database";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 database.ThrowException(false);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 }