Save: synchonise the in-memory image of the LR with the persistent image.
(LanguageResource lr)
| 355 | * image. |
| 356 | */ |
| 357 | @Override |
| 358 | public void sync(LanguageResource lr) throws PersistenceException { |
| 359 | // Out.prln("SDS: LR sync called. Saving " + lr.getClass().getName()); |
| 360 | |
| 361 | // check that this LR is one of ours (i.e. has been adopted) |
| 362 | if(lr.getDataStore() == null || ! lr.getDataStore().equals(this)) |
| 363 | throw new PersistenceException( |
| 364 | "LR " + lr.getName() + " has not been adopted by this DataStore" |
| 365 | ); |
| 366 | |
| 367 | // find the resource data for this LR |
| 368 | ResourceData lrData = |
| 369 | Gate.getCreoleRegister().get(lr.getClass().getName()); |
| 370 | |
| 371 | // create a subdirectory for resources of this type if none exists |
| 372 | File resourceTypeDirectory = new File(storageDir, lrData.getClassName()); |
| 373 | if( |
| 374 | (! resourceTypeDirectory.exists()) || |
| 375 | (! resourceTypeDirectory.isDirectory()) |
| 376 | ) { |
| 377 | // try to create the directory, throw an exception if it does not |
| 378 | // exist after this attempt. It is possible for mkdir to fail and exists |
| 379 | // still to return true if another thread managed to sneak in and |
| 380 | // create the directory in the meantime |
| 381 | if(! resourceTypeDirectory.mkdir() && ! resourceTypeDirectory.exists()) |
| 382 | throw new PersistenceException("Can't write " + resourceTypeDirectory); |
| 383 | } |
| 384 | |
| 385 | // create an indentifier for this resource |
| 386 | String lrName = null; |
| 387 | Object lrPersistenceId = null; |
| 388 | lrName = lr.getName(); |
| 389 | lrPersistenceId = lr.getLRPersistenceId(); |
| 390 | |
| 391 | if(lrName == null) |
| 392 | lrName = lrData.getName(); |
| 393 | if(lrPersistenceId == null) { |
| 394 | lrPersistenceId = constructPersistenceId(lrName); |
| 395 | lr.setLRPersistenceId(lrPersistenceId); |
| 396 | } |
| 397 | |
| 398 | //we're saving a corpus. I need to save its documents first |
| 399 | if (lr instanceof Corpus) { |
| 400 | //check if the corpus is the one we support. CorpusImpl cannot be saved! |
| 401 | if (! (lr instanceof SerialCorpusImpl)) |
| 402 | throw new PersistenceException("Can't save a corpus which " + |
| 403 | "is not of type SerialCorpusImpl!"); |
| 404 | SerialCorpusImpl corpus = (SerialCorpusImpl) lr; |
| 405 | //this is a list of the indexes of all newly-adopted documents |
| 406 | //which will be used by the SerialCorpusImpl to update the |
| 407 | //corresponding document IDs |
| 408 | for (int i = 0; i < corpus.size(); i++) { |
| 409 | //if the document is not in memory, there's little point in saving it |
| 410 | if ( (!corpus.isDocumentLoaded(i)) && corpus.isPersistentDocument(i)) |
| 411 | continue; |
| 412 | if (DEBUG) |
| 413 | Out.prln("Saving document at position " + i); |
| 414 | if (DEBUG) |