multiindex.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,855 行 · 第 1/5 页
JAVA
1,855 行
/** * Action identifier in redo log for add node action. */ static final String ADD_NODE = "ADD"; /** * Action type for add node action. */ public static final int TYPE_ADD_NODE = 1; /** * Action identifier in redo log for node delete action. */ static final String DELETE_NODE = "DEL"; /** * Action type for delete node action. */ public static final int TYPE_DELETE_NODE = 2; /** * Action identifier in redo log for transaction commit action. */ static final String COMMIT = "COM"; /** * Action type for commit action. */ public static final int TYPE_COMMIT = 3; /** * Action identifier in redo log for volatile index commit action. */ static final String VOLATILE_COMMIT = "VOL_COM"; /** * Action type for volatile index commit action. */ public static final int TYPE_VOLATILE_COMMIT = 4; /** * Action identifier in redo log for index create action. */ static final String CREATE_INDEX = "CRE_IDX"; /** * Action type for create index action. */ public static final int TYPE_CREATE_INDEX = 5; /** * Action identifier in redo log for index add action. */ static final String ADD_INDEX = "ADD_IDX"; /** * Action type for add index action. */ public static final int TYPE_ADD_INDEX = 6; /** * Action identifier in redo log for delete index action. */ static final String DELETE_INDEX = "DEL_IDX"; /** * Action type for delete index action. */ public static final int TYPE_DELETE_INDEX = 7; /** * Transaction identifier for internal actions like volatile index * commit triggered by timer thread. */ static final long INTERNAL_TRANSACTION = -1; /** * Transaction identifier for internal action that replaces indexs. */ static final long INTERNAL_TRANS_REPL_INDEXES = -2; /** * The id of the transaction that executed this action. */ private final long transactionId; /** * The action type. */ private final int type; /** * Creates a new <code>Action</code>. * * @param transactionId the id of the transaction that executed this * action. * @param type the action type. */ Action(long transactionId, int type) { this.transactionId = transactionId; this.type = type; } /** * Returns the transaction id for this <code>Action</code>. * * @return the transaction id for this <code>Action</code>. */ long getTransactionId() { return transactionId; } /** * Returns the action type. * * @return the action type. */ int getType() { return type; } /** * Executes this action on the <code>index</code>. * * @param index the index where to execute the action. * @throws IOException if the action fails due to some I/O error in * the index or some other error. */ public abstract void execute(MultiIndex index) throws IOException; /** * Executes the inverse operation of this action. That is, does an undo * of this action. This default implementation does nothing, but returns * silently. * * @param index the index where to undo the action. * @throws IOException if the action cannot be undone. */ public void undo(MultiIndex index) throws IOException { } /** * Returns a <code>String</code> representation of this action that can be * written to the {@link RedoLog}. * * @return a <code>String</code> representation of this action. */ public abstract String toString(); /** * Parses an line in the redo log and created an {@link Action}. * * @param line the line from the redo log. * @return an <code>Action</code>. * @throws IllegalArgumentException if the line is malformed. */ static Action fromString(String line) throws IllegalArgumentException { int endTransIdx = line.indexOf(' '); if (endTransIdx == -1) { throw new IllegalArgumentException(line); } long transactionId; try { transactionId = Long.parseLong(line.substring(0, endTransIdx)); } catch (NumberFormatException e) { throw new IllegalArgumentException(line); } int endActionIdx = line.indexOf(' ', endTransIdx + 1); if (endActionIdx == -1) { // action does not have arguments endActionIdx = line.length(); } String actionLabel = line.substring(endTransIdx + 1, endActionIdx); String arguments = ""; if (endActionIdx + 1 <= line.length()) { arguments = line.substring(endActionIdx + 1); } Action a; if (actionLabel.equals(Action.ADD_NODE)) { a = AddNode.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.ADD_INDEX)) { a = AddIndex.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.COMMIT)) { a = Commit.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.CREATE_INDEX)) { a = CreateIndex.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.DELETE_INDEX)) { a = DeleteIndex.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.DELETE_NODE)) { a = DeleteNode.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.START)) { a = Start.fromString(transactionId, arguments); } else if (actionLabel.equals(Action.VOLATILE_COMMIT)) { a = VolatileCommit.fromString(transactionId, arguments); } else { throw new IllegalArgumentException(line); } return a; } } /** * Adds an index to the MultiIndex's active persistent index list. */ private static class AddIndex extends Action { /** * The name of the index to add. */ private String indexName; /** * Creates a new AddIndex 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. */ AddIndex(long transactionId, String indexName) { super(transactionId, Action.TYPE_ADD_INDEX); this.indexName = indexName; } /** * Creates a new AddIndex action. * * @param transactionId the id of the transaction that executes this * action. * @param arguments the name of the index to add. * @return the AddIndex action. * @throws IllegalArgumentException if the arguments are malformed. */ static AddIndex fromString(long transactionId, String arguments) { return new AddIndex(transactionId, arguments); } /** * Adds a sub index to <code>index</code>. * * @inheritDoc */ public void execute(MultiIndex index) throws IOException { PersistentIndex idx = index.getOrCreateIndex(indexName, false); if (!index.indexNames.contains(indexName)) { index.indexNames.addName(indexName); // now that the index is in the active list let the merger know about it index.merger.indexAdded(indexName, idx.getNumDocuments()); } } /** * @inheritDoc */ public String toString() { StringBuffer logLine = new StringBuffer(); logLine.append(Long.toString(getTransactionId())); logLine.append(' '); logLine.append(Action.ADD_INDEX); logLine.append(' '); logLine.append(indexName); return logLine.toString(); } } /** * Adds a node to the index. */ private static class AddNode extends Action { /** * The maximum length of a AddNode String. */ private static final int ENTRY_LENGTH = Long.toString(Long.MAX_VALUE).length() + Action.ADD_NODE.length() + Constants.UUID_FORMATTED_LENGTH + 2; /** * The uuid of the node to add. */ private final UUID uuid; /** * The document to add to the index, or <code>null</code> if not available. */ private Document doc; /** * Creates a new AddNode action. * * @param transactionId the id of the transaction that executes this action. * @param uuid the uuid of the node to add. */ AddNode(long transactionId, UUID uuid) { super(transactionId, Action.TYPE_ADD_NODE); this.uuid = uuid; } /** * Creates a new AddNode action. * * @param transactionId the id of the transaction that executes this action. * @param doc the document to add. */ AddNode(long transactionId, Document doc) { this(transactionId, UUID.fromString(doc.get(FieldNames.UUID))); this.doc = doc; } /** * Creates a new AddNode action. * * @param transactionId the id of the transaction that executes this * action. * @param arguments the arguments to this action. The uuid of the node * to add * @return the AddNode action. * @throws IllegalArgumentException if the arguments are malformed. Not a * UUID. */ static AddNode fromString(long transactionId, String arguments) throws IllegalArgumentException { // simple length check if (arguments.length() != Constants.UUID_FORMATTED_LENGTH) { throw new IllegalArgumentException("arguments is not a uuid"); } return new AddNode(transactionId, UUID.fromString(arguments)); } /** * Adds a node to the index. * * @inheritDoc */ public void execute(MultiIndex index) throws IOException { if (doc == null) { try { doc = index.createDocument(new NodeId(uuid)); } catch (RepositoryException e) { // node does not exist anymore log.debug(e.getMessage()); } } if (doc != null) { index.volatileIndex.addDocument(doc); } } /** * @inheritDoc */ public String toString() { StringBuffer logLine = new StringBuffer(ENTRY_LENGTH); logLine.append(Long.toString(getTransactionId())); logLine.append(' '); logLine.append(Action.ADD_NODE); logLine.append(' '); logLine.append(uuid); return logLine.toString(); } } /** * Commits a transaction. */ private static class Commit extends Action { /** * Creates a new Commit action.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?