Class AbstractSQLFunctionDatabase<VF extends LSHVectorFactory>
java.lang.Object
ghidra.features.bsim.query.client.AbstractSQLFunctionDatabase<VF>
- Type Parameters:
VF
-vector factory
implementation class
- All Implemented Interfaces:
FunctionDatabase
,SQLFunctionDatabase
,AutoCloseable
- Direct Known Subclasses:
H2FileFunctionDatabase
,PostgresFunctionDatabase
public abstract class AbstractSQLFunctionDatabase<VF extends LSHVectorFactory>
extends Object
implements SQLFunctionDatabase
Defines the BSim
FunctionDatabase
backed by an SQL database.
Simple, one-column tables that only contain string data use the
SQLStringTable
class and are defined in this class. More complex
tables are defined in their own classes in the
ghidra.features.bsim.query.client.tables
package.-
Nested Class Summary
Nested classes/interfaces inherited from interface ghidra.features.bsim.query.FunctionDatabase
FunctionDatabase.BSimError, FunctionDatabase.ConnectionType, FunctionDatabase.DatabaseNonFatalException, FunctionDatabase.ErrorCategory, FunctionDatabase.Status
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final BSimJDBCDataSource
static final String
static final String
final int
protected final VF
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractSQLFunctionDatabase
(BSimJDBCDataSource ds, VF vectorFactory, int supportedLayoutVersion) -
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendEscapedLiteral
(StringBuilder buf, String str) protected Connection
beginTransaction
(boolean lockTablesForWrite) Start a transaction which will not be auto-committed.void
close()
Close down (the connection with) the databaseprotected void
Perform any required cleanup prior to connection close.int
Return -1 if info layout version is earlier than current client expectation Return 1 if info layout version is later than current client expectation Return 0 if info version and client version are the sameprotected void
convertDescriptionRows
(SimilarityResult simres, List<DescriptionTable.DescriptionRow> descvec, VectorResult vecres, DescriptionManager res, SignatureRecord srec) Convert a low-level list of function rows into full FunctionDescription objectsprotected void
createDatabase
(Configuration config) Generate tables and initialize state for a new database.protected abstract int
deleteVectors
(long id, int countdiff) Low level count decrement of a vector record from vectable, if count reaches zero, the record is deletedprotected QueryResponseRecord
doQuery
(BSimQuery<?> query, Connection c) protected abstract void
Drop this databaseprotected void
endTransaction
(boolean commit) End a transaction which was started withbeginTransaction(boolean)
and return DB connection to an auto-commit state.protected abstract void
Generate new empty database which corresponds tods
.Returns a list of all function tags in the database.getInfo()
If the last query failed to produce a response, use this method to recover the error messageReturn theserver info object
for this databaseprotected Connection
Establish PostgreSQL DB pooledConnection
for this instance.boolean
Initialize (a connection with) the database.protected void
initializeDatabase
(Configuration config) Initialize table state andconfig
from existing database.protected void
Lock appropriate tables during complex write/update operationSend a query to the database.protected int
queryFunctions
(QueryNearest query, BSimSqlClause filter, ResponseNearest response, DescriptionManager descMgr, Iterator<FunctionDescription> iter) protected abstract void
Perform nearest vector query based upon specified query which contains response record which should be filled-in with search results.protected abstract int
queryNearestVector
(List<VectorResult> resultset, LSHVector vec, double simthresh, double sigthresh, int max) protected abstract VectorResult
queryVectorId
(long id) protected int
retrieveFuncDescFromVectors
(VectorResult dresult, DescriptionManager res, int count, QueryNearest query, BSimSqlClause filter, SimilarityResult simres) GeneratesFunctionDescription
objects for each function with signature vectordresult
.protected void
protected abstract long
storeSignatureRecord
(SignatureRecord sigrec) Make sure the vector corresponding to the SignatureRecord is inserted into the vectableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface ghidra.features.bsim.query.FunctionDatabase
changePassword, getOverviewFunctionsPerStage, getQueriedFunctionsPerStage, isPasswordChangeAllowed
Methods inherited from interface ghidra.features.bsim.query.SQLFunctionDatabase
formatBitAndSQL
-
Field Details
-
SQL_TIME_FORMAT
- See Also:
-
JAVA_TIME_FORMAT
- See Also:
-
ds
-
supportedLayoutVersion
public final int supportedLayoutVersion -
vectorFactory
-
-
Constructor Details
-
AbstractSQLFunctionDatabase
protected AbstractSQLFunctionDatabase(BSimJDBCDataSource ds, VF vectorFactory, int supportedLayoutVersion)
-
-
Method Details
-
close
public void close()Description copied from interface:FunctionDatabase
Close down (the connection with) the database- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceFunctionDatabase
-
closing
Perform any required cleanup prior to connection close. Partial cleanup has already been performed.- Parameters:
c
- database connection
-
initConnection
Establish PostgreSQL DB pooledConnection
for this instance.- Returns:
- database connection
- Throws:
SQLException
- if error occurs obtaining connection
-
beginTransaction
Start a transaction which will not be auto-committed. TheendTransaction(boolean)
method must be invoked when the transaction has completed or failed and should be performed within a finally block.- Parameters:
lockTablesForWrite
- if true a table lock will be placed which will clear whenendTransaction(boolean)
is invoked.- Returns:
- the connection associated with the transaction
- Throws:
SQLException
- if an error occurs
-
lockTablesForWrite
Lock appropriate tables during complex write/update operation- Throws:
SQLException
- if an error occurs
-
endTransaction
End a transaction which was started withbeginTransaction(boolean)
and return DB connection to an auto-commit state.- Parameters:
commit
- true if transaction should be commited, false to force a rollback.- Throws:
SQLException
- if an error occurs
-
initializeDatabase
Initialize table state andconfig
from existing database. NOTE: it is left to specific implementation to override this method to properly initialize defaultconfig.weightfactory
andconfig.idflookup
.- Parameters:
config
- the database configuration- Throws:
SQLException
- if there is a problem opening the database connection
-
generateRawDatabase
Generate new empty database which corresponds tods
. A new connection may be used to the default database so that a new database may be created.- Throws:
SQLException
- if an error occurs while creating the database
-
createDatabase
Generate tables and initialize state for a new database.- Parameters:
config
- the database configuration- Throws:
SQLException
- if there is aproblem opening a connectino to the database
-
dropDatabase
Drop this database- Throws:
SQLException
- if a database error occured
-
setConnectionOnTables
-
appendEscapedLiteral
- Parameters:
buf
- the string builder objectstr
- the string to parse- Throws:
SQLException
- if there is a zero byte in the string
-
convertDescriptionRows
protected void convertDescriptionRows(SimilarityResult simres, List<DescriptionTable.DescriptionRow> descvec, VectorResult vecres, DescriptionManager res, SignatureRecord srec) throws SQLException, LSHException Convert a low-level list of function rows into full FunctionDescription objects- Parameters:
simres
- (optional -- may be null) generate a SimilarityNote for every functiondescvec
- is the list of low-level function rowsvecres
- (optional -- may be null) vector result producing these functionsres
- is the DescriptionManager holding the newly generated FunctionDescriptionssrec
- (optional -- may be null) is a description object of the vector- Throws:
SQLException
- is there is an error querying tablesLSHException
- for internal consistency errors
-
deleteVectors
Low level count decrement of a vector record from vectable, if count reaches zero, the record is deleted- Parameters:
id
- vector row IDcountdiff
- the amount to subtract from count- Returns:
- 0 if decrement short of 0, return 1 if record was removed, return -1 if there was a problem
- Throws:
SQLException
- if there is a problem creating or executing the query
-
queryVectorId
- Parameters:
id
- the vector id- Returns:
- the vector result
- Throws:
SQLException
- if there is a problem creating or executing the query
-
getStatus
- Specified by:
getStatus
in interfaceFunctionDatabase
- Returns:
- the status of the current connection with this database
-
getConnectionType
- Specified by:
getConnectionType
in interfaceFunctionDatabase
- Returns:
- the type of connection
-
getUserName
- Specified by:
getUserName
in interfaceFunctionDatabase
- Returns:
- username (being used to establish connection)
-
getLSHVectorFactory
- Specified by:
getLSHVectorFactory
in interfaceFunctionDatabase
- Returns:
- factory the database is using to create LSHVector objects
-
getInfo
- Specified by:
getInfo
in interfaceFunctionDatabase
- Returns:
- an information object giving general characteristics and descriptions of this database
-
compareLayout
public int compareLayout()Description copied from interface:FunctionDatabase
Return -1 if info layout version is earlier than current client expectation Return 1 if info layout version is later than current client expectation Return 0 if info version and client version are the same- Specified by:
compareLayout
in interfaceFunctionDatabase
- Returns:
- comparison of actual database layout with layout expected by client
-
getServerInfo
Description copied from interface:FunctionDatabase
Return theserver info object
for this database- Specified by:
getServerInfo
in interfaceFunctionDatabase
- Returns:
- the server info object
-
getURLString
- Specified by:
getURLString
in interfaceFunctionDatabase
-
getLastError
Description copied from interface:FunctionDatabase
If the last query failed to produce a response, use this method to recover the error message- Specified by:
getLastError
in interfaceFunctionDatabase
- Returns:
- a String describing the error
-
initialize
public boolean initialize()Description copied from interface:FunctionDatabase
Initialize (a connection with) the database. If initialization is not successful, this routine will return false and an error description can be obtained using getLastError- Specified by:
initialize
in interfaceFunctionDatabase
- Returns:
- true if the database ready for querying
-
storeSignatureRecord
Make sure the vector corresponding to the SignatureRecord is inserted into the vectable- Parameters:
sigrec
- is the SignatureRecord- Returns:
- the computed id of the vector
- Throws:
SQLException
- if there is a problem creating or executing the query
-
retrieveFuncDescFromVectors
protected int retrieveFuncDescFromVectors(VectorResult dresult, DescriptionManager res, int count, QueryNearest query, BSimSqlClause filter, SimilarityResult simres) throws SQLException, LSHException GeneratesFunctionDescription
objects for each function with signature vectordresult
. If a non-nullfilter
is provided,FunctionDescription
s are only created for functions with pass the filter.- Parameters:
dresult
-VectorResult
representing a matching vectorres
-DescriptionManager
container for the resultscount
- number of function descriptions which have already been returned for this base vectorquery
- contains various thresholds for the queryfilter
- specifies additional conditions functions (and exes) must meet after meeting sim/signif thresholdsimres
- receives the list of results and their similarity to the base vector- Returns:
- number of
- Throws:
SQLException
- if there is an error issuing the queryLSHException
- if executable record creation fails
-
queryNearestVector
protected abstract int queryNearestVector(List<VectorResult> resultset, LSHVector vec, double simthresh, double sigthresh, int max) throws SQLException - Parameters:
resultset
- the list of result set objects to populatevec
- the vector containing the saveSQL query statementsimthresh
- the similarity thresholdsigthresh
- the confidence thresholdmax
- the max number of results to return- Returns:
- the number of results returned
- Throws:
SQLException
- if there is a problem creating or executing the query
-
queryNearestVector
Perform nearest vector query based upon specified query which contains response record which should be filled-in with search results.- Parameters:
query
- the nearest vector query to perform- Throws:
SQLException
- if there is an error issuing the query
-
doQuery
protected QueryResponseRecord doQuery(BSimQuery<?> query, Connection c) throws LSHException, SQLException, FunctionDatabase.DatabaseNonFatalException -
query
Description copied from interface:FunctionDatabase
Send a query to the database. The response is returned as a QueryResponseRecord. If this is null, an error has occurred and an error message can be obtained from getLastError- Specified by:
query
in interfaceFunctionDatabase
- Parameters:
query
- an object describing the query- Returns:
- the response object or null if there is an error
-
queryFunctions
protected int queryFunctions(QueryNearest query, BSimSqlClause filter, ResponseNearest response, DescriptionManager descMgr, Iterator<FunctionDescription> iter) throws SQLException, LSHException - Parameters:
query
- the query to executefilter
- the function filterresponse
- the response objectdescMgr
- the executable descriptoriter
- the function iterator- Returns:
- the number of unique results found
- Throws:
SQLException
- if there is an error issuing the queryLSHException
- if there is a problem creating executable records
-
getFunctionTags
Returns a list of all function tags in the database.- Returns:
- list of function tags
-