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.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 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 void
addStorageListener(StorageListener listener)
Adds aStorageListener
to be notified when a change occurs in thisProductStorage
.void
configure(Config config)
Implement Configurablevoid
createSchema()
Attempt to create schema.Product
getProduct(ProductId id)
Get a product from storage.ProductSource
getProductSource(ProductId id)
Get a ProductSource for product in database.java.lang.String
getTable()
boolean
hasProduct(ProductId id)
Check whether product found in storage.void
notifyListeners(StorageEvent event)
NotifiesStorageListener
s of the change to theProductStorage
.void
removeProduct(ProductId id)
Remove product from storage.void
removeStorageListener(StorageListener listener)
Removes aStorageListener
from being notified when a change occurs in thisProductStorage
.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.ProductId
storeProduct(Product product)
Add product to storage.ProductId
storeProductSource(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: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
-
hasProduct
public boolean hasProduct(ProductId id) throws java.lang.Exception
Check whether product found in storage.- Specified by:
hasProduct
in 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:
getProduct
in 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:
storeProduct
in 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:
getProductSource
in 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:
storeProductSource
in 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:
removeProduct
in 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:ProductStorage
NotifiesStorageListener
s of the change to theProductStorage
.- Specified by:
notifyListeners
in interfaceProductStorage
- Parameters:
event
- StorageEvent
-
addStorageListener
public void addStorageListener(StorageListener listener)
Description copied from interface:ProductStorage
Adds aStorageListener
to be notified when a change occurs in thisProductStorage
.- Specified by:
addStorageListener
in interfaceProductStorage
- Parameters:
listener
- The listener to notify of changes.
-
removeStorageListener
public void removeStorageListener(StorageListener listener)
Description copied from interface:ProductStorage
Removes aStorageListener
from being notified when a change occurs in thisProductStorage
.- Specified by:
removeStorageListener
in interfaceProductStorage
- Parameters:
listener
- The listener to remove
-
-