square / tape

A lightning fast, transactional, file-based FIFO for Android and Java.
https://square.github.io/tape/
Apache License 2.0
2.47k stars 287 forks source link

Wrap QueueFile around an existing File? #199

Open bhavadeepk opened 6 years ago

bhavadeepk commented 6 years ago

From Javadocs,

// A persistent ObjectQueue.
ObjectQueue<String> queue = ObjectQueue.create(queueFile, converter);

I have some messages to be sent to a server from custom android devices in FIFO order. If the device is rebooted before all the messages in the queue are successfully sent, the remainder is persisted in the File I wrapped the queueFile with. But I cannot import this existing file into a new instance(after reboot) of the QueueFile unless I delete the file. #125 which loses the messages stacked before shutdown.

How can I persist the queue across reboots using this library?

Let me know if you have any questions. Thanks

JakeWharton commented 6 years ago

This should work out of the box. What exception do you get when you try to load the queue after reboot?

bhavadeepk commented 6 years ago

When I use, in my onCreate() or BootUpReceiver,

File file = // ...
QueueFile queueFile = new QueueFile.Builder(file).build();

I end up with EOFException unless I perform file.delete()operation.

himanshusahay commented 5 years ago

I'm facing the same issue where I end up with an EOF exception unless I delete the underlying file. Is there a resolution here?

steveperkins commented 5 years ago

I'm seeing the same problem as well. My purpose for using this library is to persist data locally and read it back in after an unexpected shutdown, but I I just get EOFException when trying to access the existing file (even if it's zero bytes).

steveperkins commented 5 years ago

I think core issue for me is that the example is unclear as to what // File... represents. I took it to mean I'm responsible for creating the queue file and providing that to QueueFile. This results in a 0-byte file ready for input. However, QueueFile(File) assumes the file will always either not exist or already have the requisite header, which causes the EOFException.

I'd think QueueFile would check the length of available bytes in the stream before attempting to read in the header (and if length is 0 bytes, just create the header), but apparently not.

My solution was to ignore the reference in the example and just use: QueueFile queueFile = new QueueFile.Builder(new File("fileThatWillBeCreatedIfNecessary.queue")).build();

If I have some time to dig deeper into the source I'll submit a PR.

ehartford commented 5 years ago

I have the same problem. Is there a workaround?