@Override public boolean add(Document o) { if (o == null) return false; Document doc = o; // make it accept only docs from its own datastore if (doc.getDataStore() != null && !this.dataStore.equals(doc.getDataStore())) { Err.prln("Error: Persistent corpus can only accept documents " + "from its own datastore!"); return false; } // if // add the document with its index in the docDataList // in this case, since it's going to be added to the end // the index will be the size of the docDataList before // the addition DocumentData docData = new DocumentData(doc.getName(), doc.getLRPersistenceId(), doc.getClass().getName()); boolean result = docDataList.add(docData); documents.add(doc); documentAdded(doc); fireDocumentAdded( new CorpusEvent( SerialCorpusImpl.this, doc, docDataList.size() - 1, doc.getLRPersistenceId(), CorpusEvent.DOCUMENT_ADDED)); return result; }
/** * 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); }
@Override public void add(int index, Document o) { if (o == null) return; Document doc = o; DocumentData docData = new DocumentData(doc.getName(), doc.getLRPersistenceId(), doc.getClass().getName()); docDataList.add(index, docData); documents.add(index, doc); documentAdded(doc); fireDocumentAdded( new CorpusEvent( SerialCorpusImpl.this, doc, index, doc.getLRPersistenceId(), CorpusEvent.DOCUMENT_ADDED)); }
public int findDocument(Document doc) { boolean found = false; DocumentData docData = null; // first try finding the document in memory int index = documents.indexOf(doc); if (index > -1 && index < docDataList.size()) return index; // else try finding a document with the same name and persistent ID Iterator<DocumentData> iter = docDataList.iterator(); for (index = 0; iter.hasNext(); index++) { docData = iter.next(); if (docData.getDocumentName().equals(doc.getName()) && docData.getPersistentID().equals(doc.getLRPersistenceId()) && docData.getClassType().equals(doc.getClass().getName())) { found = true; break; } } if (found && index < docDataList.size()) return index; else return -1; } // findDocument