multiindex.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,855 行 · 第 1/5 页

JAVA
1,855
字号
         *         * @param transactionId the id of the transaction that is committed.         */        Commit(long transactionId) {            super(transactionId, Action.TYPE_COMMIT);        }        /**         * Creates a new Commit action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param arguments     ignored by this method.         * @return the Commit action.         */        static Commit fromString(long transactionId, String arguments) {            return new Commit(transactionId);        }        /**         * Touches the last flush time (sets it to the current time).         *         * @inheritDoc         */        public void execute(MultiIndex index) throws IOException {            index.lastFlushTime = System.currentTimeMillis();        }        /**         * @inheritDoc         */        public String toString() {            return Long.toString(getTransactionId()) + ' ' + Action.COMMIT;        }    }    /**     * Creates an new sub index but does not add it to the active persistent index     * list.     */    private static class CreateIndex extends Action {        /**         * The name of the index to add.         */        private String indexName;        /**         * Indicates if the index is forced to be created. That is, existing         * index data is deleted.         */        private final boolean create;        /**         * Creates a new CreateIndex action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param indexName     the name of the index to add, or <code>null</code>         *                      if an index with a new name should be created.         * @param create        if <code>true</code> existing index data is         *                      overwritten.         */        CreateIndex(long transactionId, String indexName, boolean create) {            super(transactionId, Action.TYPE_CREATE_INDEX);            this.indexName = indexName;            this.create = create;        }        /**         * Creates a new CreateIndex action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param arguments     the name of the index to create.         * @return the AddIndex action.         * @throws IllegalArgumentException if the arguments are malformed.         */        static CreateIndex fromString(long transactionId, String arguments) {            // when created from String, this action is executed as redo action            // -> don't create index, simply open it.            return new CreateIndex(transactionId, arguments, false);        }        /**         * Creates a new index.         *         * @inheritDoc         */        public void execute(MultiIndex index) throws IOException {            PersistentIndex idx = index.getOrCreateIndex(indexName, create);            indexName = idx.getName();        }        /**         * @inheritDoc         */        public void undo(MultiIndex index) throws IOException {            if (index.hasIndex(indexName)) {                PersistentIndex idx = index.getOrCreateIndex(indexName, false);                idx.close();                index.deleteIndex(idx);            }        }        /**         * @inheritDoc         */        public String toString() {            StringBuffer logLine = new StringBuffer();            logLine.append(Long.toString(getTransactionId()));            logLine.append(' ');            logLine.append(Action.CREATE_INDEX);            logLine.append(' ');            logLine.append(indexName);            return logLine.toString();        }        /**         * Returns the index name that has been created. If this method is called         * before {@link #execute(MultiIndex)} it will return <code>null</code>.         *         * @return the name of the index that has been created.         */        String getIndexName() {            return indexName;        }    }    /**     * Closes and deletes an index that is no longer in use.     */    private static class DeleteIndex extends Action {        /**         * The name of the index to add.         */        private String indexName;        /**         * Creates a new DeleteIndex action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param indexName     the name of the index to delete.         */        DeleteIndex(long transactionId, String indexName) {            super(transactionId, Action.TYPE_DELETE_INDEX);            this.indexName = indexName;        }        /**         * Creates a new DeleteIndex action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param arguments     the name of the index to delete.         * @return the DeleteIndex action.         * @throws IllegalArgumentException if the arguments are malformed.         */        static DeleteIndex fromString(long transactionId, String arguments) {            return new DeleteIndex(transactionId, arguments);        }        /**         * Removes a sub index from <code>index</code>.         *         * @inheritDoc         */        public void execute(MultiIndex index) throws IOException {            // get index if it exists            for (Iterator it = index.indexes.iterator(); it.hasNext();) {                PersistentIndex idx = (PersistentIndex) it.next();                if (idx.getName().equals(indexName)) {                    idx.close();                    index.deleteIndex(idx);                    break;                }            }        }        /**         * @inheritDoc         */        public String toString() {            StringBuffer logLine = new StringBuffer();            logLine.append(Long.toString(getTransactionId()));            logLine.append(' ');            logLine.append(Action.DELETE_INDEX);            logLine.append(' ');            logLine.append(indexName);            return logLine.toString();        }    }    /**     * Deletes a node from the index.     */    private static class DeleteNode extends Action {        /**         * The maximum length of a DeleteNode String.         */        private static final int ENTRY_LENGTH = Long.toString(Long.MAX_VALUE).length()                + Action.DELETE_NODE.length()                + Constants.UUID_FORMATTED_LENGTH                + 2;        /**         * The uuid of the node to remove.         */        private final UUID uuid;        /**         * Creates a new DeleteNode action.         *         * @param transactionId the id of the transaction that executes this action.         * @param uuid the uuid of the node to delete.         */        DeleteNode(long transactionId, UUID uuid) {            super(transactionId, Action.TYPE_DELETE_NODE);            this.uuid = uuid;        }        /**         * Creates a new DeleteNode action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param arguments     the uuid of the node to delete.         * @return the DeleteNode action.         * @throws IllegalArgumentException if the arguments are malformed. Not a         *                                  UUID.         */        static DeleteNode fromString(long transactionId, String arguments) {            // simple length check            if (arguments.length() != Constants.UUID_FORMATTED_LENGTH) {                throw new IllegalArgumentException("arguments is not a uuid");            }            return new DeleteNode(transactionId, UUID.fromString(arguments));        }        /**         * Deletes a node from the index.         *         * @inheritDoc         */        public void execute(MultiIndex index) throws IOException {            String uuidString = uuid.toString();            // check if indexing queue is still working on            // this node from a previous update            Document doc = index.indexingQueue.removeDocument(uuidString);            if (doc != null) {                Util.disposeDocument(doc);            }            Term idTerm = new Term(FieldNames.UUID, uuidString);            // if the document cannot be deleted from the volatile index            // delete it from one of the persistent indexes.            int num = index.volatileIndex.removeDocument(idTerm);            if (num == 0) {                for (int i = index.indexes.size() - 1; i >= 0; i--) {                    // only look in registered indexes                    PersistentIndex idx = (PersistentIndex) index.indexes.get(i);                    if (index.indexNames.contains(idx.getName())) {                        num = idx.removeDocument(idTerm);                        if (num > 0) {                            return;                        }                    }                }            }        }        /**         * @inheritDoc         */        public String toString() {            StringBuffer logLine = new StringBuffer(ENTRY_LENGTH);            logLine.append(Long.toString(getTransactionId()));            logLine.append(' ');            logLine.append(Action.DELETE_NODE);            logLine.append(' ');            logLine.append(uuid);            return logLine.toString();        }    }    /**     * Starts a transaction.     */    private static class Start extends Action {        /**         * Creates a new Start transaction action.         *         * @param transactionId the id of the transaction that started.         */        Start(long transactionId) {            super(transactionId, Action.TYPE_START);        }        /**         * Creates a new Start action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param arguments     ignored by this method.         * @return the Start action.         */        static Start fromString(long transactionId, String arguments) {            return new Start(transactionId);        }        /**         * Sets the current transaction id on <code>index</code>.         *         * @inheritDoc         */        public void execute(MultiIndex index) throws IOException {            index.currentTransactionId = getTransactionId();        }        /**         * @inheritDoc         */        public String toString() {            return Long.toString(getTransactionId()) + ' ' + Action.START;        }    }    /**     * Commits the volatile index to disk.     */    private static class VolatileCommit extends Action {        /**         * The name of the target index to commit to.         */        private final String targetIndex;        /**         * Creates a new VolatileCommit action.         *         * @param transactionId the id of the transaction that executes this action.         */        VolatileCommit(long transactionId, String targetIndex) {            super(transactionId, Action.TYPE_VOLATILE_COMMIT);            this.targetIndex = targetIndex;        }        /**         * Creates a new VolatileCommit action.         *         * @param transactionId the id of the transaction that executes this         *                      action.         * @param arguments     ignored by this implementation.         * @return the VolatileCommit action.         */        static VolatileCommit fromString(long transactionId, String arguments) {            return new VolatileCommit(transactionId, arguments);        }        /**         * Commits the volatile index to disk.         *         * @inheritDoc         */        public void execute(MultiIndex index) throws IOException {            VolatileIndex volatileIndex = index.getVolatileIndex();            PersistentIndex persistentIndex = index.getOrCreateIndex(targetIndex, true);            persistentIndex.copyIndex(volati

⌨️ 快捷键说明

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