From fcf065e239571ecbe929df9321af2e4b47036dfa Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Sat, 15 Aug 2015 00:44:24 +0200 Subject: [PATCH] =?utf8?q?Added=20synchronized=20file=20access=20Signed-of?= =?utf8?q?f-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../base64/Base64CsvDatabaseBackend.java | 14 +-- .../database/backend/file/DatabaseFile.java | 87 +++++++++++++++++++ .../backend/file/SynchronizeableFile.java | 40 +++++++++ 3 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 src/org/mxchange/jcore/database/backend/file/DatabaseFile.java create mode 100644 src/org/mxchange/jcore/database/backend/file/SynchronizeableFile.java diff --git a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java index 7c607fb..859f228 100644 --- a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java @@ -16,6 +16,7 @@ */ package org.mxchange.jcore.database.backend.base64; +import org.mxchange.jcore.database.backend.file.DatabaseFile; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; @@ -24,14 +25,13 @@ import java.text.MessageFormat; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.codec.binary.Base64; import org.mxchange.jcore.criteria.searchable.SearchableCritera; import org.mxchange.jcore.database.backend.BaseDatabaseBackend; import org.mxchange.jcore.database.backend.DatabaseBackend; +import org.mxchange.jcore.database.backend.file.SynchronizeableFile; import org.mxchange.jcore.database.frontend.DatabaseFrontend; import org.mxchange.jcore.database.result.DatabaseResult; import org.mxchange.jcore.database.result.Result; @@ -49,7 +49,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat /** * Output stream for this storage engine */ - private RandomAccessFile storageFile; + private final SynchronizeableFile storageFile; /** * File name to access @@ -82,14 +82,14 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat this.fileName = String.format("%s/table_%s.b64", this.getProperty("database.backend.storagepath"), tableName); //NOI18N // Debug message - this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", fileName)); //NOI18N + this.getLogger().debug(MessageFormat.format("Trying to open file {0} ...", this.fileName)); //NOI18N try { // Try to initialize the storage (file instance) - this.storageFile = new RandomAccessFile(fileName, "rw"); //NOI18N + this.storageFile = new DatabaseFile(this.fileName); //NOI18N } catch (final FileNotFoundException ex) { // Did not work - this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", fileName, ex.toString())); //NOI18N + this.getLogger().error(MessageFormat.format("File {0} cannot be opened: {1}", this.fileName, ex.toString())); //NOI18N throw ex; } @@ -233,7 +233,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat * * @return Storage file instance */ - private RandomAccessFile getStorageFile () { + private SynchronizeableFile getStorageFile () { return this.storageFile; } diff --git a/src/org/mxchange/jcore/database/backend/file/DatabaseFile.java b/src/org/mxchange/jcore/database/backend/file/DatabaseFile.java new file mode 100644 index 0000000..1623d3f --- /dev/null +++ b/src/org/mxchange/jcore/database/backend/file/DatabaseFile.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcore.database.backend.file; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import org.mxchange.jcore.BaseFrameworkSystem; + +/** + * This implementation of SynchronizeableFile provides synchronized access on the RandomAccessFile + instance. This implementation is thread-safe, therefor. + * + * @author Roland Haeder + */ +public class DatabaseFile extends BaseFrameworkSystem implements SynchronizeableFile { + /** + * The actual file instance + */ + private final RandomAccessFile file; + + /** + * A constructor accepting a full-qualified file name (FQFN). + * @param fqfn Full-qualified file name + * @throws FileNotFoundException + */ + public DatabaseFile (final String fqfn) throws FileNotFoundException { + // Init it here + this.file = new RandomAccessFile(fqfn, "rw"); + } + + @Override + public void close () throws IOException{ + synchronized (this.file) { + this.file.close(); + } + } + + @Override + public long getFilePointer () throws IOException { + synchronized (this.file) { + return this.file.getFilePointer(); + } + } + + @Override + public long length () throws IOException { + synchronized (this.file) { + return this.file.length(); + } + } + + @Override + public String readLine () throws IOException { + synchronized (this.file) { + return this.file.readLine(); + } + } + + @Override + public void seek (final long i) throws IOException { + synchronized (this.file) { + this.file.seek(i); + } + } + + @Override + public void writeBytes (final String string) throws IOException { + synchronized (this.file) { + this.file.writeBytes(string); + } + } +} diff --git a/src/org/mxchange/jcore/database/backend/file/SynchronizeableFile.java b/src/org/mxchange/jcore/database/backend/file/SynchronizeableFile.java new file mode 100644 index 0000000..13425f4 --- /dev/null +++ b/src/org/mxchange/jcore/database/backend/file/SynchronizeableFile.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcore.database.backend.file; + +import java.io.IOException; +import org.mxchange.jcore.FrameworkInterface; + +/** + * An interface for sy nchronized access on files, with auto-close support. + * + * @author Roland Haeder + */ +public interface SynchronizeableFile extends FrameworkInterface, AutoCloseable{ + + public void close () throws IOException; + + public long getFilePointer () throws IOException; + + public long length () throws IOException; + + public String readLine () throws IOException; + + public void seek (final long i) throws IOException; + + public void writeBytes (final String string) throws IOException; +} -- 2.39.5