FileStorage.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Disk
  2. import Foundation
  3. protocol FileStorage {
  4. func save<Value: JSON>(_ value: Value, as name: String) throws
  5. func retrieve<Value: JSON>(_ name: String, as type: Value.Type) throws -> Value
  6. func append<Value: JSON>(_ newValue: Value, to name: String) throws
  7. func append<Value: JSON>(_ newValues: [Value], to name: String) throws
  8. func append<Value: JSON, T: Equatable>(_ newValue: Value, to name: String, uniqBy keyPath: KeyPath<Value, T>) throws
  9. func append<Value: JSON, T: Equatable>(_ newValues: [Value], to name: String, uniqBy keyPath: KeyPath<Value, T>) throws
  10. func remove(_ name: String) throws
  11. func rename(_ name: String, to newName: String) throws
  12. func transaction(_ exec: (FileStorage) throws -> Void) throws
  13. }
  14. final class BaseFileStorage: FileStorage {
  15. private let processQueue = DispatchQueue.markedQueue(label: "BaseFileStorage.processQueue", qos: .utility)
  16. private var encoder: JSONEncoder {
  17. let encoder = JSONEncoder()
  18. encoder.outputFormatting = .prettyPrinted
  19. encoder.dateEncodingStrategy = .iso8601
  20. return encoder
  21. }
  22. private var decoder: JSONDecoder {
  23. let decoder = JSONDecoder()
  24. decoder.dateDecodingStrategy = .iso8601
  25. return decoder
  26. }
  27. func save<Value: JSON>(_ value: Value, as name: String) throws {
  28. try processQueue.safeSync {
  29. try Disk.save(value, to: .documents, as: name, encoder: self.encoder)
  30. }
  31. }
  32. func retrieve<Value: JSON>(_ name: String, as type: Value.Type) throws -> Value {
  33. try processQueue.safeSync {
  34. try Disk.retrieve(name, from: .documents, as: type, decoder: decoder)
  35. }
  36. }
  37. func append<Value: JSON>(_ newValue: Value, to name: String) throws {
  38. try processQueue.safeSync {
  39. try Disk.append(newValue, to: name, in: .documents, decoder: decoder, encoder: encoder)
  40. }
  41. }
  42. func append<Value: JSON>(_ newValues: [Value], to name: String) throws {
  43. try processQueue.safeSync {
  44. try Disk.append(newValues, to: name, in: .documents, decoder: decoder, encoder: encoder)
  45. }
  46. }
  47. func append<Value: JSON, T: Equatable>(_ newValue: Value, to name: String, uniqBy keyPath: KeyPath<Value, T>) throws {
  48. if let value = try? retrieve(name, as: Value.self) {
  49. if value[keyPath: keyPath] != newValue[keyPath: keyPath] {
  50. try append(newValue, to: name)
  51. }
  52. } else if let values = try? retrieve(name, as: [Value].self) {
  53. guard values.first(where: { $0[keyPath: keyPath] == newValue[keyPath: keyPath] }) == nil else {
  54. return
  55. }
  56. try append(newValue, to: name)
  57. } else {
  58. try save(newValue, as: name)
  59. }
  60. }
  61. func append<Value: JSON, T: Equatable>(_ newValues: [Value], to name: String, uniqBy keyPath: KeyPath<Value, T>) throws {
  62. if let value = try? retrieve(name, as: Value.self) {
  63. guard newValues.first(where: { $0[keyPath: keyPath] == value[keyPath: keyPath] }) == nil else {
  64. return
  65. }
  66. try append(newValues, to: name)
  67. } else if let values = try? retrieve(name, as: [Value].self) {
  68. try newValues.forEach { newValue in
  69. guard values.first(where: { $0[keyPath: keyPath] == newValue[keyPath: keyPath] }) == nil else {
  70. return
  71. }
  72. try append(newValue, to: name)
  73. }
  74. } else {
  75. try save(newValues, as: name)
  76. }
  77. }
  78. func remove(_ name: String) throws {
  79. try processQueue.safeSync {
  80. try Disk.remove(name, from: .documents)
  81. }
  82. }
  83. func rename(_ name: String, to newName: String) throws {
  84. try processQueue.safeSync {
  85. try Disk.rename(name, in: .documents, to: newName)
  86. }
  87. }
  88. func transaction(_ exec: (FileStorage) throws -> Void) throws {
  89. try processQueue.safeSync {
  90. try exec(self)
  91. }
  92. }
  93. }