@Override public String toString() { return "[" + getPos() + "] stream data key:" + logKey + " pos:" + data.length() + " remaining:" + remaining; }
private Data getBuffer() { if (writeBuffer.length() == 0) { return writeBuffer; } return store.createData(); }
private void write(Data data) { pageOut.write(data.getBytes(), 0, data.length()); data.reset(); }
private void writeData() { if (written) { return; } check(); writeHead(); data.writeInt(childPageIds[entryCount]); for (int i = 0; i < entryCount; i++) { data.writeInt(childPageIds[i]); data.writeVarLong(keys[i]); } if (length != data.length()) { DbException.throwInternalError("expected pos: " + length + " got: " + data.length()); } written = true; }
/** * Load an undo log record row using a buffer. * * @param buff the buffer * @param log the log * @return the undo log record */ static UndoLogRecord loadFromBuffer(Data buff, UndoLog log) { UndoLogRecord rec = new UndoLogRecord(null, (short) 0, null); int pos = buff.length(); int len = buff.readInt() * Constants.FILE_BLOCK_SIZE; rec.load(buff, log); buff.setPos(pos + len); return rec; }
/** * Save the row in the file using a buffer. * * @param buff the buffer * @param file the file * @param log the undo log */ void save(Data buff, FileStore file, UndoLog log) { buff.reset(); append(buff, log); filePos = (int) (file.getFilePointer() / Constants.FILE_BLOCK_SIZE); file.write(buff.getBytes(), 0, buff.length()); row = null; state = STORED; }
/** * Write the header data. */ void initWrite() { data = store.createData(); data.writeByte((byte) Page.TYPE_STREAM_DATA); data.writeShortInt(0); data.writeInt(trunk); data.writeInt(logKey); remaining = store.getPageSize() - data.length(); }
/** * Read the page. */ private void read() { data.reset(); type = data.readByte(); data.readShortInt(); parentPageId = data.readInt(); if (type == (Page.TYPE_DATA_OVERFLOW | Page.FLAG_LAST)) { size = data.readShortInt(); nextPage = 0; } else if (type == Page.TYPE_DATA_OVERFLOW) { nextPage = data.readInt(); size = store.getPageSize() - data.length(); } else { throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "page:" + getPos() + " type:" + type); } start = data.length(); }
/** * Create a new page. * * @param index the index * @param pageId the page id * @param parentPageId the parent * @return the page */ static PageDataNode create(PageDataIndex index, int pageId, int parentPageId) { PageDataNode p = new PageDataNode(index, pageId, index.getPageStore().createData()); index.getPageStore().logUndo(p, null); p.parentPageId = parentPageId; p.writeHead(); // 4 bytes for the rightmost child page id p.length = p.data.length() + 4; return p; }
/** * Create a new page. * * @param index the index * @param pageId the page id * @param parentPageId the parent * @return the page */ static PageBtreeLeaf create(PageBtreeIndex index, int pageId, int parentPageId) { PageBtreeLeaf p = new PageBtreeLeaf(index, pageId, index.getPageStore() .createData()); index.getPageStore().logUndo(p, null); p.rows = SearchRow.EMPTY_ARRAY; p.parentPageId = parentPageId; p.writeHead(); p.start = p.data.length(); return p; }
/** * Create a new b-tree node page. * * @param index the index * @param pageId the page id * @param parentPageId the parent page id * @return the page */ static PageBtreeNode create(PageBtreeIndex index, int pageId, int parentPageId) { PageBtreeNode p = new PageBtreeNode(index, pageId, index.getPageStore() .createData()); index.getPageStore().logUndo(p, null); p.parentPageId = parentPageId; p.writeHead(); // 4 bytes for the rightmost child page id p.start = p.data.length() + 4; p.rows = SearchRow.EMPTY_ARRAY; if (p.pageStoreInternalCount) { p.rowCount = 0; } return p; }
private void writeAllRows() { if (file == null) { Database db = session.getDatabase(); String fileName = db.createTempFile(); file = db.openFile(fileName, "rw", false); file.setCheckedWriting(false); file.seek(FileStore.HEADER_LENGTH); rowBuff = Data.create(db, Constants.DEFAULT_PAGE_SIZE); file.seek(FileStore.HEADER_LENGTH); } Data buff = rowBuff; initBuffer(buff); for (int i = 0, size = list.size(); i < size; i++) { if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) { flushBuffer(buff); initBuffer(buff); } Row r = list.get(i); writeRow(buff, r); } flushBuffer(buff); file.autoDelete(); list.clear(); memory = 0; }
private void writeHead() { data.reset(); data.writeByte((byte) Page.TYPE_DATA_NODE); data.writeShortInt(0); if (SysProperties.CHECK2) { if (data.length() != START_PARENT) { DbException.throwInternalError(); } } data.writeInt(parentPageId); data.writeVarInt(index.getId()); data.writeInt(rowCountStored); data.writeShortInt(entryCount); }
/** * Create a new page. * * @param index the index * @param pageId the page id * @param parentPageId the parent * @return the page */ static PageDataLeaf create(PageDataIndex index, int pageId, int parentPageId) { PageDataLeaf p = new PageDataLeaf(index, pageId, index.getPageStore() .createData()); index.getPageStore().logUndo(p, null); p.rows = Row.EMPTY_ARRAY; p.parentPageId = parentPageId; p.columnCount = index.getTable().getColumns().length; p.writeHead(); p.start = p.data.length(); return p; }
private void writeHead() { data.reset(); int type; if (firstOverflowPageId == 0) { type = Page.TYPE_DATA_LEAF | Page.FLAG_LAST; } else { type = Page.TYPE_DATA_LEAF; } data.writeByte((byte) type); data.writeShortInt(0); if (SysProperties.CHECK2) { if (data.length() != START_PARENT) { DbException.throwInternalError(); } } data.writeInt(parentPageId); data.writeVarInt(index.getId()); data.writeVarInt(columnCount); data.writeShortInt(entryCount); }
@Override public void write(byte[] buff, int off, int len) { if (len > 0) { page.reset(); if (compress != null) { if (off != 0 || len != buff.length) { buff = Arrays.copyOfRange(buff, off, off + len); off = 0; } int uncompressed = len; buff = compress.compress(buff, compressionAlgorithm); len = buff.length; page.checkCapacity(2 * Data.LENGTH_INT + len); page.writeInt(len); page.writeInt(uncompressed); page.write(buff, off, len); } else { page.checkCapacity(Data.LENGTH_INT + len); page.writeInt(len); page.write(buff, off, len); } page.fillAligned(); store.write(page.getBytes(), 0, page.length()); } }
/** * Append the row to the buffer. * * @param buff the buffer * @param log the undo log */ void append(Data buff, UndoLog log) { int p = buff.length(); buff.writeInt(0); buff.writeInt(operation); buff.writeByte(row.isDeleted() ? (byte) 1 : (byte) 0); buff.writeInt(log.getTableId(table)); buff.writeLong(row.getKey()); buff.writeInt(row.getSessionId()); int count = row.getColumnCount(); buff.writeInt(count); for (int i = 0; i < count; i++) { Value v = row.getValue(i); buff.checkCapacity(buff.getValueLen(v)); buff.writeValue(v); } buff.fillAligned(); buff.setInt(p, (buff.length() - p) / Constants.FILE_BLOCK_SIZE); }
private void read() { data.reset(); int type = data.readByte(); data.readShortInt(); this.parentPageId = data.readInt(); onlyPosition = (type & Page.FLAG_LAST) == 0; int indexId = data.readVarInt(); if (indexId != index.getId()) { throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "page:" + getPos() + " expected index:" + index.getId() + "got:" + indexId); } entryCount = data.readShortInt(); offsets = new int[entryCount]; rows = new SearchRow[entryCount]; for (int i = 0; i < entryCount; i++) { offsets[i] = data.readShortInt(); } start = data.length(); written = true; writtenData = true; }
private void read() { data.reset(); data.readByte(); data.readShortInt(); this.parentPageId = data.readInt(); int indexId = data.readVarInt(); if (indexId != index.getId()) { throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "page:" + getPos() + " expected index:" + index.getId() + "got:" + indexId); } rowCount = rowCountStored = data.readInt(); entryCount = data.readShortInt(); childPageIds = new int[entryCount + 1]; childPageIds[entryCount] = data.readInt(); keys = Utils.newLongArray(entryCount); for (int i = 0; i < entryCount; i++) { childPageIds[i] = data.readInt(); keys[i] = data.readVarLong(); } length = data.length(); check(); written = true; }