org.apache.flink.runtime.state.gemini.engine.page.PageContext Maven / Gradle / Ivy
/*
* 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.flink.runtime.state.gemini.engine.page;
import org.apache.flink.annotation.Internal;
import org.apache.flink.runtime.state.gemini.engine.GRegionID;
import org.apache.flink.runtime.state.gemini.engine.exceptions.GeminiRuntimeException;
import static org.apache.flink.util.Preconditions.checkArgument;
import static org.apache.flink.util.Preconditions.checkNotNull;
/**
* Contex for a {@link DataPage}.
*/
@Internal
public class PageContext {
public static final PageContext INVALID_PAGE_CONTEXT = null;
/**
* Region ID of current DataPage.
*/
private final GRegionID regionID;
/**
* The index of {@link LogicalPageChain} in {@link PageIndex} which contains current data page.
* -1 means invalid.
*/
private final int logicPageIndex;
/**
* The hash code of the {@link LogicalPageChain} contains the current data page.
* -1 means invalid.
*/
private final int logicPageHashCode;
private volatile CacheStatus cacheStatues;
//----------------------------------------------------------------
// Constructor //
//----------------------------------------------------------------
public PageContext(
GRegionID gRegionID, int logicPageIndex, int logicPageHashCode, CacheStatus cacheStatues) {
checkArgument(logicPageIndex >= 0);
this.regionID = checkNotNull(gRegionID);
this.logicPageIndex = logicPageIndex;
this.logicPageHashCode = logicPageHashCode;
this.cacheStatues = checkNotNull(cacheStatues);
}
//------------------------------------------------------------------
// Help function to construct a PageContext //
//------------------------------------------------------------------
public static PageContext of(
GRegionID regionID, int logicPageIndex, int logicPageHashCode) {
return new PageContext(regionID, logicPageIndex, logicPageHashCode, CacheStatus.UNKNOW);
}
public static PageContext of(
GRegionID regionID, int logicPageIndex, int logicPageHashCode, CacheStatus cacheStatues) {
return new PageContext(regionID, logicPageIndex, logicPageHashCode, cacheStatues);
}
//---------------------------------------------------------------
// public functions //
//---------------------------------------------------------------
/**
* Returns the region id which the current data page associated with.
*/
public GRegionID getGRegionID() {
return regionID;
}
/**
* Returns the index of {@link LogicalPageChain} which contains the current data page.
*/
public int getLogicPageIndex() {
return logicPageIndex;
}
/**
* Returns the hash code of the {@link LogicalPageChain} which contains the current {@link DataPage}.
*
* @return
*/
public int getLogicPageChainHashCode() {
return logicPageHashCode;
}
/**
* Returns the cache status of current data page.
*/
public CacheStatus getCacheStatus() {
return cacheStatues;
}
public synchronized CacheStatus setCacheStatus(CacheStatus newStatus) {
CacheStatus old = cacheStatues;
this.cacheStatues = newStatus;
return old;
}
@Override
public int hashCode() {
int hashCode = regionID.hashCode();
hashCode = hashCode * 31 + logicPageIndex;
hashCode = hashCode * 31 + logicPageHashCode;
hashCode = hashCode * 31 + cacheStatues.getCode();
return hashCode;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != getClass()) {
return false;
}
PageContext other = (PageContext) obj;
return regionID.equals(other.getGRegionID()) && logicPageIndex == other.logicPageIndex && logicPageHashCode == other.logicPageHashCode && cacheStatues.equals(
other.cacheStatues);
}
/**
* Cache status for a data page.
*/
public enum CacheStatus {
UNKNOW((byte) 0), IN_LRU((byte) 1), CACHING_TO_MAIN((byte) 2);
private final byte code;
CacheStatus(byte code) {
this.code = code;
}
byte getCode() {
return code;
}
public static CacheStatus valueOf(byte code) {
switch (code) {
case 0:
return UNKNOW;
case 1:
return IN_LRU;
case 2:
return CACHING_TO_MAIN;
default:
throw new GeminiRuntimeException("Wrong cache status " + code);
}
}
}
}