Skip to content

Commit

Permalink
Fixed an issue where extra index manifests were created on certain wr…
Browse files Browse the repository at this point in the history
…ites and updates

Closes #147, #227
  • Loading branch information
dimitribouniol committed Oct 10, 2024
1 parent 4ad5a0e commit 407a013
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ extension DiskPersistence.Transaction {
)
createdIndexes.insert(newIndex)
if createdIndexes.contains(existingIndex) {
createdIndexes.insert(existingIndex)
createdIndexes.remove(existingIndex)
} else {
deletedIndexes.insert(existingIndex)
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ extension DiskPersistence.Transaction {
)
createdIndexes.insert(newIndex)
if createdIndexes.contains(existingIndex) {
createdIndexes.insert(existingIndex)
createdIndexes.remove(existingIndex)
} else {
deletedIndexes.insert(existingIndex)
}
Expand Down Expand Up @@ -1120,7 +1120,7 @@ extension DiskPersistence.Transaction {
)
createdIndexes.insert(newIndex)
if createdIndexes.contains(existingIndex) {
createdIndexes.insert(existingIndex)
createdIndexes.remove(existingIndex)
} else {
deletedIndexes.insert(existingIndex)
}
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

0 comments on commit 407a013

Please sign in to comment.