* @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
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<String, String> 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<String, String> 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
*
// 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<String, String> 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<String, String> 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;
- }
}