⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystempagestore.java

📁 shape file read and write
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                if (type == B_SHORT) {
                    def.addField(Short.class);
                } else if (type == B_INTEGER) {
                    def.addField(Integer.class);
                } else if (type == B_LONG) {
                    def.addField(Long.class);
                } else if (type == B_FLOAT) {
                    def.addField(Float.class);
                } else if (type == B_DOUBLE) {
                    def.addField(Double.class);
                }

                this.header.getInt(); // Not used
            }
        }

        this.params.setDataDef(def);

        this.rootOffset = this.header.getLong();
    }

    /**
     * DOCUMENT ME!
     * 
     * @throws IOException
     * @throws TreeException
     */
    private void prepareIndex() throws IOException, TreeException {
        if (this.params.getDataDef() == null) {
            throw new TreeException("Data definition cannot be null "
                    + "when creating a new index.");
        }

        Charset charset = Charset.forName("US-ASCII");
        ByteBuffer chBuf = charset.encode(this.params.getDataDef().getCharset()
                .name());
        chBuf.position(0);

        int headerSize = 22 + chBuf.capacity()
                + (5 * this.params.getDataDef().getFieldsCount()) + 8;

        this.header = ByteBuffer.allocate(headerSize);

        this.header.putInt(FILE_VERSION);
        this.header.putInt(headerSize);
        this.header.putInt(this.params.getMaxNodeEntries());
        this.header.putInt(this.params.getMinNodeEntries());
        this.header.putShort(this.params.getSplitAlg());

        // Store data definition
        this.header.putInt(chBuf.capacity());
        this.header.put(chBuf);

        Field field = null;
        Class clazz = null;

        for (int i = 0; i < this.params.getDataDef().getFieldsCount(); i++) {
            field = this.params.getDataDef().getField(i);
            clazz = field.getFieldClass();

            if (clazz.isAssignableFrom(Short.class)) {
                this.header.put(B_SHORT);
            } else if (clazz.isAssignableFrom(Integer.class)) {
                this.header.put(B_INTEGER);
            } else if (clazz.isAssignableFrom(Long.class)) {
                this.header.put(B_LONG);
            } else if (clazz.isAssignableFrom(Float.class)) {
                this.header.put(B_FLOAT);
            } else if (clazz.isAssignableFrom(Double.class)) {
                this.header.put(B_DOUBLE);
            } else if (clazz.isAssignableFrom(String.class)) {
                this.header.put(B_STRING);
            }

            this.header.putInt(field.getLen());
        }

        this.header.putLong(headerSize); // The initial root offset

        this.header.position(0);
        this.channel.write(this.header);

        if (this.params.getForceChannel()) {
            this.channel.force(true);
        }

        // Create the root
        FileSystemNode root = new FileSystemNode(this.params);
        root.setLeaf(true);
        root.save();

        this.rootOffset = root.getOffset();
    }

    /**
     * @see java.lang.Object#finalize()
     */
    protected void finalize() throws Throwable {
        this.close();
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getRoot()
     */
    public Node getRoot() {
        Node ret = null;

        try {
            ret = this.params.getFromCache(this.rootOffset);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
        }

        return ret;

        // return this.root;
    }

    /**
     * @see org.geotools.index.rtree.PageStore#setRoot(org.geotools.index.rtree.Node)
     */
    public void setRoot(Node node) throws TreeException {
        try {
            FileSystemNode n = (FileSystemNode) node;
            n.setParent(null);
            this.rootOffset = n.getOffset();

            this.header.position(this.header.limit() - 8);
            this.header.putLong(n.getOffset());

            this.header.position(0);

            synchronized (this.channel) {
                this.channel.position(0);
                this.channel.write(this.header);

                if (this.params.getForceChannel()) {
                    this.channel.force(true);
                }
            }
        } catch (IOException e) {
            throw new TreeException(e);
        }
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getEmptyNode(boolean)
     */
    public Node getEmptyNode(boolean isLeaf) {
        FileSystemNode node = new FileSystemNode(params);
        node.setLeaf(isLeaf);

        return node;
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getNode(org.geotools.index.rtree.Entry,
     *      org.geotools.index.rtree.Node)
     */
    public Node getNode(Entry parentEntry, Node parent) throws TreeException {
        Node node = null;
        long offset = ((Long) parentEntry.getData()).longValue();

        try {
            node = this.params.getFromCache(offset);
            node.setParent(parent);
        } catch (IOException e) {
            throw new TreeException(e);
        }

        return node;
    }

    /**
     * @see org.geotools.index.rtree.PageStore#createEntryPointingNode(org.geotools.index.rtree.Node)
     */
    public Entry createEntryPointingNode(Node node) {
        FileSystemNode fn = (FileSystemNode) node;

        return new Entry(new Envelope(fn.getBounds()), new Long(fn.getOffset()));
    }

    /**
     * @see org.geotools.index.rtree.PageStore#free(org.geotools.index.rtree.Node)
     */
    public void free(Node node) {
        try {
            FileSystemNode fn = (FileSystemNode) node;
            fn.free();
        } catch (IOException e) {
            // Ignore
        }
    }

    /**
     * @see org.geotools.index.rtree.PageStore#close()
     */
    public void close() throws TreeException {
        try {
            this.params.flushCache();

            this.header.position(0);

            synchronized (this.channel) {
                this.channel.position(0);
                this.channel.write(this.header);
                this.channel.force(true);
            }

            this.raFile.close();
        } catch (IOException e) {
            throw new TreeException(e);
        }
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getMaxNodeEntries()
     */
    public int getMaxNodeEntries() {
        return this.params.getMaxNodeEntries();
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getMinNodeEntries()
     */
    public int getMinNodeEntries() {
        return this.params.getMinNodeEntries();
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getSplitAlgorithm()
     */
    public short getSplitAlgorithm() {
        return this.params.getSplitAlg();
    }

    /**
     * @see org.geotools.index.rtree.PageStore#getKeyDefinition()
     */
    public DataDefinition getDataDefinition() {
        return this.params.getDataDef();
    }

    /**
     * If this is set to <code>true</code>, then every write to the index
     * will call a force() on the associated channel
     * 
     * @param b
     *                true or false
     */
    public void setForceChannel(boolean b) {
        this.params.setForceChannel(b);
    }

    /**
     * DOCUMENT ME!
     * 
     * @return The state of Force channel parameter
     */
    public boolean getForceChannel() {
        return this.params.getForceChannel();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -