<commit_before>package org.carlspring.strongbox.providers.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

import javax.inject.Inject;

import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.io.input.ProxyInputStream;
import org.apache.commons.io.output.CountingOutputStream;
import org.apache.commons.io.output.ProxyOutputStream;
import org.carlspring.strongbox.io.RepositoryStreamCallback;
import org.carlspring.strongbox.io.RepositoryStreamContext;
import org.carlspring.strongbox.io.RepositoryStreamReadContext;
import org.carlspring.strongbox.io.RepositoryStreamWriteContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sbespalov
 *
 */
public abstract class RepositoryStreamSupport implements RepositoryStreamCallback
{
    private static final Logger logger = LoggerFactory.getLogger(RepositoryStreamSupport.class);

    private ThreadLocal<RepositoryStreamContext> ctx = new ThreadLocal<RepositoryStreamContext>();

    @Inject
    protected RepositoryPathLock repositoryPathLock;
    
    protected void initContext(RepositoryStreamContext ctx)
    {
        this.ctx.set(ctx);
    }

    protected RepositoryStreamContext getContext()
    {
        return ctx.get();
    }

    private void clearContext()
    {
        ctx.remove();
    }

    private void open()
        throws IOException
    {
        RepositoryStreamContext ctx = getContext();
        if (ctx.isOpened())
        {
            return;
        }

        RepositoryPath path = (RepositoryPath) ctx.getPath();
        logger.info(String.format("Locking [%s]", path));
        
        ReadWriteLock lockSource = repositoryPathLock.lock(path);
        Lock lock;
        if (ctx instanceof RepositoryStreamWriteContext)
        {
            lock = lockSource.writeLock();
        }
        else
        {
            lock = lockSource.readLock();
        }

        ctx.setLock(lock);
        lock.lock();

        logger.info(String.format("Locked [%s]", path));
        
        doOpen(ctx);
    }

    private void doOpen(RepositoryStreamContext ctx)
        throws IOException
    {
        ctx.setOpened(true);

        if (ctx instanceof RepositoryStreamWriteContext)
        {
            onBeforeWrite((RepositoryStreamWriteContext) ctx);
        }
        else
        {
            onBeforeRead((RepositoryStreamReadContext) ctx);
        }
    }

    public void close()
        throws IOException
    {
        RepositoryStreamContext ctx = getContext();
        if (!ctx.isOpened())
        {
            return;
        }

        try
        {
            doClose(ctx);
        } 
        finally
        {
            ctx.getLock().unlock();
            clearContext();
        }
    }

    private void doClose(RepositoryStreamContext ctx)
        throws IOException
    {
        if (ctx instanceof RepositoryStreamWriteContext)
        {
            onAfterWrite((RepositoryStreamWriteContext) ctx);
        }
        else
        {
            onAfterRead((RepositoryStreamReadContext) ctx);
        }
    }
    
    public void commit() throws IOException
    {
        commit((RepositoryStreamWriteContext) getContext());
    }

    public class RepositoryOutputStream extends ProxyOutputStream
    {
        protected RepositoryOutputStream(Path path,
                                         OutputStream out)
        {
            super(new CountingOutputStream(out));

            RepositoryStreamWriteContext ctx = new RepositoryStreamWriteContext();
            ctx.setStream(this);
            ctx.setPath(path);

            initContext(ctx);
        }
        
        @Override
        protected void beforeWrite(int n)
            throws IOException
        {
            open();
            
            super.beforeWrite(n);
        }

        @Override
        public void flush()
            throws IOException
        {
            RepositoryStreamSupport.this.commit();
        }

        @Override
        public void close()
            throws IOException
        {
            try
            {
                super.close();
            } 
            finally
            {
                RepositoryStreamSupport.this.close();
            }
        }

    }

