/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
/*
 * EXTENSION
 * ISSUE: DVCORE-2521
 * FIXED BY: Roman Pedchenko
 * DATE: 2014-12-05
 * ADOPTED FROM: TEIID-2153, 2171, 2359, 2149, 2358, 2410, 2271, 2467, 2429, 2363, 2160, 2461, 2797, 2326, 1557
 * END EXTENSION
 */
// https://issues.jboss.org/browse/TEIID-3635
package org.teiid.common.buffer.impl;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.teiid.adminapi.impl.SessionMetadata;
import org.teiid.common.buffer.BufferManager.BufferReserveMode;
import org.teiid.common.buffer.BufferManager.TupleSourceType;
import org.teiid.common.buffer.FileStore;
import org.teiid.common.buffer.TupleBuffer;
import org.teiid.common.buffer.impl.BufferManagerImpl.BatchManagerImpl;
import org.teiid.core.TeiidComponentException;
import org.teiid.query.sql.symbol.ElementSymbol;
import org.teiid.query.util.CommandContext;
import org.teiid.query.util.Options;

public class TestBufferManagerImpl {
	
    @Test public void testReserve() throws Exception {
        BufferManagerImpl bufferManager = new BufferManagerImpl();
        bufferManager.setCache(new MemoryStorageManager());
        bufferManager.setMaxProcessingKB(1024);
        bufferManager.setMaxReserveKB(1024);
        bufferManager.initialize();
        bufferManager.setNominalProcessingMemoryMax(512000);
        
        //restricted by nominal max
        assertEquals(512000, bufferManager.reserveBuffers(1024000, BufferReserveMode.NO_WAIT));
		//forced
        assertEquals(1024000, bufferManager.reserveBuffersBlocking(1024000, new long[] {0,0}, true));
        
        //not forced, so we get noting
        assertEquals(0, bufferManager.reserveBuffersBlocking(1024000, new long[] {0,0}, false));
        // EXTENSION
    	// ISSUE: DVCORE-2599
    	// FIXED BY: Salvatore Raunich
    	// DATE: 2015.02.17
        CommandContext.pushThreadLocalContext(new CommandContext());
        // END EXTENSION
        bufferManager.releaseBuffers(512000);
        //the difference between 1mb and 1000k
        assertEquals(24576, bufferManager.reserveBuffers(1024000, BufferReserveMode.NO_WAIT));
    }

    @Test public void testLargeReserve() throws Exception {
        BufferManagerImpl bufferManager = new BufferManagerImpl();
        bufferManager.setCache(new MemoryStorageManager());
        bufferManager.setMaxReserveKB((1<<22) + 11);
        assertEquals(4194315, bufferManager.getMaxReserveKB());
    }

    @Test public void testFileStoreMax() throws Exception {
        BufferManagerImpl bufferManager = new BufferManagerImpl();
        bufferManager.setCache(new MemoryStorageManager() {
            @Override
            public long getMaxStorageSpace() {
                return 640;
            }
        });
        bufferManager.setMaxActivePlans(20);
        bufferManager.initialize();
        FileStore fs = bufferManager.createFileStore("x");
        assertThrows(IOException.class, () -> fs.write(new byte[10], 0, 10));
    }

    @Test public void testTupleBufferMax() throws Exception {
        BufferManagerImpl bufferManager = new BufferManagerImpl();
        bufferManager.setCache(new MemoryStorageManager() {
            @Override
            public long getMaxStorageSpace() {
                return 640;
            }
        });
        bufferManager.setMaxReserveKB(10);
        bufferManager.setMaxActivePlans(20);
        bufferManager.setEnforceMaxBatchManagerSizeEstimate(true);
        bufferManager.initialize();
        TupleBuffer tb = bufferManager.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, String.class)), "x", TupleSourceType.PROCESSOR);
        //fill one batch, which should then exceed the max
        assertThrows(TeiidComponentException.class,
            () -> {
                for (int i = 0; i < 1024; i++) {
                    tb.addTuple(Arrays.asList("a"));
                }
            }
        );
    }

    @Test public void testTupleBufferSessionMax() throws Exception {
        BufferManagerImpl bufferManager = new BufferManagerImpl();
        bufferManager.setCache(new MemoryStorageManager() {
            @Override
            public long getMaxStorageSpace() {
                return 64000;
            }
        });
        bufferManager.setMaxReserveKB(10);
        bufferManager.setMaxActivePlans(10);
        bufferManager.setOptions(new Options().maxSessionBufferSizeEstimate(100000));
        bufferManager.initialize();
        CommandContext context = new CommandContext();
        context.setSession(new SessionMetadata());
        CommandContext.pushThreadLocalContext(context);
        try {
            List<TupleBuffer> tupleBuffers = new ArrayList<TupleBuffer>();
            for (int i = 0; i < 36; i++) {
                TupleBuffer tb = bufferManager.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, String.class)), "x", TupleSourceType.PROCESSOR);
                try {
                    for (int j = 0; j < 50; j++) {
                        tb.addTuple(Arrays.asList("a"));
                    }
                    tb.saveBatch();
                    if (i%2==0) {
                        tb.remove();
                    }
                } catch (TeiidComponentException e) {
                    assertEquals(34, i);
                    return;
                }
                tupleBuffers.add(tb);
            }
        } finally {
            CommandContext.popThreadLocalContext();
        }
        fail();
    }

    @Test public void testRemovedException() throws TeiidComponentException {
        BufferManagerImpl bufferManager = new BufferManagerImpl();
        bufferManager.setCache(new MemoryStorageManager());
        bufferManager.initialize();
        BatchManagerImpl batchManager = bufferManager.createBatchManager(1l, new Class<?>[] {Integer.class});
        batchManager.describe(null); //should not error
        batchManager.remove();
        try {
            batchManager.getBatch(1l, false);
            fail();
        } catch (TeiidComponentException e) {

        }
    }

}
