Search in sources :

Example 1 with DateLocale

use of com.questdb.std.time.DateLocale in project questdb by bluestreak01.

the class TypeProbeCollectionTest method testTypeProbeCollectionInstantiation.

@Test
public void testTypeProbeCollectionInstantiation() throws Exception {
    String path = this.getClass().getResource("/date_test.formats").getFile();
    if (Os.type == Os.WINDOWS && path.startsWith("/")) {
        path = path.substring(1);
    }
    TypeProbeCollection typeProbeCollection = new TypeProbeCollection(path, new DateFormatFactory(), DateLocaleFactory.INSTANCE);
    Assert.assertEquals(7, typeProbeCollection.getProbeCount());
    Assert.assertTrue(typeProbeCollection.getProbe(0) instanceof IntProbe);
    Assert.assertTrue(typeProbeCollection.getProbe(1) instanceof LongProbe);
    Assert.assertTrue(typeProbeCollection.getProbe(2) instanceof DoubleProbe);
    Assert.assertTrue(typeProbeCollection.getProbe(3) instanceof BooleanProbe);
    Assert.assertTrue(typeProbeCollection.getProbe(4) instanceof DateProbe);
    Assert.assertTrue(typeProbeCollection.getProbe(5) instanceof DateProbe);
    Assert.assertTrue(typeProbeCollection.getProbe(6) instanceof DateProbe);
    DateLocale defaultLocale = DateLocaleFactory.INSTANCE.getDefaultDateLocale();
    Assert.assertEquals("dd/MM/y", typeProbeCollection.getProbe(4).getFormat());
    Assert.assertEquals(defaultLocale.getId(), typeProbeCollection.getProbe(4).getDateLocale().getId());
    Assert.assertEquals("yyyy-MM-dd HH:mm:ss", typeProbeCollection.getProbe(5).getFormat());
    Assert.assertEquals("es-PA", typeProbeCollection.getProbe(5).getDateLocale().getId());
    Assert.assertEquals("MM/dd/y", typeProbeCollection.getProbe(6).getFormat());
    Assert.assertEquals(defaultLocale.getId(), typeProbeCollection.getProbe(6).getDateLocale().getId());
}
Also used : DateLocale(com.questdb.std.time.DateLocale) DateFormatFactory(com.questdb.std.time.DateFormatFactory) Test(org.junit.Test)

Example 2 with DateLocale

use of com.questdb.std.time.DateLocale in project questdb by bluestreak01.

the class TypeProbeCollection method parseFile.

private void parseFile(CharSequence fileName, DateFormatFactory dateFormatFactory, DateLocaleFactory dateLocaleFactory) throws IOException {
    final DirectByteCharSequence dbcs = new DirectByteCharSequence();
    try (Path path = new Path().of(fileName).$()) {
        long fd = Files.openRO(path);
        if (fd < 0) {
            throw new IOException("Cannot open " + fileName + " [errno=" + Os.errno() + ']');
        }
        long sz = Files.length(fd);
        long buf = Unsafe.malloc(sz);
        try {
            Files.read(fd, buf, sz, 0);
            long p = buf;
            long hi = p + sz;
            long _lo = p;
            boolean newline = true;
            boolean comment = false;
            boolean quote = false;
            boolean space = true;
            String pattern = null;
            while (p < hi) {
                char b = (char) Unsafe.getUnsafe().getByte(p++);
                switch(b) {
                    case '#':
                        comment = newline;
                        break;
                    case '\'':
                        // inside comment, ignore
                        if (comment) {
                            continue;
                        }
                        if (quote) {
                            // we were inside quote, close out and check which part to assign result to
                            if (pattern == null) {
                                pattern = dbcs.of(_lo, p - 1).toString();
                                _lo = p;
                                space = true;
                                quote = false;
                            } else {
                                // pattern has been assigned, should never end up here
                                LOG.error().$("Internal error").$();
                            }
                        } else if (newline) {
                            // only start quote if it is at beginning of line
                            _lo = p;
                            quote = true;
                        }
                        break;
                    case ' ':
                    case '\t':
                        if (comment || quote) {
                            continue;
                        }
                        if (space) {
                            _lo = p;
                            continue;
                        }
                        space = true;
                        newline = false;
                        String s = dbcs.of(_lo, p - 1).toString();
                        if (pattern == null) {
                            pattern = s;
                            _lo = p;
                            space = true;
                        } else {
                            DateLocale locale = dateLocaleFactory.getDateLocale(s);
                            if (locale == null) {
                                LOG.error().$("Unknown date locale: ").$(s).$();
                                // skip rest of line
                                comment = true;
                                continue;
                            }
                            probes.add(new DateProbe(dateFormatFactory, locale, pattern));
                        }
                        break;
                    case '\n':
                    case '\r':
                        if (!comment) {
                            if (_lo < p - 1) {
                                s = dbcs.of(_lo, p - 1).toString();
                                if (pattern == null) {
                                    // no date locale, use default
                                    probes.add(new DateProbe(dateFormatFactory, dateLocaleFactory.getDefaultDateLocale(), s));
                                } else {
                                    DateLocale locale = dateLocaleFactory.getDateLocale(s);
                                    if (locale == null) {
                                        LOG.error().$("Unknown date locale: ").$(s).$();
                                    } else {
                                        probes.add(new DateProbe(dateFormatFactory, locale, pattern));
                                    }
                                }
                            } else if (pattern != null) {
                                probes.add(new DateProbe(dateFormatFactory, dateLocaleFactory.getDefaultDateLocale(), pattern));
                            }
                        }
                        newline = true;
                        comment = false;
                        quote = false;
                        pattern = null;
                        space = false;
                        _lo = p;
                        break;
                    default:
                        if (newline) {
                            newline = false;
                        }
                        if (space) {
                            space = false;
                        }
                        break;
                }
            }
        } finally {
            Unsafe.free(buf, sz);
        }
    }
}
Also used : Path(com.questdb.std.str.Path) DateLocale(com.questdb.std.time.DateLocale) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) IOException(java.io.IOException)

Aggregations

DateLocale (com.questdb.std.time.DateLocale)2 DirectByteCharSequence (com.questdb.std.str.DirectByteCharSequence)1 Path (com.questdb.std.str.Path)1 DateFormatFactory (com.questdb.std.time.DateFormatFactory)1 IOException (java.io.IOException)1 Test (org.junit.Test)1