Search in sources :

Example 11 with Page

use of org.h2.store.Page in project h2database by h2database.

the class WebThread method process.

@SuppressWarnings("unchecked")
private boolean process() throws IOException {
    boolean keepAlive = false;
    String head = readHeaderLine();
    if (head.startsWith("GET ") || head.startsWith("POST ")) {
        int begin = head.indexOf('/'), end = head.lastIndexOf(' ');
        String file;
        if (begin < 0 || end < begin) {
            file = "";
        } else {
            file = head.substring(begin + 1, end).trim();
        }
        trace(head + ": " + file);
        file = getAllowedFile(file);
        attributes = new Properties();
        int paramIndex = file.indexOf('?');
        session = null;
        if (paramIndex >= 0) {
            String attrib = file.substring(paramIndex + 1);
            parseAttributes(attrib);
            String sessionId = attributes.getProperty("jsessionid");
            file = file.substring(0, paramIndex);
            session = server.getSession(sessionId);
        }
        keepAlive = parseHeader();
        String hostAddr = socket.getInetAddress().getHostAddress();
        file = processRequest(file, hostAddr);
        if (file.length() == 0) {
            // asynchronous request
            return true;
        }
        String message;
        byte[] bytes;
        if (cache && ifModifiedSince != null && ifModifiedSince.equals(server.getStartDateTime())) {
            bytes = null;
            message = "HTTP/1.1 304 Not Modified\r\n";
        } else {
            bytes = server.getFile(file);
            if (bytes == null) {
                message = "HTTP/1.1 404 Not Found\r\n";
                bytes = ("File not found: " + file).getBytes(StandardCharsets.UTF_8);
                message += "Content-Length: " + bytes.length + "\r\n";
            } else {
                if (session != null && file.endsWith(".jsp")) {
                    String page = new String(bytes, StandardCharsets.UTF_8);
                    if (SysProperties.CONSOLE_STREAM) {
                        Iterator<String> it = (Iterator<String>) session.map.remove("chunks");
                        if (it != null) {
                            message = "HTTP/1.1 200 OK\r\n";
                            message += "Content-Type: " + mimeType + "\r\n";
                            message += "Cache-Control: no-cache\r\n";
                            message += "Transfer-Encoding: chunked\r\n";
                            message += "\r\n";
                            trace(message);
                            output.write(message.getBytes());
                            while (it.hasNext()) {
                                String s = it.next();
                                s = PageParser.parse(s, session.map);
                                bytes = s.getBytes(StandardCharsets.UTF_8);
                                if (bytes.length == 0) {
                                    continue;
                                }
                                output.write(Integer.toHexString(bytes.length).getBytes());
                                output.write("\r\n".getBytes());
                                output.write(bytes);
                                output.write("\r\n".getBytes());
                                output.flush();
                            }
                            output.write("0\r\n\r\n".getBytes());
                            output.flush();
                            return keepAlive;
                        }
                    }
                    page = PageParser.parse(page, session.map);
                    bytes = page.getBytes(StandardCharsets.UTF_8);
                }
                message = "HTTP/1.1 200 OK\r\n";
                message += "Content-Type: " + mimeType + "\r\n";
                if (!cache) {
                    message += "Cache-Control: no-cache\r\n";
                } else {
                    message += "Cache-Control: max-age=10\r\n";
                    message += "Last-Modified: " + server.getStartDateTime() + "\r\n";
                }
                message += "Content-Length: " + bytes.length + "\r\n";
            }
        }
        message += "\r\n";
        trace(message);
        output.write(message.getBytes());
        if (bytes != null) {
            output.write(bytes);
        }
        output.flush();
    }
    return keepAlive;
}
Also used : Iterator(java.util.Iterator) Properties(java.util.Properties) SysProperties(org.h2.engine.SysProperties)

Example 12 with Page

use of org.h2.store.Page in project h2database by h2database.

the class PageLog method removeUntil.

/**
 * Remove all pages until the given data page.
 *
 * @param trunkPage the first trunk page
 * @param firstDataPageToKeep the first data page to keep
 * @return the trunk page of the data page to keep
 */
private int removeUntil(int trunkPage, int firstDataPageToKeep) {
    trace.debug("log.removeUntil " + trunkPage + " " + firstDataPageToKeep);
    int last = trunkPage;
    while (true) {
        Page p = store.getPage(trunkPage);
        PageStreamTrunk t = (PageStreamTrunk) p;
        if (t == null) {
            throw DbException.throwInternalError("log.removeUntil not found: " + firstDataPageToKeep + " last " + last);
        }
        logKey = t.getLogKey();
        last = t.getPos();
        if (t.contains(firstDataPageToKeep)) {
            return last;
        }
        trunkPage = t.getNextTrunk();
        IntArray list = new IntArray();
        list.add(t.getPos());
        for (int i = 0; ; i++) {
            int next = t.getPageData(i);
            if (next == -1) {
                break;
            }
            list.add(next);
        }
        freeLogPages(list);
        pageOut.free(t);
    }
}
Also used : IntArray(org.h2.util.IntArray)

Example 13 with Page

use of org.h2.store.Page in project h2database by h2database.

the class PageStore method writeVariableHeader.

