org.gcube.common.searchservice.searchlibrary.resultset.helpers
Class RSFileHelper

java.lang.Object
  extended by org.gcube.common.searchservice.searchlibrary.resultset.helpers.RSFileHelper

public class RSFileHelper
extends java.lang.Object

Helper class of the ResultSet element

Author:
UoA

Constructor Summary
RSFileHelper()
           
 
Method Summary
static java.lang.String contentToHeader(java.lang.String contentName)
          Converts a content file name to the respective header one.
static void copy(ResultSet rs, java.lang.String src, java.io.DataOutputStream dst)
          Copies the source file to a new file
static void copy(java.lang.String src, java.io.DataOutputStream dst)
          Copies the source file to an output stream
static void copy(java.lang.String src, java.lang.String dst)
          Copies the source file to a new file
static boolean forceConsistency(java.lang.String filename)
          forces the filesystem to remain consistent by using metadata changing functions
static java.lang.String generateName(short type, java.lang.String seed)
          Generates a name of the type that is provided which can either be unique or have the unique value part equal to the provided one
static java.io.DataInputStream getBinaryContent(java.lang.String file, org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
          Retrieves the full content of the provided file
static byte[] getBytesFromFile(java.io.File file)
          Read file into a byte array
static java.lang.String getContent(java.lang.String file, org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
          Retrieves the full content of the provided file
static int getNumberOfResultsInTextFile(java.lang.String filename)
          Retrives the number of results in the provided file that has been structured as a text payload part
static java.lang.String headerToContent(java.lang.String headerName)
          Converts a header file name to the respective content one.
static java.lang.String headToFlowControl(java.lang.String head)
          Constructs the flag used for on demand production file that corresponds to the specfic head part
static java.lang.String headToFlowControlTmp(java.lang.String head)
          Constructs the temporary flag used for on demand production file that corresponds to the specfic head part
static boolean isReady(java.lang.String filename)
          Checks if the provided file is ready for consumption
static void notifyOnFlowCreation(java.lang.String headname)
          notidies the consumer that more records were created
static void persistContent(java.lang.String filename, java.lang.String inWrap, org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
          Persists the content of the provided source file to the provided target file
static void persistContent(java.lang.String filename, java.util.Vector<java.lang.String> results, int count, org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
          Persists the content available in the provided vector o a file whose name is given.
static void persistHeader(HeaderRef header)
          Persists a header file with the information available in its argument
static java.lang.String persistStream(java.io.InputStream content)
          persists the stream in a file and returns the filename
static HeaderRef populateHeader(java.lang.String filename)
          Populates a header info class instance from the content of the provided file
static java.util.Vector<java.lang.String> populateResults(java.lang.String filename, org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
          Populates a record vecror with the records available in the provided file
static java.lang.String publicToTmp(java.lang.String pubName)
          Converts a public file name to a private temparary one
static void requestFlowCreation(java.lang.String headname)
          notifies the producer of an RS that more records are needed
static void runGC()
          Attempt to call the garbage collector
static java.lang.String[] splitFile(java.lang.String filename)
          Splits the provided file in smaller parts as is dictated by RSConstants.partSize
static java.lang.String tmpToPublic(java.lang.String tmpName)
          Converts a tmp file name to a public one
static void touchHeader(java.lang.String filename)
          Populates a header info class instance from the content of the provided file
static boolean waitForIt(java.lang.String filename, boolean flowControl, java.lang.String headname)
          Waits until the provided file is ready to be consumed or a maximum amount of time has been reached.
static boolean waitForIt(java.lang.String filename, long time, boolean flowControl, java.lang.String headname)
          Waits until the provided file is ready to be consumed or a maximum amount of time has been reached.
static RSConstants.CONTROLFLOW waitOnFlowNotification(java.lang.String headname, long time)
          blocks waiting for notifications on result production for the specified time period
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RSFileHelper

public RSFileHelper()
Method Detail

generateName

public static java.lang.String generateName(short type,
                                            java.lang.String seed)
                                     throws java.lang.Exception
Generates a name of the type that is provided which can either be unique or have the unique value part equal to the provided one

Parameters:
type - The type of filename to generate. This can be one of RSConstants.CONTENT, RSConstants.HEADER, RSConstants.PAGEDCONTENT
seed - If a non null value is provided, The unique part of the generated file will have the supplied value. Otherwise, a unique value is generated and used
Returns:
The generated file name
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

headToFlowControl

public static java.lang.String headToFlowControl(java.lang.String head)
Constructs the flag used for on demand production file that corresponds to the specfic head part

Parameters:
head - The head file of the RS
Returns:
the flag file

headToFlowControlTmp

public static java.lang.String headToFlowControlTmp(java.lang.String head)
Constructs the temporary flag used for on demand production file that corresponds to the specfic head part

Parameters:
head - The head file of the RS
Returns:
the flag file

waitOnFlowNotification

public static RSConstants.CONTROLFLOW waitOnFlowNotification(java.lang.String headname,
                                                             long time)
blocks waiting for notifications on result production for the specified time period

Parameters:
headname - The head file
time - The timeout
Returns:
The status of the request

notifyOnFlowCreation

public static void notifyOnFlowCreation(java.lang.String headname)
notidies the consumer that more records were created

Parameters:
headname - The head name of the RS that needed more results

requestFlowCreation

public static void requestFlowCreation(java.lang.String headname)
notifies the producer of an RS that more records are needed

Parameters:
headname - The head name of the RS that needs more results

publicToTmp

public static java.lang.String publicToTmp(java.lang.String pubName)
                                    throws java.lang.Exception
Converts a public file name to a private temparary one

Parameters:
pubName - The file name to convert
Returns:
The temporary name
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

tmpToPublic

public static java.lang.String tmpToPublic(java.lang.String tmpName)
                                    throws java.lang.Exception
Converts a tmp file name to a public one

Parameters:
tmpName - the temporary name
Returns:
the public name
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

headerToContent

public static java.lang.String headerToContent(java.lang.String headerName)
                                        throws java.lang.Exception
Converts a header file name to the respective content one.

Parameters:
headerName - THe header name to convert
Returns:
The respective content one
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

contentToHeader

public static java.lang.String contentToHeader(java.lang.String contentName)
                                        throws java.lang.Exception
Converts a content file name to the respective header one.

Parameters:
contentName - The content name to convert
Returns:
The respective header one
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

persistContent

public static void persistContent(java.lang.String filename,
                                  java.util.Vector<java.lang.String> results,
                                  int count,
                                  org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
                           throws java.lang.Exception
Persists the content available in the provided vector o a file whose name is given. The number of content records to persist is given as a parameter

Parameters:
filename - The filename to perist the content in
results - The vector holding the payload
mne - the encryption class
count - The number of records to persist. If this is negative, all the available records are persisted
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

forceConsistency

public static boolean forceConsistency(java.lang.String filename)
                                throws java.lang.Exception
forces the filesystem to remain consistent by using metadata changing functions

Parameters:
filename - the filename
Returns:
whether or not the forcing was successful
Throws:
java.lang.Exception - An error has occured

persistContent

public static void persistContent(java.lang.String filename,
                                  java.lang.String inWrap,
                                  org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
                           throws java.lang.Exception
Persists the content of the provided source file to the provided target file

Parameters:
filename - The target file
inWrap - The source file
mne - the encryption class
Throws:
java.lang.Exception - An unrecoverable for the operation error occurred

persistHeader

public static void persistHeader(HeaderRef header)
                          throws java.lang.Exception
Persists a header file with the information available in its argument

Parameters:
header - The header info
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

populateHeader

public static HeaderRef populateHeader(java.lang.String filename)
                                throws java.lang.Exception
Populates a header info class instance from the content of the provided file

Parameters:
filename - The file holding the header payload
Returns:
The Header info
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

touchHeader

public static void touchHeader(java.lang.String filename)
Populates a header info class instance from the content of the provided file

Parameters:
filename - The file holding the header to touch

waitForIt

public static boolean waitForIt(java.lang.String filename,
                                boolean flowControl,
                                java.lang.String headname)
Waits until the provided file is ready to be consumed or a maximum amount of time has been reached. The maximum amount of time is the one declared in RSConstants.sleepMax in milliseconds

Parameters:
filename - The filename to wait for
flowControl - whether or not the RS supports on demand production of results
headname - The head file name of this RS
Returns:
whether or not the filename if ready to be read

waitForIt

public static boolean waitForIt(java.lang.String filename,
                                long time,
                                boolean flowControl,
                                java.lang.String headname)
Waits until the provided file is ready to be consumed or a maximum amount of time has been reached. The maximum amount of time is the one provided in milliseconds

Parameters:
filename - The filename to wait for
time - The maximum time in milliseconds to wait for the file
flowControl - whether or not the RS supports on demand production of results
headname - The head file name of this RS
Returns:
whether or not the filename if ready to be read

isReady

public static boolean isReady(java.lang.String filename)
Checks if the provided file is ready for consumption

Parameters:
filename - The name of the file to check if it is ready
Returns:
true if it is, false otherwise

populateResults

public static java.util.Vector<java.lang.String> populateResults(java.lang.String filename,
                                                                 org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
                                                          throws java.lang.Exception
Populates a record vecror with the records available in the provided file

Parameters:
filename - The file to retrieve the records from
mne - the encryption class
Returns:
The populated vector
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

getContent

public static java.lang.String getContent(java.lang.String file,
                                          org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
                                   throws java.lang.Exception
Retrieves the full content of the provided file

Parameters:
file - The file whose content must be retrieved
mne - the encryption class
Returns:
The full file content
Throws:
java.lang.Exception - An unrecoverable for the operation error occurred

getBinaryContent

public static java.io.DataInputStream getBinaryContent(java.lang.String file,
                                                       org.gcube.common.searchservice.searchlibrary.resultset.security.Mnemonic mne)
                                                throws java.lang.Exception
Retrieves the full content of the provided file

Parameters:
file - The file whose content must be retrieved
mne - the encryption class
Returns:
The full file content
Throws:
java.lang.Exception - An unrecoverable for the operation error occurred

splitFile

public static java.lang.String[] splitFile(java.lang.String filename)
                                    throws java.lang.Exception
Splits the provided file in smaller parts as is dictated by RSConstants.partSize

Parameters:
filename - The file to split
Returns:
A vector holding the names of the created parts
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

copy

public static void copy(java.lang.String src,
                        java.io.DataOutputStream dst)
                 throws java.lang.Exception
Copies the source file to an output stream

Parameters:
src - The source file
dst - The destination stream
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

copy

public static void copy(java.lang.String src,
                        java.lang.String dst)
                 throws java.lang.Exception
Copies the source file to a new file

Parameters:
src - The source file
dst - The destination file
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

copy

public static void copy(ResultSet rs,
                        java.lang.String src,
                        java.io.DataOutputStream dst)
                 throws java.lang.Exception
Copies the source file to a new file

Parameters:
rs - the RS
src - The source file
dst - The destination file
Throws:
java.lang.Exception - An unrecoverable for the operation error occurred

persistStream

public static java.lang.String persistStream(java.io.InputStream content)
                                      throws java.lang.Exception
persists the stream in a file and returns the filename

Parameters:
content - the stream to persist
Returns:
the filename
Throws:
java.lang.Exception - an unrecoverable for the operation error occurred

getNumberOfResultsInTextFile

public static int getNumberOfResultsInTextFile(java.lang.String filename)
                                        throws java.lang.Exception
Retrives the number of results in the provided file that has been structured as a text payload part

Parameters:
filename - The file to retrieve the records from
Returns:
The number of records
Throws:
java.lang.Exception - An unrecoverable for the operation error occured

getBytesFromFile

public static byte[] getBytesFromFile(java.io.File file)
                               throws java.lang.Exception
Read file into a byte array

Parameters:
file - The File handler
Returns:
A byte array that stores the file content
Throws:
java.lang.Exception - in case of error

runGC

public static void runGC()
Attempt to call the garbage collector