/** * Unloads a document from memory. * * @param index the index of the document to be unloaded. * @param sync should the document be sync'ed (i.e. saved) before unloading. */ public void unloadDocument(int index, boolean sync) { // 1. check whether its been loaded and is a persistent one // if a persistent doc is not loaded, there's nothing we need to do if ((!isDocumentLoaded(index)) && isPersistentDocument(index)) return; // 2. If requested, sync the document before releasing it from // memory, // because the creole register garbage collects all LRs which are // not used // any more if (sync) { Document doc = documents.get(index); try { // if the document is not already adopted, we need to do that // first if (doc.getLRPersistenceId() == null) { doc = (Document) this.getDataStore().adopt(doc); this.getDataStore().sync(doc); this.setDocumentPersistentID(index, doc.getLRPersistenceId()); } else // if it is adopted, just sync it this.getDataStore().sync(doc); } catch (PersistenceException ex) { throw new GateRuntimeException( "Error unloading document from corpus" + "because document sync failed: " + ex.getMessage(), ex); } } // 3. remove the document from the memory // do this, only if the saving has succeeded documents.set(index, null); }
/** Called by a datastore when a resource has been deleted */ @Override public void resourceDeleted(DatastoreEvent evt) { DataStore ds = (DataStore) evt.getSource(); // 1. check whether this datastore fired the event. If not, return. if (!ds.equals(this.dataStore)) return; Object docID = evt.getResourceID(); if (docID == null) return; if (DEBUG) Out.prln("Resource deleted called for: " + docID); // first check if it is this corpus that's been deleted, it must be // unloaded immediately if (docID.equals(this.getLRPersistenceId())) { Factory.deleteResource(this); return; } // if boolean isDirty = false; // the problem here is that I only have the doc persistent ID // and nothing else, so I need to determine the index of the doc // first for (int i = 0; i < docDataList.size(); i++) { DocumentData docData = docDataList.get(i); // we've found the correct document // don't break the loop, because it might appear more than once if (docID.equals(docData.getPersistentID())) { if (evt.getResource() == null) { // instead of calling remove() which tries to load the // document // remove it from the documents and docDataList documentRemoved(docDataList.get(i).persistentID.toString()); docDataList.remove(i); documents.remove(i); isDirty = true; i--; continue; } remove(i); isDirty = true; } // if } // for loop through the doc data if (isDirty) try { this.dataStore.sync(this); } catch (PersistenceException ex) { throw new GateRuntimeException("SerialCorpusImpl: " + ex.getMessage()); } catch (SecurityException sex) { throw new GateRuntimeException("SerialCorpusImpl: " + sex.getMessage()); } } // resourceDeleted