<commit_before>/**
 *
 * Copyright 2004 <NAME>
 *
 * 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 org.activeio.journal.active;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.activeio.Disposable;
import org.activeio.Packet;
import org.activeio.packet.ByteBufferPacket;

/**
 * Control file holds the last known good state of the journal.  It stores the state in 
 * record that is versioned and repeated twice in the file so that a failure in the
 * middle of the write of the first or second record do not not result in an unknown
 * state. 
 * 
 * @version $Revision: 1.1 $
 */
final public class ControlFile implements Disposable {

    /** The File that holds the control data. */
    private final RandomAccessFile file;
    private final FileChannel channel;
    private final ByteBufferPacket controlData;

    private long controlDataVersion=0;
    private FileLock lock;
	private boolean disposed;
    private static Set lockSet;
    private String canonicalPath;

    public ControlFile(File fileName, int controlDataSize) throws IOException {
        canonicalPath = fileName.getCanonicalPath();
        boolean existed = fileName.exists();        
        file = new RandomAccessFile(fileName, "rw");
        channel = file.getChannel();
        controlData = new ByteBufferPacket(ByteBuffer.allocateDirect(controlDataSize));

    }

    /**
     * Locks the control file.
     * @throws IOException 
     */
    public void lock() throws IOException {
        if( lock==null ) {
            Set set = getVmLockSet();
            synchronized(set) {
                if( !set.add(canonicalPath) ) {
                    throw new IOException("Journal is already opened by this application.");
                }
                
                lock = channel.tryLock();
                if( lock ==null ) {
                    set.remove(canonicalPath);
                    throw new IOException("Journal is already opened by another application");
                }
            }
        }
    }

    /**
     * Un locks the control file.
     * @throws IOException 
     */
    public void unlock() throws IOException {
        if( lock != null ) {
            Set set = getVmLockSet();
            synchronized(set) {
                lock.release();
                lock=null;
                set.remove(canonicalPath);
            }
        }
    }
    
    static private Set getVmLockSet() {
        if ( lockSet == null ) { 
            Properties properties = System.getProperties();
            synchronized(properties) {
                lockSet = (Set) properties.get("org.activeio.journal.active.lockMap");
                if( lockSet == null ) {
                    lockSet = new HashSet();
                }
                properties.put("org.activeio.journal.active.lockMap", lockSet);
            }
        }
        return lockSet;
    }

    
    public boolean load() throws IOException {
        long l = file.length();
        if( l < controlData.capacity() ) {
            controlDataVersion=0;
            controlData.position(0);
            controlData.limit(0);
            return false;
        } else {            
            file.seek(0);
            long v1 = file.readLong();
            file.seek(controlData.capacity()+8);
            long v1check = file.readLong();
            
            file.seek(controlData.capacity()+16);
            long v2 = file.readLong();
            file.seek((controlData.capacity()*2)+24);
            long v2check = file.readLong();
            
            if( v2 == v2check ) {
                controlDataVersion = v2;
                file.seek(controlData.capacity()+24);
                controlData.clear();
                channel.read(controlData.getByteBuffer());
            } else if ( v1 == v1check ){
                controlDataVersion = v1;
                file.seek(controlData.capacity()+8);
                controlData.clear();
                channel.read(controlData.getByteBuffer());
            } else {
                // Bummer.. Both checks are screwed. we don't know
                // if any of the two buffer are ok.  This should
                // only happen is data got corrupted.
                throw new IOException("Control data corrupted.");
            }         
            return true;
        }
    }
    
    public void store() throws IOException {
        controlDataVersion++;
        file.setLength((controlData.capacity()*2)+32);
        file.seek(0);
        
        // Write the first copy of the control data.
        file.writeLong(controlDataVersion);
        controlData.clear();
        channel.write(controlData.getByteBuffer());
        file.writeLong(controlDataVersion);

        // Write the second copy of the control data.
        file.writeLong(controlDataVersion);
        controlData.clear();
        channel.write(controlData.getByteBuffer());
        file.writeLong(controlDataVersion);
        
        channel.force(false);        
    }

    public Packet getControlData() {
        controlData.clear();
        return controlData;
    }

