public long insertAfter(DocumentImpl doc, long address, byte[] value) {  boolean isOverflow = false; if (value.length + 4 > fileHeader.getWorkSize()) { OverflowDOMPage overflow = new OverflowDOMPage();  overflow.write(value); value = ByteConversion.longToByte(overflow.getPageNum()); isOverflow = true;
public long insertAfter(DocumentImpl doc, Value key, byte[] value) { try { final long p = findValue(key); if (p == KEY_NOT_FOUND) return -1; return insertAfter(doc, p, value); } catch (BTreeException e) { LOG.warn("key not found", e); } catch (IOException e) { LOG.warn("IO error", e);
    public long insertAfter(DocumentImpl doc, long address, byte[] value) {        //check if we need an overflow page        boolean isOverflow = false;        if (value.length + 4 > fileHeader.getWorkSize()) {            OverflowDOMPage overflow = new OverflowDOMPage();            //			LOG.debug("creating overflow page: " + overflow.getPageNum());            overflow.write(value);            value = ByteConversion.longToByte(overflow.getPageNum());            isOverflow = true;        }        // locate the node to insert after        RecordPos rec = findRecord(address);        if (rec == null) {            LOG.warn("page not found");            return -1;        }        short l = ByteConversion.byteToShort(rec.page.data, rec.offset);        if(ItemId.isRelocated(rec.tid))            rec.offset += 8;        if (l == OVERFLOW)            rec.offset += 10;        else            rec.offset = rec.offset + l + 2;        int dataLen = rec.page.getPageHeader().getDataLength();        // insert in the middle of the page?        if (rec.offset < dataLen) {            if (dataLen + value.length + 4 < fileHeader.getWorkSize()) {//                LOG//                        .debug("copying data in page " + rec.page.getPageNum() + "; " + rec.page.page.hashCode()//                                + "; offset = " + rec.offset + "; dataLen = "//                                + dataLen);                // new value fits into the page                int end = rec.offset + value.length + 4;                System.arraycopy(rec.page.data, rec.offset, rec.page.data, end,                        dataLen - rec.offset);                rec.page.len = dataLen + value.length + 4;                rec.page.getPageHeader().setDataLength(rec.page.len);            } else {                // doesn't fit: split the page                rec = splitDataPage(doc, rec);                if (rec.offset + value.length + 4 > fileHeader.getWorkSize()) {                    // still not enough free space: create a new page                    DOMPage newPage = new DOMPage();                    LOG.debug("creating additional page: " + newPage.getPageNum());                    newPage.getPageHeader().setNextDataPage(                            rec.page.getPageHeader().getNextDataPage());                    newPage.getPageHeader().setPrevDataPage(rec.page.getPageNum());                    rec.page.getPageHeader().setNextDataPage(                            newPage.getPageNum());                    rec.page.setDirty(true);                    dataCache.add(rec.page);                    rec.page = newPage;                    rec.offset = 0;                    rec.page.len = value.length + 4;                    rec.page.getPageHeader().setDataLength(rec.page.len);                    rec.page.getPageHeader().setRecordCount((short)1);                } else {                    rec.page.len = rec.offset + value.length + 4;                    rec.page.getPageHeader().setDataLength(rec.page.len);                    dataLen = rec.offset;                }            }        } else if (dataLen + value.length + 4 > fileHeader.getWorkSize()) {            // does value fit into page?            DOMPage newPage = new DOMPage();            LOG.debug("creating new page: " + newPage.getPageNum());            newPage.getPageHeader().setNextDataPage(                    rec.page.getPageHeader().getNextDataPage());            newPage.getPageHeader().setPrevDataPage(rec.page.getPageNum());            rec.page.getPageHeader().setNextDataPage(newPage.getPageNum());            rec.page.setDirty(true);            dataCache.add(rec.page);            rec.page = newPage;            rec.offset = 0;            rec.page.len = value.length + 4;            rec.page.getPageHeader().setDataLength(rec.page.len);        } else {            rec.page.len = dataLen + value.length + 4;            rec.page.getPageHeader().setDataLength(rec.page.len);        }        // write the data//        LOG.debug("inserting " + new String(value) + " to " + rec.page.page.getPageInfo() + "; " +//                rec.page.page.hashCode());        short tid = rec.page.getPageHeader().getNextTID();        ByteConversion.shortToByte((short) tid, rec.page.data, rec.offset);        rec.offset += 2;        ByteConversion.shortToByte(isOverflow ? 0 : (short) value.length,                rec.page.data, rec.offset);        rec.offset += 2;        System.arraycopy(value, 0, rec.page.data, rec.offset, value.length);        rec.offset += value.length;        rec.page.getPageHeader().incRecordCount();        rec.page.setDirty(true);        dataCache.add(rec.page);        return StorageAddress.createPointer((int) rec.page.getPageNum(), tid);    }