## org.apache.mahout.cf.taste.impl.model.jdbc
Class AbstractJDBCDataModel

    java.lang.Object
      org.apache.mahout.cf.taste.impl.common.jdbc.AbstractJDBCComponent
          org.apache.mahout.cf.taste.impl.model.jdbc.AbstractJDBCDataModel

All Implemented Interfaces:
  Serializable, Refreshable, DataModel, JDBCDataModel

Direct Known Subclasses:
  AbstractBooleanPrefJDBCDataModel, GenericJDBCDataModel, MySQLJDBCDataModel, SQL92JDBCDataModel

    public abstract class AbstractJDBCDataModel

extends AbstractJDBCComponent
implements JDBCDataModel

An abstract superclass for `JDBCDataModel` implementations, providing most of the common functionality that any such implementation would need.

Performance will be a concern with any `JDBCDataModel`. There are going to be lots of simultaneous reads and some writes to one table. Make sure the table is set up optimally -- for example, you'll want to establish indexes.

You'll also want to use connection pooling of some kind. Most J2EE containers like Tomcat provide connection pooling, so make sure the `DataSource` it exposes is using pooling. Outside a J2EE container, you can use packages like Jakarta's DBCP to create a `DataSource` on top of your database whose `Connection`s are pooled.

See Also:
  Serialized Form

Field Summary
`static String` `DEFAULT_ITEM_ID_COLUMN`
`static String` `DEFAULT_PREFERENCE_COLUMN`
`static String` `DEFAULT_PREFERENCE_TABLE`
`static String` `DEFAULT_PREFERENCE_TIME_COLUMN`
`static String` `DEFAULT_USER_ID_COLUMN`

Fields inherited from class org.apache.mahout.cf.taste.impl.common.jdbc.AbstractJDBCComponent
`DEFAULT_DATASOURCE_NAME`

Constructor Summary

`protected`

`AbstractJDBCDataModel(DataSource dataSource, String getPreferenceSQL, String getPreferenceTimeSQL, String getUserSQL, String getAllUsersSQL, String getNumItemsSQL, String getNumUsersSQL, String setPreferenceSQL, String removePreferenceSQL, String getUsersSQL, String getItemsSQL, String getPrefsForItemSQL, String getNumPreferenceForItemSQL, String getNumPreferenceForItemsSQL, String getMaxPreferenceSQL, String getMinPreferenceSQL)`

`protected`

`AbstractJDBCDataModel(DataSource dataSource, String preferenceTable, String userIDColumn, String itemIDColumn, String preferenceColumn, String getPreferenceSQL, String getPreferenceTimeSQL, String getUserSQL, String getAllUsersSQL, String getNumItemsSQL, String getNumUsersSQL, String setPreferenceSQL, String removePreferenceSQL, String getUsersSQL, String getItemsSQL, String getPrefsForItemSQL, String getNumPreferenceForItemSQL, String getNumPreferenceForItemsSQL, String getMaxPreferenceSQL, String getMinPreferenceSQL)`

Method Summary
`protected Preference` `buildPreference(ResultSet rs)`
`protected List<Preference>` `doGetPreferencesForItem(long itemID)`
`FastByIDMap<FastIDSet>` `exportWithIDsOnly()`
`FastByIDMap<PreferenceArray>` `exportWithPrefs()`
`DataSource` `getDataSource()`
`String` `getItemIDColumn()`
`LongPrimitiveIterator` `getItemIDs()`
`FastIDSet` `getItemIDsFromUser(long userID)`
`protected long` `getLongColumn(ResultSet rs, int position)` Subclasses may wish to override this if ID values in the file are not numeric.
`float` `getMaxPreference()`
`float` `getMinPreference()`
`int` `getNumItems()`
`int` `getNumUsers()`
`int` `getNumUsersWithPreferenceFor(long itemID)`
`int` `getNumUsersWithPreferenceFor(long itemID1, long itemID2)`
`String` `getPreferenceColumn()`
`PreferenceArray` `getPreferencesForItem(long itemID)`
`PreferenceArray` `getPreferencesFromUser(long userID)`
`String` `getPreferenceTable()`
`Long` `getPreferenceTime(long userID, long itemID)`
`Float` `getPreferenceValue(long userID, long itemID)`
`String` `getUserIDColumn()`
`LongPrimitiveIterator` `getUserIDs()`
`boolean` `hasPreferenceValues()`
`void` `refresh(Collection<Refreshable> alreadyRefreshed)`
`void` `removePreference(long userID, long itemID)`
`protected void` `setLongParameter(PreparedStatement stmt, int position, long value)` Subclasses may wish to override this if ID values in the file are not numeric.
`void` `setPreference(long userID, long itemID, float value)`