    public void dispose() {
    	if( disposed )
    		return;
    	disposed=true;
        try {
            unlock();
        } catch (IOException e) {
        }
        try {
            file.close();
        } catch (IOException e) {
        }
    }
}<commit_msg>removed possible threading error<commit_after>/**
 *
 * Copyright 2004 <NAME>
 *
 * 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 org.activeio.journal.active;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.activeio.Disposable;
import org.activeio.Packet;
import org.activeio.packet.ByteBufferPacket;

/**
 * Control file holds the last known good state of the journal.  It stores the state in 
 * record that is versioned and repeated twice in the file so that a failure in the
 * middle of the write of the first or second record do not not result in an unknown
 * state. 
 * 
 * @version $Revision: 1.1 $
 */
final public class ControlFile implements Disposable {

    /** The File that holds the control data. */
    private final RandomAccessFile file;
    private final FileChannel channel;
    private final ByteBufferPacket controlData;

    private long controlDataVersion=0;
    private FileLock lock;
	private boolean disposed;
    private static Set lockSet;
    private String canonicalPath;

    public ControlFile(File fileName, int controlDataSize) throws IOException {
        canonicalPath = fileName.getCanonicalPath();
        boolean existed = fileName.exists();        
        file = new RandomAccessFile(fileName, "rw");
        channel = file.getChannel();
        controlData = new ByteBufferPacket(ByteBuffer.allocateDirect(controlDataSize));

    }

    /**
     * Locks the control file.
     * @throws IOException 
     */
    public void lock() throws IOException {
        Set set = getVmLockSet();
        synchronized (set) {
            if (lock == null) {
                if (!set.add(canonicalPath)) {
                    throw new IOException("Journal is already opened by this application.");
                }

                lock = channel.tryLock();
                if (lock == null) {
                    set.remove(canonicalPath);
                    throw new IOException("Journal is already opened by another application");
                }
            }
        }
    }

    /**
     * Un locks the control file.
     * 
     * @throws IOException
     */
    public void unlock() throws IOException {
        Set set = getVmLockSet();
        synchronized (set) {
            if (lock != null) {
                set.remove(canonicalPath);
                lock.release();
                lock = null;
            }
        }
    }
    
    static private Set getVmLockSet() {
        if ( lockSet == null ) { 
            Properties properties = System.getProperties();
            synchronized(properties) {
                lockSet = (Set) properties.get("org.activeio.journal.active.lockMap");
                if( lockSet == null ) {
                    lockSet = new HashSet();
                }
                properties.put("org.activeio.journal.active.lockMap", lockSet);
            }
        }
        return lockSet;
    }

    
    public boolean load() throws IOException {
        long l = file.length();
        if( l < controlData.capacity() ) {
            controlDataVersion=0;
            controlData.position(0);
            controlData.limit(0);
            return false;
        } else {            
            file.seek(0);
            long v1 = file.readLong();
            file.seek(controlData.capacity()+8);
            long v1check = file.readLong();
            
            file.seek(controlData.capacity()+16);
            long v2 = file.readLong();
            file.seek((controlData.capacity()*2)+24);
            long v2check = file.readLong();
            
            if( v2 == v2check ) {
                controlDataVersion = v2;
                file.seek(controlData.capacity()+24);
                controlData.clear();
                channel.read(controlData.getByteBuffer());
            } else if ( v1 == v1check ){
                controlDataVersion = v1;
                file.seek(controlData.capacity()+8);
                controlData.clear();
                channel.read(controlData.getByteBuffer());
            } else {
                // Bummer.. Both checks are screwed. we don't know
                // if any of the two buffer are ok.  This should
                // only happen is data got corrupted.
                throw new IOException("Control data corrupted.");
            }         
            return true;
        }
    }
    
    public void store() throws IOException {
        controlDataVersion++;
        file.setLength((controlData.capacity()*2)+32);
        file.seek(0);
        
        // Write the first copy of the control data.
        file.writeLong(controlDataVersion);
        controlData.clear();
        channel.write(controlData.getByteBuffer());
        file.writeLong(controlDataVersion);

        // Write the second copy of the control data.
        file.writeLong(controlDataVersion);
        controlData.clear();
        channel.write(controlData.getByteBuffer());
        file.writeLong(controlDataVersion);
        
        channel.force(false);        
    }

    public Packet getControlData() {
        controlData.clear();
        return controlData;
    }

    public void dispose() {
    	if( disposed )
    		return;
    	disposed=true;
        try {
            unlock();
        } catch (IOException e) {
        }
        try {
            file.close();
        } catch (IOException e) {
        }
    }
}
{'fraction_non_alphanumeric': 0.09188544152744631, 'fraction_numerical': 0.006046141607000796, 'mean_word_length': 1.717466493731085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1790935', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3071, 'n_words': 907}