/******************************************************************************************************************* * * ******************************************************************************************************************/ private void save() { if (fileSystem == null) { log.warn("Not saving observations, no FileSystem availble"); } else { log.info(">>>> saving {}...", observationSet); try { log.info("Saving observations to {}...", NAME); final long time = System.currentTimeMillis(); final @Cleanup ObjectOutputStream oos = new ObjectOutputStream(fileSystem.openFileOutput(NAME)); oos.writeObject(observationSet); log.info(">>>> saved {} items in {} msec", observationSet.find(Observation).count(), System.currentTimeMillis() - time); } catch (Throwable t) { log.error("Can't save observations", t); } } } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull public synchronized ObservationSet findOrCreateObservationSetById (final @Nonnull Id id) { if (observationSet == null) { try { log.info("Loading observations from {}...", NAME); final long time = System.currentTimeMillis(); final @Cleanup ObjectInputStream ois = new ObjectInputStream(fileSystem.openFileInput(NAME)); observationSet = (SimpleObservationSet)ois.readObject(); log.info(">>>> loaded %d items in {} msec", observationSet.find(Observation).count(), System.currentTimeMillis() - time); } catch (Throwable t) { log.warn("Cannot load observations", t); observationSet = new SimpleObservationSet(); } observationSet.addListener(saveAnyChangeListener); } return observationSet; }