Search in sources :

Example 86 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TestStreams method testLZFStreams.

private void testLZFStreams() throws IOException {
    Random random = new Random(1);
    int max = getSize(100, 1000);
    for (int i = 0; i < max; i += 3) {
        byte[] buffer = getRandomBytes(random);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        LZFOutputStream comp = new LZFOutputStream(out);
        if (random.nextInt(10) == 1) {
            comp.write(buffer);
        } else {
            for (int j = 0; j < buffer.length; ) {
                int[] sizes = { 0, 1, random.nextInt(100), random.nextInt(100000) };
                int size = sizes[random.nextInt(sizes.length)];
                size = Math.min(size, buffer.length - j);
                if (size == 1) {
                    comp.write(buffer[j]);
                } else {
                    comp.write(buffer, j, size);
                }
                j += size;
            }
        }
        comp.close();
        byte[] compressed = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(compressed);
        LZFInputStream decompress = new LZFInputStream(in);
        byte[] test = new byte[buffer.length];
        for (int j = 0; j < buffer.length; ) {
            int[] sizes = { 0, 1, random.nextInt(100), random.nextInt(100000) };
            int size = sizes[random.nextInt(sizes.length)];
            if (size == 1) {
                int x = decompress.read();
                if (x < 0) {
                    break;
                }
                test[j++] = (byte) x;
            } else {
                size = Math.min(size, test.length - j);
                int l = decompress.read(test, j, size);
                if (l < 0) {
                    break;
                }
                j += l;
            }
        }
        decompress.close();
        assertEquals(buffer, test);
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) LZFInputStream(org.h2.compress.LZFInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LZFOutputStream(org.h2.compress.LZFOutputStream)

Example 87 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TestCompress method testVariableEnd.

private void testVariableEnd() {
    CompressTool utils = CompressTool.getInstance();
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < 90; i++) {
        b.append('0');
    }
    String prefix = b.toString();
    for (int i = 0; i < 100; i++) {
        b = new StringBuilder(prefix);
        for (int j = 0; j < i; j++) {
            b.append((char) ('1' + j));
        }
        String test = b.toString();
        byte[] in = test.getBytes();
        assertEquals(in, utils.expand(utils.compress(in, "LZF")));
    }
}
Also used : CompressTool(org.h2.tools.CompressTool)

Example 88 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class TestDate method testDateTimeUtils.

private void testDateTimeUtils() {
    ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16");
    ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16");
    ValueTime t1 = (ValueTime) ts1.convertTo(Value.TIME);
    ValueTime t2 = (ValueTime) ts2.convertTo(Value.TIME);
    ValueDate d1 = (ValueDate) ts1.convertTo(Value.DATE);
    ValueDate d2 = (ValueDate) ts2.convertTo(Value.DATE);
    assertEquals("-999-08-07 13:14:15.16", ts1.getString());
    assertEquals("-999-08-07", d1.getString());
    assertEquals("13:14:15.16", t1.getString());
    assertEquals("19999-08-07 13:14:15.16", ts2.getString());
    assertEquals("19999-08-07", d2.getString());
    assertEquals("13:14:15.16", t2.getString());
    ValueTimestamp ts1a = DateTimeUtils.convertTimestamp(ts1.getTimestamp(), DateTimeUtils.createGregorianCalendar());
    ValueTimestamp ts2a = DateTimeUtils.convertTimestamp(ts2.getTimestamp(), DateTimeUtils.createGregorianCalendar());
    assertEquals("-999-08-07 13:14:15.16", ts1a.getString());
    assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
    // test for bug on Java 1.8.0_60 in "Europe/Moscow" timezone.
    // Doesn't affect most other timezones
    long millis = 1407437460000L;
    long result1 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis));
    long result2 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis));
    assertEquals(result1, result2);
}
Also used : ValueTime(org.h2.value.ValueTime) ValueTimestamp(org.h2.value.ValueTimestamp) ValueDate(org.h2.value.ValueDate)

Example 89 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class H2Database method create.

/**
 * Create a new in-memory database.
 *
 * @param factory the cursor factory
 * @return a connection to this database
 */
public static H2Database create(H2Database.CursorFactory factory) {
    ConnectionInfo ci = new ConnectionInfo("mem:");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 90 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.

the class Railroads method map.

private void map(String key, ResultSet rs, boolean railroads) throws Exception {
    ArrayList<HashMap<String, String>> list;
    list = new ArrayList<>();
    while (rs.next()) {
        HashMap<String, String> map = new HashMap<>();
        ResultSetMetaData meta = rs.getMetaData();
        for (int i = 0; i < meta.getColumnCount(); i++) {
            String k = StringUtils.toLowerEnglish(meta.getColumnLabel(i + 1));
            String value = rs.getString(i + 1);
            value = value.trim();
            map.put(k, PageParser.escapeHtml(value));
        }
        String topic = rs.getString("TOPIC");
        String syntax = rs.getString("SYNTAX").trim();
        if (railroads) {
            BnfRailroad r = new BnfRailroad();
            String railroad = r.getHtml(bnf, syntax);
            map.put("railroad", railroad);
        }
        BnfSyntax visitor = new BnfSyntax();
        String syntaxHtml = visitor.getHtml(bnf, syntax);
        map.put("syntax", syntaxHtml);
        // remove newlines in the regular text
        String text = map.get("text");
        if (text != null) {
            // text is enclosed in <p> .. </p> so this works.
            text = StringUtils.replaceAll(text, "<br /><br />", "</p><p>");
            text = StringUtils.replaceAll(text, "<br />", " ");
            map.put("text", text);
        }
        String link = topic.toLowerCase();
        link = link.replace(' ', '_');
        // link = StringUtils.replaceAll(link, "_", "");
        link = link.replace('@', '_');
        map.put("link", StringUtils.urlEncode(link));
        list.add(map);
    }
    session.put(key, list);
    int div = 3;
    int part = (list.size() + div - 1) / div;
    for (int i = 0, start = 0; i < div; i++, start += part) {
        List<HashMap<String, String>> listThird = list.subList(start, Math.min(start + part, list.size()));
        session.put(key + "-" + i, listThird);
    }
    rs.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap) BnfRailroad(org.h2.build.doc.BnfRailroad) BnfSyntax(org.h2.build.doc.BnfSyntax)

Aggregations

SQLException (java.sql.SQLException)63 Connection (java.sql.Connection)59 DbException (org.h2.message.DbException)56 PreparedStatement (java.sql.PreparedStatement)54 ResultSet (java.sql.ResultSet)47 Statement (java.sql.Statement)44 Value (org.h2.value.Value)40 IOException (java.io.IOException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)30 InputStream (java.io.InputStream)29 Column (org.h2.table.Column)24 ArrayList (java.util.ArrayList)23 SimpleResultSet (org.h2.tools.SimpleResultSet)23 Random (java.util.Random)19 Expression (org.h2.expression.Expression)18 JdbcConnection (org.h2.jdbc.JdbcConnection)18 Index (org.h2.index.Index)16 ValueString (org.h2.value.ValueString)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)15