@@ -265,27 +265,28 @@ extension DiskPersistence.Transaction: DatastoreInterfaceProtocol {
265265 func apply( descriptor: DatastoreDescriptor , for datastoreKey: DatastoreKey ) async throws {
266266 try checkIsActive ( )
267267
268- if let rootObject = try await rootObject ( for: datastoreKey) {
269- var manifest = try await rootObject . manifest
268+ if let existingRootObject = try await rootObject ( for: datastoreKey) {
269+ let datastore = existingRootObject . datastore
270270
271- // TODO: Do a better merge of these descriptors, especially since size is something we want to preserve, amongst other properties
272- guard manifest. descriptor != descriptor else { return }
271+ let ( rootManifest, newIndexes) = try await existingRootObject. manifest ( applying: descriptor)
273272
274- manifest. id = DatastoreRootIdentifier ( )
275- manifest. modificationDate = Date ( )
276- manifest. descriptor = descriptor
273+ /// No change occured, bail early
274+ guard existingRootObject. id != rootManifest. id else { return }
277275
278- let newRoot = DiskPersistence . Datastore. RootObject (
279- datastore: rootObject. datastore,
280- id: manifest. id,
281- rootObject: manifest
282- )
283- rootObjects [ datastoreKey] = newRoot
284- createdRootObjects. insert ( newRoot)
285- deletedRootObjects. insert ( rootObject)
286- await newRoot. datastore. adopt ( rootObject: newRoot)
276+ for newIndex in newIndexes {
277+ createdIndexes. insert ( newIndex)
278+ await datastore. adopt ( index: newIndex)
279+ }
287280
288- // TODO: Don't forget to create the new index objects too!
281+ let newRootObject = DiskPersistence . Datastore. RootObject (
282+ datastore: datastore,
283+ id: rootManifest. id,
284+ rootObject: rootManifest
285+ )
286+ createdRootObjects. insert ( newRootObject)
287+ deletedRootObjects. insert ( existingRootObject)
288+ await datastore. adopt ( rootObject: newRootObject)
289+ rootObjects [ datastoreKey] = newRootObject
289290 } else {
290291 let ( datastore, _) = try await persistence. persistenceDatastore ( for: datastoreKey)
291292
@@ -345,6 +346,9 @@ extension DiskPersistence.Transaction: DatastoreInterfaceProtocol {
345346 await datastore. adopt ( index: index)
346347 }
347348
349+ var descriptor = descriptor
350+ descriptor. size = 0
351+
348352 /// Create the root object from the indexes that were created
349353 let manifest = DatastoreRootManifest (
350354 id: DatastoreRootIdentifier ( ) ,
@@ -555,12 +559,13 @@ extension DiskPersistence.Transaction {
555559
556560 let datastore = existingRootObject. datastore
557561
558- let ( indexManifest, newPages, removedPages) = try await {
562+ /// 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.
563+ let ( ( indexManifest, newPages, removedPages) , newEntryCount) = try await {
559564 switch try cursor ( for: someCursor) {
560565 case . insertion( let cursor) :
561- return try await existingIndex. manifest ( inserting: entry, at: cursor)
566+ return ( try await existingIndex. manifest ( inserting: entry, at: cursor) , 1 )
562567 case . instance( let cursor) :
563- return try await existingIndex. manifest ( replacing: entry, at: cursor)
568+ return ( try await existingIndex. manifest ( replacing: entry, at: cursor) , 0 )
564569 }
565570 } ( )
566571
@@ -582,7 +587,12 @@ extension DiskPersistence.Transaction {
582587 deletedIndexes. insert ( existingIndex)
583588 await datastore. adopt ( index: newIndex)
584589
585- let rootManifest = try await existingRootObject. manifest ( replacing: newIndex. id)
590+ var rootManifest = try await existingRootObject. manifest ( replacing: newIndex. id)
591+
592+ /// If the index we are modifying is the primary one, update the number of entries we are managing.
593+ if case . primary = newIndex. id {
594+ rootManifest. descriptor. size += newEntryCount
595+ }
586596
587597 /// No change occured, bail early
588598 guard existingRootObject. id != rootManifest. id else { return }
@@ -652,16 +662,21 @@ extension DiskPersistence.Transaction {
652662 }
653663 deletedPages. formUnion ( removedPages)
654664
655- let newPrimaryIndex = DiskPersistence . Datastore. Index (
665+ let newIndex = DiskPersistence . Datastore. Index (
656666 datastore: datastore,
657667 id: existingIndex. id. with ( manifestID: indexManifest. id) ,
658668 manifest: indexManifest
659669 )
660- createdIndexes. insert ( newPrimaryIndex )
670+ createdIndexes. insert ( newIndex )
661671 deletedIndexes. insert ( existingIndex)
662- await datastore. adopt ( index: newPrimaryIndex )
672+ await datastore. adopt ( index: newIndex )
663673
664- let rootManifest = try await existingRootObject. manifest ( replacing: newPrimaryIndex. id)
674+ var rootManifest = try await existingRootObject. manifest ( replacing: newIndex. id)
675+
676+ /// If the index we are modifying is the primary one, update the number of entries we are managing.
677+ if case . primary = newIndex. id {
678+ rootManifest. descriptor. size -= 1
679+ }
665680
666681 /// No change occured, bail early
667682 guard existingRootObject. id != rootManifest. id else { return }
0 commit comments