ru.biosoft.access
Class BTreeIndex

java.lang.Object
  extended byjava.util.AbstractMap
      extended byru.biosoft.access.BTreeIndex
All Implemented Interfaces:
Index, java.util.Map
Direct Known Subclasses:
BTreeRangeIndex

public class BTreeIndex
extends java.util.AbstractMap
implements Index

Maps keys to entries. A index cannot contain duplicate keys; each key can map to at most one entry.

Key and entries stored in file as Balanced Tree.

Keys are strings and entryes are Index.IndexEntry.

Since:
1.2
Version:
1.0
Author:
DevelopmentOnTheEdge
See Also:
Index.IndexEntry
To do:
Comment, Optimize for speed, Decrease size of index file when indexes removed., HIGH way for specify location for index (or create in temporary) - if dataFile on CD., Describe perfomance.

Nested Class Summary
protected  class BTreeIndex.Block
          One block of index file.
static class BTreeIndex.IntKey
           
static class BTreeIndex.NodeKey
           
 
Nested classes inherited from class ru.biosoft.access.Index
Index.IndexEntry, Index.StringIndexEntry
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Field Summary
protected  int currSize
          Current size of index
static int DEFAULT_BLOCK_SIZE
          Default file block size.
protected  java.io.RandomAccessFile indexFile
          Index file
protected  BTreeIndex.Block root
          Root
static int ROOT_BLOCK_OFFSET
          Root block offset
 
Fields inherited from interface ru.biosoft.access.Index
DEFAULT_INDEX_NAME
 
Constructor Summary
BTreeIndex(java.io.File dataFile, java.lang.String indexName, java.lang.String indexPath)
          Create index with DEFAULT_BLOCK_SIZE.
BTreeIndex(java.io.File dataFile, java.lang.String indexName, java.lang.String indexPath, int blockSize)
          Construct index based on specified file.
 
Method Summary
 void clear()
          Removes all keys and entries from this index.
 void close()
          Close this index and releases resources (index file).
 boolean containsKey(java.lang.Object key)
          Returns true if this index contains entry for the specified key.
 java.lang.Object createNode(byte[] buffer, int pos)
           
 java.lang.Object createNode(byte[] buffer, int pos, java.lang.String key, Index.IndexEntry value)
           
 java.util.Set entrySet()
          Returns a set view of the mappings contained in this index.
protected  void finalize()
           
 java.lang.Object get(java.lang.Object key)
          Returns the entry to which this index maps the specified key.
protected  BTreeIndex.Block getBlock(int offset)
           
 java.io.File getIndexFile()
          Returns index file.
 boolean isValid()
          Check is index file is valid.
 java.util.Iterator keyIterator(Key key)
           
 java.util.Iterator nodeIterator(Key key)
           
 java.lang.Object put(java.lang.Object key, java.lang.Object entry)
          Associates the specified entry with the specified key in this index.
 java.lang.Object remove(java.lang.Object key)
          Removes the entry for this key from this index if present.
 int size()
          Returns the number of key-entry mappings in this index.
 
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsValue, equals, hashCode, isEmpty, keySet, putAll, values
 

Field Detail

DEFAULT_BLOCK_SIZE

public static final int DEFAULT_BLOCK_SIZE
Default file block size.

See Also:
Constant Field Values

ROOT_BLOCK_OFFSET

public static final int ROOT_BLOCK_OFFSET
Root block offset

See Also:
Constant Field Values

indexFile

protected java.io.RandomAccessFile indexFile
Index file


currSize

protected int currSize
Current size of index


root

protected BTreeIndex.Block root
Root

Constructor Detail

BTreeIndex

public BTreeIndex(java.io.File dataFile,
                  java.lang.String indexName,
                  java.lang.String indexPath,
                  int blockSize)
           throws java.io.IOException
Construct index based on specified file. If index file already created and blockSize of file not equals specified blockSize, then all operations with index will be invalid and index file may be damaged.

Parameters:
dataFile - File for which index will be created. (or already created)
indexName - Name of the index. Will be added to index file name.
blockSize - Size of block for file operations.
Throws:
java.io.IOException - if an I/O error occurs.
To do:
Extended Comments needed.

BTreeIndex

public BTreeIndex(java.io.File dataFile,
                  java.lang.String indexName,
                  java.lang.String indexPath)
           throws java.io.IOException
