Search in sources :

Example 1 with DirectByteCharSequence

use of com.questdb.std.str.DirectByteCharSequence in project questdb by bluestreak01.

the class UrlDecodeTest method testSingleQuote.

@Test
public void testSingleQuote() {
    String v = "x=%27a%27&y==b";
    long p = TestUtils.toMemory(v);
    try {
        int o = Misc.urlDecode(p, p + v.length(), map, pool);
        DirectByteCharSequence cs = new DirectByteCharSequence().of(p, p + v.length() - o);
        TestUtils.assertEquals("x='a'&y==b", cs);
        TestUtils.assertEquals("'a'", map.get("x"));
        TestUtils.assertEquals("b", map.get("y"));
    } finally {
        Unsafe.free(p, v.length());
    }
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) Test(org.junit.Test)

Example 2 with DirectByteCharSequence

use of com.questdb.std.str.DirectByteCharSequence in project questdb by bluestreak01.

the class RequestHeaderBuffer method parseContentType.

private void parseContentType() throws MalformedHeaderException {
    CharSequence seq = get("Content-Type");
    if (seq == null) {
        return;
    }
    long p = ((DirectByteCharSequence) seq).getLo();
    long _lo = p;
    long hi = ((DirectByteCharSequence) seq).getHi();
    DirectByteCharSequence name = null;
    boolean contentType = true;
    boolean swallowSpace = true;
    while (p <= hi) {
        char b = (char) Unsafe.getUnsafe().getByte(p++);
        if (b == ' ' && swallowSpace) {
            _lo = p;
            continue;
        }
        if (p > hi || b == ';') {
            if (contentType) {
                this.contentType = pool.next().of(_lo, p - 1);
                _lo = p;
                contentType = false;
                continue;
            }
            if (name == null) {
                LOG.error().$("Malformed content-type header").$();
                throw MalformedHeaderException.INSTANCE;
            }
            if (Chars.equals("encoding", name)) {
                // would be encoding, but we don't use it yet
                // this.encoding = pool.next().of(_lo, p - 1);
                _lo = p;
                continue;
            }
            if (Chars.equals("boundary", name)) {
                this.boundary = pool.next().of(_lo, p - 1);
                _lo = p;
                continue;
            }
            if (p > hi) {
                break;
            }
        } else if (b == '=') {
            name = name == null ? pool.next().of(_lo, p - 1) : name.of(_lo, p - 1);
            _lo = p;
            swallowSpace = false;
        }
    }
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence)

Example 3 with DirectByteCharSequence

use of com.questdb.std.str.DirectByteCharSequence in project questdb by bluestreak01.

the class RequestHeaderBuffer method parseContentDisposition.

private void parseContentDisposition() throws MalformedHeaderException {
    CharSequence contentDisposition = get("Content-Disposition");
    if (contentDisposition == null) {
        return;
    }
    long p = ((DirectByteCharSequence) contentDisposition).getLo();
    long _lo = p;
    long hi = ((DirectByteCharSequence) contentDisposition).getHi();
    boolean expectFormData = true;
    boolean swallowSpace = true;
    DirectByteCharSequence name = null;
    while (p <= hi) {
        char b = (char) Unsafe.getUnsafe().getByte(p++);
        if (b == ' ' && swallowSpace) {
            _lo = p;
            continue;
        }
        if (p > hi || b == ';') {
            if (expectFormData) {
                _lo = p;
                expectFormData = false;
                continue;
            }
            if (name == null) {
                LOG.error().$("Malformed content-disposition header").$();
                throw MalformedHeaderException.INSTANCE;
            }
            if (Chars.equals("name", name)) {
                this.contentDispositionName = unquote(pool.next().of(_lo, p - 1));
                swallowSpace = true;
                _lo = p;
                continue;
            }
            if (Chars.equals("filename", name)) {
                this.contentDispositionFilename = unquote(pool.next().of(_lo, p - 1));
                _lo = p;
                continue;
            }
            if (p > hi) {
                break;
            }
        } else if (b == '=') {
            name = name == null ? pool.next().of(_lo, p - 1) : name.of(_lo, p - 1);
            _lo = p;
            swallowSpace = false;
        }
    }
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence)

Example 4 with DirectByteCharSequence

use of com.questdb.std.str.DirectByteCharSequence in project questdb by bluestreak01.

the class PlainTextStoringParser method onFields.

