/** Insert a node into the index */ void insert(Session session, Row row, int offset) throws HsqlException { Node n = getRoot(session); Node x = n; boolean isleft = true; int compare = -1; while (true) { if (n == null) { if (x == null) { setRoot(session, row.getNode(offset)); return; } set(x, isleft, row.getNode(offset)); break; } compare = compareRowUnique(session, row, n.getRow()); if (compare == 0) { throw Trace.error(Trace.VIOLATION_OF_UNIQUE_INDEX, indexName.name); } isleft = compare < 0; x = n; n = child(x, isleft); } balance(session, x, isleft); }
/** * Advances to the next available value. * * <p> * * @return true if a next value is available upon exit * @throws HsqlException if a database access error occurs */ boolean next() throws HsqlException { nonJoinIsNull = false; isCurrentOuter = false; currentNode = filterIndex.next(currentNode); while (currentNode != null) { currentData = currentNode.getData(); currentRow = currentNode.getRow(); if (!(eEnd == null || eEnd.test(null))) { break; } if (eAnd == null || eAnd.test(null)) { return true; } currentNode = filterIndex.next(currentNode); } currentData = emptyData; currentRow = null; return false; }
/** * Returns the row for the last node of the index * * @return last row * @throws HsqlException */ Row lastRow(Session session) throws HsqlException { Node x = getRoot(session); Node l = x; while (l != null) { x = l; l = x.getRight(); } return x == null ? null : x.getRow(); }
/** * Finds the first row in the table (using an index if there is one) and checks it against the * eEnd (range) and eAnd (other conditions) Expression objects. (fredt) * * @return true if first row was found, else false */ boolean findFirst() throws HsqlException { nonJoinIsNull = false; isCurrentOuter = false; if (filterIndex == null) { filterIndex = filterTable.getPrimaryIndex(); } if (this.isMultiFindFirst) { Object[] data = filterTable.getNewRow(); int[] types = filterTable.getColumnTypes(); for (int i = 0; i < findFirstExpressions.length; i++) { Expression e = findFirstExpressions[i]; if (e != null) { data[i] = e.getValue(null, types[i]); } } currentNode = filterIndex.findFirst(data); } else if (eStart == null) { currentNode = eEnd == null ? filterIndex.first() : filterIndex.findFirstNotNull(); } else { int type = eStart.getArg().getDataType(); Object o = eStart.getArg2().getValue(null, type); currentNode = filterIndex.findFirst(o, eStart.getType()); } while (currentNode != null) { currentData = currentNode.getData(); currentRow = currentNode.getRow(); if (!(eEnd == null || eEnd.test(null))) { break; } if (eAnd == null || eAnd.test(null)) { return true; } currentNode = filterIndex.next(currentNode); } currentData = emptyData; currentRow = null; return false; }
/** * Search for the end of the maze using the frontier. Keep going until you find a solution or run * out of elements on the frontier. */ private boolean solve() { Node current = new Node(startRow, startCol, null); placesToGo.add(current); while (placesToGo.hasNext()) { current = placesToGo.next(); if (maze[current.getRow()][current.getCol()] == 'E') { return true; } neighbors(current); if (animate) { wait(100); toString(); } } return false; }
public Row next() { if (hasNext()) { try { Row row = nextnode.getRow(); nextnode = index.next(nextnode); return row; } catch (Exception e) { throw new NoSuchElementException(); } } else { return null; } }
public boolean neighbors(Node current) { try { if (maze[current.getRow() + 1][current.getCol()] == ' ') { placesToGo.add(new Node(current.getRow() + 1, current.getCol(), current)); } if (maze[current.getRow() - 1][current.getCol()] == ' ') { placesToGo.add(new Node(current.getRow() - 1, current.getCol(), current)); } if (maze[current.getRow()][current.getCol() + 1] == ' ') { placesToGo.add(new Node(current.getRow(), current.getCol() + 1, current)); } if (maze[current.getRow()][current.getCol() - 1] == ' ') { placesToGo.add(new Node(current.getRow(), current.getCol() - 1, current)); } return true; } catch (IndexOutOfBoundsException e) { return false; } }
/** * Find a node with matching data * * @param row row data * @return matching node * @throws HsqlException */ Node search(Session session, Row row) throws HsqlException { Object[] d = row.getData(); Node x = getRoot(session); while (x != null) { int c = compareRowUnique(session, row, x.getRow()); if (c == 0) { return x; } else if (c < 0) { x = x.getLeft(); } else { x = x.getRight(); } } return null; }
/** @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; }
public Row findRow(Session session, Row row) throws HsqlException { Node node = search(session, row); return node == null ? null : node.getRow(); }