/** * @param <T> * @param storagePath * absolute path to the file * @return a new instance * @throws IOException */ public static <T> RTree<T> loadFromDisk( String storagePath ) throws IOException { RTree<T> tree = null; File storageFile = new File( storagePath ); DataInputStream dis = new DataInputStream( new FileInputStream( storageFile ) ); float[] loadedBbox = loadBboxFromDisk( dis ); int loadedBigM = dis.readInt(); tree = new RTree<T>( loadedBbox, loadedBigM ); tree.root = tree.loadNodeFromDisk( dis ); dis.close(); return tree; }
/** * @throws IOException */ @SuppressWarnings("unchecked") private NodeEntry<T>[] loadNodeFromDisk( DataInputStream dis ) throws IOException { // the number of non-null entries in this node int n = dis.readInt(); NodeEntry<T>[] node = null; try { node = new NodeEntry[bigM + 1]; } catch ( OutOfMemoryError e ) { LOG.error( "Out of memory when reading rtree." ); throw new IOException( "out of memory" ); } for ( int i = 0; i < n; i++ ) { node[i] = new NodeEntry<T>(); node[i].bbox = loadBboxFromDisk( dis ); Long value = dis.readLong(); if ( Long.MIN_VALUE == value ) { node[i].entryValue = null; node[i].next = loadNodeFromDisk( dis ); } else { node[i].entryValue = (T) value; } } return node; }