Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015 HaiYang Li
*
* Licensed 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 com.landawn.abacus.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.util.ExceptionUtil;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.N;
// TODO: Auto-generated Javadoc
/**
*
* @author Haiyang Li
* @since 0.8
*/
public final class CacheZipper {
private static final Logger logger = LoggerFactory.getLogger(CacheZipper.class);
private static final int CPU_CORES_FOR_ZIP = N.max(1, IOUtil.CPU_CORES / 2);
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(CPU_CORES_FOR_ZIP);
private volatile long lastScanTime = System.currentTimeMillis();
// static {
// Runtime.getRuntime().addShutdownHook(new Thread() {
// @Override
// public void run() {
// logger.warn("Starting to shutdown task in CacheZipper");
//
// try {
// EXECUTOR_SERVICE.shutdown();
//
// while (EXECUTOR_SERVICE.isTerminated() == false) {
// N.sleepUninterruptibly(100);
// }
// } finally {
// logger.warn("Completed to shutdown task in CacheZipper");
// }
// }
// });
// }
/**
*
* @param cacheList
*/
public void zip(Collection extends QueryCache> cacheList) {
boolean isCacheUpdated = false;
for (QueryCache queryCache : cacheList) {
if (queryCache.getLastUpdateTime() > lastScanTime) {
isCacheUpdated = true;
}
}
if (!isCacheUpdated) {
return;
}
// Runtime.getRuntime().gc();
final AtomicInteger activeThreadNum = new AtomicInteger();
final Object[] hashArray = CacheZipper.createBigArray();
if (hashArray.length > 0) {
final List> threadCachesList = divideCacheByProcessor(cacheList);
for (final List threadCaches : threadCachesList) {
final Runnable command = new Runnable() {
@Override
public void run() {
try {
zipCache(hashArray, threadCaches);
} finally {
activeThreadNum.decrementAndGet();
}
}
};
activeThreadNum.incrementAndGet();
EXECUTOR_SERVICE.execute(command);
}
while (activeThreadNum.get() > 0) {
N.sleep(1);
}
lastScanTime = System.currentTimeMillis();
// Runtime.getRuntime().gc();
}
}
/**
*
* @param hashArray
* @param cacheList
*/
@SuppressWarnings("unchecked")
void zipCache(final Object[] hashArray, final List extends QueryCache> cacheList) {
final int BIT_INDEX = hashArray.length - 1;
int hitNum = 0;
for (QueryCache queryCache : cacheList) {
final DataGrid