org.crosswire.jsword.book.sword
Class ZVerseBackend

java.lang.Object
  extended by org.crosswire.jsword.book.sword.AbstractBackend
      extended by org.crosswire.jsword.book.sword.ZVerseBackend
All Implemented Interfaces:
Activatable

public class ZVerseBackend
extends AbstractBackend

A backend to read compressed data verse based files. While the text file contains data compressed with ZIP or LZSS, it cannot be uncompressed using a stand alone zip utility, such as WinZip or gzip. The reason for this is that the data file is a concatenation of blocks of compressed data.

The blocks can either be "b", book (aka testament); "c", chapter or "v", verse. The choice is a matter of trade offs. The program needs to uncompress a block into memory. Having it at the book level is very memory expensive. Having it at the verse level is very disk expensive, but takes the least amount of memory. The most common is chapter.

In order to find the data in the text file, we need to find the block. The first index (comp) is used for this. Each verse is indexed to a tuple (block number, verse start, verse size). This data allows us to find the correct block, and to extract the verse from the uncompressed block, but it does not help us uncompress the block.

Once the block is known, then the next index (idx) gives the location of the compressed block, its compressed size and its uncompressed size.

There are 3 files for each testament, 2 (comp and idx) are indexes into the third (text) which contains the data. The key into each index is the verse index within that testament, which is determined by book, chapter and verse of that key.

All numbers are stored 2-complement, little endian.

Then proceed as follows, at all times working on the set of files for the testament in question:

 in the comp file, seek to the index * 10
 read 10 bytes.
 the block-index is the first 4 bytes (32-bit number)
 the next bytes are the verse offset and length of the uncompressed block.
 in the idx file seek to block-index * 12
 read 12 bytes
 the text-block-index is the first 4 bytes
 the data-size is the next 4 bytes
 the uncompressed-size is the next 4 bytes
 in the text file seek to the text-block-index
 read data-size bytes
 decipher them if they are encrypted
 unGZIP them into a byte uncompressed-size
 

Author:
Joe Walker [joe at eireneh dot com]
See Also:
for license details.
The copyright to this program is held by it's authors.

Field Summary
private  boolean active
          Are we active
private  BlockType blockType
          Whether the book is blocked by Book, Chapter or Verse.
private static int COMP_ENTRY_SIZE
          How many bytes in the comp index?
private static int IDX_ENTRY_SIZE
          How many bytes in the idx index?
private  long lastBlockNum
           
private  Testament lastTestament
           
private  byte[] lastUncompressed
           
private static Logger log
          The log stream
private  File ntCompFile
           
private  RandomAccessFile ntCompRaf
           
private  File ntIdxFile
           
private  RandomAccessFile ntIdxRaf
           
private  File ntTextFile
           
private  RandomAccessFile ntTextRaf
           
private  File otCompFile
          The compressed random access files
private  RandomAccessFile otCompRaf
          The compressed random access files
private  File otIdxFile
          The index random access files
private  RandomAccessFile otIdxRaf
          The index random access files
private  File otTextFile
          The data random access files
private  RandomAccessFile otTextRaf
          The data random access files
private static String SUFFIX_COMP
           
private static String SUFFIX_INDEX
           
private static String SUFFIX_PART1
           
private static String SUFFIX_TEXT
           
 
Constructor Summary
ZVerseBackend(SwordBookMetaData sbmd, BlockType blockType)
          Simple ctor
 
Method Summary
 void activate(Lock lock)
          Called to indicate that the Book should initialize itself, and consume whatever system resources it needs to be able to respond to other queries.
protected  void checkActive()
          Helper method so we can quickly activate ourselves on access
 boolean contains(Key key)
          Determine whether this Book contains the key in question
 void deactivate(Lock lock)
          Called to indicate that the Book should release whatever system resources it can to make way for other uses.
 String getRawText(Key key)
          Get the text allotted for the given entry
 void setAliasKey(Key alias, Key source)
          Sets alias for a comment on a verse range I.e.
 void setRawText(Key key, String text)
          Set the text allotted for the given verse
 
