All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.phoenix.index.PhoenixIndexMetaData Maven / Gradle / Ivy

There is a newer version: 5.1.0-HBase-2.0.0.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.phoenix.index;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.phoenix.cache.GlobalCache;
import org.apache.phoenix.cache.IndexMetaDataCache;
import org.apache.phoenix.cache.ServerCacheClient;
import org.apache.phoenix.cache.TenantCache;
import org.apache.phoenix.coprocessor.BaseScannerRegionObserver;
import org.apache.phoenix.coprocessor.BaseScannerRegionObserver.ReplayWrite;
import org.apache.phoenix.exception.SQLExceptionCode;
import org.apache.phoenix.exception.SQLExceptionInfo;
import org.apache.phoenix.hbase.index.covered.IndexMetaData;
import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
import org.apache.phoenix.transaction.PhoenixTransactionContext;
import org.apache.phoenix.transaction.TransactionFactory;
import org.apache.phoenix.util.PhoenixRuntime;
import org.apache.phoenix.util.ServerUtil;

public class PhoenixIndexMetaData implements IndexMetaData {
    private final Map attributes;
    private final IndexMetaDataCache indexMetaDataCache;
    private final ReplayWrite replayWrite;
    private final boolean isImmutable;
    
    private static IndexMetaDataCache getIndexMetaData(RegionCoprocessorEnvironment env, Map 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 indexMaintainers = IndexMaintainer.deserialize(md, useProto);
            final PhoenixTransactionContext txnContext = TransactionFactory.getTransactionFactory().getTransactionContext(txState);
            return new IndexMetaDataCache() {

                @Override
                public void close() throws IOException {}

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

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

            };
        } 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();
                ServerUtil.throwIOException("Index update failed", e); // will not return
            }
            return indexCache;
        }

    }

    public static boolean isIndexRebuild(Map attributes) {
        return attributes.get(BaseScannerRegionObserver.REPLAY_WRITES) != null;
    }

    public static ReplayWrite getReplayWrite(Map attributes) {
        return ReplayWrite.fromBytes(attributes.get(BaseScannerRegionObserver.REPLAY_WRITES));
    }

    public PhoenixIndexMetaData(RegionCoprocessorEnvironment env, Map attributes) throws IOException {
        this.indexMetaDataCache = getIndexMetaData(env, attributes);
        boolean isImmutable = true;
        for (IndexMaintainer maintainer : indexMetaDataCache.getIndexMaintainers()) {
            isImmutable &= maintainer.isImmutableRows();
        }
        this.isImmutable = isImmutable;
        this.attributes = attributes;
        this.replayWrite = getReplayWrite(attributes);
    }
    
    public PhoenixTransactionContext getTransactionContext() {
        return indexMetaDataCache.getTransactionContext();
    }
    
    public List getIndexMaintainers() {
        return indexMetaDataCache.getIndexMaintainers();
    }

    public Map getAttributes() {
        return attributes;
    }
    
    public ReplayWrite getReplayWrite() {
        return replayWrite;
    }

    @Override
    public boolean isImmutableRows() {
        return isImmutable;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy