/** * Increments the operation counter, and commit the persistence back-end if the number of operation is equals to * {@code OPS_BETWEEN_COMMITS_DEFAULT}. */ private void incrementAndCommit() { opCount = (opCount + 1) % OPS_BETWEEN_COMMITS_DEFAULT; if (opCount == 0) { backend.save(); } }
@Override public final void close() { close(true); }
/** * Constructs a new {@code AbstractBackend} with the given {@code name}. * * @param name the unique name of this backend */ protected AbstractBackend(@Nullable String name) { this.name = name; if (isPersistent()) { BackendManager.getInstance().register(this); } }
/** * Checks the copy of a {@link Backend} to another. */ @Test public void testCopyBackend() throws IOException { ImmutableConfig config = context().config(); File file = currentTempFile(); try (Backend transientBackend = new InMemoryBackendFactory().createBackend(new InMemoryUriFactory().createLocalUri(file), new InMemoryConfig())) { try (Backend persistentBackend = context().factory().createBackend(context().createUri(currentTempFile()), config)) { transientBackend.copyTo(persistentBackend); } } }
/** * Check the creation of chaining between the default {@link Store}s and a {@link Backend}. */ @Test public void testCreatePersistentStore() throws IOException { try (Backend backend = context().factory().createBackend(context().createUri(currentTempFile()), context().config())) { try (Store store = StoreFactory.getInstance().createStore(backend, context().config())) { assertThat(store).isInstanceOf(NoopStore.class); assertThat(store.backend()).isSameAs(backend); } } }
@BeforeAll static void registerFactory() { BackendFactoryRegistry.getInstance().register(SCHEME, new AbstractBackendFactory<BaseConfig<?>>("fake") {}); }
/** * Cleanly closes this back-end, and releases any system resources associated with it. All modifications are saved * before closing. * <p> * If the back-end is already closed, then invoking this method has no effect. * * @param clean {@code true} if the registry must be cleaned after closure */ void close(boolean clean) { if (isClosed) { return; } if (clean && isPersistent()) { BackendManager.getInstance().unregister(this); } try { save(); internalClose(); } catch (Exception e) { Log.warn(e); } finally { isClosed = true; } }
/** * Constructs a new {@code DefaultPersistentResource} with the given {@code uri}. * * @param uri the URI of the resource */ public DefaultPersistentResource(URI uri) { super(uri); factory = BackendFactoryRegistry.getInstance().getFactoryFor(uri.scheme()); // Creates a transient backend until a call to `save()`/`load()` eStore = createTransientStore(); logState("created"); }
@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()); }
@AfterAll static void unregisterFactory() { BackendFactoryRegistry.getInstance().unregister(SCHEME); }
/** * Checks the creation of a {@link fr.inria.atlanmod.neoemf.data.Backend}. */ @ParameterizedTest @MethodSource("allMappings") public void testCreateBackend(ImmutableConfig config, Class<? extends Backend> expectedType) throws IOException { try (Backend backend = context().factory().createBackend(context().createUri(currentTempFile()), config)) { assertThat(backend).isInstanceOf(expectedType); } }
/** * Constructs a new {@code MockBackendFactory}. */ @VisibleForReflection public MockBackendFactory() { this(new InvalidBackend("You need to register the instance you want to use with BackendFactoryRegistry.register() first")); }
@Nonnull @Override public Backend createBackend(URI uri, ImmutableConfig baseConfig) { checkNotNull(uri, "uri"); checkNotNull(baseConfig, "baseConfig"); checkArgument(uri.isFile() || uri.isHierarchical(), "URI '%s' must be either file-based or hierarchical", uri.toString()); try { return uri.isFile() ? createLocalBackend(uri, baseConfig) : createRemoteBackend(uri, baseConfig); } catch (Exception e) { throw new InvalidBackendException("Unable to create/open the database", e); } }
/** * Creates a new {@link StoreAdapter} on top of a newly created {@link Backend}, using the current {@link * #factory}. * * @param config the configuration of the store/back-end * * @return a new store */ @Nonnull private StoreAdapter createStore(ImmutableConfig config) { return createStore(factory.createBackend(uri, config), config); }
/** * Retrieves the URI scheme for the specified {@code factory}. * * @param factory the factory * * @return the URI scheme * * @throws BindingException if no scheme is defined for this {@code type} */ @Nonnull public static String schemeOf(BackendFactory factory) { return UriFactory.createScheme(factory.name()); }
@Override public final void save() { try { internalSave(); } catch (Exception e) { Log.warn(e); } }
@BeforeEach void setUp() { invalidBackend = new InvalidBackend(""); }
@Override public void save() { backend.save(); }
/** * Cleanly closes all registered back-ends. */ private synchronized void onShutdown() { if (!activeBackends.isEmpty()) { activeBackends.parallelStream().forEachOrdered(b -> b.close(false)); activeBackends.clear(); } }
/** * Retrieves the name for the specified {@code type}. * <p> * The {@code type} <b>must</b> be annotated with {@link FactoryBinding}, or implements {@link BackendFactory}. * * @param type the type * * @return the name * * @throws BindingException if no name is defined for this {@code type} */ @Nonnull public static String nameOf(Class<?> type) { return findFactory(type).name(); }