Package gov.usgs.earthquake.indexer
Class JDBCProductIndex
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.util.JDBCConnection
-
- gov.usgs.earthquake.indexer.JDBCProductIndex
-
- All Implemented Interfaces:
ProductIndex
,Configurable
,java.lang.AutoCloseable
- Direct Known Subclasses:
ExtentIndex
public class JDBCProductIndex extends JDBCConnection implements ProductIndex
JDBC Implementation ofProductIndex
.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
JDBC_DEFAULT_FILE
Default index file.static java.lang.String
SUMMARY_PRODUCT_INDEX_ID
Public var for summary product index IDs
-
Constructor Summary
Constructors Constructor Description JDBCProductIndex()
Constructor.JDBCProductIndex(java.lang.String sqliteFileName)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Event
addAssociation(Event event, ProductSummary summary)
Create an association between the given event and product summary.Event
addEvent(Event event)
Add an event to the databaseprotected void
addProductLinks(long productId, java.util.Map<java.lang.String,java.util.List<java.net.URI>> links)
Save the links in the database and associate them to the given productIdprotected void
addProductProperties(long productId, java.util.Map<java.lang.String,java.lang.String> properties)
Save the properties in the database and associate them to the given productIdProductSummary
addProductSummary(ProductSummary summary)
Add a product summary to the databaseprotected java.util.List<java.lang.String>
buildProductClauses(ProductIndexQuery query)
Build a list of all the pieces of the WHERE clause relevant to the productSummary table.protected java.lang.String
buildProductQuery(ProductIndexQuery query)
Create the full SELECT query for the products table using the default clauseList.protected java.lang.String
buildProductQuery(ProductIndexQuery query, java.util.List<java.lang.String> clauseList)
Create the full SELECT query for the products table using a custom clauseList.void
configure(Config config)
Grab values from the Config object and put them into private variables.java.sql.Connection
connect()
Return a connection to the database.void
eventsUpdated(java.util.List<Event> events)
Called when the indexer is done updating events after a product is processed.java.util.List<Event>
getEvents(ProductIndexQuery query)
Return all events from the database that meet the parameters specified in the ProductIndexQuery object.java.util.List<ProductSummary>
getProducts(ProductIndexQuery query)
Return all products that meet the parameters specified in the ProductIndexQuery object.java.util.List<ProductSummary>
getProducts(ProductIndexQuery query, boolean loadDetails)
Load product summaries.java.util.List<ProductSummary>
getUnassociatedProducts(ProductIndexQuery query)
Return all products that aren't associated with an event.boolean
hasProduct(ProductId id)
Check whether product summary is in index.protected void
loadProductSummaries(java.util.List<ProductSummary> summaries)
Populate links and properties for provided product summaries.protected double
normalizeLongitude(double lon)
Convert the given longitude to be between -180 and 180.protected java.math.BigDecimal
normalizeLongitude(java.math.BigDecimal lon)
Wrapper to normalize BigDecimal longitudesprotected ProductSummary
parseProductSummary(java.sql.ResultSet results)
Parse ProductSummary without loading links or properties.Event
removeAssociation(Event event, ProductSummary summary)
Delete the association, if it exists, between the given event and product summary.java.util.List<ProductId>
removeEvent(Event event)
Delete an event from the database.java.util.List<ProductId>
removeProductSummaries(java.util.List<ProductSummary> summaries)
ProductId
removeProductSummary(ProductSummary summary)
Delete a product summary from the database If the summary doesn't have an indexId value set, throw an exception-
Methods inherited from class gov.usgs.earthquake.util.JDBCConnection
beginTransaction, close, commitTransaction, getConnection, getDriver, getUrl, rollbackTransaction, setDriver, setUrl, shutdown, startup, verifyConnection
-
Methods inherited from class gov.usgs.util.DefaultConfigurable
getName, setName
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gov.usgs.util.Configurable
getName, setName, shutdown, startup
-
Methods inherited from interface gov.usgs.earthquake.indexer.ProductIndex
beginTransaction, commitTransaction, rollbackTransaction
-
-
-
-
Field Detail
-
JDBC_DEFAULT_FILE
public static final java.lang.String JDBC_DEFAULT_FILE
Default index file. Created by copying JDBC_DEFAULT_INDEX out of Jar if doesn't already exist in file system- See Also:
- Constant Field Values
-
SUMMARY_PRODUCT_INDEX_ID
public static final java.lang.String SUMMARY_PRODUCT_INDEX_ID
Public var for summary product index IDs- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JDBCProductIndex
public JDBCProductIndex() throws java.lang.Exception
Constructor. Sets index_file to the default value JDBC_DEFAULT_FILE- Throws:
java.lang.Exception
- if error occurs
-
JDBCProductIndex
public JDBCProductIndex(java.lang.String sqliteFileName) throws java.lang.Exception
Constructor. Uses custom index_file- Parameters:
sqliteFileName
- String for sqlite file name- Throws:
java.lang.Exception
- if error occurs
-
-
Method Detail
-
configure
public void configure(Config config) throws java.lang.Exception
Grab values from the Config object and put them into private variables.- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classJDBCConnection
- Parameters:
config
- Configuration for the product index- Throws:
java.lang.Exception
- Exception
-
connect
public java.sql.Connection connect() throws java.lang.Exception
Return a connection to the database.- Overrides:
connect
in classJDBCConnection
- Returns:
- Connection object
- Throws:
java.lang.Exception
- if error occurs
-
getEvents
public java.util.List<Event> getEvents(ProductIndexQuery query) throws java.lang.Exception
Return all events from the database that meet the parameters specified in the ProductIndexQuery object.- Specified by:
getEvents
in interfaceProductIndex
- Parameters:
query
- A description of which events to retrieve.- Returns:
- List of Event objects
- Throws:
java.lang.Exception
- if error occurs
-
addEvent
public Event addEvent(Event event) throws java.lang.Exception
Add an event to the database- Specified by:
addEvent
in interfaceProductIndex
- Parameters:
event
- Event to store- Returns:
- Event object with eventId set to the database id
- Throws:
java.lang.Exception
- if error occurs
-
removeEvent
public java.util.List<ProductId> removeEvent(Event event) throws java.lang.Exception
Delete an event from the database.- Specified by:
removeEvent
in interfaceProductIndex
- Parameters:
event
- Event to remove- Returns:
- List containing all the ProductIds that were deleted by the method call
- Throws:
java.lang.Exception
- if the event is associated to products.
-
getUnassociatedProducts
public java.util.List<ProductSummary> getUnassociatedProducts(ProductIndexQuery query) throws java.lang.Exception
Return all products that aren't associated with an event.- Specified by:
getUnassociatedProducts
in interfaceProductIndex
- Parameters:
query
- ProductIndexQuery used to further limit the results- Returns:
- List of unassociated Products
- Throws:
java.lang.IllegalArgumentException
- when query event search type is SEARCH_EVENT_PREFERRED.java.lang.Exception
- if error occurs
-
getProducts
public java.util.List<ProductSummary> getProducts(ProductIndexQuery query) throws java.lang.Exception
Return all products that meet the parameters specified in the ProductIndexQuery object.- Specified by:
getProducts
in interfaceProductIndex
- Parameters:
query
- A description of which products to retrieve.- Returns:
- List of ProductSummary objects
- Throws:
java.lang.IllegalArgumentException
- when query event search type is SEARCH_EVENT_PREFERRED.java.lang.Exception
- if error occurs
-
getProducts
public java.util.List<ProductSummary> getProducts(ProductIndexQuery query, boolean loadDetails) throws java.lang.Exception
Load product summaries.- Parameters:
query
- product queryloadDetails
- whether to callloadProductSummaries(List)
, which loads links and properties with additional queries.- Returns:
- A list of loaded product summaries
- Throws:
java.lang.Exception
- if error occurs
-
hasProduct
public boolean hasProduct(ProductId id) throws java.lang.Exception
Check whether product summary is in index.- Specified by:
hasProduct
in interfaceProductIndex
- Parameters:
id
- product to search.- Returns:
- boolean if index has product
- Throws:
java.lang.Exception
- if error occurs
-
addProductSummary
public ProductSummary addProductSummary(ProductSummary summary) throws java.lang.Exception
Add a product summary to the database- Specified by:
addProductSummary
in interfaceProductIndex
- Parameters:
summary
- ProductSummary object to store. Must not be null.- Returns:
- Copy of the product summary object with the indexId set to the newly inserted id.
- Throws:
java.lang.Exception
- if error occurs
-
removeProductSummary
public ProductId removeProductSummary(ProductSummary summary) throws java.lang.Exception
Delete a product summary from the database If the summary doesn't have an indexId value set, throw an exception- Specified by:
removeProductSummary
in interfaceProductIndex
- Parameters:
summary
- ProductSummary object to delete- Returns:
- id of removed summary.
- Throws:
java.lang.Exception
- if error occurs
-
addAssociation
public Event addAssociation(Event event, ProductSummary summary) throws java.lang.Exception
Create an association between the given event and product summary. This assumes that both the event and the product are already stored in their respective tables.- Specified by:
addAssociation
in interfaceProductIndex
- Parameters:
event
- Event to add association tosummary
- ProductSummary to add association to- Returns:
- Copy of event with summary added to the event's products list
- Throws:
java.lang.Exception
- if error occurs
-
removeAssociation
public Event removeAssociation(Event event, ProductSummary summary) throws java.lang.Exception
Delete the association, if it exists, between the given event and product summary. NOTE: this removes the association between the event and ALL versions of the product summary.- Specified by:
removeAssociation
in interfaceProductIndex
- Parameters:
event
- An event to remove an association withsummary
- A ProductSummary to remove an association with- Returns:
- Copy of event with summary removed from the products list
- Throws:
java.lang.Exception
- if error occurs
-
buildProductClauses
protected java.util.List<java.lang.String> buildProductClauses(ProductIndexQuery query)
Build a list of all the pieces of the WHERE clause relevant to the productSummary table. If the query doesn't set any properties, this method will return an empty list. It is up to the calling methods to check if the clause list is empty when they build their WHERE clause.- Parameters:
query
- ProductIndexQuery- Returns:
- list containing clauses in the form: column="value"
-
buildProductQuery
protected java.lang.String buildProductQuery(ProductIndexQuery query)
Create the full SELECT query for the products table using the default clauseList.- Parameters:
query
- Query to build.- Returns:
- String containing the full SELECT query
- See Also:
buildProductClauses(ProductIndexQuery)
-
buildProductQuery
protected java.lang.String buildProductQuery(ProductIndexQuery query, java.util.List<java.lang.String> clauseList)
Create the full SELECT query for the products table using a custom clauseList.- Parameters:
query
- Query to build.clauseList
- List of clauses for WHERE- Returns:
- String containing the full SELECT query
-
loadProductSummaries
protected void loadProductSummaries(java.util.List<ProductSummary> summaries) throws java.lang.Exception
Populate links and properties for provided product summaries.- Parameters:
summaries
- List of ProductSummaries- Throws:
java.lang.Exception
- if error occurs
-
parseProductSummary
protected ProductSummary parseProductSummary(java.sql.ResultSet results) throws java.lang.Exception
Parse ProductSummary without loading links or properties.- Parameters:
results
- ResultSet to parse- Returns:
- ProductSummary object without links or properties.
- Throws:
java.lang.Exception
- if error occurs
-
removeProductSummaries
public java.util.List<ProductId> removeProductSummaries(java.util.List<ProductSummary> summaries) throws java.lang.Exception
- Parameters:
summaries
- List of product summaries to remove- Returns:
- List of ProductIds that were removed
- Throws:
java.lang.Exception
- if error occurs
-
addProductProperties
protected void addProductProperties(long productId, java.util.Map<java.lang.String,java.lang.String> properties) throws java.sql.SQLException
Save the properties in the database and associate them to the given productId- Parameters:
productId
- long product ID to associate toproperties
- Map of properties to save- Throws:
java.sql.SQLException
- if SQL error occurs
-
addProductLinks
protected void addProductLinks(long productId, java.util.Map<java.lang.String,java.util.List<java.net.URI>> links) throws java.sql.SQLException
Save the links in the database and associate them to the given productId- Parameters:
productId
- Index id of the product to selectlinks
- Map of relations to URIs- Throws:
java.sql.SQLException
- if sql error occurs
-
normalizeLongitude
protected double normalizeLongitude(double lon)
Convert the given longitude to be between -180 and 180. If the given value is already in the range, this method just returns the value.- Parameters:
lon
- Double longitude- Returns:
- double normalized between -180 and 180
-
normalizeLongitude
protected java.math.BigDecimal normalizeLongitude(java.math.BigDecimal lon)
Wrapper to normalize BigDecimal longitudes- Parameters:
lon
- BigDecimal Longitude- Returns:
- Normalized BigDecimal latitude
-
eventsUpdated
public void eventsUpdated(java.util.List<Event> events) throws java.lang.Exception
Called when the indexer is done updating events after a product is processed. Stores the preferred attributes for each event in the list- Specified by:
eventsUpdated
in interfaceProductIndex
- Parameters:
events
- the events that have been updated.- Throws:
java.lang.Exception
- if error occurs
-
-