Package org.h2.store

Examples of org.h2.store.Data


        rs.addRow(3, "Peace");
        testValue(ValueResultSet.get(rs));
    }

    private void testValue(Value v) {
        Data data = Data.create(null, 1024);
        data.checkCapacity((int) v.getPrecision());
        data.writeValue(v);
        data.writeInt(123);
        data.reset();
        Value v2 = data.readValue();
        assertEquals(v.getType(), v2.getType());
        assertEquals(0, v.compareTo(v2, compareMode));
        assertEquals(123, data.readInt());
    }
View Full Code Here


        assertEquals(123, data.readInt());
    }


    private void testAll() {
        Data page = Data.create(this, 128);

        char[] data = new char[0x10000];
        for (int i = 0; i < data.length; i++) {
            data[i] = (char) i;
        }
        String s = new String(data);
        page.checkCapacity(s.length() * 4);
        page.writeString(s);
        int len = page.length();
        assertEquals(len, Data.getStringLen(s));
        page.reset();
        assertEquals(s, page.readString());
        page.reset();

        page.writeString("H\u1111!");
        page.writeString("John\tBrack's \"how are you\" M\u1111ller");
        page.writeValue(ValueInt.get(10));
        page.writeValue(ValueString.get("test"));
        page.writeValue(ValueFloat.get(-2.25f));
        page.writeValue(ValueDouble.get(10.40));
        page.writeValue(ValueNull.INSTANCE);
        trace(new String(page.getBytes()));
        page.reset();

        trace(page.readString());
        trace(page.readString());
        trace(page.readValue().getInt());
        trace(page.readValue().getString());
        trace("" + page.readValue().getFloat());
        trace("" + page.readValue().getDouble());
        trace(page.readValue().toString());
        page.reset();

        page.writeInt(0);
        page.writeInt(Integer.MAX_VALUE);
        page.writeInt(Integer.MIN_VALUE);
        page.writeInt(1);
        page.writeInt(-1);
        page.writeInt(1234567890);
        page.writeInt(54321);
        trace(new String(page.getBytes()));
        page.reset();
        trace(page.readInt());
        trace(page.readInt());
        trace(page.readInt());
        trace(page.readInt());
        trace(page.readInt());
        trace(page.readInt());
        trace(page.readInt());

        page = null;
    }
