From d7f017872752e06d80a1bff0bef94cce3c97f9f8 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 24 Aug 2015 11:59:58 +0200 Subject: [PATCH] =?utf8?q?Sorted=20members=20Signed-off-by:Roland=20H?= =?utf8?q?=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../base64/Base64CsvDatabaseBackend.java | 233 +++++++++--------- 1 file changed, 116 insertions(+), 117 deletions(-) diff --git a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java index 885710e..d47904a 100644 --- a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java @@ -44,16 +44,15 @@ import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; * @author Roland Haeder */ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend { - /** - * Output stream for this storage engine + * File name to access */ - private final SynchronizeableFile storageFile; + private final String fileName; /** - * File name to access + * Output stream for this storage engine */ - private final String fileName; + private final SynchronizeableFile storageFile; /** * Constructor with table name @@ -227,6 +226,118 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat this.getLogger().trace("EXIT!"); //NOI18N } + @Override + public final Long getTotalRows () throws IOException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Init count + Long count = 0L; + + // First rewind + this.rewind(); + + // Walk through all rows + while (!this.isEndOfFile()) { + // Get next line + String line = this.readLine(); + + // Debug message + this.getLogger().debug(MessageFormat.format("line={0}", line)); //NOI18N + + // Count one up + count++; + } + + // Trace message + this.getLogger().trace(MessageFormat.format("count={0} - EXIT!", count)); //NOI18N + + // Return it + return count; + } + + /** + * Tries to interpret the given decoded line and puts its key/value pairs into a map. + * + * @param line Decoded line from database file + * @return A Map with keys and values from line + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the file is believed damaged + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + */ + private Map getMapFromLine (final String line) throws CorruptedDatabaseFileException, BadTokenException { + // Trace message + this.getLogger().debug(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N + + // "line" must not be null or empty + if (line == null) { + // Is null + throw new NullPointerException("line is null"); //NOI18N + } else if (line.isEmpty()) { + // Is empty + throw new IllegalArgumentException("line is empty, maybe isEndOfFile() was not called?"); //NOI18N + } else if (!line.endsWith(";")) { //NOI18N + // Bad line found + throw new CorruptedDatabaseFileException(this.fileName, "No semicolon at end of line"); //NOI18N + } else if (!line.contains("key=")) { //NOI18N + // Bad line found + throw new CorruptedDatabaseFileException(this.fileName, "No \"key=bla\" found."); //NOI18N + } else if (!line.contains("value=")) { //NOI18N + // Bad line found + throw new CorruptedDatabaseFileException(this.fileName, "No \"value=bla\" found."); //NOI18N + } + + Pattern pattern = Pattern.compile("(key=([a-z0-9_]{1,}),value=\"([^\"]*)\";){1,}"); //NOI18N + Matcher matcher = pattern.matcher(line); + + // Debug message + this.getLogger().debug(MessageFormat.format("matches={0}", matcher.matches())); //NOI18N + + // Matches? + if (!matcher.matches()) { + // Corrupted file found + throw new CorruptedDatabaseFileException(this.fileName, MessageFormat.format("line {0} doesn't match regular expression.", line)); //NOI18N + } + + // Instance map + Map map = new HashMap<>(line.length() / 40); + + pattern = Pattern.compile("(key=([a-z0-9_]{1,}),value=\"([^\"]*)\";)"); //NOI18N + matcher = pattern.matcher(line); + + // Init group count + int init = 0; + + // Then get all + while (matcher.find(init)) { + // Get group match + String match = matcher.group(1); + String key = matcher.group(2); + String value = matcher.group(3); + + // key must noch be empty + assert((key != null) && (!key.isEmpty())) : MessageFormat.format("key={0} is not valid", key); //NOI18N + + // Get start and end + int start = matcher.start(); + int end = matcher.end(); + + // Debug message + this.getLogger().debug(MessageFormat.format("init={0},start={1},end={2},match={3},key={4},value={5}", init, start, end, match, key, value)); //NOI18N + + // Add key/value to map + map.put(key, value); + + // Hop to next match + init = end; + } + + // Trace message + this.getLogger().trace(MessageFormat.format("map()={0} - EXIT!", map.size())); //NOI18N + + // Return finished map + return map; + } + /** * Returns storage file * @@ -356,116 +467,4 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat // Trace message this.getLogger().trace("EXIT!"); //NOI18N } - - /** - * Tries to interpret the given decoded line and puts its key/value pairs into a map. - * - * @param line Decoded line from database file - * @return A Map with keys and values from line - * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the file is believed damaged - * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found - */ - private Map getMapFromLine (final String line) throws CorruptedDatabaseFileException, BadTokenException { - // Trace message - this.getLogger().debug(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N - - // "line" must not be null or empty - if (line == null) { - // Is null - throw new NullPointerException("line is null"); //NOI18N - } else if (line.isEmpty()) { - // Is empty - throw new IllegalArgumentException("line is empty, maybe isEndOfFile() was not called?"); //NOI18N - } else if (!line.endsWith(";")) { //NOI18N - // Bad line found - throw new CorruptedDatabaseFileException(this.fileName, "No semicolon at end of line"); //NOI18N - } else if (!line.contains("key=")) { //NOI18N - // Bad line found - throw new CorruptedDatabaseFileException(this.fileName, "No \"key=bla\" found."); //NOI18N - } else if (!line.contains("value=")) { //NOI18N - // Bad line found - throw new CorruptedDatabaseFileException(this.fileName, "No \"value=bla\" found."); //NOI18N - } - - Pattern pattern = Pattern.compile("(key=([a-z0-9_]{1,}),value=\"([^\"]*)\";){1,}"); //NOI18N - Matcher matcher = pattern.matcher(line); - - // Debug message - this.getLogger().debug(MessageFormat.format("matches={0}", matcher.matches())); //NOI18N - - // Matches? - if (!matcher.matches()) { - // Corrupted file found - throw new CorruptedDatabaseFileException(this.fileName, MessageFormat.format("line {0} doesn't match regular expression.", line)); //NOI18N - } - - // Instance map - Map map = new HashMap<>(line.length() / 40); - - pattern = Pattern.compile("(key=([a-z0-9_]{1,}),value=\"([^\"]*)\";)"); //NOI18N - matcher = pattern.matcher(line); - - // Init group count - int init = 0; - - // Then get all - while (matcher.find(init)) { - // Get group match - String match = matcher.group(1); - String key = matcher.group(2); - String value = matcher.group(3); - - // key must noch be empty - assert((key != null) && (!key.isEmpty())) : MessageFormat.format("key={0} is not valid", key); //NOI18N - - // Get start and end - int start = matcher.start(); - int end = matcher.end(); - - // Debug message - this.getLogger().debug(MessageFormat.format("init={0},start={1},end={2},match={3},key={4},value={5}", init, start, end, match, key, value)); //NOI18N - - // Add key/value to map - map.put(key, value); - - // Hop to next match - init = end; - } - - // Trace message - this.getLogger().trace(MessageFormat.format("map()={0} - EXIT!", map.size())); //NOI18N - - // Return finished map - return map; - } - - @Override - public final Long getTotalRows () throws IOException { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - // Init count - Long count = 0L; - - // First rewind - this.rewind(); - - // Walk through all rows - while (!this.isEndOfFile()) { - // Get next line - String line = this.readLine(); - - // Debug message - this.getLogger().debug(MessageFormat.format("line={0}", line)); //NOI18N - - // Count one up - count++; - } - - // Trace message - this.getLogger().trace(MessageFormat.format("count={0} - EXIT!", count)); //NOI18N - - // Return it - return count; - } } -- 2.39.5