Search in sources :

Example 1 with Key2StringMapper

use of org.infinispan.persistence.keymappers.Key2StringMapper in project infinispan by infinispan.

the class JdbcStringBasedStore method createTableOperations.

@Override
protected TableOperations<K, V> createTableOperations(InitializationContext ctx, JdbcStringBasedStoreConfiguration configuration) {
    this.configuration = ctx.getConfiguration();
    this.marshalledEntryFactory = ctx.getMarshallableEntryFactory();
    this.marshaller = ctx.getPersistenceMarshaller();
    this.timeService = ctx.getTimeService();
    this.keyPartitioner = configuration.segmented() ? ctx.getKeyPartitioner() : null;
    int numSegments = ctx.getCache().getCacheConfiguration().clustering().hash().numSegments();
    if (configuration.shared()) {
        this.sizeSegments = IntSets.immutableRangeSet(numSegments);
    } else {
        this.sizeSegments = IntSets.concurrentSet(numSegments);
        this.sizeSegments.addAll(IntSets.immutableRangeSet(numSegments));
    }
    String cacheName = ctx.getCache().getName();
    TableManager<K, V> tableManager = TableManagerFactory.getManager(ctx, connectionFactory, configuration, ctx.getCache().getName());
    tableManager.start();
    if (!configuration.table().createOnStart()) {
        Connection connection = null;
        try {
            connection = connectionFactory.getConnection();
            // If meta exists, then ensure that the stored configuration is compatible with the current settings
            if (tableManager.metaTableExists(connection)) {
                TableManager.Metadata meta = tableManager.getMetadata(connection);
                int storedSegments = meta.getSegments();
                if (!configuration.segmented()) {
                    // ISPN-13135 number of segments was previously written incorrectly, so don't validate number for older versions
                    String versionStr = Version.decodeVersion(meta.getVersion());
                    List<Integer> versionParts = Arrays.stream(versionStr.split("\\.")).map(Integer::parseInt).collect(Collectors.toList());
                    // Ignore check if version < 12.1.5. Meta table only created since 12.0.0
                    if ((versionParts.get(0) > 12 || versionParts.get(2) > 4) && storedSegments != -1)
                        throw log.existingStoreNoSegmentation();
                }
                int configuredSegments = numSegments;
                if (configuration.segmented() && storedSegments != configuredSegments)
                    throw log.existingStoreSegmentMismatch(storedSegments, configuredSegments);
                tableManager.updateMetaTable(connection);
            } else {
                // The meta table does not exist, therefore we must be reading from a 11.x store. Migrate the old data
                org.infinispan.util.logging.Log.PERSISTENCE.startMigratingPersistenceData(cacheName);
                try {
                    migrateFromV11(ctx, tableManager);
                } catch (SQLException e) {
                    throw org.infinispan.util.logging.Log.PERSISTENCE.persistedDataMigrationFailed(cacheName, e);
                }
                tableManager.createMetaTable(connection);
                org.infinispan.util.logging.Log.PERSISTENCE.persistedDataSuccessfulMigrated(cacheName);
            }
        } finally {
            connectionFactory.releaseConnection(connection);
        }
    }
    try {
        Object mapper = Util.loadClassStrict(configuration.key2StringMapper(), ctx.getGlobalConfiguration().classLoader()).newInstance();
        if (mapper instanceof Key2StringMapper)
            key2StringMapper = (Key2StringMapper) mapper;
    } catch (Exception e) {
        log.errorf("Trying to instantiate %s, however it failed due to %s", configuration.key2StringMapper(), e.getClass().getName());
        throw new IllegalStateException("This should not happen.", e);
    }
    if (log.isTraceEnabled()) {
        log.tracef("Using key2StringMapper: %s", key2StringMapper.getClass().getName());
    }
    if (configuration.preload()) {
        enforceTwoWayMapper("preload");
    }
    if (ctx.getCache().getCacheConfiguration() != null && ctx.getCache().getCacheConfiguration().clustering().cacheMode().isDistributed()) {
        enforceTwoWayMapper("distribution/rehashing");
    }
    return tableManager;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) TwoWayKey2StringMapper(org.infinispan.persistence.keymappers.TwoWayKey2StringMapper) Key2StringMapper(org.infinispan.persistence.keymappers.Key2StringMapper) PersistenceException(org.infinispan.persistence.spi.PersistenceException) SQLException(java.sql.SQLException) TableManager(org.infinispan.persistence.jdbc.impl.table.TableManager)

Example 2 with Key2StringMapper

use of org.infinispan.persistence.keymappers.Key2StringMapper in project infinispan by infinispan.

the class AbstractTableManager method start.

@Override
public void start() throws PersistenceException {
    if (config.createOnStart()) {
        Connection conn = null;
        try {
            conn = connectionFactory.getConnection();
            if (!tableExists(conn, metaTableName)) {
                createMetaTable(conn);
            }
            if (!tableExists(conn, dataTableName)) {
                createDataTable(conn);
            }
            createIndex(conn, timestampIndexExt, config.timestampColumnName());
            if (!dbMetadata.isSegmentedDisabled()) {
                createIndex(conn, segmentIndexExt, config.segmentColumnName());
            }
        } finally {
            connectionFactory.releaseConnection(conn);
        }
    }
    JdbcStringBasedStoreConfiguration configuration = ctx.getConfiguration();
    try {
        Object mapper = Util.loadClassStrict(configuration.key2StringMapper(), ctx.getGlobalConfiguration().classLoader()).newInstance();
        if (mapper instanceof Key2StringMapper)
            key2StringMapper = (Key2StringMapper) mapper;
    } catch (Exception e) {
        log.errorf("Trying to instantiate %s, however it failed due to %s", configuration.key2StringMapper(), e.getClass().getName());
        throw new IllegalStateException("This should not happen.", e);
    }
}
Also used : Connection(java.sql.Connection) JdbcStringBasedStoreConfiguration(org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfiguration) Key2StringMapper(org.infinispan.persistence.keymappers.Key2StringMapper) TwoWayKey2StringMapper(org.infinispan.persistence.keymappers.TwoWayKey2StringMapper) UnsupportedKeyTypeException(org.infinispan.persistence.keymappers.UnsupportedKeyTypeException) SQLException(java.sql.SQLException) PersistenceException(org.infinispan.persistence.spi.PersistenceException)

Aggregations

Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Key2StringMapper (org.infinispan.persistence.keymappers.Key2StringMapper)2 TwoWayKey2StringMapper (org.infinispan.persistence.keymappers.TwoWayKey2StringMapper)2 PersistenceException (org.infinispan.persistence.spi.PersistenceException)2 JdbcStringBasedStoreConfiguration (org.infinispan.persistence.jdbc.configuration.JdbcStringBasedStoreConfiguration)1 TableManager (org.infinispan.persistence.jdbc.impl.table.TableManager)1 UnsupportedKeyTypeException (org.infinispan.persistence.keymappers.UnsupportedKeyTypeException)1