Dan Grahn created AVRO-2188:
-------------------------------
Summary: SpecificDatumReader Corrupts Bytes Field When Using
Next(R)
Key: AVRO-2188
URL: https://issues.apache.org/jira/browse/AVRO-2188
Project: Avro
Issue Type: Bug
Components: java
Affects Versions: 1.8.2
Reporter: Dan Grahn
I am loading large sets of data into Avro files and cataloging them for quick
access. My code for the load looks a bit like this.
{code:java}
DataFileWriter<MyData> writer;
final long offset = writer.sync();
writer.append(data);
{code}
When I am ready to read the data, that was the code that I initially used.
{code:java}
SpecificDatumReader<MyDat> reader;
reader.sync(offset);
while (reader.hasNext()) {
MyData data = reader.next();
if (matchesId(data)) return data;
}
{code}
This worked for the majority of cases. But a few of them had a problem with the
call to sync. In these cases a call to "reader.tell()" indicated that the sync
was actually PAST the offset. This meant that I would never retrieve the file
that I wanted.
In order to work around this issue, I implemented a simple reversal algorithm
which works roughly like this.
{code:java}
SpecificDatumReader<MyDat> reader;
reader.sync(offset);
long reversal = 10;
while (reader.tell() >= offset) {
reader.sync(offset - reversal);
reversal *= 2;
}
while (reader.hasNext()) {
MyData data = reader.next();
if (matchesId(data)) return data;
}{code}
This works correctly in, what I believe, are all cases. Now I am SURE that I am
doing something wrong, since this process seems like an extremely convoluted
way to retrieve data. However, the issue is what happened next.
To see if performance could be improved, I changed my last loop to this.
{code:java}
MyData data;
while (reader.hasNext()) {
data = reader.next(data);
if (matchesId(data)) return data;
}{code}
My schema has several string fields and a contents field which is bytes. It
looks something like this.
{code:java}
{
"title": "the title",
"contents": "[base 64 file contents]"
}
{code}
Once I made the change for performance, I started seeing data returned like
this.
{code:java}
{
"title": "the title",
"contents": "[file contents][fragment of contents from previous file]"
}
{code}
I'm thinking that this is because the byte array is being reused. Any ideas on
this?
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)