Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading the legacy storage format #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Pantry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
658AA9001C0C6E0C00DD4834 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 658AA8FF1C0C6E0C00DD4834 /* Assets.xcassets */; };
658AA9051C0C6E1100DD4834 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 658AA9011C0C6E1100DD4834 /* LaunchScreen.storyboard */; };
658AA9061C0C6E1100DD4834 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 658AA9031C0C6E1100DD4834 /* Main.storyboard */; };
A5DD953320BEC8AB00784A82 /* StorageType.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5DD953220BEC8AB00784A82 /* StorageType.swift */; };
AF3C06E51C3CAC9800234504 /* EnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF3C06E31C3CAC1400234504 /* EnumTests.swift */; };
AF5486601C16360C00E00CD0 /* MemoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF54865F1C16360C00E00CD0 /* MemoryTests.swift */; };
AF5486621C16385500E00CD0 /* TestTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF5486611C16385500E00CD0 /* TestTypes.swift */; };
Expand Down Expand Up @@ -65,6 +66,7 @@
658AA9071C0C6E1800DD4834 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = PantryExample/Info.plist; sourceTree = SOURCE_ROOT; };
658AA9081C0C7CC700DD4834 /* Storable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Storable.swift; path = Pantry/Storable.swift; sourceTree = SOURCE_ROOT; };
658AA90C1C0C823800DD4834 /* JSONWarehouse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSONWarehouse.swift; path = Pantry/JSONWarehouse.swift; sourceTree = SOURCE_ROOT; };
A5DD953220BEC8AB00784A82 /* StorageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = StorageType.swift; path = Pantry/StorageType.swift; sourceTree = SOURCE_ROOT; };
AF3C06E31C3CAC1400234504 /* EnumTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EnumTests.swift; path = PantryTests/EnumTests.swift; sourceTree = SOURCE_ROOT; };
AF5486561C160B2200E00CD0 /* Warehousable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Warehousable.swift; path = Pantry/Warehousable.swift; sourceTree = SOURCE_ROOT; };
AF5486591C16269A00E00CD0 /* MemoryWarehouse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MemoryWarehouse.swift; path = Pantry/MemoryWarehouse.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -148,6 +150,7 @@
658AA90C1C0C823800DD4834 /* JSONWarehouse.swift */,
658AA8F41C0C6D9B00DD4834 /* Pantry.h */,
658AA8F61C0C6DA200DD4834 /* Pantry.swift */,
A5DD953220BEC8AB00784A82 /* StorageType.swift */,
658AA9081C0C7CC700DD4834 /* Storable.swift */,
AF5486561C160B2200E00CD0 /* Warehousable.swift */,
AF54865C1C162B6A00E00CD0 /* WarehouseCacheable.swift */,
Expand Down Expand Up @@ -326,6 +329,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A5DD953320BEC8AB00784A82 /* StorageType.swift in Sources */,
ECF52F611C53F3D000FBC6BA /* TestEnumTypes.swift in Sources */,
E3E39F201C19D574008989E9 /* MemoryWarehouse.swift in Sources */,
E3E39F211C19D574008989E9 /* JSONWarehouse.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
90 changes: 75 additions & 15 deletions Pantry/JSONWarehouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ JSONWarehouse serializes and deserializes data
A `JSONWarehouse` is passed in the init function of a struct that conforms to `Storable`
*/
open class JSONWarehouse: Warehouseable, WarehouseCacheable {

var storageType: StorageType
var key: String
var context: Any?

public init(key: String) {
public init(storageType: StorageType, key: String) {
self.storageType = storageType
self.key = key
}

public init(context: Any) {
public init(storageType: StorageType, context: Any) {
self.storageType = storageType
self.key = ""
self.context = context
}
Expand Down Expand Up @@ -78,7 +82,7 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {
return nil
}

let warehouse = JSONWarehouse(context: result)
let warehouse = JSONWarehouse(storageType: storageType, context: result)
return T(warehouse: warehouse)
}

Expand All @@ -98,7 +102,7 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {

var unpackedItems = [T]()
for case let item as [String: Any] in result {
let warehouse = JSONWarehouse(context: item)
let warehouse = JSONWarehouse(storageType: storageType, context: item)
if let item = T(warehouse: warehouse) {
unpackedItems.append(item)
}
Expand All @@ -108,7 +112,7 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {
}

func write(_ object: Any, expires: StorageExpiry) {
let cacheLocation = cacheFileURL()
let cacheLocation = storageFileUrl()
var storableDictionary: [String: Any] = [:]

storableDictionary["expires"] = expires.toDate().timeIntervalSince1970
Expand All @@ -130,15 +134,15 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {

func removeCache() {
do {
try FileManager.default.removeItem(at: cacheFileURL())
try FileManager.default.removeItem(at: storageFileUrl())
} catch {
print("error removing cache", error)
}
}

static func removeAllCache() {
static func removeAllCache(for storageType: StorageType) {
do {
try FileManager.default.removeItem(at: JSONWarehouse.cacheDirectory)
try FileManager.default.removeItem(at: JSONWarehouse.storageDirectory(for: storageType))
} catch {
print("error removing all cache",error)
}
Expand All @@ -149,16 +153,22 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {
return context
}

let cacheLocation = cacheFileURL()
let cacheLocation = storageFileUrl()

// legacy format
if let metaDictionary = NSDictionary(contentsOf: cacheLocation),
let cache = metaDictionary["storage"] {
return cache
}

if let data = try? Data(contentsOf: cacheLocation),
let metaDictionary = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let cache = metaDictionary?["storage"] {
return cache
}

if let data = try? Data(contentsOf: cacheLocation),
let metaDictionary = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let metaDictionary = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let cache = metaDictionary?["storage"] {
return cache
}
Expand All @@ -167,13 +177,28 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {
}

func cacheExists() -> Bool {
guard FileManager.default.fileExists(atPath: cacheFileURL().path),
let data = try? Data(contentsOf: cacheFileURL()),
let metaDictionary = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
return false
let cacheFileURL = self.cacheFileURL()

guard FileManager.default.fileExists(atPath: storageFileUrl.path) else { return false }

var optionalDictionary: [String: Any?]? = nil

// legacy format
if let dictionary = NSDictionary(contentsOf: storageFileUrl) as? [String: Any?] {
optionalDictionary = dictionary
}

// new format
if let data = try? Data(contentsOf: storageFileUrl),
let dictionary = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
optionalDictionary = dictionary
}

guard let metaDictionary = optionalDictionary else {
return false
}

guard let expires = metaDictionary?["expires"] as? TimeInterval else {
guard let expires = metaDictionary["expires"] as? TimeInterval else {
// no expire time means old cache, never expires
return true
}
Expand All @@ -188,6 +213,41 @@ open class JSONWarehouse: Warehouseable, WarehouseCacheable {
}
}

static func storageDirectory(for storageType: StorageType) -> URL {
switch storageType {
case .volatile: return cacheDirectory
case .permanent: return applicationSupportDirectory
}
}

private func storageFileUrl() -> URL {
switch storageType {
case .volatile: return cacheFileURL()
case .permanent: return applicationSupportFileURL()
}
}

static var applicationSupportDirectory: URL {
let url = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!

let writeDirectory = url.appendingPathComponent("com.thatthinginswift.pantry")
return writeDirectory
}

func applicationSupportFileURL() -> URL {
let applicationSupportDirectory = JSONWarehouse.applicationSupportDirectory

let applicationSupportLocation = applicationSupportDirectory.appendingPathComponent(self.key)

do {
try FileManager.default.createDirectory(at: applicationSupportDirectory, withIntermediateDirectories: true, attributes: nil)
} catch {
print("couldn't create directories to \(applicationSupportLocation)")
}

return applicationSupportLocation
}

static var cacheDirectory: URL {
let url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!

Expand Down
2 changes: 1 addition & 1 deletion Pantry/MemoryWarehouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension MemoryWarehouse: WarehouseCacheable {
MemoryWarehouse.globalCache.removeValue(forKey: key)
}

static func removeAllCache() {
static func removeAllCache(for storageType: StorageType) {
MemoryWarehouse.globalCache = [:]
}

Expand Down
Loading