/
BitarchiveRecord.java
executable file
·233 lines (212 loc) · 9.54 KB
/
BitarchiveRecord.java
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
/*
* #%L
* Netarchivesuite - common
* %%
* Copyright (C) 2005 - 2018 The Royal Danish Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.common.distribute.arcrepository;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import org.archive.io.ArchiveReader;
import org.archive.io.ArchiveReaderFactory;
import org.archive.io.ArchiveRecord;
import org.archive.io.arc.ARCRecord;
import org.archive.io.warc.WARCRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dk.netarkivet.common.CommonSettings;
import dk.netarkivet.common.distribute.RemoteFile;
import dk.netarkivet.common.distribute.RemoteFileFactory;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.exceptions.IllegalState;
import dk.netarkivet.common.utils.FileUtils;
import dk.netarkivet.common.utils.Settings;
import dk.netarkivet.common.utils.arc.ARCUtils;
import dk.netarkivet.common.utils.warc.WARCUtils;
/**
* Class to hold the result of a lookup operation in the bitarchive: The metadata information associated with the record
* The actual byte content The name of the file the data were retrieved from If length of record exceeds value of
* Settings.BITARCHIVE_LIMIT_FOR_RECORD_DATATRANSFER_IN_FILE The record is stored in a RemoteFile.
*/
@SuppressWarnings({"serial"})
public class BitarchiveRecord implements Serializable {
/** the log. */
private static final transient Logger log = LoggerFactory.getLogger(BitarchiveRecord.class);
/** The file the data were retrieved from. */
private String fileName;
/** The actual data. */
private byte[] objectBuffer;
/** The offset of the ArchiveRecord contained. */
private long offset;
/** The length of the ArchiveRecord contained. */
private long length;
/** The actual data as a remote file. */
private RemoteFile objectAsRemoteFile;
/** Is the data stored in a RemoteFile. */
private boolean isStoredAsRemoteFile = false;
/** Set after deleting RemoteFile. */
private boolean hasRemoteFileBeenDeleted = false;
/** How large the ARCRecord can before saving as RemoteFile. */
private final long LIMIT_FOR_SAVING_DATA_IN_OBJECT_BUFFER = Settings
.getLong(CommonSettings.BITARCHIVE_LIMIT_FOR_RECORD_DATATRANSFER_IN_FILE);
/**
* Creates a BitarchiveRecord from the a ArchiveRecord, which can be either a ARCRecord or WARCRecord. Note that
* record metadata is not included with the BitarchiveRecord, only the payload of the record.
* <p>
* If the length of the record is higher than Settings .BITARCHIVE_LIMIT_FOR_RECORD_DATATRANSFER_IN_FILE the data is
* stored in a RemoteFile, otherwise the data is stored in a byte array.
*
* @param record the ArchiveRecord that the data should come from. We do not close the ArchiveRecord.
* @param filename The filename of the ArchiveFile
*/
public BitarchiveRecord(ArchiveRecord record, String filename) {
ArgumentNotValid.checkNotNull(record, "ArchiveRecord record");
ArgumentNotValid.checkNotNull(filename, "String filename");
this.fileName = filename;
this.offset = record.getHeader().getOffset();
if (record instanceof ARCRecord) {
length = record.getHeader().getLength();
} else if (record instanceof WARCRecord) {
// The length of the payload of the warc-record is not getLength(),
// but getLength minus getContentBegin(), which is the number of
// bytes used for the record-header!
length = record.getHeader().getLength() - record.getHeader().getContentBegin();
} else {
throw new ArgumentNotValid("Unknown type of ArchiveRecord");
}
if (length > LIMIT_FOR_SAVING_DATA_IN_OBJECT_BUFFER) {
// copy arc-data to local file and create a RemoteFile based on this
log.info("Record exceeds limit of {} bytes. Length is {} bytes, Storing as instance of {}",
LIMIT_FOR_SAVING_DATA_IN_OBJECT_BUFFER, length, Settings.get(CommonSettings.REMOTE_FILE_CLASS));
if (RemoteFileFactory.isExtendedRemoteFile()) {
objectAsRemoteFile = RemoteFileFactory.getExtendedInstance(record);
isStoredAsRemoteFile = true;
} else {
File localTmpFile = null;
try {
localTmpFile = File.createTempFile("BitarchiveRecord-" + fileName, ".tmp", FileUtils.getTempDir());
record.dump(new FileOutputStream(localTmpFile));
objectAsRemoteFile = RemoteFileFactory.getMovefileInstance(localTmpFile);
isStoredAsRemoteFile = true;
} catch (IOException e) {
throw new IOFailure("Unable to store record(" + fileName + "," + offset + ") as remotefile", e);
}
}
} else { // Store data in objectbuffer
try {
if (record instanceof ARCRecord) {
objectBuffer = ARCUtils.readARCRecord((ARCRecord) record);
} else if (record instanceof WARCRecord) {
objectBuffer = WARCUtils.readWARCRecord((WARCRecord) record);
}
log.debug("Bytes stored in objectBuffer: {}", objectBuffer.length);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
}
/**
* Returns the file that this information was loaded from.
*
* @return the file that this ARC record comes from.
*/
public String getFile() {
return fileName;
}
/**
* Returns the length of the ARCRecord contained.
*
* @return the length of the ARCRecord contained
*/
public long getLength() {
return length;
}
/**
* Retrieve the data in the record. If data is in RemoteFile, this operation deletes the RemoteFile.
*
* @return the data from the ARCRecord as an InputStream.
* @throws IllegalState if remotefile already deleted
*/
public InputStream getData() {
InputStream result = null;
if (isStoredAsRemoteFile) {
if (hasRemoteFileBeenDeleted) {
throw new IllegalState("RemoteFile has already been deleted");
}
log.info("Reading {} bytes from RemoteFile", length);
InputStream rfInputStream = objectAsRemoteFile.getInputStream();
result = new FilterInputStream(rfInputStream) {
public void close() throws IOException {
super.close();
objectAsRemoteFile.cleanup();
hasRemoteFileBeenDeleted = true;
}
};
} else {
log.debug("Reading {} bytes from objectBuffer", length);
result = new ByteArrayInputStream(objectBuffer);
}
return result;
}
/**
* Deliver the data in the record to a given OutputStream. If data is in RemoteFile, this operation deletes the
* RemoteFile
*
* @param out deliver the data to this outputstream
* @throws IOFailure if any IOException occurs reading or writing the data
* @throws IllegalState if remotefile already deleted
*/
public void getData(OutputStream out) {
ArgumentNotValid.checkNotNull(out, "OutputStream out");
if (isStoredAsRemoteFile) {
if (hasRemoteFileBeenDeleted) {
throw new IllegalState("RemoteFile has already been deleted");
}
try {
log.debug("Reading {} bytes from RemoteFile", length);
objectAsRemoteFile.appendTo(out);
} finally {
log.trace("Deleting the RemoteFile '{}'.", objectAsRemoteFile.getName());
objectAsRemoteFile.cleanup();
hasRemoteFileBeenDeleted = true;
}
} else {
try {
log.debug("Reading {} bytes from objectBuffer", length);
out.write(objectBuffer, 0, objectBuffer.length);
} catch (IOException e) {
throw new IOFailure("Unable to write data from " + "objectBuffer to the outputstream", e);
}
}
}
public static BitarchiveRecord getBitarchiveRecord(String filename, File f, long index) {
try (ArchiveReader reader = ArchiveReaderFactory.get(f, index); ArchiveRecord record = reader.get();){
return new BitarchiveRecord(record, filename);
} catch (IOException e) {
throw new IOFailure("Error reading record from '" + filename + "' in file '"+f.getAbsolutePath()+"' offset " + index, e);
}
}
}