Search in sources :

Example 51 with JournalEntryWriter

use of com.questdb.store.JournalEntryWriter in project questdb by bluestreak01.

the class DDLTests method testCast.

public void testCast(int from, int to) throws Exception {
    int n = 100;
    try (JournalWriter w1 = compiler.createWriter(getFactory(), "create table y (a " + ColumnType.nameOf(from) + ") record hint 100")) {
        Rnd rnd = new Rnd();
        for (int i = 0; i < n; i++) {
            JournalEntryWriter ew = w1.entryWriter();
            switch(from) {
                case ColumnType.INT:
                    ew.putInt(0, rnd.nextInt());
                    break;
                case ColumnType.LONG:
                    ew.putLong(0, rnd.nextLong());
                    break;
                case ColumnType.DATE:
                    ew.putDate(0, rnd.nextLong());
                    break;
                case ColumnType.BYTE:
                    ew.put(0, rnd.nextByte());
                    break;
                case ColumnType.SHORT:
                    ew.putShort(0, rnd.nextShort());
                    break;
                case ColumnType.FLOAT:
                    ew.putFloat(0, rnd.nextFloat());
                    break;
                case ColumnType.DOUBLE:
                    ew.putDouble(0, rnd.nextDouble());
                    break;
                case ColumnType.SYMBOL:
                    ew.putSym(0, rnd.nextChars(10));
                    break;
                case ColumnType.STRING:
                    ew.putStr(0, rnd.nextChars(10));
                    break;
                default:
                    break;
            }
            ew.append();
        }
        w1.commit();
    }
    exec("create table x as (y), cast(a as " + ColumnType.nameOf(to) + ") record hint 100");
    try (RecordSource rs = compiler.compile(getFactory(), "x")) {
        Rnd rnd = new Rnd();
        Assert.assertEquals(to, rs.getMetadata().getColumnQuick(0).getType());
        RecordCursor cursor = rs.prepareCursor(getFactory());
        try {
            while (cursor.hasNext()) {
                switch(from) {
                    case ColumnType.INT:
                        switch(to) {
                            case ColumnType.SHORT:
                                Assert.assertEquals((short) rnd.nextInt(), cursor.next().getShort(0));
                                break;
                            case ColumnType.LONG:
                                Assert.assertEquals((long) rnd.nextInt(), cursor.next().getLong(0));
                                break;
                            case ColumnType.BYTE:
                                Assert.assertEquals((byte) rnd.nextInt(), cursor.next().getByte(0));
                                break;
                            case ColumnType.FLOAT:
                                Assert.assertEquals((float) rnd.nextInt(), cursor.next().getFloat(0), 0.000000001f);
                                break;
                            case ColumnType.DOUBLE:
                                Assert.assertEquals((double) rnd.nextInt(), cursor.next().getDouble(0), 0.000000001);
                                break;
                            case ColumnType.DATE:
                                Assert.assertEquals((long) rnd.nextInt(), cursor.next().getDate(0));
                                break;
                            case ColumnType.INT:
                                Assert.assertEquals(rnd.nextInt(), cursor.next().getInt(0));
                                break;
                            default:
                                break;
                        }
                        break;
                    case ColumnType.LONG:
                    case ColumnType.DATE:
                        switch(to) {
                            case ColumnType.SHORT:
                                Assert.assertEquals((short) rnd.nextLong(), cursor.next().getShort(0));
                                break;
                            case ColumnType.LONG:
                                Assert.assertEquals(rnd.nextLong(), cursor.next().getLong(0));
                                break;
                            case ColumnType.BYTE:
                                Assert.assertEquals((byte) rnd.nextLong(), cursor.next().getByte(0));
                                break;
                            case ColumnType.FLOAT:
                                Assert.assertEquals((float) rnd.nextLong(), cursor.next().getFloat(0), 0.000000001f);
                                break;
                            case ColumnType.DOUBLE:
                                Assert.assertEquals((double) rnd.nextLong(), cursor.next().getDouble(0), 0.000000001);
                                break;
                            case ColumnType.DATE:
                                Assert.assertEquals(rnd.nextLong(), cursor.next().getDate(0));
                                break;
                            case ColumnType.INT:
                                Assert.assertEquals((int) rnd.nextLong(), cursor.next().getInt(0));
                                break;
                            default:
                                break;
                        }
                        break;
                    case ColumnType.BYTE:
                        switch(to) {
                            case ColumnType.SHORT:
                                Assert.assertEquals((short) rnd.nextByte(), cursor.next().getShort(0));
                                break;
                            case ColumnType.LONG:
                                Assert.assertEquals((long) rnd.nextByte(), cursor.next().getLong(0));
                                break;
                            case ColumnType.BYTE:
                                Assert.assertEquals(rnd.nextByte(), cursor.next().getByte(0));
                                break;
                            case ColumnType.FLOAT:
                                Assert.assertEquals((float) rnd.nextByte(), cursor.next().getFloat(0), 0.000000001f);
                                break;
                            case ColumnType.DOUBLE:
                                Assert.assertEquals((double) rnd.nextByte(), cursor.next().getDouble(0), 0.000000001);
                                break;
                            case ColumnType.DATE:
                                Assert.assertEquals((long) rnd.nextByte(), cursor.next().getDate(0));
                                break;
                            case ColumnType.INT:
                                Assert.assertEquals((int) rnd.nextByte(), cursor.next().getInt(0));
                                break;
                            default:
                                break;
                        }
                        break;
                    case ColumnType.SHORT:
                        switch(to) {
                            case ColumnType.SHORT:
                                Assert.assertEquals(rnd.nextShort(), cursor.next().getShort(0));
                                break;
                            case ColumnType.LONG:
                                Assert.assertEquals((long) rnd.nextShort(), cursor.next().getLong(0));
                                break;
                            case ColumnType.BYTE:
                                Assert.assertEquals((byte) rnd.nextShort(), cursor.next().getByte(0));
                                break;
                            case ColumnType.FLOAT:
                                Assert.assertEquals((float) rnd.nextShort(), cursor.next().getFloat(0), 0.000000001f);
                                break;
                            case ColumnType.DOUBLE:
                                Assert.assertEquals((double) rnd.nextShort(), cursor.next().getDouble(0), 0.000000001);
                                break;
                            case ColumnType.DATE:
                                Assert.assertEquals((long) rnd.nextShort(), cursor.next().getDate(0));
                                break;
                            case ColumnType.INT:
                                Assert.assertEquals((int) rnd.nextShort(), cursor.next().getInt(0));
                                break;
                            default:
                                break;
                        }
                        break;
                    case ColumnType.FLOAT:
                        switch(to) {
                            case ColumnType.SHORT:
                                Assert.assertEquals((short) rnd.nextFloat(), cursor.next().getShort(0));
                                break;
                            case ColumnType.LONG:
                                Assert.assertEquals((long) rnd.nextFloat(), cursor.next().getLong(0));
                                break;
                            case ColumnType.BYTE:
                                Assert.assertEquals((byte) rnd.nextFloat(), cursor.next().getByte(0));
                                break;
                            case ColumnType.FLOAT:
                                Assert.assertEquals(rnd.nextFloat(), cursor.next().getFloat(0), 0.000000001f);
                                break;
                            case ColumnType.DOUBLE:
                                Assert.assertEquals((double) rnd.nextFloat(), cursor.next().getDouble(0), 0.000000001);
                                break;
                            case ColumnType.DATE:
                                Assert.assertEquals((long) rnd.nextFloat(), cursor.next().getDate(0));
                                break;
                            case ColumnType.INT:
                                Assert.assertEquals((int) rnd.nextFloat(), cursor.next().getInt(0));
                                break;
                            default:
                                break;
                        }
                        break;
                    case ColumnType.DOUBLE:
                        switch(to) {
                            case ColumnType.SHORT:
                                Assert.assertEquals((short) rnd.nextDouble(), cursor.next().getShort(0));
                                break;
                            case ColumnType.LONG:
                                Assert.assertEquals((long) rnd.nextDouble(), cursor.next().getLong(0));
                                break;
                            case ColumnType.BYTE:
                                Assert.assertEquals((byte) rnd.nextDouble(), cursor.next().getByte(0));
                                break;
                            case ColumnType.FLOAT:
                                Assert.assertEquals((float) rnd.nextDouble(), cursor.next().getFloat(0), 0.000000001f);
                                break;
                            case ColumnType.DOUBLE:
                                Assert.assertEquals(rnd.nextDouble(), cursor.next().getDouble(0), 0.000000001);
                                break;
                            case ColumnType.DATE:
                                Assert.assertEquals((long) rnd.nextDouble(), cursor.next().getDate(0));
                                break;
                            case ColumnType.INT:
                                Assert.assertEquals((int) rnd.nextDouble(), cursor.next().getInt(0));
                                break;
                            default:
                                break;
                        }
                        break;
                    case ColumnType.STRING:
                        switch(to) {
                            case ColumnType.SYMBOL:
                                TestUtils.assertEquals(rnd.nextChars(10), cursor.next().getSym(0));
                                break;
                            default:
                                TestUtils.assertEquals(rnd.nextChars(10), cursor.next().getFlyweightStr(0));
                                break;
                        }
                        break;
                    case ColumnType.SYMBOL:
                        switch(to) {
                            case ColumnType.STRING:
                                TestUtils.assertEquals(rnd.nextChars(10), cursor.next().getFlyweightStr(0));
                                break;
                            default:
                                TestUtils.assertEquals(rnd.nextChars(10), cursor.next().getSym(0));
                                break;
                        }
                        break;
                    default:
                        break;
                }
            }
        } finally {
            cursor.releaseCursor();
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) RecordCursor(com.questdb.common.RecordCursor) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Example 52 with JournalEntryWriter

use of com.questdb.store.JournalEntryWriter in project questdb by bluestreak01.

the class OperatorTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    // this does thread local allocations that
    // should not be accounted for while
    // measuring query allocations and de-allocations
    FACTORY_CONTAINER.getFactory().getConfiguration().exists("");
    try (JournalWriter w = FACTORY_CONTAINER.getFactory().writer(new JournalStructure("abc").$int("i").$double("d").$float("f").$byte("b").$long("l").$str("str").$bool("boo").$sym("sym").$short("sho").$date("date").$ts().$())) {
        int n = 1000;
        String[] sym = { "AX", "XX", "BZ", "KK" };
        Rnd rnd = new Rnd();
        long t = Dates.toMillis(2016, 5, 1, 10, 20);
        long d = Dates.toMillis(2016, 5, 1, 10, 20);
        for (int i = 0; i < n; i++) {
            JournalEntryWriter ew = w.entryWriter(t += 60000);
            ew.putInt(0, (rnd.nextPositiveInt() & 15) == 0 ? Numbers.INT_NaN : rnd.nextInt());
            ew.putDouble(1, (rnd.nextPositiveInt() & 15) == 0 ? Double.NaN : rnd.nextDouble());
            ew.putFloat(2, (rnd.nextPositiveInt() & 15) == 0 ? Float.NaN : rnd.nextFloat());
            ew.put(3, (byte) rnd.nextInt());
            ew.putLong(4, (rnd.nextPositiveInt() & 15) == 0 ? Numbers.LONG_NaN : rnd.nextLong());
            ew.putStr(5, (rnd.nextPositiveInt() & 15) == 0 ? null : sym[rnd.nextPositiveInt() % sym.length]);
            ew.putBool(6, rnd.nextBoolean());
            ew.putSym(7, (rnd.nextPositiveInt() & 15) == 0 ? null : sym[rnd.nextPositiveInt() % sym.length]);
            ew.putShort(8, (short) rnd.nextInt());
            d += 45000;
            ew.putDate(9, (rnd.nextPositiveInt() & 15) == 0 ? Numbers.LONG_NaN : d);
            ew.append();
        }
        w.commit();
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter) BeforeClass(org.junit.BeforeClass)

Example 53 with JournalEntryWriter

use of com.questdb.store.JournalEntryWriter in project questdb by bluestreak01.

the class RenameJournalTest method create.

private void create(String name) throws JournalException, ParserException {
    try (JournalWriter w = compiler.createWriter(getFactory(), "create table " + name + "(a int) record hint 100")) {
        JournalEntryWriter ew = w.entryWriter();
        ew.putInt(0, 999);
        ew.append();
        w.commit();
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Example 54 with JournalEntryWriter

use of com.questdb.store.JournalEntryWriter in project questdb by bluestreak01.

the class SymbolNullQueryTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    int tradeCount = 100;
    int quoteCount = 300;
    JournalWriter trades = FACTORY_CONTAINER.getFactory().writer(new JournalStructure("trades").$int("quoteId").$sym("tag1").$double("amount").recordCountHint(tradeCount).$ts());
    JournalWriter quotes = FACTORY_CONTAINER.getFactory().writer(new JournalStructure("quotes").$int("quoteId").$sym("tag").$double("rate").recordCountHint(quoteCount).$ts());
    int tsIncrementMax = 10000;
    long timestamp = DateFormatUtils.parseDateTime("2015-03-23T00:00:00.000Z");
    Rnd rnd = new Rnd();
    ObjList<String> tags = new ObjList<>();
    for (int i = 0; i < 500; i++) {
        tags.add(rnd.nextBoolean() ? rnd.nextString(rnd.nextInt() & 15) : null);
    }
    for (int i = 0; i < quoteCount; i++) {
        JournalEntryWriter w = quotes.entryWriter();
        w.putInt(0, i);
        w.putSym(1, tags.getQuick(rnd.nextPositiveInt() % tags.size()));
        w.putDouble(2, rnd.nextDouble());
        w.putDate(3, timestamp += rnd.nextPositiveInt() % tsIncrementMax);
        w.append();
    }
    quotes.commit();
    timestamp = DateFormatUtils.parseDateTime("2015-03-23T00:00:00.000Z");
    for (int i = 0; i < tradeCount; i++) {
        JournalEntryWriter w = trades.entryWriter();
        w.putInt(0, rnd.nextPositiveInt() % quoteCount);
        w.putSym(1, tags.getQuick(rnd.nextPositiveInt() % tags.size()));
        w.putDouble(2, rnd.nextDouble());
        w.putDate(3, timestamp += rnd.nextPositiveInt() % tsIncrementMax);
        w.append();
    }
    quotes.close();
    trades.close();
}
Also used : JournalWriter(com.questdb.store.JournalWriter) ObjList(com.questdb.std.ObjList) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter) BeforeClass(org.junit.BeforeClass)

Example 55 with JournalEntryWriter

use of com.questdb.store.JournalEntryWriter in project questdb by bluestreak01.

the class GenericBinaryTest method testUnclosedOutputOutput.

@Test
public void testUnclosedOutputOutput() throws Exception {
    try (JournalWriter writer = getGenericWriter()) {
        List<byte[]> expected = getBytes();
        for (int i = 0; i < expected.size(); i++) {
            JournalEntryWriter w = writer.entryWriter();
            w.putBin(0).write(expected.get(i));
            w.append();
        }
        writer.commit();
        assertEquals(expected, readOutputStream());
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalEntryWriter(com.questdb.store.JournalEntryWriter) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

JournalEntryWriter (com.questdb.store.JournalEntryWriter)67 JournalWriter (com.questdb.store.JournalWriter)59 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)47 Rnd (com.questdb.std.Rnd)43 Test (org.junit.Test)31 AbstractTest (com.questdb.test.tools.AbstractTest)28 BeforeClass (org.junit.BeforeClass)10 RecordCursor (com.questdb.common.RecordCursor)5 Record (com.questdb.common.Record)4 RecordSource (com.questdb.ql.RecordSource)4 StringSink (com.questdb.std.str.StringSink)4 BootstrapEnv (com.questdb.BootstrapEnv)3 ServerConfiguration (com.questdb.ServerConfiguration)3 ObjHashSet (com.questdb.std.ObjHashSet)3 Factory (com.questdb.store.factory.Factory)3 AbstractOptimiserTest (com.questdb.parser.sql.AbstractOptimiserTest)2 DoubleRecordSourceColumn (com.questdb.ql.ops.col.DoubleRecordSourceColumn)2 DoubleConstant (com.questdb.ql.ops.constant.DoubleConstant)2 AddDoubleOperator (com.questdb.ql.ops.plus.AddDoubleOperator)2 VirtualColumnRecordSource (com.questdb.ql.virtual.VirtualColumnRecordSource)2