public int getSize(CachedRow r) throws SQLException { reset(); try { setSystemId(r.getTable().iColumnCount != r.getTable().iVisibleColumns); writeSize(0); writeData(r.getData(), r.getTable()); writePos(0); } catch (IOException e) { throw (Trace.error(Trace.FILE_IO_ERROR, e + "")); } int rowsize = size(); reset(); return rowsize; }
/** @todo fredt - use an upward estimate of number of rows based on Index */ int[] writeTableToDataFile(Table table, ScaledRAFile destFile) throws IOException, HsqlException { BinaryServerRowOutput rowOut = new BinaryServerRowOutput(); // rowOut.setSystemId(true); DoubleIntTable pointerLookup = new DoubleIntTable(1000000); int[] rootsArray = table.getIndexRootsArray(); Index index = table.getPrimaryIndex(); // erik long pos = destFile.getFilePointer() / cacheFileScale; long pos = destFile.getFilePointer(); int[] pointerPair = new int[2]; int count = 0; Trace.printSystemOut("lookup begins: " + stopw.elapsedTime()); for (Node n = index.first(); n != null; count++) { CachedRow row = (CachedRow) n.getRow(); pointerLookup.add(row.iPos, (int) pos); // erik pos += row.storageSize / cacheFileScale; pos += row.storageSize; if (count % 50000 == 0) { // System.gc(); Trace.printSystemOut( "pointer pair for row " + count + " " + pointerPair[0] + " " + pointerPair[1]); } n = index.next(n); } Trace.printSystemOut(table.getName().name + " list done ", stopw.elapsedTime()); count = 0; for (Node n = index.first(); n != null; count++) { CachedRow row = (CachedRow) n.getRow(); // erik int rowPointer = (int) destFile.getFilePointer() / cacheFileScale; int rowPointer = (int) destFile.getFilePointer(); rowOut.reset(); // code should be moved to CachedRow.java rowOut.writeSize(row.storageSize); Node rownode = row.nPrimaryNode; while (rownode != null) { ((DiskNode) rownode).writeTranslate(rowOut, pointerLookup); rownode = rownode.nNext; } rowOut.writeData(row.getData(), row.getTable()); rowOut.writePos(rowPointer); // end destFile.write(rowOut.getOutputStream().getBuffer(), 0, rowOut.size()); if ((count) % 50000 == 0) { Trace.printSystemOut(count + " rows " + stopw.elapsedTime()); } n = index.next(n); } for (int i = 0; i < rootsArray.length; i++) { int lookupIndex = pointerLookup.find(0, rootsArray[i]); if (lookupIndex == -1) { throw Trace.error(Trace.DataFileDefrag_writeTableToDataFile); } rootsArray[i] = pointerLookup.get(lookupIndex, 1); } Trace.printSystemOut(table.getName().name, " : table converted"); return rootsArray; }
protected void setStorageSize(CachedRow r) throws HsqlException { r.storageSize = rowOut.getSize(r); }