From e3ce7c064738606c8a365cb931761d9eb43dce81 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 29 Jul 2021 10:40:44 +0400 Subject: [PATCH] #5 -- save metadata --- src/main/java/ru/ulstu/db/DbFileService.java | 17 ++++++++++++----- src/main/java/ru/ulstu/db/DbService.java | 4 ++-- .../java/ru/ulstu/db/model/TimeSeriesMeta.java | 11 +++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/ulstu/db/DbFileService.java b/src/main/java/ru/ulstu/db/DbFileService.java index de69472..220be1a 100644 --- a/src/main/java/ru/ulstu/db/DbFileService.java +++ b/src/main/java/ru/ulstu/db/DbFileService.java @@ -72,24 +72,31 @@ public class DbFileService implements DbService { } @Override - public boolean addSet(String key) { + public void addSet(String key) { TimeSeriesSet timeSeriesSet = new TimeSeriesSet(key); if (isTimeSeriesSetExists(timeSeriesSet)) { throw new RuntimeException(String.format("Time series set %s already exists", timeSeriesSet.getKey())); } else { - return Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().mkdirs(); + Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile().mkdirs(); } } @Override - public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException { + public void addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException { if (!isTimeSeriesSetExists(timeSeriesSet)) { addSet(timeSeriesSet.getKey()); } BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), timeSeries.getName() + ".csv").toFile())); writer.write(new UtilService().getTimeSeriesToDateValueString(timeSeries)); writer.close(); - return true; + createMetaFile(timeSeriesSet, timeSeries); + } + + private void createMetaFile(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException { + TimeSeriesMeta timeSeriesMeta = new TimeSeriesMeta(timeSeries); + new ObjectMapper() + .writeValue(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), timeSeries.getName() + ".csv.meta") + .toFile(), timeSeriesMeta); } @Override @@ -138,7 +145,7 @@ public class DbFileService implements DbService { } private List getTimeSeriesMetaFiles(TimeSeriesSet timeSeriesSet) { - return Arrays.asList(getSetPath(timeSeriesSet).listFiles(f -> f.getName().endsWith(".meta"))); + return Arrays.asList(getSetPath(timeSeriesSet).listFiles(f -> f.getName().endsWith(".csv.meta"))); } private File getTimeSeriesFile(TimeSeriesSet timeSeriesSet, String timeSeriesKey) { diff --git a/src/main/java/ru/ulstu/db/DbService.java b/src/main/java/ru/ulstu/db/DbService.java index f24bea7..a3457e9 100644 --- a/src/main/java/ru/ulstu/db/DbService.java +++ b/src/main/java/ru/ulstu/db/DbService.java @@ -20,9 +20,9 @@ public interface DbService { TimeSeries getTimeSeries(TimeSeriesSet timeSeriesSet, String timeSeriesKey) throws IOException; - boolean addSet(String key); + void addSet(String key); - boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException; + void addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException; void deleteTimeSeries(TimeSeriesSet set, String timeSeriesKey) throws IOException; diff --git a/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java b/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java index a885d1f..f0e1c5d 100644 --- a/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java +++ b/src/main/java/ru/ulstu/db/model/TimeSeriesMeta.java @@ -6,11 +6,22 @@ package ru.ulstu.db.model; +import ru.ulstu.datamodel.ts.TimeSeries; + public class TimeSeriesMeta { private String key; private int size; private boolean hasDateTime; + public TimeSeriesMeta() { + } + + public TimeSeriesMeta(TimeSeries timeSeries) { + this.key = timeSeries.getName(); + this.hasDateTime = timeSeries.getValues().stream().anyMatch(v -> v.getDate() != null); + this.size = timeSeries.getLength(); + } + public String getKey() { return key; }