Methods inherited from class org.crosswire.jsword.book.sword.AbstractBackend
create, decipher, encipher, getBookMetaData, getExpandedDataPath, isSupported, isWritable, readIndex
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUFFIX_COMP

private static final String SUFFIX_COMP
See Also:
Constant Field Values

SUFFIX_INDEX

private static final String SUFFIX_INDEX
See Also:
Constant Field Values

SUFFIX_PART1

private static final String SUFFIX_PART1
See Also:
Constant Field Values

SUFFIX_TEXT

private static final String SUFFIX_TEXT
See Also:
Constant Field Values

blockType

private BlockType blockType
Whether the book is blocked by Book, Chapter or Verse.


lastTestament

private Testament lastTestament

lastBlockNum

private long lastBlockNum

lastUncompressed

private byte[] lastUncompressed

active

private boolean active
Are we active


otIdxRaf

private RandomAccessFile otIdxRaf
The index random access files


ntIdxRaf

private RandomAccessFile ntIdxRaf

otTextRaf

private RandomAccessFile otTextRaf
The data random access files


ntTextRaf

private RandomAccessFile ntTextRaf

otCompRaf

private RandomAccessFile otCompRaf
The compressed random access files


ntCompRaf

private RandomAccessFile ntCompRaf

otIdxFile

private File otIdxFile
The index random access files


ntIdxFile

private File ntIdxFile

otTextFile

private File otTextFile
The data random access files


ntTextFile

private File ntTextFile

otCompFile

private File otCompFile
The compressed random access files


ntCompFile

private File ntCompFile

COMP_ENTRY_SIZE

private static final int COMP_ENTRY_SIZE
How many bytes in the comp index?

See Also:
Constant Field Values

IDX_ENTRY_SIZE

private static final int IDX_ENTRY_SIZE
How many bytes in the idx index?

See Also:
Constant Field Values

log

private static final Logger log
The log stream

Constructor Detail

ZVerseBackend

public ZVerseBackend(SwordBookMetaData sbmd,
                     BlockType blockType)
Simple ctor

Method Detail

activate

public final void activate(Lock lock)
Description copied from interface: Activatable
Called to indicate that the Book should initialize itself, and consume whatever system resources it needs to be able to respond to other queries.

Parameters:
lock - An attempt to ensure that only the Activator calls this method

deactivate

public final void deactivate(Lock lock)
Description copied from interface: Activatable
Called to indicate that the Book should release whatever system resources it can to make way for other uses.

Parameters:
lock - An attempt to ensure that only the Activator calls this method

contains

public boolean contains(Key key)
Description copied from class: AbstractBackend
Determine whether this Book contains the key in question

Specified by:
contains in class AbstractBackend
Parameters:
key - The key whose presence is desired.
Returns:
true if the Book contains the key

getRawText

public String getRawText(Key key)
                  throws BookException
Description copied from class: AbstractBackend
Get the text allotted for the given entry

Specified by:
getRawText in class AbstractBackend
Parameters:
key - The key to fetch
Returns:
String The data for the verse in question
Throws:
BookException - If the data can not be read.

setAliasKey

public void setAliasKey(Key alias,
                        Key source)
                 throws IOException
Description copied from class: AbstractBackend
Sets alias for a comment on a verse range I.e. setRawText() was for verse range Gen.1.1-3 then setAliasKey should be called for Gen.1.1.2 and Gen.1.1.3

Specified by:
setAliasKey in class AbstractBackend
Parameters:
alias - Alias Key
source - Source Key
Throws:
IOException - Exception when anything goes wrong on writing the alias

setRawText

public void setRawText(Key key,
                       String text)
                throws BookException,
                       IOException
Description copied from class: AbstractBackend
Set the text allotted for the given verse

Specified by:
setRawText in class AbstractBackend
Parameters:
key - The key to set text to
text - The text to be set for key
Throws:
BookException - If the data can not be set.
IOException - If the module data path could not be created.

checkActive

protected final void checkActive()
Helper method so we can quickly activate ourselves on access


Copyright ? 2003-2011