View Full Code Here

        if (!SysProperties.STORE_LOCAL_TIME) {
            return;
        }
        TimeZone defaultTimeZone = TimeZone.getDefault();
        ArrayList<TimeZone> distinct = TestDate.getDistinctTimeZones();
        Data d = Data.create(null, 10240);
        try {
            for (TimeZone tz : distinct) {
                TimeZone.setDefault(tz);
                DateTimeUtils.resetCalendar();
                d.reset();
                for (int m = 1; m <= 12; m++) {
                    for (int h = 0; h <= 23; h++) {
                        if (h == 0 || h == 2 || h == 3) {
                            // those hours may not exist for all days in all
                            // timezones because of daylight saving
                            continue;
                        }
                        String s = "2000-" + (m < 10 ? "0" + m : m) + "-01 " + (h < 10 ? "0" + h : h) + ":00:00.0";
                        d.writeValue(ValueString.get(s));
                        d.writeValue(ValueTimestamp.get(Timestamp.valueOf(s)));
                    }
                }
                d.writeValue(ValueNull.INSTANCE);
                d.reset();
                for (TimeZone target : distinct) {
                    if ("Pacific/Kiritimati".equals(target)) {
                        // there is a problem with this time zone, but it seems
                        // unrelated to this database (possibly wrong timezone
                        // information?)
                        continue;
                    }
                    TimeZone.setDefault(target);
                    DateTimeUtils.resetCalendar();
                    while (true) {
                        Value v = d.readValue();
                        if (v == ValueNull.INSTANCE) {
                            break;
                        }
                        String a = v.getString();
                        String b = d.readValue().getString();
                        if (!a.equals(b)) {
                            assertEquals("source: " + tz.getID() + " target: " + target.getID(), a, b);
                        }
                    }
                }
View Full Code Here

        testValues();
        testAll();
    }

    private static void testPerformance() {
        Data data = Data.create(null, 1024);
        for (int j = 0; j < 4; j++) {
            long time = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                data.reset();
                for (int k = 0; k < 30; k++) {
                    data.writeString("Hello World");
                }
            }
            //            for (int i = 0; i < 5000000; i++) {
            //                data.reset();
            //                for (int k = 0; k < 100; k++) {
            //                    data.writeInt(k * k);
            //                }
            //            }
            //            for (int i = 0; i < 200000; i++) {
            //                data.reset();
            //                for (int k = 0; k < 100; k++) {
            //                    data.writeVarInt(k * k);
            //                }
            //            }
            System.out.println("write: " + (System.currentTimeMillis() - time) + " ms");
        }
        for (int j = 0; j < 4; j++) {
            long time = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
                data.reset();
                for (int k = 0; k < 30; k++) {
                    data.readString();
                }
            }
            //            for (int i = 0; i < 3000000; i++) {
            //                data.reset();
            //                for (int k = 0; k < 100; k++) {
View Full Code Here

            try {
                store.init();
            } catch (Exception e) {
                writeError(writer, e);
            }
            Data s = Data.create(this, 128);
            seek(0);
            store.readFully(s.getBytes(), 0, 128);
            s.setPos(48);
            pageSize = s.readInt();
            int writeVersion = s.readByte();
            int readVersion = s.readByte();
            writer.println("-- pageSize: " + pageSize +
                    " writeVersion: " + writeVersion +
                    " readVersion: " + readVersion);
            if (pageSize < PageStore.PAGE_SIZE_MIN || pageSize > PageStore.PAGE_SIZE_MAX) {
                pageSize = Constants.DEFAULT_PAGE_SIZE;
                writer.println("-- ERROR: page size; using " + pageSize);
            }
            long pageCount = length / pageSize;
            parents = new int[(int) pageCount];
            s = Data.create(this, pageSize);
            for (long i = 3; i < pageCount; i++) {
                s.reset();
                seek(i);
                store.readFully(s.getBytes(), 0, 32);
                s.readByte();
                s.readShortInt();
                parents[(int) i] = s.readInt();
            }
            int logKey = 0, logFirstTrunkPage = 0, logFirstDataPage = 0;
            s = Data.create(this, pageSize);
            for (long i = 1;; i++) {
                if (i == 3) {
                    break;
                }
                s.reset();
                seek(i);
                store.readFully(s.getBytes(), 0, pageSize);
                CRC32 crc = new CRC32();
                crc.update(s.getBytes(), 4, pageSize - 4);
                int expected = (int) crc.getValue();
                int got = s.readInt();
                long writeCounter = s.readLong();
                int key = s.readInt();
                int firstTrunkPage = s.readInt();
                int firstDataPage = s.readInt();
                if (expected == got) {
                    logKey = key;
                    logFirstTrunkPage = firstTrunkPage;
                    logFirstDataPage = firstDataPage;
                }
View Full Code Here

        }
        return "[" + type + "]";
    }

    private void dumpPageStore(PrintWriter writer, long pageCount) {
        Data s = Data.create(this, pageSize);
        for (long page = 3; page < pageCount; page++) {
            s = Data.create(this, pageSize);
            seek(page);
            store.readFully(s.getBytes(), 0, pageSize);
            dumpPage(writer, s, page, pageCount);
        }
    }
