<commit_before>/* ============================================================
 * JRobin : Pure java implementation of RRDTool's functionality
 * ============================================================
 *
 * Project Info:  http://www.jrobin.org
 * Project Lead:  Sasa Markovic (<EMAIL>);
 *
 * (C) Copyright 2003, by <NAME>.
 *
 * Developers:    <NAME> (<EMAIL>)
 *                <NAME> (<EMAIL>)
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package org.jrobin.core;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import sun.nio.ch.DirectBuffer;
import java.util.Timer;
import java.util.TimerTask;

/**
 * JRobin backend which is used to store RRD data to ordinary disk files
 * by using fast java.nio.* package. This is the default backend engine since JRobin 1.4.0.
 */
public class RrdNioBackend extends RrdFileBackend {
	private static final Timer fileSyncTimer = new Timer(true);

	private MappedByteBuffer byteBuffer;
	private int syncMode;
	private TimerTask syncTask;

	/**
	 * Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.
	 * @param path Path to a file
	 * @param readOnly True, if file should be open in a read-only mode. False otherwise
	 * @param lockMode Locking mode, as described in {@link RrdDb#getLockMode()}
	 * @param syncMode See {@link RrdNioBackendFactory#setSyncMode(int)} for explanation
	 * @param syncPeriod See {@link RrdNioBackendFactory#setSyncMode(int)} for explanation
	 * @throws IOException Thrown in case of I/O error
	 */
	protected RrdNioBackend(String path, boolean readOnly, int lockMode, int syncMode, int syncPeriod)
			throws IOException {
		super(path, readOnly, lockMode);
		this.syncMode = syncMode;
		mapFile();
		if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) {
			syncTask = new TimerTask() {
				public void run() {
					sync();
				}
			};
			fileSyncTimer.schedule(syncTask, syncPeriod * 1000L, syncPeriod * 1000L);
		}
	}

	private void mapFile() throws IOException {
		long length = getLength();
		if(length > 0) {
			FileChannel.MapMode mapMode =
				readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE;
			byteBuffer = channel.map(mapMode, 0, length);
		}
	}

	private void unmapFile() {
		if(byteBuffer != null) {
			((DirectBuffer) byteBuffer).cleaner().clean();
			byteBuffer = null;
		}
	}

	/**
	 * Sets length of the underlying RRD file. This method is called only once, immediately
	 * after a new RRD file gets created.
	 * @param newLength Length of the RRD file
	 * @throws IOException Thrown in case of I/O error.
	 */<commit_msg>minor changes<commit_after>/* ============================================================
 * JRobin : Pure java implementation of RRDTool's functionality
 * ============================================================
 *
 * Project Info:  http://www.jrobin.org
 * Project Lead:  <NAME> (<EMAIL>);
 *
 * (C) Copyright 2003, by <NAME>.
 *
 * Developers:    <NAME> (<EMAIL>)
 *                <NAME> (<EMAIL>)
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package org.jrobin.core;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import sun.nio.ch.DirectBuffer;
import java.util.Timer;
import java.util.TimerTask;

/**
 * JRobin backend which is used to store RRD data to ordinary disk files
 * by using fast java.nio.* package. This is the default backend engine since JRobin 1.4.0.
 */
public class RrdNioBackend extends RrdFileBackend {
	private static final Timer fileSyncTimer = new Timer(true);

	private MappedByteBuffer byteBuffer;
	private int syncMode;
	private TimerTask syncTask;

	/**
	 * Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.
	 * @param path Path to a file
	 * @param readOnly True, if file should be open in a read-only mode. False otherwise
	 * @param lockMode Locking mode, as described in {@link RrdDb#getLockMode()}
	 * @param syncMode See {@link RrdNioBackendFactory#setSyncMode(int)} for explanation
	 * @param syncPeriod See {@link RrdNioBackendFactory#setSyncMode(int)} for explanation
	 * @throws IOException Thrown in case of I/O error
	 */
	protected RrdNioBackend(String path, boolean readOnly, int lockMode, int syncMode, int syncPeriod)
			throws IOException {
		super(path, readOnly, lockMode);
		this.syncMode = syncMode;
		mapFile();
		if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) {
			syncTask = new TimerTask() {
				public void run() {
					sync();
				}
			};
			fileSyncTimer.schedule(syncTask, syncPeriod * 1000L, syncPeriod * 1000L);
		}
	}

	private void mapFile() throws IOException {
		long length = getLength();
		if(length > 0) {
			FileChannel.MapMode mapMode =
				readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE;
			byteBuffer = channel.map(mapMode, 0, length);
		}
	}

	private void unmapFile() {
		if(byteBuffer != null) {
			if(byteBuffer instanceof DirectBuffer) {
				((DirectBuffer) byteBuffer).cleaner().clean();
			}
			byteBuffer = null;
		}
	}

	/**
	 * Sets length of the underlying RRD file. This method is called only once, immediately
	 * after a new RRD file gets created.
	 * @param newLength Length of the RRD file
	 * @throws IOException Thrown in case of I/O error.
	 */
{'fraction_non_alphanumeric': 0.12457057505601195, 'fraction_numerical': 0.009858103061986557, 'mean_word_length': 4.053584905660378, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5511370', 'n_tokens_mistral': 2066, 'n_tokens_neox': 1874, 'n_words': 733}