📄 storagemodifier.java
字号:
*/ public void createFeed(StorageFeedWrapper wrapper) throws StorageException { this.lock.lock(); try { this.forceWriteDocuments.add(wrapper.getLuceneDocument()); storageModified(); } finally { this.lock.unlock(); } } /** * Adds a new accountr to the storage. User action will be not buffered. Call to * this method forces the index to be written. * * @param account * -the wrapper containig the user to be persisted * @throws StorageException - * if the user can not be persisted. */ public void createAccount(StorageAccountWrapper account) throws StorageException { this.lock.lock(); try { this.forceWriteDocuments.add(account.getLuceneDocument()); storageModified(); } finally { this.lock.unlock(); } } /** * Deletes the user with the given username. User action will be not * buffered. Call to this method forces the index to be written. * * @param accountName - * the user to be deleted * @throws StorageException - * If the user could not be deleted */ public void deleteAccount(String accountName) throws StorageException { this.lock.lock(); try { //TODO delete all feeds and entries of this account this.forceWriteTerms.add(new Term( StorageAccountWrapper.FIELD_ACCOUNTNAME, accountName)); storageModified(); } finally { this.lock.unlock(); } } /** * User action will be not buffered. Call to this method forces the index to * be written. * * @param user * -the wrapper containig the user to be persisted * @throws StorageException - * if the user can not be persisted. */ public void updateAccount(final StorageAccountWrapper user) throws StorageException { this.lock.lock(); try { this.forceWriteTerms.add(new Term( StorageAccountWrapper.FIELD_ACCOUNTNAME, user.getUser() .getName())); this.forceWriteDocuments.add(user.getLuceneDocument()); storageModified(); } finally { this.lock.unlock(); } } /** * Feed action will be not buffered. Call to this method forces the index to * be written. * * @param wrapper - * the wrapper containig the feed * @throws StorageException - * if the feed can not be persisted */ public void updateFeed(final StorageFeedWrapper wrapper) throws StorageException { this.lock.lock(); try { this.forceWriteTerms.add(new Term(StorageFeedWrapper.FIELD_FEED_ID, wrapper.getFeed().getId())); this.forceWriteDocuments.add(wrapper.getLuceneDocument()); storageModified(); } finally { this.lock.unlock(); } } /** * Deletes the feed with the given feed id Feed action will be not buffered. * Call to this method forces the index to be written. * All entries referencing the given feed id will be deleted as well! * @param feedId - * the id of the feed to delete * @throws StorageException - * if the feed can not be deleted */ public void deleteFeed(final String feedId) throws StorageException { this.lock.lock(); try { this.deletedDocumentQueue.add(new Term(StorageEntryWrapper.FIELD_FEED_REFERENCE,feedId)); this.forceWriteTerms.add(new Term(StorageFeedWrapper.FIELD_FEED_ID, feedId)); storageModified(); } finally { this.lock.unlock(); } } private void storageModified() throws StorageException { if(this.isClosed.get()) throw new IllegalStateException("StorageModifier is already closed"); try { if(this.isClosed.get()) throw new IllegalStateException("StorageModifier is already closed"); incrementCounter(); if (this.persistFactor > this.modifiedCounter && this.forceWriteDocuments.size() <= 0 && this.forceWriteTerms.size() <= 0) return; if (LOG.isInfoEnabled()) LOG.info("Storage modified for " + this.modifiedCounter + " times. Write Persistent index"); writePersistentIndex((this.optimizeCounter >= this.optimizeInterval)); requestNewIndexModifier(); this.modifiedCounter = 0; } catch (IOException e) { LOG.error("Writing persistent index failed - Recovering", e); throw new StorageException("could not write to storage index -- "+e.getMessage(),e); } } /** * Persists all changes imediately * @throws IOException -- if an IO Exception occures */ public void forceWrite() throws IOException { if(this.isClosed.get()) throw new IllegalStateException("StorageModifier is already closed"); this.lock.lock(); try { if (LOG.isInfoEnabled()) LOG.info("ForceWrite called -- current modifiedCounter: " + this.modifiedCounter + " - persisting changes"); writePersistentIndex(true); requestNewIndexModifier(); this.modifiedCounter = 0; } finally { this.lock.unlock(); } } private void requestNewIndexModifier() throws IOException { this.controller.registerNewRecoverWriter(); this.controller.registerNewStorageQuery(); this.buffer = this.controller.releaseNewStorageBuffer(); this.modifier = this.controller.createIndexModifier(); } private void writePersistentIndex(final boolean optimize) throws IOException { try { /* * first delete all updated documents */ for (Term entryIdTerm : this.deletedForUpdateDocumentQueue) { this.modifier.deleteDocuments(entryIdTerm); } for (Term term : this.forceWriteTerms) { this.modifier.deleteDocuments(term); } /* * add all documents */ Collection<Document> documents = this.documentMap.values(); for (Document doc : documents) { this.modifier.addDocument(doc); } /* * write all users or feeds */ for (Document docs : this.forceWriteDocuments) { this.modifier.addDocument(docs); } /* * delete all documents marked as deleted. As the DocumentIDs are * unique the document marked as deleted must not persist after the * index has been written. In the case of an update of a document * and a previous delete the concurrency component will not allow an * update. new inserted entries can not be deleted accidently- */ for (Term entryIdTerm : this.deletedDocumentQueue) { this.modifier.deleteDocuments(entryIdTerm); } this.modifier.flush(); if (optimize) { if (LOG.isInfoEnabled()) LOG.info("Optimizing index -- optimize interval " + this.optimizeInterval); this.modifier.optimize(); } } finally { if (optimize) this.optimizeCounter = 0; this.modifier.close(); this.deletedForUpdateDocumentQueue.clear(); this.deletedDocumentQueue.clear(); this.documentMap.clear(); this.forceWriteDocuments.clear(); this.forceWriteTerms.clear(); } } protected void close() throws IOException { if(this.isClosed.get()) throw new IllegalStateException("StorageModifier is already closed"); this.lock.lock(); try { if(this.isClosed.get()) throw new IllegalStateException("StorageModifier is already closed"); this.isClosed.set(true); if (LOG.isInfoEnabled()) LOG.info("ForceWrite called -- current modifiedCounter: " + this.modifiedCounter + " - persisting changes"); writePersistentIndex(true); this.modifiedCounter = 0; } finally { this.lock.unlock(); } } private void incrementCounter() { this.optimizeCounter++; this.modifiedCounter++; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -