Skip to content

Commit

Permalink
Expose the ability to close the file backing a parsed database.
Browse files Browse the repository at this point in the history
Also needed to switch from using default ByteBufferKaitaiStream to
explicitly creating RandomAccessFileKaitaiStream instances because
of kaitai-io/kaitai_struct#497
  • Loading branch information
brunchboy committed Dec 5, 2018
1 parent 9af02fa commit fa0e818
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/org/deepsymmetry/cratedigger/Database.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.deepsymmetry.cratedigger;

import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.RandomAccessFileKaitaiStream;
import org.deepsymmetry.cratedigger.pdb.RekordboxPdb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
Expand All @@ -14,7 +16,7 @@
* <p>Parses rekordbox database export files, providing access to the information they contain.</p>
*/
@SuppressWarnings("WeakerAccess")
public class Database {
public class Database implements Closeable {

private static final Logger logger = LoggerFactory.getLogger(Database.class);

Expand All @@ -31,7 +33,11 @@ public class Database {
/**
* Construct a database access instance from the specified recordbox export file.
* The file can obtained either from the SD or USB media, or directly from a player
* using {@link FileFetcher#fetch(InetAddress, String, String, File)}
* using {@link FileFetcher#fetch(InetAddress, String, String, File)}.
*
* Be sure to call {@link #close()} when you are done using the parsed database
* to close the underlying file or users will be unable to unmount the drive holding
* it until they quit your program.
*
* @param sourceFile an export.pdb file
*
Expand All @@ -40,7 +46,7 @@ public class Database {
@SuppressWarnings("WeakerAccess")
public Database(File sourceFile) throws IOException {
this.sourceFile = sourceFile;
pdb = RekordboxPdb.fromFile(sourceFile.getAbsolutePath());
pdb = new RekordboxPdb(new RandomAccessFileKaitaiStream(sourceFile.getAbsolutePath()));

final SortedMap<String, SortedSet<Long>> mutableTrackTitleIndex = new TreeMap<String, SortedSet<Long>>(String.CASE_INSENSITIVE_ORDER);
final SortedMap<Long, SortedSet<Long>> mutableTrackArtistIndex = new TreeMap<Long, SortedSet<Long>>();
Expand Down Expand Up @@ -663,6 +669,18 @@ public void rowFound(KaitaiStruct row) {
return Collections.unmodifiableMap(result);
}

/**
* Close the file underlying the parsed database. This needs to be called if you want to be able
* to unmount the media on which that file resides, but once it is done, you can no longer access
* lazy elements within the database which have not already been parsed.
*
* @throws IOException if there is a problem closing the file
*/
@Override
public void close() throws IOException {
pdb._io().close();
}

/**
* Helper function to extract the text value from one of the strings found in the database, which
* have a variety of obscure representations.
Expand Down

0 comments on commit fa0e818

Please sign in to comment.