com.limegroup.gnutella
Class FileManager

java.lang.Object
  extended bycom.limegroup.gnutella.FileManager
Direct Known Subclasses:
MetaFileManager

public abstract class FileManager
extends java.lang.Object

The list of all shared files. Provides operations to add and remove individual files, directory, or sets of directories. Provides a method to efficiently query for files whose names contain certain keywords.

This class is thread-safe.


Field Summary
static java.lang.String BROWSE_QUERY
          The string used by LimeWire to browse hosts.
static java.lang.String DELIMETERS
          Characters used to tokenize queries and file names.
static java.io.FilenameFilter DIRECTORY_FILTER
          The only DirectoryFilter object that should be used.
static java.lang.String INDEXING_QUERY
          The string used by Clip2 reflectors to index hosts.
static java.io.FilenameFilter SHAREABLE_FILE_FILTER
          The only ShareableFileFilter object that should be used.
 
Constructor Summary
FileManager()
          Creates a new FileManager instance.
 
Method Summary
 FileDesc addFileIfShared(java.io.File file)
           
abstract  FileDesc addFileIfShared(java.io.File file, java.util.List metadata)
           
 void addIncompleteFile(java.io.File incompleteFile, java.util.Set urns, java.lang.String name, int size, VerifyingFile vf)
          Adds an incomplete file to be used for partial file sharing.
protected abstract  void addXMLToResponse(Response res, FileDesc desc)
          This implementation does nothing.
static void debug(java.lang.String out)
           
 FileDesc fileChanged(java.io.File f)
          Notification that a file has changed and new hashes should be calculated.
 FileDesc get(int i)
          Returns the file descriptor with the given index.
 FileDesc[] getAllSharedFileDescriptors()
          Returns a list of all shared file descriptors.
 FileDesc getFileDescForFile(java.io.File f)
          Returns the FileDesc that is wrapping this File or null if the file is not shared.
 FileDesc getFileDescForUrn(URN urn)
          Returns the FileDesc for the specified URN.
static java.io.File[] getFilesRecursive(java.io.File directory, java.lang.String[] filter)
           
 FileDesc[] getIncompleteFileDescriptors()
          Returns a list of all shared incomplete file descriptors.
 java.util.List getIndivisibleKeyWords()
           
 java.util.List getKeyWords()
          called when a query route table has to be made.
 int getNumFiles()
          Returns the number of files.
 int getNumIncompleteFiles()
          Returns the number of shared incomplete files.
 int getNumPendingFiles()
          Returns the number of pending files.
 FileDesc[] getSharedFileDescriptors(java.io.File directory)
          Returns a list of all shared file descriptors in the given directory, in any order.
 int getSize()
          Returns the size of all files, in bytes.
static boolean isFileShareable(java.io.File file, long fileLength)
          Returns true if this file is sharable.
 boolean isValidIndex(int i)
          Determines whether or not the specified index is valid.
 void loadSettings(boolean notifyOnClear)
          Ensures this contains exactly the files specified by the EXTENSIONS_TO_SHARE and DIRECTORIES_TO_SHARE properties.
protected  void loadSettingsBlocking(boolean notifyOnClear)
          Clears this', reloads this' extensions, generates an array of directories, and then indexes the generated directories files.
protected  boolean loadThreadInterrupted()
          Returns true if the load thread has been interrupted an this should stop loading files.
 Response[] query(QueryRequest request)
          Returns an array of all responses matching the given request.
 FileDesc removeFileIfShared(java.io.File f)
           
 boolean renameFileIfShared(java.io.File oldName, java.io.File newName)
          If oldName isn't shared, returns false.
protected  void repOk()
           
protected  IntSet search(java.lang.String query, IntSet priors)
          Returns a set of indices of files matching q, or null if there are no matches.
protected abstract  boolean shouldIncludeXMLInResponse(QueryRequest qr)
          A normal FileManager will never include XML.
 void start()
          Asynchronously loads all files by calling loadSettings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INDEXING_QUERY

