Search in sources :

Example 1 with IndexMetaDataCache

use of org.apache.phoenix.cache.IndexMetaDataCache in project phoenix by apache.

the class MutationState method newTxIndexMutationGenerator.

private PhoenixTxIndexMutationGenerator newTxIndexMutationGenerator(PTable table, List<PTable> indexes, Map<String, byte[]> attributes) {
    final List<IndexMaintainer> indexMaintainers = Lists.newArrayListWithExpectedSize(indexes.size());
    for (PTable index : indexes) {
        IndexMaintainer maintainer = index.getIndexMaintainer(table, connection);
        indexMaintainers.add(maintainer);
    }
    IndexMetaDataCache indexMetaDataCache = new IndexMetaDataCache() {

        @Override
        public void close() throws IOException {
        }

        @Override
        public List<IndexMaintainer> getIndexMaintainers() {
            return indexMaintainers;
        }

        @Override
        public PhoenixTransactionContext getTransactionContext() {
            return phoenixTransactionContext;
        }

        @Override
        public int getClientVersion() {
            return MetaDataProtocol.PHOENIX_VERSION;
        }
    };
    try {
        PhoenixIndexMetaData indexMetaData = new PhoenixIndexMetaData(indexMetaDataCache, attributes);
        return new PhoenixTxIndexMutationGenerator(connection.getQueryServices().getConfiguration(), indexMetaData, table.getPhysicalName().getBytes());
    } catch (IOException e) {
        // Impossible
        throw new RuntimeException(e);
    }
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) PhoenixIndexMetaData(org.apache.phoenix.index.PhoenixIndexMetaData) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) IOException(java.io.IOException) PTable(org.apache.phoenix.schema.PTable)

Example 2 with IndexMetaDataCache

use of org.apache.phoenix.cache.IndexMetaDataCache in project phoenix by apache.

the class PhoenixIndexMetaData method getIndexMetaData.