Methods inherited from class org.apache.mahout.cf.taste.impl.common.jdbc.AbstractJDBCComponent
`checkNotNullAndLog, checkNotNullAndLog, getFetchSize, lookupDataSource`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DEFAULT_PREFERENCE_TABLE

    public static final String DEFAULT_PREFERENCE_TABLE

See Also:
  Constant Field Values

### DEFAULT_USER_ID_COLUMN

    public static final String DEFAULT_USER_ID_COLUMN

See Also:
  Constant Field Values

### DEFAULT_ITEM_ID_COLUMN

    public static final String DEFAULT_ITEM_ID_COLUMN

See Also:
  Constant Field Values

### DEFAULT_PREFERENCE_COLUMN

    public static final String DEFAULT_PREFERENCE_COLUMN

See Also:
  Constant Field Values

### DEFAULT_PREFERENCE_TIME_COLUMN

    public static final String DEFAULT_PREFERENCE_TIME_COLUMN

See Also:
  Constant Field Values

Constructor Detail

### AbstractJDBCDataModel

    protected AbstractJDBCDataModel(DataSource dataSource,
                                    String getPreferenceSQL,
                                    String getPreferenceTimeSQL,
                                    String getUserSQL,
                                    String getAllUsersSQL,
                                    String getNumItemsSQL,
                                    String getNumUsersSQL,
                                    String setPreferenceSQL,
                                    String removePreferenceSQL,
                                    String getUsersSQL,
                                    String getItemsSQL,
                                    String getPrefsForItemSQL,
                                    String getNumPreferenceForItemSQL,
                                    String getNumPreferenceForItemsSQL,
                                    String getMaxPreferenceSQL,
                                    String getMinPreferenceSQL)

### AbstractJDBCDataModel

    protected AbstractJDBCDataModel(DataSource dataSource,
                                    String preferenceTable,
                                    String userIDColumn,
                                    String itemIDColumn,
                                    String preferenceColumn,
                                    String getPreferenceSQL,
                                    String getPreferenceTimeSQL,
                                    String getUserSQL,
                                    String getAllUsersSQL,
                                    String getNumItemsSQL,
                                    String getNumUsersSQL,
                                    String setPreferenceSQL,
                                    String removePreferenceSQL,
                                    String getUsersSQL,
                                    String getItemsSQL,
                                    String getPrefsForItemSQL,
                                    String getNumPreferenceForItemSQL,
                                    String getNumPreferenceForItemsSQL,
                                    String getMaxPreferenceSQL,
                                    String getMinPreferenceSQL)

Method Detail

### getDataSource

    public DataSource getDataSource()

  Specified by:
    `getDataSource` in interface `JDBCDataModel`
  Returns:
    the `DataSource` that this instance is using

### getPreferenceTable

    public String getPreferenceTable()

### getUserIDColumn

    public String getUserIDColumn()

### getItemIDColumn

    public String getItemIDColumn()

### getPreferenceColumn

    public String getPreferenceColumn()

### getUserIDs

    public LongPrimitiveIterator getUserIDs()
                                     throws TasteException

  Specified by:
    `getUserIDs` in interface `DataModel`
  Throws:
    `TasteException`

### getPreferencesFromUser

    public PreferenceArray getPreferencesFromUser(long userID)
                                           throws TasteException

  Specified by:
    `getPreferencesFromUser` in interface `DataModel`
  Throws:
    `NoSuchUserException` - if there is no such user
    `TasteException`

### exportWithPrefs

    public FastByIDMap<PreferenceArray> exportWithPrefs()
                                                 throws TasteException

  Specified by:
    `exportWithPrefs` in interface `JDBCDataModel`
  Throws:
    `TasteException`

