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.String
DEFAULT_DRIVER
Variable for the default driverstatic java.lang.String
DEFAULT_TABLE
Variable for the default tablestatic java.lang.String
DEFAULT_URL
Variable 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 void
addNotification(Notification notification)
Add a notification to the index.void
configure(Config config)
Implement Configurablevoid
createSchema()
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.String
getTable()
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)
Creates and returns aNotification
based on the provided data.void
removeNotification(Notification notification)
Remove notification from index.void
removeNotifications(java.util.List<Notification> notifications)
Remove notifications from index.boolean
schemaExists()
Check whether schema exists.void
setTable(java.lang.String table)
void
startup()
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:JDBCConnection
Implement Configurable- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classJDBCConnection
- Parameters:
config
- Config to set driver and URL in- Throws:
java.lang.Exception
- Exception
-
startup
public void startup() throws java.lang.Exception
After normal startup, check whether schema exists and attempt to create.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classJDBCConnection
- Throws:
java.lang.Exception
- if error occurs
-
schemaExists
public boolean schemaExists() throws java.lang.Exception
Check whether schema exists.- Returns:
- boolean
- Throws:
java.lang.Exception
- if error occurs
-
createSchema
public void createSchema() throws java.lang.Exception
Attempt 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:
addNotification
in 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:
removeNotification
in 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:
removeNotifications
in 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:
findNotifications
in 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:
findNotifications
in 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:
findExpiredNotifications
in 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:
findNotifications
in 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 aNotification
based on the provided data.- Return a JSONNotification if
created
anddata
are set - Return a URLNotification if
url
is 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
-
-