1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
##
# ChunkedIO Engine
#
# Choose a chunk_store with your purpose
# This class is designed that it's compatible with IO class (https://ruby-doc.org/core-2.3.1/IO.html)
module Gitlab
module Ci
class Trace
module ChunkedFile
class ChunkedIO
# extend ChunkedFile::Concerns::Opener
include ChunkedFile::Concerns::Errors
include ChunkedFile::Concerns::Hooks
include ChunkedFile::Concerns::Callbacks
prepend ChunkedFile::Concerns::Permissions
attr_reader :size
attr_reader :tell
attr_reader :chunk, :chunk_range
attr_reader :job_id
attr_reader :mode
alias_method :pos, :tell
def initialize(job_id, size = nil, mode = 'rb', &block)
raise NotImplementedError, "Mode 'w' is not supported" if mode.include?('w')
@size = size || calculate_size(job_id)
@tell = 0
@job_id = job_id
@mode = mode
if block_given?
begin
yield self
ensure
self.close
end
end
end
def close
end
def binmode
# no-op
end
def binmode?
true
end
def seek(amount, where = IO::SEEK_SET)
new_pos =
case where
when IO::SEEK_END
size + amount
when IO::SEEK_SET
amount
when IO::SEEK_CUR
tell + amount
else
-1
end
raise ArgumentError, 'new position is outside of file' if new_pos < 0 || new_pos > size
@tell = new_pos
end
def eof?
tell == size
end
def each_line
until eof?
line = readline
break if line.nil?
yield(line)
end
end
def read(length = nil)
out = ""
until eof? || (length && out.length >= length)
data = get_chunk
break if data.empty?
out << data
@tell += data.bytesize
end
out = out[0, length] if length && out.length > length
out
end
def readline
out = ""
until eof?
data = get_chunk
new_line = data.index("\n")
if !new_line.nil?
out << data[0..new_line]
@tell += new_line + 1
break
else
out << data
@tell += data.bytesize
end
end
out
end
def write(data)
raise ArgumentError, 'Could not write empty data' unless data.present?
if mode.include?('w')
write_as_overwrite(data)
elsif mode.include?('a')
write_as_append(data)
end
end
def truncate(offset)
raise NotImplementedError
end
def flush
# no-op
end
def present?
chunks_count > 0
end
def delete
chunk_store.delete_all
end
private
def in_range?
@chunk_range&.include?(tell)
end
def get_chunk
unless in_range?
chunk_store.open(job_id, chunk_index, params_for_store) do |store|
@chunk = store.get
@chunk_range = (chunk_start...(chunk_start + chunk.length))
end
end
@chunk[chunk_offset..buffer_size]
end
def write_as_overwrite(data)
raise NotImplementedError, "Overwrite is not supported"
end
def write_as_append(data)
@tell = size
data_size = data.size
new_tell = tell + data_size
data_offset = 0
until tell == new_tell
writable_size = buffer_size - chunk_offset
writable_data = data[data_offset...(data_offset + writable_size)]
written_size = write_chunk(writable_data)
data_offset += written_size
@tell += written_size
@size = [tell, size].max
end
data_size
end
def write_chunk(data)
written_size = 0
chunk_store.open(job_id, chunk_index, params_for_store) do |store|
with_callbacks(:write_chunk, store) do
written_size = if store.size > 0 # # rubocop:disable ZeroLengthPredicate
store.append!(data)
else
store.write!(data)
end
raise WriteError, 'Written size mismatch' unless data.length == written_size
end
end
written_size
end
def params_for_store(c_index = chunk_index)
{
buffer_size: buffer_size,
chunk_start: c_index * buffer_size,
chunk_index: c_index
}
end
def chunk_offset
tell % buffer_size
end
def chunk_start
chunk_index * buffer_size
end
def chunk_end
[chunk_start + buffer_size, size].min
end
def chunk_index
(tell / buffer_size)
end
def chunks_count
(size / buffer_size.to_f).ceil
end
def last_range
((size / buffer_size) * buffer_size..size)
end
def chunk_store
raise NotImplementedError
end
def buffer_size
raise NotImplementedError
end
def calculate_size(job_id)
chunk_store.chunks_size(job_id)
end
end
end
end
end
end
|