private static IndexMetaDataCache getIndexMetaData(RegionCoprocessorEnvironment env, Map<String, byte[]> attributes) throws IOException {
    if (attributes == null) {
        return IndexMetaDataCache.EMPTY_INDEX_META_DATA_CACHE;
    }
    byte[] uuid = attributes.get(PhoenixIndexCodec.INDEX_UUID);
    if (uuid == null) {
        return IndexMetaDataCache.EMPTY_INDEX_META_DATA_CACHE;
    }
    boolean useProto = false;
    byte[] md = attributes.get(PhoenixIndexCodec.INDEX_PROTO_MD);
    useProto = md != null;
    if (md == null) {
        md = attributes.get(PhoenixIndexCodec.INDEX_MD);
    }
    byte[] txState = attributes.get(BaseScannerRegionObserver.TX_STATE);
    if (md != null) {
        final List<IndexMaintainer> indexMaintainers = IndexMaintainer.deserialize(md, useProto);
        final Transaction txn = MutationState.decodeTransaction(txState);
        return new IndexMetaDataCache() {

            @Override
            public void close() throws IOException {
            }

            @Override
            public List<IndexMaintainer> getIndexMaintainers() {
                return indexMaintainers;
            }

            @Override
            public Transaction getTransaction() {
                return txn;
            }
        };
    } else {
        byte[] tenantIdBytes = attributes.get(PhoenixRuntime.TENANT_ID_ATTRIB);
        ImmutableBytesPtr tenantId = tenantIdBytes == null ? null : new ImmutableBytesPtr(tenantIdBytes);
        TenantCache cache = GlobalCache.getTenantCache(env, tenantId);
        IndexMetaDataCache indexCache = (IndexMetaDataCache) cache.getServerCache(new ImmutableBytesPtr(uuid));
        if (indexCache == null) {
            String msg = "key=" + ServerCacheClient.idToString(uuid) + " region=" + env.getRegion() + "host=" + env.getRegionServerServices().getServerName();
            SQLException e = new SQLExceptionInfo.Builder(SQLExceptionCode.INDEX_METADATA_NOT_FOUND).setMessage(msg).build().buildException();
            // will not return
            ServerUtil.throwIOException("Index update failed", e);
        }
        return indexCache;
    }
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) TenantCache(org.apache.phoenix.cache.TenantCache) Transaction(org.apache.tephra.Transaction) SQLException(java.sql.SQLException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 3 with IndexMetaDataCache

use of org.apache.phoenix.cache.IndexMetaDataCache in project phoenix by apache.

the class IndexMetaDataCacheFactory method newCache.

@Override
public Closeable newCache(ImmutableBytesWritable cachePtr, byte[] txState, final MemoryChunk chunk, boolean useProtoForIndexMaintainer) throws SQLException {
    // just use the standard keyvalue builder - this doesn't really need to be fast
    final List<IndexMaintainer> maintainers = IndexMaintainer.deserialize(cachePtr, GenericKeyValueBuilder.INSTANCE, useProtoForIndexMaintainer);
    final Transaction txn;
    try {
        txn = txState.length != 0 ? MutationState.decodeTransaction(txState) : null;
    } catch (IOException e) {
        throw new SQLException(e);
    }
    return new IndexMetaDataCache() {

        @Override
        public void close() throws IOException {
            chunk.close();
        }

        @Override
        public List<IndexMaintainer> getIndexMaintainers() {
            return maintainers;
        }

        @Override
        public Transaction getTransaction() {
            return txn;
        }
    };
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) Transaction(org.apache.tephra.Transaction) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 4 with IndexMetaDataCache

use of org.apache.phoenix.cache.IndexMetaDataCache in project phoenix by apache.

the class PhoenixIndexMetaDataBuilder method getIndexMetaDataCache.

private static IndexMetaDataCache getIndexMetaDataCache(RegionCoprocessorEnvironment env, Map<String, byte[]> attributes) throws IOException {
    if (attributes == null) {
        return IndexMetaDataCache.EMPTY_INDEX_META_DATA_CACHE;
    }
    byte[] uuid = attributes.get(PhoenixIndexCodec.INDEX_UUID);
    if (uuid == null) {
        return IndexMetaDataCache.EMPTY_INDEX_META_DATA_CACHE;
    }
    byte[] md = attributes.get(PhoenixIndexCodec.INDEX_PROTO_MD);
    if (md == null) {
        md = attributes.get(PhoenixIndexCodec.INDEX_MD);
    }
    if (md != null) {
        boolean useProto = md != null;
        byte[] txState = attributes.get(BaseScannerRegionObserver.TX_STATE);
        final List<IndexMaintainer> indexMaintainers = IndexMaintainer.deserialize(md, useProto);
        final PhoenixTransactionContext txnContext = TransactionFactory.getTransactionProvider().getTransactionContext(txState);
        byte[] clientVersionBytes = attributes.get(PhoenixIndexCodec.CLIENT_VERSION);
        final int clientVersion = clientVersionBytes == null ? IndexMetaDataCache.UNKNOWN_CLIENT_VERSION : Bytes.toInt(clientVersionBytes);
        return new IndexMetaDataCache() {

            @Override
            public void close() throws IOException {
            }

            @Override
            public List<IndexMaintainer> getIndexMaintainers() {
                return indexMaintainers;
            }

            @Override
            public PhoenixTransactionContext getTransactionContext() {
                return txnContext;
            }

            @Override
            public int getClientVersion() {
                return clientVersion;
            }
        };
    } else {
        byte[] tenantIdBytes = attributes.get(PhoenixRuntime.TENANT_ID_ATTRIB);
        ImmutableBytesPtr tenantId = tenantIdBytes == null ? null : new ImmutableBytesPtr(tenantIdBytes);
        TenantCache cache = GlobalCache.getTenantCache(env, tenantId);
        IndexMetaDataCache indexCache = (IndexMetaDataCache) cache.getServerCache(new ImmutableBytesPtr(uuid));
        if (indexCache == null) {
            String msg = "key=" + ServerCacheClient.idToString(uuid) + " region=" + env.getRegion() + "host=" + env.getRegionServerServices().getServerName();
            SQLException e = new SQLExceptionInfo.Builder(SQLExceptionCode.INDEX_METADATA_NOT_FOUND).setMessage(msg).build().buildException();
            // will not return
            ServerUtil.throwIOException("Index update failed", e);
        }
        return indexCache;
    }
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) TenantCache(org.apache.phoenix.cache.TenantCache) SQLException(java.sql.SQLException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PhoenixTransactionContext(org.apache.phoenix.transaction.PhoenixTransactionContext) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 5 with IndexMetaDataCache

use of org.apache.phoenix.cache.IndexMetaDataCache in project phoenix by apache.

the class IndexMetaDataCacheFactory method newCache.

@Override
public Closeable newCache(ImmutableBytesWritable cachePtr, byte[] txState, final MemoryChunk chunk, boolean useProtoForIndexMaintainer, final int clientVersion) throws SQLException {
    // just use the standard keyvalue builder - this doesn't really need to be fast
    final List<IndexMaintainer> maintainers = IndexMaintainer.deserialize(cachePtr, GenericKeyValueBuilder.INSTANCE, useProtoForIndexMaintainer);
    final PhoenixTransactionContext txnContext;
    try {
        txnContext = txState.length != 0 ? TransactionFactory.getTransactionProvider().getTransactionContext(txState) : null;
    } catch (IOException e) {
        throw new SQLException(e);
    }
    return new IndexMetaDataCache() {

        @Override
        public void close() throws IOException {
            chunk.close();
        }

        @Override
        public List<IndexMaintainer> getIndexMaintainers() {
            return maintainers;
        }

        @Override
        public PhoenixTransactionContext getTransactionContext() {
            return txnContext;
        }

        @Override
        public int getClientVersion() {
            return clientVersion;
        }
    };
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) SQLException(java.sql.SQLException) IOException(java.io.IOException) PhoenixTransactionContext(org.apache.phoenix.transaction.PhoenixTransactionContext)

Aggregations

IndexMetaDataCache (org.apache.phoenix.cache.IndexMetaDataCache)5 SQLException (java.sql.SQLException)4 IOException (java.io.IOException)3 TenantCache (org.apache.phoenix.cache.TenantCache)2 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)2 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)2 PhoenixTransactionContext (org.apache.phoenix.transaction.PhoenixTransactionContext)2 Transaction (org.apache.tephra.Transaction)2 IndexMaintainer (org.apache.phoenix.index.IndexMaintainer)1 PhoenixIndexMetaData (org.apache.phoenix.index.PhoenixIndexMetaData)1 PTable (org.apache.phoenix.schema.PTable)1