From 4946466f80a3403ec31583e0a7aac13d3babbbc0 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 8 Sep 2021 07:14:44 +0200 Subject: [PATCH] Introduce experimental redundant file based datastore. --- .../cms/internal/jcr/LocalFsDataStore.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 org.argeo.cms/src/org/argeo/cms/internal/jcr/LocalFsDataStore.java diff --git a/org.argeo.cms/src/org/argeo/cms/internal/jcr/LocalFsDataStore.java b/org.argeo.cms/src/org/argeo/cms/internal/jcr/LocalFsDataStore.java new file mode 100644 index 000000000..dba005cb4 --- /dev/null +++ b/org.argeo.cms/src/org/argeo/cms/internal/jcr/LocalFsDataStore.java @@ -0,0 +1,68 @@ +package org.argeo.cms.internal.jcr; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.apache.jackrabbit.core.data.DataIdentifier; +import org.apache.jackrabbit.core.data.DataRecord; +import org.apache.jackrabbit.core.data.DataStoreException; +import org.apache.jackrabbit.core.data.FileDataStore; + +/** + * experimental Duplicate added entries in another directory (typically a + * remote mount). + */ +@SuppressWarnings("restriction") +public class LocalFsDataStore extends FileDataStore { + String redundantPath; + FileDataStore redundantStore; + + @Override + public void init(String homeDir) { + // init primary first + super.init(homeDir); + + if (redundantPath != null) { + // redundant directory must be created first + // TODO implement some polling? + if (Files.exists(Paths.get(redundantPath))) { + redundantStore = new FileDataStore(); + redundantStore.setPath(redundantPath); + redundantStore.init(homeDir); + } + } + } + + @Override + public DataRecord addRecord(InputStream input) throws DataStoreException { + DataRecord dataRecord = super.addRecord(input); + syncRedundantRecord(dataRecord); + return dataRecord; + } + + @Override + public DataRecord getRecord(DataIdentifier identifier) throws DataStoreException { + DataRecord dataRecord = super.getRecord(identifier); + syncRedundantRecord(dataRecord); + return dataRecord; + } + + protected void syncRedundantRecord(DataRecord dataRecord) throws DataStoreException { + if (redundantStore == null) + return; + if (redundantStore.getRecordIfStored(dataRecord.getIdentifier()) == null) { + try (InputStream redundant = dataRecord.getStream()) { + redundantStore.addRecord(redundant); + } catch (IOException e) { + throw new DataStoreException("Cannot add redundant record.", e); + } + } + } + + public void setRedundantPath(String redundantPath) { + this.redundantPath = redundantPath; + } + +} -- 2.30.2