Create index with DEFAULT_BLOCK_SIZE.

Parameters:
dataFile - File for which index will be created. (or already created)
indexName - Name of the index. Will be added to index file name.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
#BTreeIndex(File,String,int)
To do:
Extended Comments needed.
Method Detail

createNode

public java.lang.Object createNode(byte[] buffer,
                                   int pos,
                                   java.lang.String key,
                                   Index.IndexEntry value)

createNode

public java.lang.Object createNode(byte[] buffer,
                                   int pos)

keyIterator

public java.util.Iterator keyIterator(Key key)

nodeIterator

public java.util.Iterator nodeIterator(Key key)
Specified by:
nodeIterator in interface Index

isValid

public boolean isValid()
Check is index file is valid. Check existence and format of index file. If file corrupt you must clear index.

Specified by:
isValid in interface Index
Returns:
true if index valid, false otherwise.
See Also:
clear()

size

public int size()
Returns the number of key-entry mappings in this index. If the index contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface java.util.Map
Returns:
the number of key-entry mappings in this index.

entrySet

public java.util.Set entrySet()
Returns a set view of the mappings contained in this index. Each element in the returned set is a Map.Entry. The set is backed by the index, so changes to the index are reflected in the set, and vice-versa. If the index is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the index, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface java.util.Map
Returns:
a set view of the mappings contained in this index.
To do:
HIGH Test changes reflection!!!.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this index contains entry for the specified key.

Specified by:
containsKey in interface java.util.Map
Parameters:
key - key whose presence in this index is to be tested.
Returns:
true if this index contains entry for the specified key.
Throws:
java.lang.ClassCastException - if the key is not of type String.
java.lang.NullPointerException - key is null and this index does not permit null keys.
ChainRuntimeException - if an I/O error occurs.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object entry)
Associates the specified entry with the specified key in this index. If the index previously contained entry for this key, the old entry is replaced.

Specified by:
put in interface java.util.Map
Parameters:
entry - entry to be associated with the specified key.
Returns:
previous entry associated with specified key, or null if there was no mapping for key.
Throws:
java.lang.ClassCastException - if the key is not of type String. or entry is not of type Index.IndexEntry.
java.lang.IllegalArgumentException - if some aspect of this key or entry prevents it from being stored in this index.
java.lang.NullPointerException - this index does not permit null keys or entries, and the specified key or entry is null.
ChainRuntimeException - if an I/O error occurs.

get

public java.lang.Object get(java.lang.Object key)
Returns the entry to which this index maps the specified key. Returns null if the index contains no entry for this key.

Specified by:
get in interface java.util.Map
Parameters:
key - key whose associated entry is to be returned.
Returns:
the entry to which this index maps the specified key, or null if the index not contains entry for this key.
Throws:
java.lang.ClassCastException - if the key is not of type String.
java.lang.NullPointerException - key is null and this index does not permit null keys.
ChainRuntimeException - if an I/O error occurs.
See Also:
Index.IndexEntry

remove

public java.lang.Object remove(java.lang.Object key)
Removes the entry for this key from this index if present.

Specified by:
remove in interface java.util.Map
Parameters:
key - key whose entry is to be removed from the index.
Returns:
previous entry associated with specified key, or null if there was no entry for key.
Throws:
java.lang.ClassCastException - if the key is not of type String.
ChainRuntimeException - if an I/O error occurs.

getIndexFile

public java.io.File getIndexFile()
Returns index file. This information is essential to remove index files.

Specified by:
getIndexFile in interface Index
Returns:
index file.

clear

public void clear()
Removes all keys and entries from this index. File stored key-entries recreates.

Specified by:
clear in interface java.util.Map
Throws:
ChainRuntimeException - if an I/O error occurs.

close

public void close()
           throws java.io.IOException
Close this index and releases resources (index file). A closed index cannot perform any operations and cannot be reopened.

Specified by:
close in interface Index
Throws:
java.io.IOException - if an I/O error occurs.

finalize

protected void finalize()
                 throws java.lang.Throwable
Throws:
java.lang.Throwable

getBlock

protected BTreeIndex.Block getBlock(int offset)
                             throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2001-2003 Biosof.Ru. All Rights Reserved.