Class 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 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 use
        url - 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 use
        url - URL to use
        table - 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 interface Configurable
        Overrides:
        configure in class JDBCConnection
        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 interface Configurable
        Overrides:
        startup in class JDBCConnection
        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 interface ProductStorage
        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 interface ProductStorage
        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 interface ProductStorage
        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 interface ProductStorage
        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 interface ProductStorage
        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 interface ProductStorage
        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
        Notifies StorageListeners of the change to the ProductStorage.
        Specified by:
        notifyListeners in interface ProductStorage
        Parameters:
        event - StorageEvent
      • addStorageListener

        public void addStorageListener​(StorageListener listener)
        Description copied from interface: ProductStorage
        Adds a StorageListener to be notified when a change occurs in this ProductStorage.
        Specified by:
        addStorageListener in interface ProductStorage
        Parameters:
        listener - The listener to notify of changes.
      • removeStorageListener

        public void removeStorageListener​(StorageListener listener)
        Description copied from interface: ProductStorage
        Removes a StorageListener from being notified when a change occurs in this ProductStorage.
        Specified by:
        removeStorageListener in interface ProductStorage
        Parameters:
        listener - The listener to remove