Package nxt.db

Class FullTextTrigger

java.lang.Object
nxt.db.FullTextTrigger
All Implemented Interfaces:
TransactionalDb.TransactionCallback, org.h2.api.Trigger

public class FullTextTrigger extends Object implements org.h2.api.Trigger, TransactionalDb.TransactionCallback
FullTextTrigger provides Lucene search support. Each searchable database has a database trigger defined. The Lucene index is updated whenever a row is inserted, updated or deleted. The DB_ID column is used to identify each row and will be returned as the COLUMNS and KEYS values in the search results. Schema, table and column names are expected to be in uppercase to match the way H2 stores the information. Function aliases are created in the default schema (PUBLIC) and triggers are created in the schema of the table being indexed. The database aliases are defined as follows: CREATE ALIAS FTL_CREATE_INDEX FOR "nxt.db.FullTextTrigger.createIndex" CALL FTL_CREATE(schema, table, columnList) CREATE ALIAS FTL_DROP_INDEX FOR "nxt.db.FullTextTrigger.dropIndex" CALL FTL_DROP(schema, table) CREATE ALIAS FTL_SEARCH FOR "nxt.db.FullTextTrigger.search" CALL FTL_SEARCH(schema, table, query, limit, offset) FTL_CREATE_INDEX is called to create a fulltext index for a table. It is provided as a convenience for use in NxtDbVersion when creating a new index after the database has been created. FTL_DROP_INDEX is called to drop a fulltext index for a table. It is provided as a convenience for use in NxtDbVersion when dropping a table after the database has been created. FTL_SEARCH is used to return the search result set as part of a SELECT statement. The result set columns are the following: SCHEMA - the schema name (String) TABLE - the table name (String) COLUMNS - the primary key columns (String[]) - this is always DB_ID for NRS KEYS - the primary key values (Long[]) - DB_ID value for the row SCORE - the search hit score (Float) The table index trigger is defined as follows: CREATE TRIGGER trigger_name AFTER INSERT,UPDATE,DELETE ON table_name FOR EACH ROW CALL "nxt.db.FullTextTrigger"
  • Field Summary

    Fields inherited from interface org.h2.api.Trigger

    DELETE, INSERT, SELECT, UPDATE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the trigger (Trigger interface)
    void
    Commit the table changes for the current transaction (TransactionCallback interface)
    static void
    createIndex(Connection conn, String schema, String table, String columnList)
    Create the fulltext index for a table
    static void
    Drop all fulltext indexes
    static void
    dropIndex(Connection conn, String schema, String table)
    Drop the fulltext index for a table
    void
    fire(Connection conn, Object[] oldRow, Object[] newRow)
    Trigger has fired (Trigger interface)
    void
    init(Connection conn, String schema, String trigger, String table, boolean before, int type)
    Initialize the trigger (Trigger interface)
    static void
    Initialize the fulltext support for a new database This method should be called from NxtDbVersion when performing the database version update that enables NRS fulltext search support
    static void
     
    static void
    Reindex all of the indexed tables
    void
    Remove the trigger (Trigger interface)
    void
    Discard the table changes for the current transaction (TransactionCallback interface)
    static ResultSet
    search(Connection conn, String schema, String table, String queryText, int limit, int offset)
    Search the Lucene index The result set will have the following columns: SCHEMA - Schema name (String) TABLE - Table name (String) COLUMNS - Primary key column names (String[]) - this is always DB_ID KEYS - Primary key values (Long[]) - this is always the DB_ID value for the table row SCORE - Lucene score (Float)
    static void
    setActive(boolean active)
    This method is called by NRS initialization to indicate NRS is active.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FullTextTrigger

      public FullTextTrigger()
  • Method Details

    • setActive

      public static void setActive(boolean active)
      This method is called by NRS initialization to indicate NRS is active. This is required since database triggers will be initialized when the database is opened outside the NRS environment (for example, from the H2 console) The database triggers cannot be re-activated after they have been deactivated.
      Parameters:
      active - TRUE to enable database triggers
    • init

      public static void init(BasicDb db)
      Initialize the fulltext support for a new database This method should be called from NxtDbVersion when performing the database version update that enables NRS fulltext search support
      Parameters:
      db - BasicDb to init
    • migrateToV7

      public static void migrateToV7(BasicDb db)
    • reindex

      public static void reindex(Connection conn) throws SQLException
      Reindex all of the indexed tables
      Parameters:
      conn - SQL connection
      Throws:
      SQLException - Unable to reindex tables
    • createIndex

      public static void createIndex(Connection conn, String schema, String table, String columnList) throws SQLException
      Create the fulltext index for a table
      Parameters:
      conn - SQL connection
      schema - Schema name
      table - Table name
      columnList - Indexed column names separated by commas
      Throws:
      SQLException - Unable to create fulltext index
    • dropIndex

      public static void dropIndex(Connection conn, String schema, String table) throws SQLException
      Drop the fulltext index for a table
      Parameters:
      conn - SQL connection
      schema - Schema name
      table - Table name
      Throws:
      SQLException - Unable to drop fulltext index
    • dropAll

      public static void dropAll(Connection conn) throws SQLException
      Drop all fulltext indexes
      Parameters:
      conn - SQL connection
      Throws:
      SQLException - Unable to drop fulltext indexes
    • search

      public static ResultSet search(Connection conn, String schema, String table, String queryText, int limit, int offset) throws SQLException
      Search the Lucene index The result set will have the following columns: SCHEMA - Schema name (String) TABLE - Table name (String) COLUMNS - Primary key column names (String[]) - this is always DB_ID KEYS - Primary key values (Long[]) - this is always the DB_ID value for the table row SCORE - Lucene score (Float)
      Parameters:
      conn - SQL connection
      schema - Schema name
      table - Table name
      queryText - Query expression
      limit - Number of rows to return
      offset - Offset with result set
      Returns:
      Search results
      Throws:
      SQLException - Unable to search the index
    • init

      public void init(Connection conn, String schema, String trigger, String table, boolean before, int type) throws SQLException
      Initialize the trigger (Trigger interface)
      Specified by:
      init in interface org.h2.api.Trigger
      Parameters:
      conn - Database connection
      schema - Database schema name
      trigger - Database trigger name
      table - Database table name
      before - TRUE if trigger is called before database operation
      type - Trigger type
      Throws:
      SQLException - A SQL error occurred
    • close

      public void close()
      Close the trigger (Trigger interface)
      Specified by:
      close in interface org.h2.api.Trigger
    • remove

      public void remove()
      Remove the trigger (Trigger interface)
      Specified by:
      remove in interface org.h2.api.Trigger
    • fire

      public void fire(Connection conn, Object[] oldRow, Object[] newRow)
      Trigger has fired (Trigger interface)
      Specified by:
      fire in interface org.h2.api.Trigger
      Parameters:
      conn - Database connection
      oldRow - The old row or null
      newRow - The new row or null
    • commit

      public void commit()
      Commit the table changes for the current transaction (TransactionCallback interface)
      Specified by:
      commit in interface TransactionalDb.TransactionCallback
    • rollback

      public void rollback()
      Discard the table changes for the current transaction (TransactionCallback interface)
      Specified by:
      rollback in interface TransactionalDb.TransactionCallback