private void writeVariableHeader() {
    trace.debug("writeVariableHeader");
    if (logMode == LOG_MODE_SYNC) {
        file.sync();
    }
    Data page = createData();
    page.writeInt(0);
    page.writeLong(getWriteCountTotal());
    page.writeInt(logKey);
    page.writeInt(logFirstTrunkPage);
    page.writeInt(logFirstDataPage);
    CRC32 crc = new CRC32();
    crc.update(page.getBytes(), 4, pageSize - 4);
    page.setInt(0, (int) crc.getValue());
    file.seek(pageSize);
    file.write(page.getBytes(), 0, pageSize);
    file.seek(pageSize + pageSize);
    file.write(page.getBytes(), 0, pageSize);
// don't increment the write counter, because it was just written
}
Also used : CRC32(java.util.zip.CRC32) CreateTableData(org.h2.command.ddl.CreateTableData)

Example 14 with Page

use of org.h2.store.Page in project h2database by h2database.

the class PageStore method readVariableHeader.

private void readVariableHeader() {
    Data page = createData();
    for (int i = 1; ; i++) {
        if (i == 3) {
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, fileName);
        }
        page.reset();
        readPage(i, page);
        CRC32 crc = new CRC32();
        crc.update(page.getBytes(), 4, pageSize - 4);
        int expected = (int) crc.getValue();
        int got = page.readInt();
        if (expected == got) {
            writeCountBase = page.readLong();
            logKey = page.readInt();
            logFirstTrunkPage = page.readInt();
            logFirstDataPage = page.readInt();
            break;
        }
    }
}
Also used : CRC32(java.util.zip.CRC32) CreateTableData(org.h2.command.ddl.CreateTableData)

Example 15 with Page

use of org.h2.store.Page in project h2database by h2database.

the class PageStore method getPage.

/**
 * Read a page from the store.
 *
 * @param pageId the page id
 * @return the page
 */
public synchronized Page getPage(int pageId) {
    Page p = (Page) cache.get(pageId);
    if (p != null) {
        return p;
    }
    Data data = createData();
    readPage(pageId, data);
    int type = data.readByte();
    if (type == Page.TYPE_EMPTY) {
        return null;
    }
    data.readShortInt();
    data.readInt();
    if (!checksumTest(data.getBytes(), pageId, pageSize)) {
        throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "wrong checksum");
    }
    switch(type & ~Page.FLAG_LAST) {
        case Page.TYPE_FREE_LIST:
            p = PageFreeList.read(this, data, pageId);
            break;
        case Page.TYPE_DATA_LEAF:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageDataIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a data index " + indexId + " " + idx);
                }
                PageDataIndex index = (PageDataIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageDataLeaf.read(index, data, pageId);
                break;
            }
        case Page.TYPE_DATA_NODE:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageDataIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a data index " + indexId + " " + idx);
                }
                PageDataIndex index = (PageDataIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageDataNode.read(index, data, pageId);
                break;
            }
        case Page.TYPE_DATA_OVERFLOW:
            {
                p = PageDataOverflow.read(this, data, pageId);
                if (statistics != null) {
                    statisticsIncrement("overflow read");
                }
                break;
            }
        case Page.TYPE_BTREE_LEAF:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageBtreeIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a btree index " + indexId + " " + idx);
                }
                PageBtreeIndex index = (PageBtreeIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageBtreeLeaf.read(index, data, pageId);
                break;
            }
        case Page.TYPE_BTREE_NODE:
            {
                int indexId = data.readVarInt();
                PageIndex idx = metaObjects.get(indexId);
                if (idx == null) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "index not found " + indexId);
                }
                if (!(idx instanceof PageBtreeIndex)) {
                    throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "not a btree index " + indexId + " " + idx);
                }
                PageBtreeIndex index = (PageBtreeIndex) idx;
                if (statistics != null) {
                    statisticsIncrement(index.getTable().getName() + "." + index.getName() + " read");
                }
                p = PageBtreeNode.read(index, data, pageId);
                break;
            }
        case Page.TYPE_STREAM_TRUNK:
            p = PageStreamTrunk.read(this, data, pageId);
            break;
        case Page.TYPE_STREAM_DATA:
            p = PageStreamData.read(this, data, pageId);
            break;
        default:
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "page=" + pageId + " type=" + type);
    }
    cache.put(p);
    return p;
}
Also used : PageDataIndex(org.h2.index.PageDataIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) CreateTableData(org.h2.command.ddl.CreateTableData) PageIndex(org.h2.index.PageIndex)

Aggregations

CreateTableData (org.h2.command.ddl.CreateTableData)8 Page (org.h2.mvstore.Page)7 Data (org.h2.store.Data)7 Column (org.h2.table.Column)5 IndexColumn (org.h2.table.IndexColumn)5 Value (org.h2.value.Value)5 MVStore (org.h2.mvstore.MVStore)4 Row (org.h2.result.Row)4 SearchRow (org.h2.result.SearchRow)4 IOException (java.io.IOException)3 Connection (java.sql.Connection)3 CRC32 (java.util.zip.CRC32)3 PageBtreeIndex (org.h2.index.PageBtreeIndex)3 PageDataIndex (org.h2.index.PageDataIndex)3 PageIndex (org.h2.index.PageIndex)3 DbException (org.h2.message.DbException)3 Page (org.h2.store.Page)3 ValueString (org.h2.value.ValueString)3 PrintWriter (java.io.PrintWriter)2 ResultSet (java.sql.ResultSet)2