### exportWithIDsOnly

    public FastByIDMap<FastIDSet> exportWithIDsOnly()
                                             throws TasteException

  Specified by:
    `exportWithIDsOnly` in interface `JDBCDataModel`
  Throws:
    `TasteException`

### getItemIDsFromUser

    public FastIDSet getItemIDsFromUser(long userID)
                                 throws TasteException

  Specified by:
    `getItemIDsFromUser` in interface `DataModel`
  Throws:
    `NoSuchUserException` - if there is no such user
    `TasteException`

### getPreferenceValue

    public Float getPreferenceValue(long userID,
                                    long itemID)
                             throws TasteException

  Specified by:
    `getPreferenceValue` in interface `DataModel`
  Throws:
    `TasteException`

### getPreferenceTime

    public Long getPreferenceTime(long userID,
                                  long itemID)
                           throws TasteException

  Specified by:
    `getPreferenceTime` in interface `DataModel`
  Throws:
    `TasteException`

### getItemIDs

    public LongPrimitiveIterator getItemIDs()
                                     throws TasteException

  Specified by:
    `getItemIDs` in interface `DataModel`
  Throws:
    `TasteException`

### getPreferencesForItem

    public PreferenceArray getPreferencesForItem(long itemID)
                                          throws TasteException

  Specified by:
    `getPreferencesForItem` in interface `DataModel`
  Throws:
    `TasteException`

### doGetPreferencesForItem

    protected List<Preference> doGetPreferencesForItem(long itemID)
                                                throws TasteException

  Throws:
    `TasteException`

### getNumItems

    public int getNumItems()
                    throws TasteException

  Specified by:
    `getNumItems` in interface `DataModel`
  Throws:
    `TasteException`

### getNumUsers

    public int getNumUsers()
                    throws TasteException

  Specified by:
    `getNumUsers` in interface `DataModel`
  Throws:
    `TasteException`

### getNumUsersWithPreferenceFor

    public int getNumUsersWithPreferenceFor(long itemID)
                                     throws TasteException

  Specified by:
    `getNumUsersWithPreferenceFor` in interface `DataModel`
  Throws:
    `TasteException`

### getNumUsersWithPreferenceFor

    public int getNumUsersWithPreferenceFor(long itemID1,
                                            long itemID2)
                                     throws TasteException

  Specified by:
    `getNumUsersWithPreferenceFor` in interface `DataModel`
  Throws:
    `TasteException`

### setPreference

    public void setPreference(long userID,
                              long itemID,
                              float value)
                       throws TasteException

  Specified by:
    `setPreference` in interface `DataModel`
  Throws:
    `TasteException`

### removePreference

    public void removePreference(long userID,
                                 long itemID)
                          throws TasteException

  Specified by:
    `removePreference` in interface `DataModel`
  Throws:
    `TasteException`

### refresh

    public void refresh(Collection<Refreshable> alreadyRefreshed)

  Specified by:
    `refresh` in interface `Refreshable`

### hasPreferenceValues

    public boolean hasPreferenceValues()

  Specified by:
    `hasPreferenceValues` in interface `DataModel`

### getMaxPreference

    public float getMaxPreference()

  Specified by:
    `getMaxPreference` in interface `DataModel`

### getMinPreference

    public float getMinPreference()

  Specified by:
    `getMinPreference` in interface `DataModel`

### buildPreference

    protected Preference buildPreference(ResultSet rs)
                                  throws SQLException

  Throws:
    `SQLException`

### getLongColumn

    protected long getLongColumn(ResultSet rs,
                                 int position)
                          throws SQLException

  Subclasses may wish to override this if ID values in the file are not numeric. This provides a hook by which subclasses can inject an `IDMigrator` to perform translation.

  Throws:
    `SQLException`

### setLongParameter

    protected void setLongParameter(PreparedStatement stmt,
                                    int position,
                                    long value)
                             throws SQLException

  Subclasses may wish to override this if ID values in the file are not numeric. This provides a hook by which subclasses can inject an `IDMigrator` to perform translation.

  Throws:
    `SQLException`

Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.