Class AbstractSQLFunctionDatabase<VF extends LSHVectorFactory>
java.lang.Object
ghidra.features.bsim.query.client.AbstractSQLFunctionDatabase<VF>
- Type Parameters:
VF-vector factoryimplementation 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 BSimJDBCDataSourcestatic final Stringstatic final Stringfinal intprotected final VF -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractSQLFunctionDatabase(BSimJDBCDataSource ds, VF vectorFactory, int supportedLayoutVersion) -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendEscapedLiteral(StringBuilder buf, String str) protected ConnectionbeginTransaction(boolean lockTablesForWrite) Start a transaction which will not be auto-committed.voidclose()Close down (the connection with) the databaseprotected voidPerform any required cleanup prior to connection close.intReturn -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 voidconvertDescriptionRows(SimilarityResult simres, List<DescriptionTable.DescriptionRow> descvec, VectorResult vecres, DescriptionManager res, SignatureRecord srec) Convert a low-level list of function rows into full FunctionDescription objectsprotected voidcreateDatabase(Configuration config) Generate tables and initialize state for a new database.protected abstract intdeleteVectors(long id, int countdiff) Low level count decrement of a vector record from vectable, if count reaches zero, the record is deletedprotected QueryResponseRecorddoQuery(BSimQuery<?> query, Connection c) protected abstract voidDrop this databaseprotected voidendTransaction(boolean commit) End a transaction which was started withbeginTransaction(boolean)and return DB connection to an auto-commit state.protected abstract voidGenerate 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 objectfor this databaseprotected ConnectionEstablish PostgreSQL DB pooledConnectionfor this instance.booleanInitialize (a connection with) the database.protected voidinitializeDatabase(Configuration config) Initialize table state andconfigfrom existing database.protected voidLock appropriate tables during complex write/update operationSend a query to the database.protected intqueryFunctions(QueryNearest query, BSimSqlClause filter, ResponseNearest response, DescriptionManager descMgr, Iterator<FunctionDescription> iter) protected abstract voidPerform nearest vector query based upon specified query which contains response record which should be filled-in with search results.protected abstract intqueryNearestVector(List<VectorResult> resultset, LSHVector vec, double simthresh, double sigthresh, int max) protected abstract VectorResultqueryVectorId(long id) protected intretrieveFuncDescFromVectors(VectorResult dresult, DescriptionManager res, int count, QueryNearest query, BSimSqlClause filter, SimilarityResult simres) GeneratesFunctionDescriptionobjects for each function with signature vectordresult.protected voidprotected abstract longstoreSignatureRecord(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, waitMethods inherited from interface ghidra.features.bsim.query.FunctionDatabase
changePassword, getOverviewFunctionsPerStage, getQueriedFunctionsPerStage, isPasswordChangeAllowedMethods 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:FunctionDatabaseClose down (the connection with) the database- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceFunctionDatabase
-
closing
Perform any required cleanup prior to connection close. Partial cleanup has already been performed.- Parameters:
c- database connection
-
initConnection
Establish PostgreSQL DB pooledConnectionfor 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 andconfigfrom existing database. NOTE: it is left to specific implementation to override this method to properly initialize defaultconfig.weightfactoryandconfig.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:
getStatusin interfaceFunctionDatabase- Returns:
- the status of the current connection with this database
-
getConnectionType
- Specified by:
getConnectionTypein interfaceFunctionDatabase- Returns:
- the type of connection
-
getUserName
- Specified by:
getUserNamein interfaceFunctionDatabase- Returns:
- username (being used to establish connection)
-
getLSHVectorFactory
- Specified by:
getLSHVectorFactoryin interfaceFunctionDatabase- Returns:
- factory the database is using to create LSHVector objects
-
getInfo
- Specified by:
getInfoin interfaceFunctionDatabase- Returns:
- an information object giving general characteristics and descriptions of this database
-
compareLayout
public int compareLayout()Description copied from interface:FunctionDatabaseReturn -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:
compareLayoutin interfaceFunctionDatabase- Returns:
- comparison of actual database layout with layout expected by client
-
getServerInfo
Description copied from interface:FunctionDatabaseReturn theserver info objectfor this database- Specified by:
getServerInfoin interfaceFunctionDatabase- Returns:
- the server info object
-
getURLString
- Specified by:
getURLStringin interfaceFunctionDatabase
-
getLastError
Description copied from interface:FunctionDatabaseIf the last query failed to produce a response, use this method to recover the error message- Specified by:
getLastErrorin interfaceFunctionDatabase- Returns:
- a String describing the error
-
initialize
public boolean initialize()Description copied from interface:FunctionDatabaseInitialize (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:
initializein 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 GeneratesFunctionDescriptionobjects for each function with signature vectordresult. If a non-nullfilteris provided,FunctionDescriptions are only created for functions with pass the filter.- Parameters:
dresult-VectorResultrepresenting a matching vectorres-DescriptionManagercontainer 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:FunctionDatabaseSend 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:
queryin 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
-