Class 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 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 use
        url - 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 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
      • addNotification

        public void addNotification​(Notification notification)
                             throws java.lang.Exception
        Add a notification to the index. TrackerURLs are ignored.
        Specified by:
        addNotification in interface NotificationIndex
        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 interface NotificationIndex
        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 interface NotificationIndex
        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 interface NotificationIndex
        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 interface NotificationIndex
        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 interface NotificationIndex
        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 interface NotificationIndex
        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 a Notification based on the provided data.
        • Return a JSONNotification if created and data are set
        • Return a URLNotification if url is set
        • Otherwise, return a DefaultNotification
        Parameters:
        created - When created
        expires - When notification expires
        source - sources
        type - types
        code - codes
        updateTime - updateTime
        url - URL
        data - data
        Returns:
        Notification, JSONNotification, URLNotification, or DefaultNotification
        Throws:
        java.lang.Exception - if error occurs