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

Extra Index Manifests #236

Merged
merged 2 commits into from
Oct 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ extension DiskPersistence.Transaction: DatastoreInterfaceProtocol {
rootObject: rootManifest
)
createdRootObjects.insert(newRootObject)
if createdRootObjects.contains(existingRootObject) {
createdRootObjects.remove(existingRootObject)
} else {
if createdRootObjects.remove(existingRootObject) == nil {
deletedRootObjects.insert(existingRootObject)
}
await datastore.adopt(rootObject: newRootObject)
Expand Down Expand Up @@ -925,14 +923,12 @@ extension DiskPersistence.Transaction {
let datastore = existingRootObject.datastore

/// Depending on the cursor type, insert or replace the entry in the index, capturing the new manifesr, added and removed pages, and change in the number of entries.
let ((indexManifest, newPages, removedPages), newEntryCount) = try await {
switch try cursor(for: someCursor) {
case .insertion(let cursor):
return (try await existingIndex.manifest(inserting: entry, at: cursor), 1)
case .instance(let cursor):
return (try await existingIndex.manifest(replacing: entry, at: cursor), 0)
}
}()
let ((indexManifest, newPages, removedPages), newEntryCount) = switch try cursor(for: someCursor) {
case .insertion(let cursor):
(try await existingIndex.manifest(inserting: entry, at: cursor), 1)
case .instance(let cursor):
(try await existingIndex.manifest(replacing: entry, at: cursor), 0)
}

/// No change occured, bail early
guard existingIndex.id.manifestID != indexManifest.id else { return }
Expand All @@ -949,9 +945,7 @@ extension DiskPersistence.Transaction {
manifest: indexManifest
)
createdIndexes.insert(newIndex)
if createdIndexes.contains(existingIndex) {
createdIndexes.insert(existingIndex)
} else {
if createdIndexes.remove(existingIndex) == nil {
deletedIndexes.insert(existingIndex)
}
await datastore.adopt(index: newIndex)
Expand All @@ -972,9 +966,7 @@ extension DiskPersistence.Transaction {
rootObject: rootManifest
)
createdRootObjects.insert(newRootObject)
if createdRootObjects.contains(existingRootObject) {
createdRootObjects.remove(existingRootObject)
} else {
if createdRootObjects.remove(existingRootObject) == nil {
deletedRootObjects.insert(existingRootObject)
}
await datastore.adopt(rootObject: newRootObject)
Expand Down Expand Up @@ -1043,9 +1035,7 @@ extension DiskPersistence.Transaction {
manifest: indexManifest
)
createdIndexes.insert(newIndex)
if createdIndexes.contains(existingIndex) {
createdIndexes.insert(existingIndex)
} else {
if createdIndexes.remove(existingIndex) == nil {
deletedIndexes.insert(existingIndex)
}
await datastore.adopt(index: newIndex)
Expand All @@ -1066,9 +1056,7 @@ extension DiskPersistence.Transaction {
rootObject: rootManifest
)
createdRootObjects.insert(newRootObject)
if createdRootObjects.contains(existingRootObject) {
createdRootObjects.remove(existingRootObject)
} else {
if createdRootObjects.remove(existingRootObject) == nil {
deletedRootObjects.insert(existingRootObject)
}
await datastore.adopt(rootObject: newRootObject)
Expand Down Expand Up @@ -1119,9 +1107,7 @@ extension DiskPersistence.Transaction {
manifest: indexManifest
)
createdIndexes.insert(newIndex)
if createdIndexes.contains(existingIndex) {
createdIndexes.insert(existingIndex)
} else {
if createdIndexes.remove(existingIndex) == nil {
deletedIndexes.insert(existingIndex)
}
await datastore.adopt(index: newIndex)
Expand All @@ -1140,9 +1126,7 @@ extension DiskPersistence.Transaction {
rootObject: rootManifest
)
createdRootObjects.insert(newRootObject)
if createdRootObjects.contains(existingRootObject) {
createdRootObjects.remove(existingRootObject)
} else {
if createdRootObjects.remove(existingRootObject) == nil {
deletedRootObjects.insert(existingRootObject)
}
await datastore.adopt(rootObject: newRootObject)
Expand Down Expand Up @@ -1227,9 +1211,7 @@ extension DiskPersistence.Transaction {
rootObject: rootManifest
)
createdRootObjects.insert(newRootObject)
if createdRootObjects.contains(existingRootObject) {
createdRootObjects.remove(existingRootObject)
} else {
if createdRootObjects.remove(existingRootObject) == nil {
deletedRootObjects.insert(existingRootObject)
}
await datastore.adopt(rootObject: newRootObject)
Expand Down Expand Up @@ -1310,9 +1292,7 @@ extension DiskPersistence.Transaction {
rootObject: rootManifest
)
createdRootObjects.insert(newRootObject)
if createdRootObjects.contains(existingRootObject) {
createdRootObjects.remove(existingRootObject)
} else {
if createdRootObjects.remove(existingRootObject) == nil {
deletedRootObjects.insert(existingRootObject)
}
await datastore.adopt(rootObject: newRootObject)
Expand Down
157 changes: 157 additions & 0 deletions Tests/CodableDatastoreTests/DiskPersistenceDatastoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,163 @@ final class DiskPersistenceDatastoreTests: XCTestCase, @unchecked Sendable {
try? FileManager.default.removeItem(at: temporaryStoreURL)
}

func testCreatingEmptyPersistence() async throws {
struct TestFormat: DatastoreFormat {
enum Version: Int, CaseIterable {
case zero
}

struct Instance: Codable, Identifiable {
var id: String
var value: String
var index: Int
var bucket: Int
}

static let defaultKey: DatastoreKey = "test"
static let currentVersion = Version.zero

let index = OneToOneIndex(\.index)
@Direct var bucket = Index(\.bucket)
}

let persistence = try DiskPersistence(readWriteURL: temporaryStoreURL)

_ = Datastore.JSONStore(
persistence: persistence,
format: TestFormat.self,
migrations: [
.zero: { data, decoder in
try decoder.decode(TestFormat.Instance.self, from: data)
}
]
)

try await persistence.createPersistenceIfNecessary()

let snapshotContents = try FileManager().contentsOfDirectory(at: temporaryStoreURL.appendingPathComponent("Snapshots", isDirectory: true), includingPropertiesForKeys: nil)
XCTAssertEqual(snapshotContents.count, 0)
}

func testCreatingEmptyDatastoreIndexesAfterRead() async throws {
struct TestFormat: DatastoreFormat {
enum Version: Int, CaseIterable {
case zero
}

struct Instance: Codable, Identifiable {
var id: String
var value: String
var index: Int
var bucket: Int
}

static let defaultKey: DatastoreKey = "test"
static let currentVersion = Version.zero

let index = OneToOneIndex(\.index)
@Direct var bucket = Index(\.bucket)
}

let persistence = try DiskPersistence(readWriteURL: temporaryStoreURL)

let datastore = Datastore.JSONStore(
persistence: persistence,
format: TestFormat.self,
migrations: [
.zero: { data, decoder in
try decoder.decode(TestFormat.Instance.self, from: data)
}
]
)

let count = try await datastore.count
XCTAssertEqual(count, 0)

// TODO: Add code to verify that the Datastores directory is empty. This is true as of 2024-10-10, but has only been validated manually.
}

func testCreatingEmptyDatastoreIndexesAfterSingleWrite() async throws {
struct TestFormat: DatastoreFormat {
enum Version: Int, CaseIterable {
case zero
}

struct Instance: Codable, Identifiable {
var id: String
var value: String
var index: Int
var bucket: Int
}

static let defaultKey: DatastoreKey = "test"
static let currentVersion = Version.zero

let index = OneToOneIndex(\.index)
@Direct var bucket = Index(\.bucket)
}

let persistence = try DiskPersistence(readWriteURL: temporaryStoreURL)

let datastore = Datastore.JSONStore(
persistence: persistence,
format: TestFormat.self,
migrations: [
.zero: { data, decoder in
try decoder.decode(TestFormat.Instance.self, from: data)
}
]
)

try await datastore.persist(.init(id: "0", value: "0", index: 0, bucket: 0))

let count = try await datastore.count
XCTAssertEqual(count, 1)

// TODO: Add code to verify that the Index directories have a single manifest each. This is true as of 2024-10-10, but has only been validated manually.
}

func testCreatingUnreferencedDatastoreIndexesAfterUpdate() async throws {
struct TestFormat: DatastoreFormat {
enum Version: Int, CaseIterable {
case zero
}

struct Instance: Codable, Identifiable {
var id: String
var value: String
var index: Int
var bucket: Int
}

static let defaultKey: DatastoreKey = "test"
static let currentVersion = Version.zero

let index = OneToOneIndex(\.index)
@Direct var bucket = Index(\.bucket)
}

let persistence = try DiskPersistence(readWriteURL: temporaryStoreURL)

let datastore = Datastore.JSONStore(
persistence: persistence,
format: TestFormat.self,
migrations: [
.zero: { data, decoder in
try decoder.decode(TestFormat.Instance.self, from: data)
}
]
)

try await datastore.persist(.init(id: "0", value: "0", index: 0, bucket: 0))
try await datastore.persist(.init(id: "0", value: "0", index: 0, bucket: 0))

let count = try await datastore.count
XCTAssertEqual(count, 1)

// TODO: Add code to verify that the Index directories have exactly two index manifests each. This is true as of 2024-10-10, but has only been validated manually.
}

func testWritingEntry() async throws {
struct TestFormat: DatastoreFormat {
enum Version: Int, CaseIterable {
Expand Down
Loading