Search in sources :

Example 11 with IntList

use of com.questdb.std.IntList in project questdb by bluestreak01.

the class PlainTextDelimiterLexer method of.

@SuppressWarnings("ConstantConditions")
public void of(long address, int len) {
    long lim = address + len;
    long p = address;
    boolean suspended = false;
    int line = 0;
    int comma = 0;
    int pipe = 0;
    int tab = 0;
    int semicolon = 0;
    // previous values
    int _comma = 0;
    int _pipe = 0;
    int _tab = 0;
    int _semicolon = 0;
    commas.clear();
    pipes.clear();
    tabs.clear();
    semicolons.clear();
    this.avgRecLen = 0;
    this.stdDev = Double.POSITIVE_INFINITY;
    this.delimiter = 0;
    boolean eol = false;
    while (p < lim && line < maxLines) {
        char b = (char) Unsafe.getUnsafe().getByte(p++);
        if (suspended && b != '"') {
            continue;
        }
        switch(b) {
            case ',':
                comma++;
                if (eol) {
                    eol = false;
                }
                break;
            case '|':
                pipe++;
                if (eol) {
                    eol = false;
                }
                break;
            case '\t':
                tab++;
                if (eol) {
                    eol = false;
                }
                break;
            case ';':
                semicolon++;
                if (eol) {
                    eol = false;
                }
                break;
            case '"':
                suspended = !suspended;
                if (eol) {
                    eol = false;
                }
                break;
            case '\n':
                if (eol) {
                    break;
                }
                line++;
                commas.add(comma - _comma);
                pipes.add(pipe - _pipe);
                tabs.add(tab - _tab);
                semicolons.add(semicolon - _semicolon);
                _comma = comma;
                _pipe = pipe;
                _tab = tab;
                _semicolon = semicolon;
                eol = true;
                break;
            default:
                if (eol) {
                    eol = false;
                }
                break;
        }
    }
    if (line == 0) {
        return;
    }
    this.avgRecLen = len / line;
    heap.clear();
    heap.add(CSV, comma);
    heap.add(PIPE, pipe);
    heap.add(TAB, tab);
    heap.add(';', semicolon);
    this.delimiter = (char) heap.peekBottom();
    IntList test;
    switch(delimiter) {
        case CSV:
            test = commas;
            break;
        case PIPE:
            test = pipes;
            break;
        case TAB:
            test = tabs;
            break;
        case ';':
            test = semicolons;
            break;
        default:
            throw new IllegalArgumentException("Unsupported delimiter: " + delimiter);
    }
    // compute variance on test delimiter
    double temp;
    int n = test.size();
    if (n == 0) {
        delimiter = 0;
        return;
    }
    temp = 0;
    for (int i = 0; i < n; i++) {
        temp += test.getQuick(i);
    }
    double mean = temp / n;
    temp = 0;
    for (int i = 0; i < n; i++) {
        int v = test.getQuick(i);
        temp += (mean - v) * (mean - v);
    }
    this.stdDev = Math.sqrt(temp / n);
}
Also used : IntList(com.questdb.std.IntList)

Aggregations

IntList (com.questdb.std.IntList)11 AbstractOptimiserTest (com.questdb.parser.sql.AbstractOptimiserTest)6 Test (org.junit.Test)6 BytecodeAssembler (com.questdb.std.BytecodeAssembler)4 Album (com.questdb.model.Album)3 Band (com.questdb.model.Band)3 HashJoinRecordSource (com.questdb.ql.join.HashJoinRecordSource)3 RecordKeyCopierCompiler (com.questdb.ql.map.RecordKeyCopierCompiler)3 SelectedColumnsRecordSource (com.questdb.ql.select.SelectedColumnsRecordSource)3 StringSink (com.questdb.std.str.StringSink)3 RecordSource (com.questdb.ql.RecordSource)2 JournalEntryWriter (com.questdb.store.JournalEntryWriter)2 JournalWriter (com.questdb.store.JournalWriter)2 Record (com.questdb.common.Record)1 RecordCursor (com.questdb.common.RecordCursor)1 LongList (com.questdb.std.LongList)1 JournalException (com.questdb.std.ex.JournalException)1 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)1