<commit_before>/*
 *
 *  Copyright 2017 <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.distbc.data.structures;

import com.google.inject.Inject;
import org.distbc.GuiceJUnit4Runner;
import org.distbc.GuiceModules;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(GuiceJUnit4Runner.class)
@GuiceModules({ DataStructureModule.class, TxnManagerModule.class })
public class GalaxyTempTableTest {
    @Inject
    private InternalDataStructureFactory dsFactory;

    @Inject
    private TxnManager txnManager;

    @Test
    public void testBasic() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        AtomicInteger i = new AtomicInteger(0);
        t.keys().forEach(guid -> {
            assertNotNull(tt.get(guid));
            assertEquals(guid, tt.get(guid).getGuid());
            assertEquals(t.get(guid), tt.get(guid));
            i.getAndAdd(1);
        });

        assertEquals(3, i.get());
    }

    @Test
    public void testRemoveColumnNames() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        tt.removeColumnWithName("foo".toUpperCase(), txn);
        txn.commit();

        List<Tuple> columns = tt.getColumnMetadata();
        assertEquals(2, columns.size());
        assertEquals("tup_num".toUpperCase(), columns.get(0).get(0));
        assertEquals("moep".toUpperCase(), columns.get(1).get(0));
    }

    @Test
    public void testDelete() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        List<GUID> keys = tt.keys().collect(Collectors.toList());
        tt.delete(keys.get(1), txn);
        txn.commit();

        keys.remove(1);

        txn = txnManager.beginTransaction();
        TempTable tt2 = dsFactory.loadTempTableFromId(tt.getId(), txn);

        AtomicInteger i = new AtomicInteger(0);
        tt2.keys()
                .forEach(guid -> {
                    assertTrue(keys.contains(guid));
                    i.getAndAdd(1);
                });

        assertEquals(2, i.get());
    }

    @Test
    public void testSaveLoad() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        TempTable tt2 = dsFactory.loadTempTableFromId(tt.getId(), txn);
        txn.commit();

        AtomicInteger i = new AtomicInteger(0);
        tt.keys()
                .forEach(guid -> {
                    assertNotNull(tt2.get(guid));
                    i.getAndAdd(1);
                });

        assertEquals(3, i.get());

        AtomicInteger i2 = new AtomicInteger(0);
        tt2.keys()
                .forEach(guid -> {
                    assertNotNull(tt.get(guid));
                    i2.getAndAdd(1);
                });

        assertEquals(3, i2.get());
    }

    private Table createDummyTable() throws IOException {
        Txn txn = txnManager.beginTransaction();

        Table.Builder tableBuilder = Table.Builder.newBuilder("narf_" + UUID.randomUUID().toString() + "_" + System.currentTimeMillis())
                .withColumn("tup_num", String.class)
                .withColumn("moep", String.class)
                .withColumn("foo", String.class);

        Table table = dsFactory.newTable(tableBuilder, txn);

        Tuple tup1 = new Tuple(3);
        tup1.put(0, "tup1_narf");
        tup1.put(1, "moep");<commit_msg>- add test for TempTables<commit_after>/*
 *
 *  Copyright 2017 <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.distbc.data.structures;

import com.google.inject.Inject;
import org.distbc.GuiceJUnit4Runner;
import org.distbc.GuiceModules;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(GuiceJUnit4Runner.class)
@GuiceModules({ DataStructureModule.class, TxnManagerModule.class })
public class GalaxyTempTableTest {
    @Inject
    private InternalDataStructureFactory dsFactory;

    @Inject
    private TxnManager txnManager;

    @Test
    public void testBasic() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        AtomicInteger i = new AtomicInteger(0);
        t.keys().forEach(guid -> {
            assertNotNull(tt.get(guid));
            assertEquals(guid, tt.get(guid).getGuid());
            assertEquals(t.get(guid), tt.get(guid));
            i.getAndAdd(1);
        });

        assertEquals(3, i.get());
    }

    @Test
    public void testRemoveColumnNames() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        tt.removeColumnWithName("foo".toUpperCase(), txn);
        txn.commit();

        List<Tuple> columns = tt.getColumnMetadata();
        assertEquals(2, columns.size());
        assertEquals("tup_num".toUpperCase(), columns.get(0).get(0));
        assertEquals("moep".toUpperCase(), columns.get(1).get(0));
    }

    @Test
    public void testDelete() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        List<GUID> keys = tt.keys().collect(Collectors.toList());
        tt.delete(keys.get(1), txn);
        txn.commit();

        keys.remove(1);

        txn = txnManager.beginTransaction();
        TempTable tt2 = dsFactory.loadTempTableFromId(tt.getId(), txn);

        AtomicInteger i = new AtomicInteger(0);
        tt2.keys()
                .forEach(guid -> {
                    assertTrue(keys.contains(guid));
                    i.getAndAdd(1);
                });

        assertEquals(2, i.get());
    }

    @Test
    public void testSaveLoad() throws IOException {
        Table t = createDummyTable();
        Txn txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(t, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        TempTable tt2 = dsFactory.loadTempTableFromId(tt.getId(), txn);
        txn.commit();

        AtomicInteger i = new AtomicInteger(0);
        tt.keys()
                .forEach(guid -> {
                    assertNotNull(tt2.get(guid));
                    i.getAndAdd(1);
                });

        assertEquals(3, i.get());

        AtomicInteger i2 = new AtomicInteger(0);
        tt2.keys()
                .forEach(guid -> {
                    assertNotNull(tt.get(guid));
                    i2.getAndAdd(1);
                });

        assertEquals(3, i2.get());
    }

    @Test
    public void testEmptyTable() throws IOException {
        Txn txn = txnManager.beginTransaction();

        Table.Builder tableBuilder = Table.Builder.newBuilder("narf_" + UUID.randomUUID().toString() + "_" + System.currentTimeMillis())
                .withColumn("tup_num", String.class)
                .withColumn("moep", String.class)
                .withColumn("foo", String.class);

        Table table = dsFactory.newTable(tableBuilder, txn);
        txn.commit();

        txn = txnManager.beginTransaction();
        TempTable tt = dsFactory.newTempTableFromTable(table, txn);
        txn.commit();

        AtomicInteger i = new AtomicInteger(0);
        tt.keys()
                .forEach(guid -> i.getAndAdd(1));

        assertEquals(0, i.get());
    }

    private Table createDummyTable() throws IOException {
        Txn txn = txnManager.beginTransaction();

        Table.Builder tableBuilder = Table.Builder.newBuilder("narf_" + UUID.randomUUID().toString() + "_" + System.currentTimeMillis())
                .withColumn("tup_num", String.class)
                .withColumn("moep", String.class)
                .withColumn("foo", String.class);

        Table table = dsFactory.newTable(tableBuilder, txn);

        Tuple tup1 = new Tuple(3);
        tup1.put(0, "tup1_narf");
        tup1.put(1, "moep");
{'fraction_non_alphanumeric': 0.1194883225118713, 'fraction_numerical': 0.008818683981005911, 'mean_word_length': 2.4735779198922923, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5632030', 'n_tokens_mistral': 3039, 'n_tokens_neox': 2948, 'n_words': 656}