@Override public void save(Map<?, ?> options) throws IOException { if (nonNull(this.options)) { // Check that the save options do not collide with previous load options for (Entry<?, ?> entry : options.entrySet()) { if (this.options.containsKey(entry.getKey()) && !Objects.equals(entry.getValue(), this.options.get(entry.getKey()))) { throw new InvalidOptionException(MessageFormat.format("key = {0}; value = {1}", entry.getKey().toString(), entry.getValue().toString())); } } } if (!isLoaded() || !isPersistent) { PersistenceBackend newBackend = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createPersistentBackend(getFile(), options); PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).copyBackend(backend, newBackend); this.backend = newBackend; this.store = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createPersistentStore(this, backend, options); this.isLoaded = true; this.isPersistent = true; } backend.save(); NeoLogger.info("{0} saved: {1}", PersistentResource.class.getSimpleName(), uri); }
@Override public void close() { this.backend.close(); this.backend = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createTransientBackend(); this.store = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createTransientStore(this, backend); this.isPersistent = false; this.isLoaded = false; NeoLogger.info("{0} closed: {1}", PersistentResource.class.getSimpleName(), getURI()); }
/** * Constructs a new {@code DefaultPersistentResource} with the given {@code uri}. * * @param uri the {@link URI} of the resource */ public DefaultPersistentResource(URI uri) { super(uri); this.dummyRootEObject = new DummyRootEObject(this); this.backend = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createTransientBackend(); this.store = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createTransientStore(this, backend); this.isPersistent = false; PersistenceBackendShutdownHook.closeOnExit(backend, getURI()); NeoLogger.info("{0} created", PersistentResource.class.getSimpleName()); }
@Override public void load(Map<?, ?> options) throws IOException { try { isLoading = true; if (!isLoaded) { if (getFile().exists() || nonNull(uri.authority())) { // Check authority to enable remote resource loading this.backend = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createPersistentBackend(getFile(), options); this.store = PersistenceBackendFactoryRegistry.getFactoryProvider(uri.scheme()).createPersistentStore(this, backend, options); this.isPersistent = true; dummyRootEObject.setMapped(true); } else { throw new FileNotFoundException(uri.toFileString()); } this.options = options; isLoaded = true; } } finally { isLoading = false; NeoLogger.info("{0} loaded: {1}", PersistentResource.class.getSimpleName(), uri); } }