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) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* 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.hazelcast.internal.nearcache.impl.preloader;
import com.hazelcast.config.NearCachePreloaderConfig;
import com.hazelcast.internal.adapter.DataStructureAdapter;
import com.hazelcast.internal.monitor.impl.NearCacheStatsImpl;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.internal.serialization.impl.HeapData;
import com.hazelcast.internal.util.BufferingInputStream;
import com.hazelcast.internal.util.Timer;
import com.hazelcast.internal.util.collection.InflatableSet;
import com.hazelcast.internal.util.collection.InflatableSet.Builder;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.memory.MemoryUnit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.util.Iterator;
import static com.hazelcast.internal.nio.Bits.INT_SIZE_IN_BYTES;
import static com.hazelcast.internal.nio.Bits.readIntB;
import static com.hazelcast.internal.nio.Bits.writeIntB;
import static com.hazelcast.internal.nio.IOUtil.closeResource;
import static com.hazelcast.internal.nio.IOUtil.deleteQuietly;
import static com.hazelcast.internal.nio.IOUtil.readFullyOrNothing;
import static com.hazelcast.internal.nio.IOUtil.rename;
import static com.hazelcast.internal.nio.IOUtil.toFileName;
import static com.hazelcast.internal.util.StringUtil.isNullOrEmpty;
import static java.lang.String.format;
import static java.nio.ByteBuffer.allocate;
/**
* Loads and stores the keys from a Near Cache into a file.
*
* @param type of the {@link com.hazelcast.internal.nearcache.NearCacheRecord} keys
*/
public class NearCachePreloader {
/**
* File format for the file header.
*/
private enum FileFormat {
INTERLEAVED_LENGTH_FIELD
}
/**
* Magic bytes for the file header.
*/
private static final int MAGIC_BYTES = 0xEA3CAC4E;
/**
* Base-2 logarithm of buffer size.
*/
private static final int LOG_OF_BUFFER_SIZE = 16;
/**
* Buffer size used for file I/O. Invariant: buffer size is a power of two.
*/
private static final int BUFFER_SIZE = 1 << LOG_OF_BUFFER_SIZE;
/**
* Batch size for the pre-loader.
*/
private static final int LOAD_BATCH_SIZE = 100;
private final ILogger logger = Logger.getLogger(NearCachePreloader.class);
private final byte[] tmpBytes = new byte[INT_SIZE_IN_BYTES];
private final String nearCacheName;
private final NearCacheStatsImpl nearCacheStats;
private final SerializationService serializationService;
private final NearCachePreloaderLock lock;
private final File storeFile;
private final File tmpStoreFile;
private ByteBuffer buf;
private int lastWrittenBytes;
private int lastKeyCount;
public NearCachePreloader(String nearCacheName, NearCachePreloaderConfig preloaderConfig,
NearCacheStatsImpl nearCacheStats, SerializationService serializationService) {
this.nearCacheName = nearCacheName;
this.nearCacheStats = nearCacheStats;
this.serializationService = serializationService;
String filename = getFilename(preloaderConfig.getDirectory(), nearCacheName);
this.lock = new NearCachePreloaderLock(logger, filename + ".lock");
this.storeFile = new File(filename);
this.tmpStoreFile = new File(filename + "~");
}
public void destroy() {
lock.release();
}
/**
* Loads the values via a stored key file into the supplied {@link DataStructureAdapter}.
*
* @param adapter the {@link DataStructureAdapter} to load the values from
*/
public void loadKeys(DataStructureAdapter