<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.channels.FileChannel;
import java.nio.MappedByteBuffer;
import java.util.TimerTask;
import java.util.Timer;

/**
 * JRobin backend which is used to store RRD data to ordinary files on the disk
 * by using java.nio.* package. This is the default backend engine since JRobin 1.4.0.
 */
public class RrdNioBackend extends RrdFileBackend {
	/**
	 * Defines if the <code>System.gc()</code> method should be executed when necessary.
	 * NIO backend uses large in-memory buffer to cache file data. The buffer remains 'active'
	 * (by prohibiting file re-creation, for example) as long as it is not garbage collected.
	 * By forcing <code>gc()</code> where appropriate, memory gets freed sooner and file
	 * re-creation won't fail.<p>
	 *
	 * The constant is set to true initially and currently there is no API to change it
	 * during runtime.<p>
	 */
	public static final boolean SHOULD_GC = true;

	static {
		if(SHOULD_GC) {
			Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {
					System.gc();
				}
			});
		}
	}

	private static final Timer syncTimer = new Timer(true);

	private int syncMode;
	MappedByteBuffer byteBuffer;
	private TimerTask syncTask;

	protected RrdNioBackend(String path, boolean readOnly, int lockMode, int syncMode, int syncPeriod)
			throws IOException {
		super(path, readOnly, lockMode);
		map(false, readOnly);
		this.syncMode = syncMode;
		if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) {
			createSyncTask(syncPeriod);
		}
	}

	private void map(boolean isShorter, boolean readOnly) throws IOException {
		if(isShorter) {
			byteBuffer = null;
			if(SHOULD_GC) {
				System.gc();
			}
		}
		long newLength = getLength();
		if(newLength > 0) {
			FileChannel.MapMode mapMode =
				readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE;
			byteBuffer = channel.map(mapMode, 0, newLength);
		}
	}

	private void createSyncTask(int syncPeriod) {
		syncTask = new TimerTask() {
			public void run() {
				sync();
			}
		};
		syncTimer.schedule(syncTask, syncPeriod * 1000L, syncPeriod * 1000L);
	}

	/**
	 * 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.
	 */
	protected void setLength(long newLength) throws IOException {
		boolean isShorter = newLength < getLength();
		super.setLength(newLength);
		map(isShorter, false);
	}

	/**
	 * Writes bytes to the underlying RRD file on the disk
	 * @param offset Starting file offset
	 * @param b Bytes to be written.
	 */
	protected void write(long offset, byte[] b) {
		synchronized(byteBuffer) {
			byteBuffer.position((int)offset);
			byteBuffer.put(b);
		}
	}

	/**
	 * Reads a number of bytes from the RRD file on the disk
	 * @param offset Starting file offset
	 * @param b Buffer which receives bytes read from the file.
	 */
	protected void read(long offset, byte[] b) {
		synchronized(byteBuffer) {
			byteBuffer.position((int)offset);
			byteBuffer.get(b);
		}
	}

   	/**
	 * Closes the underlying RRD file.
	 * @throws IOException Thrown in case of I/O error
	 */
	public void close() throws IOException {<commit_msg>Workaround for strange NIO exceptions (finally).<commit_after>/* ============================================================
 * 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.channels.FileChannel;
import java.nio.MappedByteBuffer;
import java.util.TimerTask;
import java.util.Timer;

/**
 * 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 {
	/**
	 * Defines <code>System.gc()</code> usage policy for this backend.<p>
	 *
	 * NIO backend uses potentially large in-memory buffer to cache file data.
	 * The buffer remains 'active' (by prohibiting file re-creation with the smaller file size)
	 * as long as it is not garbage-collected. By forcing <code>System.gc()</code> call where
	 * appropriate, this backend will free in-memory buffers sooner and file re-creation won't fail.<p>
	 *
	 * The constant is set to <b><code>true</code></b> initially and currently there is no
	 * API to change it during runtime.
	 *
	 * Garbage collection will be forced only in some special circumstances.
	 * It should not affect the speed of your application significantly.<p>
	 */
	public static final boolean SHOULD_GC = true;

	static {
		if(SHOULD_GC) {
			final Runtime runtime = Runtime.getRuntime();
			runtime.addShutdownHook(new Thread() {
				public void run() {
					runtime.runFinalization();
					runtime.gc();
				}
			});
		}
	}

	private static final Timer syncTimer = new Timer(true);

	private int syncMode;
	MappedByteBuffer byteBuffer;
	private TimerTask syncTask;

	protected RrdNioBackend(String path, boolean readOnly, int lockMode, int syncMode, int syncPeriod)
			throws IOException {
		super(path, readOnly, lockMode);
		map(readOnly);
		this.syncMode = syncMode;
		if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) {
			createSyncTask(syncPeriod);
		}
	}

	private void map(boolean readOnly) 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);
		}
		else {
			byteBuffer = null;
		}
	}

	private void createSyncTask(int syncPeriod) {
		syncTask = new TimerTask() {
			public void run() {
				sync();
			}
		};
		syncTimer.schedule(syncTask, syncPeriod * 1000L, syncPeriod * 1000L);
	}

	/**
	 * 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.
	 */
	protected void setLength(long newLength) throws IOException {
		if(newLength < getLength()) {
			// the file will be truncated
			if(SHOULD_GC) {
				byteBuffer = null;
				System.gc();
			}
		}
		super.setLength(newLength);
		map(false);
	}

	/**
	 * Writes bytes to the underlying RRD file on the disk
	 * @param offset Starting file offset
	 * @param b Bytes to be written.
	 */
	protected void write(long offset, byte[] b) {
		synchronized(byteBuffer) {
			byteBuffer.position((int)offset);
			byteBuffer.put(b);
		}
	}

	/**
	 * Reads a number of bytes from the RRD file on the disk
	 * @param offset Starting file offset
	 * @param b Buffer which receives bytes read from the file.
	 */
	protected void read(long offset, byte[] b) {
		synchronized(byteBuffer) {
			byteBuffer.position((int)offset);
			byteBuffer.get(b);
		}
	}

   	/**
	 * Closes the underlying RRD file.
	 * @throws IOException Thrown in case of I/O error
	 */
	public void close() throws IOException {
{'fraction_non_alphanumeric': 0.11896476729012614, 'fraction_numerical': 0.007177033492822967, 'mean_word_length': 3.7334019557385485, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '6330356', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2617, 'n_words': 1006}