Package gov.usgs.earthquake.aws
Class JsonProductStorage
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.util.JDBCConnection
-
- gov.usgs.earthquake.aws.JsonProductStorage
-
- All Implemented Interfaces:
ProductStorage,Configurable,java.lang.AutoCloseable
public class JsonProductStorage extends JDBCConnection implements ProductStorage
Store Products in a database. Note that this storage does not store Product Content, and is intended for Products that use URLContent and can be serialized using JsonProduct. 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_storage (id INTEGER PRIMARY KEY AUTO_INCREMENT , source VARCHAR(255) , type VARCHAR(255) , code VARCHAR(255) , updatetime BIGINT , data LONGTEXT , UNIQUE KEY product_index (source, type, code, updatetime) ) 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 JsonProductStorage()Create a JsonProductStorage using defaults.JsonProductStorage(java.lang.String driver, java.lang.String url)Create a JsonProductStorage with a default table.JsonProductStorage(java.lang.String driver, java.lang.String url, java.lang.String table)Create a JsonProductStorage with a custom driver, url, and table.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddStorageListener(StorageListener listener)Adds aStorageListenerto be notified when a change occurs in thisProductStorage.voidconfigure(Config config)Implement ConfigurablevoidcreateSchema()Attempt to create schema.ProductgetProduct(ProductId id)Get a product from storage.ProductSourcegetProductSource(ProductId id)Get a ProductSource for product in database.java.lang.StringgetTable()booleanhasProduct(ProductId id)Check whether product found in storage.voidnotifyListeners(StorageEvent event)NotifiesStorageListeners of the change to theProductStorage.voidremoveProduct(ProductId id)Remove product from storage.voidremoveStorageListener(StorageListener listener)Removes aStorageListenerfrom being notified when a change occurs in thisProductStorage.booleanschemaExists()Check whether schema exists.voidsetTable(java.lang.String table)voidstartup()After normal startup, check whether schema exists and attempt to create.ProductIdstoreProduct(Product product)Add product to storage.ProductIdstoreProductSource(ProductSource input)Store a ProductSource.-
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
-
JsonProductStorage
public JsonProductStorage()
Create a JsonProductStorage using defaults.
-
JsonProductStorage
public JsonProductStorage(java.lang.String driver, java.lang.String url)Create a JsonProductStorage with a default table.- Parameters:
driver- Driver to useurl- URL to use
-
JsonProductStorage
public JsonProductStorage(java.lang.String driver, java.lang.String url, java.lang.String table)Create a JsonProductStorage with a 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
-
hasProduct
public boolean hasProduct(ProductId id) throws java.lang.Exception
Check whether product found in storage.- Specified by:
hasProductin interfaceProductStorage- Parameters:
id- the product to check.- Returns:
- true if the product is in this storage, false otherwise.
- Throws:
java.lang.Exception- if an error occurs while checking.
-
getProduct
public Product getProduct(ProductId id) throws java.lang.Exception
Get a product from storage.- Specified by:
getProductin interfaceProductStorage- Parameters:
id- The product to get.- Returns:
- product if found, otherwise null.
- Throws:
java.lang.Exception- if errors occur while retrieving product.
-
storeProduct
public ProductId storeProduct(Product product) throws java.lang.Exception
Add product to storage.- Specified by:
storeProductin interfaceProductStorage- Parameters:
product- the product to store.- Returns:
- the stored product's id.
- Throws:
ProductAlreadyInStorageException- if product already in storage.java.lang.Exception- if errors occur while storing product.
-
getProductSource
public ProductSource getProductSource(ProductId id) throws java.lang.Exception
Get a ProductSource for product in database.- Specified by:
getProductSourcein interfaceProductStorage- Parameters:
id- which product to retrieve.- Returns:
- ObjectProductSource or null if product not found.
- Throws:
java.lang.Exception- if any errors occur while getting the ProductInput.
-
storeProductSource
public ProductId storeProductSource(ProductSource input) throws java.lang.Exception
Store a ProductSource. Uses ObjectProductHandler to read Product, then calls storeProduct.- Specified by:
storeProductSourcein interfaceProductStorage- Parameters:
input- the product to store.- Returns:
- the stored product's id.
- Throws:
ProductAlreadyInStorageException- if product already in storage.java.lang.Exception- if errors occur while storing product.
-
removeProduct
public void removeProduct(ProductId id) throws java.lang.Exception
Remove product from storage.- Specified by:
removeProductin interfaceProductStorage- Parameters:
id- which product to remove.- Throws:
java.lang.Exception- if errors occur while removing product.
-
notifyListeners
public void notifyListeners(StorageEvent event)
Description copied from interface:ProductStorageNotifiesStorageListeners of the change to theProductStorage.- Specified by:
notifyListenersin interfaceProductStorage- Parameters:
event- StorageEvent
-
addStorageListener
public void addStorageListener(StorageListener listener)
Description copied from interface:ProductStorageAdds aStorageListenerto be notified when a change occurs in thisProductStorage.- Specified by:
addStorageListenerin interfaceProductStorage- Parameters:
listener- The listener to notify of changes.
-
removeStorageListener
public void removeStorageListener(StorageListener listener)
Description copied from interface:ProductStorageRemoves aStorageListenerfrom being notified when a change occurs in thisProductStorage.- Specified by:
removeStorageListenerin interfaceProductStorage- Parameters:
listener- The listener to remove
-
-