diff --git a/KissMeConsole/Sources/KissConsole+DB.swift b/KissMeConsole/Sources/KissConsole+DB.swift index ed5dcf3..59b907f 100644 --- a/KissMeConsole/Sources/KissConsole+DB.swift +++ b/KissMeConsole/Sources/KissConsole+DB.swift @@ -247,37 +247,39 @@ extension KissConsole { func collectCandleMinuteFiles(productNo: String?, year: String?, month: String?, day: String?) -> [String: [URL]] { print("Collecting candle csv for \(productNo ?? "all") at \(year ?? "")\(month ?? "")\(day ?? "") .....") - guard let enumerator = subPathForProduct(productNo: productNo) else { - return [:] - } - - let candleMinName = CandleMinuteFileName() - var candleFiles = [String: [URL]]() - for case let fileUrl as URL in enumerator { - guard let (fileProductNo, yyyyMMdd) = candleMinName.matchedUrl(fileUrl.path) else { - continue - } - if let productNo = productNo, productNo != fileProductNo { - continue - } - if let year = year, yyyyMMdd.prefix(4) != year { - continue - } - if let month = month, yyyyMMdd.dropFirst(4).prefix(2) != month { - continue - } - if let day = day, yyyyMMdd.suffix(2) != day { - continue + return autoreleasepool { + guard let enumerator = subPathForProduct(productNo: productNo) else { + return [:] } - if candleFiles.keys.contains(fileProductNo) { - candleFiles[fileProductNo]!.append(fileUrl) - } - else { - candleFiles[fileProductNo] = [fileUrl] + let candleMinName = CandleMinuteFileName() + var candleFiles = [String: [URL]]() + for case let fileUrl as URL in enumerator { + guard let (fileProductNo, yyyyMMdd) = candleMinName.matchedUrl(fileUrl.path) else { + continue + } + if let productNo = productNo, productNo != fileProductNo { + continue + } + if let year = year, yyyyMMdd.prefix(4) != year { + continue + } + if let month = month, yyyyMMdd.dropFirst(4).prefix(2) != month { + continue + } + if let day = day, yyyyMMdd.suffix(2) != day { + continue + } + + if candleFiles.keys.contains(fileProductNo) { + candleFiles[fileProductNo]!.append(fileUrl) + } + else { + candleFiles[fileProductNo] = [fileUrl] + } } + return candleFiles } - return candleFiles } /// - Parameters: @@ -293,41 +295,8 @@ extension KissConsole { } try? FileManager.default.createDirectory(at: yearDbPath, withIntermediateDirectories: true) - do { - let candles = try [Domestic.Candle].readCsv(fromFile: csvFile) - - let db = try KissDB(directory: yearDbPath) - try db.begin() - - var isFirstValidated = false - for candle in candles { - if trimAfterMarket { - guard candle.isValidInMarketTime else { - continue - } - } - let candleData = try CandleData(candle: candle) - - if !isFirstValidated { - try db.selectData(key: candleData.key, into: { dataItem -> Bool in - if dataItem.key == candleData.key { - isFirstValidated = true - print("Skipped by first validation: \(csvFile)") - } - return true - }) - } - let item = KissDB.DataItem(key: candleData.key, value: candleData.data) - try db.insertData(item: item) - - if candleData.candle != candle { - assertionFailure("invalid candle data") - } - } - - try db.commit() - } catch { - print("\(error)") + if false == buildCandleMinuteDB(to: yearDbPath, from: csvFile, trimAfterMarket: trimAfterMarket) { + print("Stopped by error") return false } } @@ -335,6 +304,49 @@ extension KissConsole { return true } + private func buildCandleMinuteDB(to yearDbPath: URL, from csvFile: URL, trimAfterMarket: Bool) -> Bool { + autoreleasepool { + do { + let candles = try [Domestic.Candle].readCsv(fromFile: csvFile) + + let db = try KissDB(directory: yearDbPath) + try db.begin() + + var isFirstValidated = false + for candle in candles { + if trimAfterMarket { + guard candle.isValidInMarketTime else { + continue + } + } + let candleData = try CandleData(candle: candle) + + if !isFirstValidated { + try db.selectData(key: candleData.key, into: { dataItem -> Bool in + if dataItem.key == candleData.key { + isFirstValidated = true + print("Skipped by first validation: \(csvFile)") + } + return true + }) + } + let item = KissDB.DataItem(key: candleData.key, value: candleData.data) + try db.insertData(item: item) + + if candleData.candle != candle { + assertionFailure("invalid candle data") + } + } + + try db.commit() + return true + } catch { + print("\(error)") + return false + } + } + } + static func candleMinuteDBPath(productNo: String, year: String) -> URL { let dataDbPath = URL.currentDirectory().appending(path: "data-db") let yearDbPath = dataDbPath.appending(path: "\(productNo)/min/candle-\(year).db1")