    public class RepositoryInputStream
            extends ProxyInputStream
    {

        protected RepositoryInputStream(Path path,
                                        InputStream in)
        {
            super(new CountingInputStream(in));<commit_msg>SB-1347: OConcurrentModificationException fixed in tests<commit_after>package org.carlspring.strongbox.providers.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

import javax.inject.Inject;

import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.io.input.ProxyInputStream;
import org.apache.commons.io.output.CountingOutputStream;
import org.apache.commons.io.output.ProxyOutputStream;
import org.carlspring.strongbox.io.RepositoryStreamCallback;
import org.carlspring.strongbox.io.RepositoryStreamContext;
import org.carlspring.strongbox.io.RepositoryStreamReadContext;
import org.carlspring.strongbox.io.RepositoryStreamWriteContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sbespalov
 *
 */
public abstract class RepositoryStreamSupport implements RepositoryStreamCallback
{
    private static final Logger logger = LoggerFactory.getLogger(RepositoryStreamSupport.class);

    private ThreadLocal<RepositoryStreamContext> ctx = new ThreadLocal<RepositoryStreamContext>();

    @Inject
    protected RepositoryPathLock repositoryPathLock;
    
    protected void initContext(RepositoryStreamContext ctx)
    {
        this.ctx.set(ctx);
    }

    protected RepositoryStreamContext getContext()
    {
        return ctx.get();
    }

    private void clearContext()
    {
        ctx.remove();
    }

    private void open()
        throws IOException
    {
        RepositoryStreamContext ctx = getContext();
        if (ctx.isOpened())
        {
            return;
        }

        RepositoryPath path = (RepositoryPath) ctx.getPath();
        logger.info(String.format("Locking [%s]", path));
        
        ReadWriteLock lockSource = repositoryPathLock.lock(path);
        Lock lock;
        if (ctx instanceof RepositoryStreamWriteContext)
        {
            lock = lockSource.writeLock();
        }
        else
        {
            lock = lockSource.readLock();
        }

        ctx.setLock(lock);
        lock.lock();

        logger.info(String.format("Locked [%s]", path));
        
        doOpen(ctx);
    }

    private void doOpen(RepositoryStreamContext ctx)
        throws IOException
    {
        ctx.setOpened(true);

        if (ctx instanceof RepositoryStreamWriteContext)
        {
            onBeforeWrite((RepositoryStreamWriteContext) ctx);
        }
        else
        {
            onBeforeRead((RepositoryStreamReadContext) ctx);
        }
    }

    public void close()
        throws IOException
    {
        RepositoryStreamContext ctx = getContext();
        if (!ctx.isOpened())
        {
            return;
        }

        try
        {
            doClose(ctx);
        } 
        finally
        {
            ctx.getLock().unlock();
            clearContext();
        }
    }

    private void doClose(RepositoryStreamContext ctx)
        throws IOException
    {
        if (ctx instanceof RepositoryStreamWriteContext)
        {
            onAfterWrite((RepositoryStreamWriteContext) ctx);
        }
        else
        {
            onAfterRead((RepositoryStreamReadContext) ctx);
        }
    }
    
    public void commit() throws IOException
    {
        commit((RepositoryStreamWriteContext) getContext());
    }

    public class RepositoryOutputStream extends ProxyOutputStream
    {
        protected RepositoryOutputStream(Path path,
                                         OutputStream out)
        {
            super(new CountingOutputStream(out));

            RepositoryStreamWriteContext ctx = new RepositoryStreamWriteContext();
            ctx.setStream(this);
            ctx.setPath(path);

            initContext(ctx);
        }
        
        @Override
        protected void beforeWrite(int n)
            throws IOException
        {
            open();
            
            super.beforeWrite(n);
        }

        @Override
        public void flush()
            throws IOException
        {
            RepositoryStreamSupport.this.commit();
            super.flush();
        }

        @Override
        public void close()
            throws IOException
        {
            try
            {
                super.close();
            } 
            finally
            {
                RepositoryStreamSupport.this.close();
            }
        }

    }

    public class RepositoryInputStream
            extends ProxyInputStream
    {

        protected RepositoryInputStream(Path path,
                                        InputStream in)
        {
            super(new CountingInputStream(in));
{'fraction_non_alphanumeric': 0.08222080949353677, 'fraction_numerical': 0.000847637211273575, 'mean_word_length': 1.9350124378109452, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533882', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2303, 'n_words': 452}