Package gov.usgs.earthquake.aws
Class JsonNotificationIndex
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.util.JDBCConnection
-
- gov.usgs.earthquake.aws.JsonNotificationIndex
-
- All Implemented Interfaces:
NotificationIndex,Configurable,java.lang.AutoCloseable
public class JsonNotificationIndex extends JDBCConnection implements NotificationIndex
Store Notifications in a database. Only SQLITE or local development should rely on createSchema. Products (data column) have exceeded 64kb, plan accordingly. Mysql Schema Example:
CREATE TABLE IF NOT EXISTS indexer_receiver_index (id INTEGER PRIMARY KEY AUTO_INCREMENT , created VARCHAR(255) , expires VARCHAR(255) , source VARCHAR(255) , type VARCHAR(255) , code VARCHAR(255) , updatetime BIGINT , url TEXT , data LONGTEXT , KEY source_index (source) , KEY type_index (type) , KEY code_index (code) , KEY expires_index (expires) ) ENGINE=innodb CHARSET=utf8;
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_DRIVERVariable for the default driverstatic java.lang.StringDEFAULT_TABLEVariable for the default tablestatic java.lang.StringDEFAULT_URLVariable for the default URL
-
Constructor Summary
Constructors Constructor Description JsonNotificationIndex()Construct a JsonNotification using defaults.JsonNotificationIndex(java.lang.String driver, java.lang.String url)Construct a JsonNotificationIndex with the default table.JsonNotificationIndex(java.lang.String driver, java.lang.String url, java.lang.String table)Construct a JsonNotificationIndex with custom driver, url, and table.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddNotification(Notification notification)Add a notification to the index.voidconfigure(Config config)Implement ConfigurablevoidcreateSchema()Attempt to create schema.java.util.List<Notification>findExpiredNotifications()Find notifications with expires time before or equal to current time.java.util.List<Notification>findNotifications(ProductId id)Search index for notifications for a specific product.java.util.List<Notification>findNotifications(java.lang.String source, java.lang.String type, java.lang.String code)Search index for notifications.java.util.List<Notification>findNotifications(java.util.List<java.lang.String> sources, java.util.List<java.lang.String> types, java.util.List<java.lang.String> codes)Search index for notifications.java.util.List<Notification>getMissingNotifications(java.lang.String otherTable)This method is used to find notifications present in this index but not present in another JsonNotificationIndex table in the same database.protected java.util.List<Notification>getNotifications(java.sql.PreparedStatement ps)Parse notifications from a statement ready to be executed.java.lang.StringgetTable()protected NotificationparseNotification(java.lang.String created, java.lang.String expires, java.lang.String source, java.lang.String type, java.lang.String code, java.lang.Long updateTime, java.lang.String url, java.lang.String data)Creates and returns aNotificationbased on the provided data.voidremoveNotification(Notification notification)Remove notification from index.voidremoveNotifications(java.util.List<Notification> notifications)Remove notifications from index.booleanschemaExists()Check whether schema exists.voidsetTable(java.lang.String table)voidstartup()After normal startup, check whether schema exists and attempt to create.-
Methods inherited from class gov.usgs.earthquake.util.JDBCConnection
beginTransaction, close, commitTransaction, connect, getConnection, getDriver, getUrl, rollbackTransaction, setDriver, setUrl, shutdown, 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
-
-
-
-
Field Detail
-
DEFAULT_DRIVER
public static final java.lang.String DEFAULT_DRIVER
Variable for the default driver- See Also:
- Constant Field Values
-
DEFAULT_TABLE
public static final java.lang.String DEFAULT_TABLE
Variable for the default table- See Also:
- Constant Field Values
-
DEFAULT_URL
public static final java.lang.String DEFAULT_URL
Variable for the default URL- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JsonNotificationIndex
public JsonNotificationIndex()
Construct a JsonNotification using defaults.
-
JsonNotificationIndex
public JsonNotificationIndex(java.lang.String driver, java.lang.String url)Construct a JsonNotificationIndex with the default table.- Parameters:
driver- Driver to useurl- URL to use
-
JsonNotificationIndex
public JsonNotificationIndex(java.lang.String driver, java.lang.String url, java.lang.String table)Construct a JsonNotificationIndex with custom driver, url, and table.- Parameters:
driver- Driver to useurl- URL to usetable- Table to use
-
-
Method Detail
-
getTable
public java.lang.String getTable()
- Returns:
- table
-
setTable
public void setTable(java.lang.String table)
- Parameters:
table- Table to set
-
configure
public void configure(Config config) throws java.lang.Exception
Description copied from class:JDBCConnectionImplement Configurable- Specified by:
configurein interfaceConfigurable- Overrides:
configurein classJDBCConnection- Parameters:
config- Config to set driver and URL in- Throws:
java.lang.Exception- Exception
-
startup
public void startup() throws java.lang.ExceptionAfter normal startup, check whether schema exists and attempt to create.- Specified by:
startupin interfaceConfigurable- Overrides:
startupin classJDBCConnection- Throws:
java.lang.Exception- if error occurs
-
schemaExists
public boolean schemaExists() throws java.lang.ExceptionCheck whether schema exists.- Returns:
- boolean
- Throws:
java.lang.Exception- if error occurs
-
createSchema
public void createSchema() throws java.lang.ExceptionAttempt to create schema. Only supports sqlite or mysql. When not using sqlite, relying on this method is only recommended for local development.- Throws:
java.lang.Exception- if error occurs
-
addNotification
public void addNotification(Notification notification) throws java.lang.Exception
Add a notification to the index. TrackerURLs are ignored.- Specified by:
addNotificationin interfaceNotificationIndex- Parameters:
notification- To be added to index- Throws:
java.lang.Exception- if error occurs
-
removeNotification
public void removeNotification(Notification notification) throws java.lang.Exception
Remove notification from index. Tracker URLs are ignored.- Specified by:
removeNotificationin interfaceNotificationIndex- Parameters:
notification- to be removed from index- Throws:
java.lang.Exception- if error occurs
-
removeNotifications
public void removeNotifications(java.util.List<Notification> notifications) throws java.lang.Exception
Remove notifications from index. Tracker URLs are ignored.- Specified by:
removeNotificationsin interfaceNotificationIndex- Parameters:
notifications- notifications to be removed from index- Throws:
java.lang.Exception- if error occurs
-
findNotifications
public java.util.List<Notification> findNotifications(java.lang.String source, java.lang.String type, java.lang.String code) throws java.lang.Exception
Search index for notifications.- Specified by:
findNotificationsin interfaceNotificationIndex- Parameters:
source- source, or null for all sources.type- type, or null for all types.code- code, or null for all codes.- Returns:
- list with matching notifications, empty if not found.
- Throws:
java.lang.Exception- if error occurs
-
findNotifications
public java.util.List<Notification> findNotifications(java.util.List<java.lang.String> sources, java.util.List<java.lang.String> types, java.util.List<java.lang.String> codes) throws java.lang.Exception
Search index for notifications.- Specified by:
findNotificationsin interfaceNotificationIndex- Parameters:
sources- sources, or null for all sources.types- types, or null for all types.codes- codes, or null for all codes.- Returns:
- list with matching notifications, empty if not found.
- Throws:
java.lang.Exception- if error occurs
-
findExpiredNotifications
public java.util.List<Notification> findExpiredNotifications() throws java.lang.Exception
Find notifications with expires time before or equal to current time.- Specified by:
findExpiredNotificationsin interfaceNotificationIndex- Returns:
- list with matching notifications, empty if not found.
- Throws:
java.lang.Exception- if error occurs
-
findNotifications
public java.util.List<Notification> findNotifications(ProductId id) throws java.lang.Exception
Search index for notifications for a specific product.- Specified by:
findNotificationsin interfaceNotificationIndex- Parameters:
id- the product id to search.- Returns:
- list with matching notifications, empty if not found.
- Throws:
java.lang.Exception- if error occurs
-
getMissingNotifications
public java.util.List<Notification> getMissingNotifications(java.lang.String otherTable) throws java.lang.Exception
This method is used to find notifications present in this index but not present in another JsonNotificationIndex table in the same database. This is used to optimize the queuing process at startup and returns DefaultNotifications. The receiver process will look up the actual notification object during processing.- Parameters:
otherTable- name of table in same database.- Returns:
- list of notifications found in this indexes table, but not found in the other table.
- Throws:
java.lang.Exception- if error occurs
-
getNotifications
protected java.util.List<Notification> getNotifications(java.sql.PreparedStatement ps) throws java.lang.Exception
Parse notifications from a statement ready to be executed.- Parameters:
ps- PreparedStatement to be parsed- Returns:
- List of notifications
- Throws:
java.lang.Exception- if error occurs
-
parseNotification
protected Notification parseNotification(java.lang.String created, java.lang.String expires, java.lang.String source, java.lang.String type, java.lang.String code, java.lang.Long updateTime, java.lang.String url, java.lang.String data) throws java.lang.Exception
Creates and returns aNotificationbased on the provided data.- Return a JSONNotification if
createdanddataare set - Return a URLNotification if
urlis set - Otherwise, return a DefaultNotification
- Parameters:
created- When createdexpires- When notification expiressource- sourcestype- typescode- codesupdateTime- updateTimeurl- URLdata- data- Returns:
- Notification, JSONNotification, URLNotification, or DefaultNotification
- Throws:
java.lang.Exception- if error occurs
- Return a JSONNotification if
-
-