Grok 20.3.2
FetchCommon.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <string>
21#include <cstdint>
22#include <memory>
23#include <vector>
24#include <set>
25#include <future>
26#include <functional>
27
28#include "ChunkBuffer.h"
29
30namespace grk
31{
32
34{
35 std::string host;
36 std::string bucket;
37 std::string key;
38 int port = 443;
39};
40
42{
43 std::string username_;
44 std::string password_;
45 std::string bearer_token_;
46 std::vector<std::string> custom_headers_;
47 std::string region_;
48 std::string session_token_; // Added for AWS_SESSION_TOKEN
49 std::string s3_endpoint_; // Custom S3 endpoint URL (e.g. MinIO)
50 int8_t s3_use_https_ = 0; // 0 = auto, 1 = HTTPS, -1 = HTTP
51 int8_t s3_use_virtual_hosting_ = 0; // 0 = auto, 1 = virtual-hosted, -1 = path-style
52 bool s3_no_sign_request_ = false; // true = skip authentication
53 bool s3_allow_insecure_ = false; // true = disable SSL certificate verification
54
55 // HTTP cookies (CURLOPT_COOKIE / COOKIEFILE / COOKIEJAR)
56 std::string cookie_; // inline cookie string ("name1=value1; name2=value2")
57 std::string cookie_file_; // file to read cookies from
58 std::string cookie_jar_; // file to write cookies to on cleanup
59
60 // .netrc credentials (CURLOPT_NETRC / CURLOPT_NETRC_FILE)
61 bool netrc_ = false; // enable .netrc lookup
62 std::string netrc_file_; // optional explicit .netrc path
63
64 // Proxy
65 std::string proxy_; // proxy URL
66 std::string proxy_userpwd_; // proxy credentials ("user:password")
67
68 // S3 requester pays
69 std::string request_payer_; // e.g. "requester"
70
71 // User agent
72 std::string user_agent_;
73
74 // Timeouts (seconds, 0 = use default)
75 long timeout_ = 0;
77
78 // Retry configuration (0 = use default)
79 uint32_t max_retry_ = 0;
80 uint32_t retry_delay_ = 0;
81
82 FetchAuth() = default;
83 FetchAuth(const std::string& u, const std::string& p, const std::string& t,
84 const std::vector<std::string>& h = {}, const std::string& r = "",
85 const std::string& st = "")
88 {}
89};
90
91// Unified fetch result — replaces TileResult<C> and ChunkResult
93{
94 FetchResult() = default;
95 FetchResult(size_t id) : requestIndex_(id) {}
96
97 std::shared_ptr<void> ctx_; // type-erased context (TileFetchContext or ChunkContext)
98 size_t requestIndex_ = 0;
99 std::vector<uint8_t> data_;
101 bool success_ = false;
102 uint32_t retryCount_ = 0;
103};
104
105// Job struct for tile fetch queue
107{
108 FetchJob(std::set<uint16_t>&& s) : slated(std::move(s)) {}
109 std::set<uint16_t> slated;
110 std::promise<bool> promise_;
111};
112
114{
115 DataSlice(uint64_t offset, uint64_t length)
116 : offset_(offset), length_(length), end_(length_ > 0 ? offset_ + length_ - 1 : offset)
117 {}
118 uint64_t offset_;
119 uint64_t length_;
120 uint64_t end_;
121};
122
123// Request for a chunk fetch
124struct ChunkRequest : public DataSlice
125{
126 ChunkRequest(uint16_t id, uint64_t start, uint64_t end)
127 : DataSlice(start, end >= start ? end - start + 1 : 0), requestIndex_(id)
128 {}
130};
131
133{
134 ChunkContext(std::shared_ptr<ChunkBuffer<>> chunkBuffer,
135 std::shared_ptr<std::vector<ChunkRequest>> requests)
136 : chunkBuffer_(chunkBuffer), requests_(requests)
137 {}
138 std::shared_ptr<ChunkBuffer<>> chunkBuffer_;
139 std::shared_ptr<std::vector<ChunkRequest>> requests_;
140};
141
142// Job struct for chunk fetch queue
144{
145 ChunkTask(std::shared_ptr<ChunkBuffer<>> chunkBuffer,
146 std::shared_ptr<std::vector<ChunkRequest>> reqs)
147 : chunkBuffer_(chunkBuffer), requests_(reqs)
148 {
149 promises_.resize(requests_->size());
150 }
151 std::shared_ptr<ChunkBuffer<>> chunkBuffer_;
152 std::shared_ptr<std::vector<ChunkRequest>> requests_;
153 std::vector<std::promise<FetchResult>> promises_;
154};
155
156// Abstract interface for iterating over fetch requests
158{
159 virtual ~IRequestBatch() = default;
160 virtual bool hasMore() const = 0;
161 virtual size_t remaining() const = 0;
162 // Returns (offset, end) for the next request
163 virtual std::pair<uint64_t, uint64_t> next() = 0;
164};
165
166// Unified scheduled fetch — replaces ScheduledTileFetch and ScheduledChunkFetch
168{
169 ScheduledFetch() = default;
170 ScheduledFetch(std::shared_ptr<void> ctx, std::unique_ptr<IRequestBatch> requests,
171 std::shared_ptr<std::vector<FetchResult>> results,
172 std::shared_ptr<std::vector<std::promise<FetchResult>>> promises = nullptr)
173 : ctx_(std::move(ctx)), requests_(std::move(requests)), results_(std::move(results)),
174 promises_(std::move(promises))
175 {}
176
177 std::shared_ptr<void> ctx_;
178 std::unique_ptr<IRequestBatch> requests_;
179 std::shared_ptr<std::vector<FetchResult>> results_;
180 std::shared_ptr<std::vector<std::promise<FetchResult>>> promises_; // null for tile path
181 size_t scheduled_ = 0;
182 size_t completed_ = 0;
183};
184
185// IRequestBatch adapter for chunk requests
187{
188 ChunkRequestBatch(std::shared_ptr<std::vector<ChunkRequest>> requests)
189 : requests_(std::move(requests))
190 {
191 iter_ = requests_->begin();
192 }
193 bool hasMore() const override
194 {
195 return iter_ != requests_->end();
196 }
197 size_t remaining() const override
198 {
199 return static_cast<size_t>(requests_->end() - iter_);
200 }
201 std::pair<uint64_t, uint64_t> next() override
202 {
203 auto& req = *iter_++;
204 return {req.offset_, req.end_};
205 }
206
207private:
208 std::shared_ptr<std::vector<ChunkRequest>> requests_;
209 std::vector<ChunkRequest>::iterator iter_;
210};
211
212} // namespace grk
Manages a partially ordered deque of buffer chunks that are added asynchronously out of order.
Definition ChunkBuffer.h:47
ResWindow.
Definition CompressedChunkCache.h:36
std::shared_ptr< std::vector< ChunkRequest > > requests_
Definition FetchCommon.h:139
std::shared_ptr< ChunkBuffer<> > chunkBuffer_
Definition FetchCommon.h:138
ChunkContext(std::shared_ptr< ChunkBuffer<> > chunkBuffer, std::shared_ptr< std::vector< ChunkRequest > > requests)
Definition FetchCommon.h:134
std::pair< uint64_t, uint64_t > next() override
Definition FetchCommon.h:201
size_t remaining() const override
Definition FetchCommon.h:197
bool hasMore() const override
Definition FetchCommon.h:193
ChunkRequestBatch(std::shared_ptr< std::vector< ChunkRequest > > requests)
Definition FetchCommon.h:188
std::shared_ptr< std::vector< ChunkRequest > > requests_
Definition FetchCommon.h:208
std::vector< ChunkRequest >::iterator iter_
Definition FetchCommon.h:209
ChunkRequest(uint16_t id, uint64_t start, uint64_t end)
Definition FetchCommon.h:126
uint16_t requestIndex_
Definition FetchCommon.h:129
std::shared_ptr< ChunkBuffer<> > chunkBuffer_
Definition FetchCommon.h:151
std::shared_ptr< std::vector< ChunkRequest > > requests_
Definition FetchCommon.h:152
std::vector< std::promise< FetchResult > > promises_
Definition FetchCommon.h:153
ChunkTask(std::shared_ptr< ChunkBuffer<> > chunkBuffer, std::shared_ptr< std::vector< ChunkRequest > > reqs)
Definition FetchCommon.h:145
uint64_t end_
Definition FetchCommon.h:120
DataSlice(uint64_t offset, uint64_t length)
Definition FetchCommon.h:115
uint64_t length_
Definition FetchCommon.h:119
uint64_t offset_
Definition FetchCommon.h:118
std::string password_
Definition FetchCommon.h:44
int8_t s3_use_virtual_hosting_
Definition FetchCommon.h:51
long connect_timeout_
Definition FetchCommon.h:76
std::vector< std::string > custom_headers_
Definition FetchCommon.h:46
uint32_t max_retry_
Definition FetchCommon.h:79
std::string bearer_token_
Definition FetchCommon.h:45
std::string netrc_file_
Definition FetchCommon.h:62
std::string s3_endpoint_
Definition FetchCommon.h:49
std::string cookie_
Definition FetchCommon.h:56
long timeout_
Definition FetchCommon.h:75
std::string request_payer_
Definition FetchCommon.h:69
int8_t s3_use_https_
Definition FetchCommon.h:50
std::string cookie_jar_
Definition FetchCommon.h:58
FetchAuth()=default
std::string proxy_
Definition FetchCommon.h:65
std::string session_token_
Definition FetchCommon.h:48
std::string proxy_userpwd_
Definition FetchCommon.h:66
std::string user_agent_
Definition FetchCommon.h:72
std::string region_
Definition FetchCommon.h:47
bool s3_no_sign_request_
Definition FetchCommon.h:52
FetchAuth(const std::string &u, const std::string &p, const std::string &t, const std::vector< std::string > &h={}, const std::string &r="", const std::string &st="")
Definition FetchCommon.h:83
std::string cookie_file_
Definition FetchCommon.h:57
uint32_t retry_delay_
Definition FetchCommon.h:80
bool netrc_
Definition FetchCommon.h:61
std::string username_
Definition FetchCommon.h:43
bool s3_allow_insecure_
Definition FetchCommon.h:53
FetchJob(std::set< uint16_t > &&s)
Definition FetchCommon.h:108
std::promise< bool > promise_
Definition FetchCommon.h:110
std::set< uint16_t > slated
Definition FetchCommon.h:109
std::shared_ptr< void > ctx_
Definition FetchCommon.h:97
bool success_
Definition FetchCommon.h:101
long responseCode_
Definition FetchCommon.h:100
std::vector< uint8_t > data_
Definition FetchCommon.h:99
uint32_t retryCount_
Definition FetchCommon.h:102
FetchResult()=default
size_t requestIndex_
Definition FetchCommon.h:98
FetchResult(size_t id)
Definition FetchCommon.h:95
Definition FetchCommon.h:158
virtual ~IRequestBatch()=default
virtual size_t remaining() const =0
virtual bool hasMore() const =0
virtual std::pair< uint64_t, uint64_t > next()=0
Definition FetchCommon.h:34
int port
Definition FetchCommon.h:38
std::string bucket
Definition FetchCommon.h:36
std::string key
Definition FetchCommon.h:37
std::string host
Definition FetchCommon.h:35
size_t scheduled_
Definition FetchCommon.h:181
size_t completed_
Definition FetchCommon.h:182
std::shared_ptr< std::vector< std::promise< FetchResult > > > promises_
Definition FetchCommon.h:180
ScheduledFetch()=default
std::unique_ptr< IRequestBatch > requests_
Definition FetchCommon.h:178
ScheduledFetch(std::shared_ptr< void > ctx, std::unique_ptr< IRequestBatch > requests, std::shared_ptr< std::vector< FetchResult > > results, std::shared_ptr< std::vector< std::promise< FetchResult > > > promises=nullptr)
Definition FetchCommon.h:170
std::shared_ptr< std::vector< FetchResult > > results_
Definition FetchCommon.h:179
std::shared_ptr< void > ctx_
Definition FetchCommon.h:177