public static final java.lang.String INDEXING_QUERY
The string used by Clip2 reflectors to index hosts.

See Also:
Constant Field Values

BROWSE_QUERY

public static final java.lang.String BROWSE_QUERY
The string used by LimeWire to browse hosts.

See Also:
Constant Field Values

SHAREABLE_FILE_FILTER

public static java.io.FilenameFilter SHAREABLE_FILE_FILTER
The only ShareableFileFilter object that should be used.


DIRECTORY_FILTER

public static java.io.FilenameFilter DIRECTORY_FILTER
The only DirectoryFilter object that should be used.


DELIMETERS

public static final java.lang.String DELIMETERS
Characters used to tokenize queries and file names.

See Also:
Constant Field Values
Constructor Detail

FileManager

public FileManager()
Creates a new FileManager instance.

Method Detail

start

public void start()
Asynchronously loads all files by calling loadSettings. Sets this' callback to be "callback", and notifies "callback" of all file loads.

See Also:
loadSettings

getSize

public int getSize()
Returns the size of all files, in bytes. Note that the largest value that can be returned is Integer.MAX_VALUE, i.e., ~2GB. If more bytes are being shared, returns this value.


getNumFiles

public int getNumFiles()
Returns the number of files.


getNumIncompleteFiles

public int getNumIncompleteFiles()
Returns the number of shared incomplete files.


getNumPendingFiles

public int getNumPendingFiles()
Returns the number of pending files.


get

public FileDesc get(int i)
Returns the file descriptor with the given index. Throws IndexOutOfBoundsException if the index is out of range. It is also possible for the index to be within range, but for this method to return null, such as the case where the file has been unshared.

Parameters:
i - the index of the FileDesc to access
Returns:
the FileDesc at the specified index, which may be null
Throws:
IndexOutOfBoundsException - if the index is out of range

isValidIndex

public boolean isValidIndex(int i)
Determines whether or not the specified index is valid. The index is valid if it is within range of the number of files shared, i.e., if:

i >= 0 && i < _files.size()

Parameters:
i - the index to check
Returns:
true if the index is within range of our shared file data structure, otherwise false

getFileDescForFile

public FileDesc getFileDescForFile(java.io.File f)
Returns the FileDesc that is wrapping this File or null if the file is not shared.


getFileDescForUrn

public FileDesc getFileDescForUrn(URN urn)
Returns the FileDesc for the specified URN. This only returns one FileDesc, even though multiple indeces are possible with HUGE v. 0.93.

Parameters:
urn - the urn for the file
Returns:
the FileDesc corresponding to the requested urn, or null if no matching FileDesc could be found

getIncompleteFileDescriptors

public FileDesc[] getIncompleteFileDescriptors()
Returns a list of all shared incomplete file descriptors.


getAllSharedFileDescriptors

public FileDesc[] getAllSharedFileDescriptors()
Returns a list of all shared file descriptors.


getSharedFileDescriptors

public FileDesc[] getSharedFileDescriptors(java.io.File directory)
Returns a list of all shared file descriptors in the given directory, in any order. Returns null if directory is not shared, or a zero-length array if it is shared but contains no files. This method is not recursive; files in any of the directory's children are not returned.


getFilesRecursive

public static java.io.File[] getFilesRecursive(java.io.File directory,
                                               java.lang.String[] filter)
Parameters:
directory - Gets all files under this directory RECURSIVELY.
filter - If null, then returns all files. Else, only returns files extensions in the filter array.
Returns:
A Array of Files recursively obtained from the directory, according to the filter. TODO:: add test!

debug

public static void debug(java.lang.String out)

loadSettings

public void loadSettings(boolean notifyOnClear)
Ensures this contains exactly the files specified by the EXTENSIONS_TO_SHARE and DIRECTORIES_TO_SHARE properties. That is, clears this and loads all files with the given extensions in the given directories and their children. Note that some files in this before the call will not be in this after the call, or they may have a different index. If DIRECTORIES_TO_SHARE contains duplicate directories, the duplicates will be ignored. If it contains files, they will be ignored.