@Override
public void onFields(int line, ObjList<DirectByteCharSequence> values, int hi) {
    boolean append = true;
    try {
        JournalEntryWriter w = writer.entryWriter();
        for (int i = 0; i < hi; i++) {
            if (values.getQuick(i).length() == 0) {
                continue;
            }
            try {
                DirectByteCharSequence charField;
                ImportedColumnMetadata m = metadata.getQuick(i);
                switch(m.importedColumnType) {
                    case ColumnType.STRING:
                        utf8Sink.clear();
                        charField = values.getQuick(i);
                        Chars.utf8Decode(charField.getLo(), charField.getHi(), utf8Sink);
                        w.putStr(i, (DirectBytes) utf8Sink);
                        break;
                    case ColumnType.DOUBLE:
                        w.putDouble(i, Numbers.parseDouble(values.getQuick(i)));
                        break;
                    case ColumnType.INT:
                        w.putInt(i, Numbers.parseInt(values.getQuick(i)));
                        break;
                    case ColumnType.FLOAT:
                        w.putFloat(i, Numbers.parseFloat(values.getQuick(i)));
                        break;
                    case ColumnType.DATE:
                        if (m.dateFormat != null && m.dateLocale != null) {
                            w.putDate(i, m.dateFormat.parse(values.getQuick(i), m.dateLocale));
                        } else {
                            throw NumericException.INSTANCE;
                        }
                        break;
                    case ColumnType.SYMBOL:
                        utf8Sink.clear();
                        charField = values.getQuick(i);
                        Chars.utf8Decode(charField.getLo(), charField.getHi(), utf8Sink);
                        w.putSym(i, utf8Sink);
                        break;
                    case ColumnType.LONG:
                        w.putLong(i, Numbers.parseLong(values.getQuick(i)));
                        break;
                    case ColumnType.BOOLEAN:
                        w.putBool(i, Chars.equalsIgnoreCase(values.getQuick(i), "true"));
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                switch(atomicity) {
                    case ATOMICITY_STRICT:
                        LOG.info().$("Error at (").$(line).$(',').$(i).$(')').$();
                        throw new JournalRuntimeException("Error on line: " + line + ", col: " + i);
                    default:
                        errors.increment(i);
                        LOG.debug().$("Error at (").$(line).$(',').$(i).$(") as ").$(metadata.getQuick(i).importedColumnType).$(": ").$(e.getMessage()).$();
                        append = false;
                }
                break;
            }
        }
        if (append) {
            w.append();
        }
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
}
Also used : ImportedColumnMetadata(com.questdb.parser.ImportedColumnMetadata) JournalException(com.questdb.std.ex.JournalException) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) JournalRuntimeException(com.questdb.common.JournalRuntimeException) JournalEntryWriter(com.questdb.store.JournalEntryWriter) ImportColumnCountException(com.questdb.ex.ImportColumnCountException) JournalException(com.questdb.std.ex.JournalException) NumericException(com.questdb.common.NumericException) JournalRuntimeException(com.questdb.common.JournalRuntimeException) ImportNameException(com.questdb.ex.ImportNameException)

Example 5 with DirectByteCharSequence

use of com.questdb.std.str.DirectByteCharSequence in project questdb by bluestreak01.

the class Misc method urlDecode.

public static int urlDecode(long lo, long hi, CharSequenceObjHashMap<CharSequence> map, ObjectPool<DirectByteCharSequence> pool) {
    long _lo = lo;
    long rp = lo;
    long wp = lo;
    final DirectByteCharSequence temp = pool.next();
    int offset = 0;
    CharSequence name = null;
    while (rp < hi) {
        char b = (char) Unsafe.getUnsafe().getByte(rp++);
        switch(b) {
            case '=':
                if (_lo < wp) {
                    name = pool.next().of(_lo, wp);
                }
                _lo = rp - offset;
                break;
            case '&':
                if (name != null) {
                    map.put(name, pool.next().of(_lo, wp));
                    name = null;
                } else if (_lo < wp) {
                    map.put(pool.next().of(_lo, wp), "");
                }
                _lo = rp - offset;
                break;
            case '+':
                Unsafe.getUnsafe().putByte(wp++, (byte) ' ');
                continue;
            case '%':
                try {
                    if (rp + 1 < hi) {
                        Unsafe.getUnsafe().putByte(wp++, (byte) Numbers.parseHexInt(temp.of(rp, rp += 2)));
                        offset += 2;
                        continue;
                    }
                } catch (NumericException ignore) {
                }
                name = null;
                break;
            default:
                break;
        }
        Unsafe.getUnsafe().putByte(wp++, (byte) b);
    }
    if (_lo < wp) {
        if (name != null) {
            map.put(name, pool.next().of(_lo, wp));
        } else {
            map.put(pool.next().of(_lo, wp), "");
        }
    }
    return offset;
}
Also used : DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) NumericException(com.questdb.common.NumericException)

Aggregations

DirectByteCharSequence (com.questdb.std.str.DirectByteCharSequence)12 Test (org.junit.Test)4 NumericException (com.questdb.common.NumericException)2 StringSink (com.questdb.std.str.StringSink)2 Ignore (org.junit.Ignore)2 JournalRuntimeException (com.questdb.common.JournalRuntimeException)1 ImportColumnCountException (com.questdb.ex.ImportColumnCountException)1 ImportNameException (com.questdb.ex.ImportNameException)1 ImportedColumnMetadata (com.questdb.parser.ImportedColumnMetadata)1 PlainTextLexer (com.questdb.parser.plaintext.PlainTextLexer)1 PlainTextParser (com.questdb.parser.plaintext.PlainTextParser)1 TypeProbe (com.questdb.parser.typeprobe.TypeProbe)1 JournalException (com.questdb.std.ex.JournalException)1 Path (com.questdb.std.str.Path)1 DateLocale (com.questdb.std.time.DateLocale)1 JournalEntryWriter (com.questdb.store.JournalEntryWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1