View Full Code Here

        }
    }

    private void dumpPageLogStream(PrintWriter writer, int logKey,
            int logFirstTrunkPage, int logFirstDataPage, long pageCount) throws IOException {
        Data s = Data.create(this, pageSize);
        DataReader in = new DataReader(
                new PageInputStream(writer, this, store, logKey, logFirstTrunkPage, logFirstDataPage, pageSize)
        );
        writer.println("---- Transaction log ----");
        CompressLZF compress = new CompressLZF();
        while (true) {
            int x = in.readByte();
            if (x < 0) {
                break;
            }
            if (x == PageLog.NOOP) {
                // ignore
            } else if (x == PageLog.UNDO) {
                int pageId = in.readVarInt();
                int size = in.readVarInt();
                byte[] data = new byte[pageSize];
                if (size == 0) {
                    in.readFully(data, 0, pageSize);
                } else if (size == 1) {
                    // empty
                } else {
                    byte[] compressBuffer = new byte[size];
                    in.readFully(compressBuffer, 0, size);
                    try {
                        compress.expand(compressBuffer, 0, size, data, 0, pageSize);
                    } catch (ArrayIndexOutOfBoundsException e) {
                        throw DbException.convertToIOException(e);
                    }
                }
                String typeName = "";
                int type = data[0];
                boolean last = (type & Page.FLAG_LAST) != 0;
                type &= ~Page.FLAG_LAST;
                switch (type) {
                case Page.TYPE_EMPTY:
                    typeName = "empty";
                    break;
                case Page.TYPE_DATA_LEAF:
                    typeName = "data leaf " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_DATA_NODE:
                    typeName = "data node " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_DATA_OVERFLOW:
                    typeName = "data overflow " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_BTREE_LEAF:
                    typeName = "b-tree leaf " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_BTREE_NODE:
                    typeName = "b-tree node " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_FREE_LIST:
                    typeName = "free list " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_STREAM_TRUNK:
                    typeName = "log trunk";
                    break;
                case Page.TYPE_STREAM_DATA:
                    typeName = "log data";
                    break;
                default:
                    typeName = "ERROR: unknown type " + type;
                    break;
                }
                writer.println("-- undo page " + pageId + " " + typeName);
                if (trace) {
                    Data d = Data.create(null, data);
                    dumpPage(writer, d, pageId, pageCount);
                }
            } else if (x == PageLog.ADD) {
                int sessionId = in.readVarInt();
                setStorage(in.readVarInt());
View Full Code Here

        stat.pageDataEmpty += empty;
        if (trace) {
            writer.println("--   empty: " + empty);
        }
        if (!last) {
            Data s2 = Data.create(this, pageSize);
            s.setPos(pageSize);
            long parent = pageId;
            while (true) {
                checkParent(writer, parent, new int[]{(int) next}, 0);
                parent = next;
                seek(next);
                store.readFully(s2.getBytes(), 0, pageSize);
                s2.reset();
                int type = s2.readByte();
                s2.readShortInt();
                s2.readInt();
                if (type == (Page.TYPE_DATA_OVERFLOW | Page.FLAG_LAST)) {
                    int size = s2.readShortInt();
                    writer.println("-- chain: " + next + " type: " + type + " size: " + size);
                    s.checkCapacity(size);
                    s.write(s2.getBytes(), s2.length(), size);
                    break;
                } else if (type == Page.TYPE_DATA_OVERFLOW) {
                    next = s2.readInt();
                    if (next == 0) {
                        writeDataError(writer, "next:0", s2.getBytes());
                        break;
                    }
                    int size = pageSize - s2.length();
                    writer.println("-- chain: " + next + " type: " + type + " size: " + size + " next: " + next);
                    s.checkCapacity(size);
                    s.write(s2.getBytes(), s2.length(), size);
                } else {
                    writeDataError(writer, "type: " + type, s2.getBytes());
                    break;
                }
            }
        }
        for (int i = 0; i < entryCount; i++) {
View Full Code Here

                int last = storedEntriesPos.size() - 1;
                long pos = storedEntriesPos.get(last);
                storedEntriesPos.remove(last);
                long end = file.length();
                int bufferLength = (int) (end - pos);
                Data buff = Data.create(database, bufferLength);
                file.seek(pos);
                file.readFully(buff.getBytes(), 0, bufferLength);
                while (buff.length() < bufferLength) {
                    UndoLogRecord e = UndoLogRecord.loadFromBuffer(buff, this);
                    records.add(e);
                    memoryUndo++;
                }
                storedEntries -= records.size();
View Full Code Here

                    String fileName = database.createTempFile();
                    file = database.openFile(fileName, "rw", false);
                    file.setCheckedWriting(false);
                    file.setLength(FileStore.HEADER_LENGTH);
                }
                Data buff = Data.create(database, Constants.DEFAULT_PAGE_SIZE);
                for (int i = 0; i < records.size(); i++) {
                    UndoLogRecord r = records.get(i);
                    buff.checkCapacity(Constants.DEFAULT_PAGE_SIZE);
                    r.append(buff, this);
                    if (i == records.size() - 1 || buff.length() > Constants.UNDO_BLOCK_SIZE) {
                        storedEntriesPos.add(file.getFilePointer());
                        file.write(buff.getBytes(), 0, buff.length());
                        buff.reset();
                    }
                }
                storedEntries += records.size();
                memoryUndo = 0;
                records.clear();
                file.autoDelete();
                return;
            }
        } else {
            if (!entry.isStored()) {
                memoryUndo++;
            }
            if (memoryUndo > database.getMaxMemoryUndo() && database.isPersistent() && !database.isMultiVersion()) {
                if (file == null) {
                    String fileName = database.createTempFile();
                    file = database.openFile(fileName, "rw", false);
                    file.setCheckedWriting(false);
                    file.seek(FileStore.HEADER_LENGTH);
                    rowBuff = Data.create(database, Constants.DEFAULT_PAGE_SIZE);
                    Data buff = rowBuff;
                    for (int i = 0; i < records.size(); i++) {
                        UndoLogRecord r = records.get(i);
                        saveIfPossible(r, buff);
                    }
                } else {
View Full Code Here

TOP

Related Classes of org.h2.store.Data

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.