This method is thread-safe but non-blocking. When the method returns, the directory and extension settings used by addFileIfShared() are initialized. However, files will actually be indexed asynchronously in another thread. This is useful because indexing may take up to 30 seconds or so if sharing many files. If loadSettings is subsequently called before the indexing is complete, the original settings are discarded, and loading starts over immediately. Modification 8/01 - This method is still non-blocking and thread safe, but it was refactored to make for easier subclassing of FileManager. Now, a protected method called loadSettingsBlocking() is used to index the files asynchronously. Subclasses can override or extend this method to impose their own functionality. For example, see MetaFileManager.

Parameters:
notifyOnClear - if true, callback is notified via clearSharedFiles when the previous load settings thread has been killed.

loadThreadInterrupted

protected boolean loadThreadInterrupted()
Returns true if the load thread has been interrupted an this should stop loading files.


loadSettingsBlocking

protected void loadSettingsBlocking(boolean notifyOnClear)
Clears this', reloads this' extensions, generates an array of directories, and then indexes the generated directories files. NOTE TO SUBCLASSES: extend this method as needed, it shall be threaded and run asynchronously as to not slow down the main thread (the GUI).


addFileIfShared

public FileDesc addFileIfShared(java.io.File file)
Returns:
the FileDesc for the new file if it was successfully added, otherwise null

addFileIfShared

public abstract FileDesc addFileIfShared(java.io.File file,
                                         java.util.List metadata)
Returns:
the FileDesc for the new file if it was successfully added, otherwise null

addIncompleteFile

public void addIncompleteFile(java.io.File incompleteFile,
                              java.util.Set urns,
                              java.lang.String name,
                              int size,
                              VerifyingFile vf)
Adds an incomplete file to be used for partial file sharing.

Parameters:
incompleteFile - the incomplete file.
urns - the set of all known URNs for this incomplete file
name - the completed name of this incomplete file
size - the completed size of this incomplete file
vf - the VerifyingFile containing the ranges for this inc. file

fileChanged

public FileDesc fileChanged(java.io.File f)
Notification that a file has changed and new hashes should be calculated.

Returns:
the new FileDesc for the file if it was successfully changed, otherwise null

removeFileIfShared

public FileDesc removeFileIfShared(java.io.File f)

renameFileIfShared

public boolean renameFileIfShared(java.io.File oldName,
                                  java.io.File newName)
If oldName isn't shared, returns false. Otherwise removes "oldName", adds "newName", and returns true iff newName is actually shared. The new file may or may not have the same index as the original. Note that this does not change the disk.


isFileShareable

public static boolean isFileShareable(java.io.File file,
                                      long fileLength)
Returns true if this file is sharable.


getKeyWords

public java.util.List getKeyWords()
called when a query route table has to be made. The current implementaion just takes all the file names and they are split internally when added the QRT


getIndivisibleKeyWords

public java.util.List getIndivisibleKeyWords()
Returns:
A List of KeyWords from the FS that one does NOT want broken upon hashing into a QRT. Initially being used for schema hashing. We add all the hashes of the files we share so queries with hashes can be checked for potential positives against a QRT.

query

public Response[] query(QueryRequest request)
Returns an array of all responses matching the given request. If there are no matches, the array will be empty (zero size). Incomplete Files are NOT returned in responses to queries. Design note: returning an empty array requires no extra allocations, as empty arrays are immutable.


shouldIncludeXMLInResponse

protected abstract boolean shouldIncludeXMLInResponse(QueryRequest qr)
A normal FileManager will never include XML. It is expected that MetaFileManager overrides this and returns true in some instances.


addXMLToResponse

protected abstract void addXMLToResponse(Response res,
                                         FileDesc desc)
This implementation does nothing.


search

protected IntSet search(java.lang.String query,
                        IntSet priors)
Returns a set of indices of files matching q, or null if there are no matches. Subclasses may override to provide different notions of matching. The caller of this method must not mutate the returned value.


repOk

protected void repOk()