forked from marbl/canu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordFile.C
345 lines (265 loc) · 9.35 KB
/
recordFile.C
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/******************************************************************************
*
* This file is part of canu, a software program that assembles whole-genome
* sequencing reads into contigs.
*
* This software is based on:
* 'Celera Assembler' (http://wgs-assembler.sourceforge.net)
* the 'kmer package' (http://kmer.sourceforge.net)
* both originally distributed by Applera Corporation under the GNU General
* Public License, version 2.
*
* Canu branched from Celera Assembler at its revision 4587.
* Canu branched from the kmer project at its revision 1994.
*
* Modifications by:
*
* Brian P. Walenz from 2008-JUL-08 to 2014-APR-11
* are Copyright 2008,2014 J. Craig Venter Institute, and
* are subject to the GNU General Public License version 2
*
* File 'README.licenses' in the root directory of this distribution contains
* full conditions and disclaimers for each license.
*/
#include "util++.H"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
// N.B. any read() / write() pair (either order) must have a seek (or
// a fflush) in between.
uint64 recordFileMagic1 = 0x694664726f636572llu;
uint64 recordFileMagic2 = 0x000000000000656cllu;
recordFile::recordFile(char const *name,
uint32 headerSize,
uint32 recordSize,
char mode) {
_file = 0;
_name = new char [strlen(name) + 1];
strcpy(_name, name);
_numRecords = 0;
_recordSize = recordSize;
_headerSize = headerSize;
_header = new char [_headerSize];
memset(_header, 0, sizeof(char) * _headerSize);
_bfrmax = MAX(1048576 / _recordSize, 16);
_bfr = new char [_bfrmax * _recordSize];
_limit = ~uint32ZERO;
_pos = uint64ZERO;
_rec = 0;
memset(_bfr, 0, sizeof(char) * _bfrmax * _recordSize);
_bfrDirty = false;
_isReadOnly = true;
if ((mode != 'r') && (mode != 'w') && (mode |= 'a')) {
fprintf(stderr, "recordFile::recordFile()-- Invalid mode '%c'.\n", mode);
exit(1);
}
// If the file doesn't exist, or we're opening for write, we're
// basically done. Do that first.
// Write the magic.
// Write the metadata.
// Write the header.
if (((mode == 'w')) ||
((mode == 'a') && (fileExists(_name) == false))) {
errno = 0;
_file = open(_name,
O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (errno)
fprintf(stderr, "recordFile::recordFile()-- failed to open '%s': %s\n",
_name, strerror(errno)), exit(1);
_isReadOnly = false;
write(_file, &recordFileMagic1, sizeof(uint64));
write(_file, &recordFileMagic2, sizeof(uint64));
write(_file, &_numRecords, sizeof(uint64));
write(_file, &_recordSize, sizeof(uint32));
write(_file, &_headerSize, sizeof(uint32));
write(_file, _header, sizeof(char) * _headerSize);
if (errno)
fprintf(stderr, "recordFile::recordFile()-- failed to write header to '%s': %s\n",
_name, strerror(errno)), exit(1);
return;
}
// File does exist. If we're not appending, open it read-only.
// Otherwise, open read-write.
if (mode == 'r') {
errno = 0;
_file = open(_name,
O_RDONLY | O_LARGEFILE,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (errno)
fprintf(stderr, "recordFile::recordFile()-- failed to open '%s': %s\n",
_name, strerror(errno)), exit(1);
_isReadOnly = true;
} else {
errno = 0;
_file = open(_name,
O_RDWR | O_LARGEFILE,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (errno)
fprintf(stderr, "recordFile::recordFile()-- failed to open for write '%s': %s\n",
_name, strerror(errno)), exit(1);
_isReadOnly = false;
}
// Read the magic, metadata and header.
{
uint64 m1, m2;
errno = 0;
read(_file, &m1, sizeof(uint64));
read(_file, &m2, sizeof(uint64));
read(_file, &_numRecords, sizeof(uint64));
read(_file, &_recordSize, sizeof(uint32));
read(_file, &_headerSize, sizeof(uint32));
read(_file, _header, sizeof(char) * _headerSize);
if (errno)
fprintf(stderr, "recordFile::recordFile()-- failed to read header from '%s': %s\n",
_name, strerror(errno)), exit(1);
if ((m1 != recordFileMagic1) || (m2 != recordFileMagic2))
fprintf(stderr, "recordFile::recordFile()-- magic number disagreement; '%s' not a recordFile?\n",
_name), exit(1);
}
if (mode == 'a') {
_pos = _numRecords;
_rec = 0;
errno = 0;
lseek(_file, 0, SEEK_END);
if (errno)
fprintf(stderr, "recordFile::recordFile()-- seek to end of '%s' failed: %s\n", _name, strerror(errno)), exit(1);
} else {
seek(0, true);
}
}
recordFile::~recordFile() {
flushDirty();
if (_isReadOnly == false) {
errno = 0;
lseek(_file, 0, SEEK_SET);
if (errno)
fprintf(stderr, "recordFile::~recordFile()-- seek to start of '%s' failed: %s\n", _name, strerror(errno)), exit(1);
write(_file, &recordFileMagic1, sizeof(uint64));
write(_file, &recordFileMagic2, sizeof(uint64));
write(_file, &_numRecords, sizeof(uint64));
write(_file, &_recordSize, sizeof(uint32));
write(_file, &_headerSize, sizeof(uint32));
write(_file, _header, sizeof(char) * _headerSize);
if (errno)
fprintf(stderr, "recordFile::~recordFile()-- failed to write header to '%s': %s\n",
_name, strerror(errno)), exit(1);
}
close(_file);
if (errno)
fprintf(stderr, "recordFile::~recordFile()-- failed to close '%s': %s\n",
_name, strerror(errno)), exit(1);
delete [] _bfr;
delete [] _name;
delete [] _header;
}
// If the page is dirty, flush it to disk
//
void
recordFile::flushDirty(void) {
if (_bfrDirty == false)
return;
if (_isReadOnly)
fprintf(stderr, "recordFile::recordFile()-- '%s' is readonly, but is dirty!\n", _name), exit(1);
errno = 0;
lseek(_file, 32 + _headerSize + _pos * _recordSize, SEEK_SET);
if (errno)
fprintf(stderr, "recordFile::seek()-- '%s' failed: %s\n", _name, strerror(errno)), exit(1);
// Write records up to, not including, _rec. Unlike the
// bitPackedFile, there is no issue with partially filled words
// here.
//
errno = 0;
write(_file, _bfr, _recordSize * _rec);
if (errno)
fprintf(stderr, "recordFile::write()-- '%s' failed: %s\n", _name, strerror(errno)), exit(1);
_bfrDirty = false;
}
// Seeks to rec in the file, reads in a new block.
//
void
recordFile::seek(uint64 rec, bool forced) {
// If we are seeking to somewhere in the current block, don't do a
// real seek, just move our position within the block.
//
if ((forced == false) && (_pos <= rec) && (rec < _pos + _bfrmax)) {
_rec = rec - _pos;
return;
}
flushDirty();
_pos = rec; // Root of buffer is now here
_rec = 0; // See?
errno = 0;
lseek(_file, 32 + _headerSize + _pos * _recordSize, SEEK_SET);
if (errno)
fprintf(stderr, "recordFile::seek() '%s' seek to record="uint64FMT" at fileposition="uint64FMT" failed: %s\n",
_name, _pos, _headerSize + _pos * _recordSize, strerror(errno)), exit(1);
errno = 0;
read(_file, _bfr, _recordSize * _bfrmax);
if (errno)
fprintf(stderr, "recordFile::seek() '%s' read of "uint64FMT" bytes failed at record "uint64FMT", fileposition "uint64FMT"': %s\n",
_name, _recordSize * _bfrmax, _pos, _headerSize + _pos * _recordSize, strerror(errno)), exit(1);
}
uint32
recordFile::getRecord(void *record, uint32 num) {
uint32 maxnum = _bfrmax / 2;
// Reading large blocks -- bigger than the in-core size? Loop and
// recurse.
//
if (num > maxnum) {
uint32 numread = 0;
uint32 pos = 0;
uint32 len = 0;
while (num > 0) {
len = MIN(maxnum, num);
len = getRecord((char *)record + pos * _recordSize, len);
if (len == 0)
return(numread);
num -= len;
pos += len;
numread += len;
}
return(numread);
}
// If asked to read too many records, read whatever is left.
//
if (_numRecords < _pos + _rec + num)
num = _numRecords - _pos - _rec;
if (_limit < _pos + _rec + num)
num = _limit - _pos - _rec;
// If the current position is already past eof, return without
// reading. The previous 'if' ensures we will never read a block
// past eof.
//
if ((_numRecords < _pos + _rec) || (_limit < _pos + _rec))
return(0);
if (_bfrmax < _rec + num + 1)
seek(_pos + _rec, true);
memcpy(record, _bfr + _rec * _recordSize, _recordSize * num);
_rec += num;
return(num);
}
void
recordFile::putRecord(void *record, uint32 num) {
uint32 maxnum = _bfrmax / 2;
if (num > maxnum) {
uint32 pos = 0;
uint32 len = 0;
while (num > 0) {
len = MIN(maxnum, num);
putRecord((char *)record + pos * _recordSize, len);
num -= len;
pos += len;
}
} else {
if (_bfrmax < _rec + num + 1)
seek(_pos + _rec, true);
memcpy(_bfr + _rec * _recordSize, record, _recordSize * num);
_rec += num;
_numRecords += num;
_bfrDirty = true;
}
}