Skip to content

Diagnostics: new FileHandler for abstracting file operations#2673

Merged
NachoSoto merged 2 commits into
mainfrom
diagnostics/file-handler
Jun 22, 2023
Merged

Diagnostics: new FileHandler for abstracting file operations#2673
NachoSoto merged 2 commits into
mainfrom
diagnostics/file-handler

Conversation

@NachoSoto

Copy link
Copy Markdown
Contributor

Implemented as an actor to easily synchronize operations on a file.

Simple interface:
Screenshot 2023-06-16 at 16 45 14

@NachoSoto NachoSoto requested a review from a team June 16, 2023 23:45
@NachoSoto NachoSoto force-pushed the diagnostics/file-handler branch 3 times, most recently from ffd0d4e to c45de52 Compare June 16, 2023 23:53
Comment thread Sources/Diagnostics/FileHandler.swift Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation avoids loading the entire file in memory.

@NachoSoto NachoSoto force-pushed the diagnostics/file-handler branch from c45de52 to 07d6d75 Compare June 16, 2023 23:54

@tonidero tonidero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

throw Error.failedMovingNewFile(from: otherURL, toURL: self.url, error)
}

self.fileHandle.closeAndLogErrors()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So closing the fileHandle AFTER we've removed the original item won't be a problem? I'd imagine we would need to close it before deleting the file but I might be wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically just a file descriptor inside. I did this after moving it because we don't want to leave an inconsistent state if any of the move operations fail.

return result
}

func replaceHandler(with otherURL: URL) throws {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this function should be executed atomically, so if there are multiple threads using this handler, this doesn't cause any issues (We don't do this in Android either)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's handled by the fact that this type is an actor :)
https://www.swiftbysundell.com/articles/swift-actors/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right!

await self.handler.append(line: content)

let data = try await self.handler.readFile()
expect(data).to(matchLines(content))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a test making sure the file ends with a new line break?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it wasn't that important to add an explicit test, as long as the other logic works as expected (appending an existing file, etc). What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense 👍

@NachoSoto NachoSoto force-pushed the diagnostics/file-handler branch from 07d6d75 to 8a69cf4 Compare June 22, 2023 19:44
@NachoSoto NachoSoto enabled auto-merge (squash) June 22, 2023 19:44
@NachoSoto NachoSoto merged commit f05fd88 into main Jun 22, 2023
@NachoSoto NachoSoto deleted the diagnostics/file-handler branch June 22, 2023 20:02
tonidero pushed a commit that referenced this pull request Jun 26, 2023
**This is an automatic release.**

### Bugfixes
* Fix google play purchases missing purchase date (#2703) via Toni Rico
(@tonidero)
### Other Changes
* `PurchaseTester`: fixed `watchOS` build and ASC deployment (#2701) via
NachoSoto (@NachoSoto)
* Add `Data.sha1` (#2696) via NachoSoto (@NachoSoto)
* Refactor: extract `ErrorResponse` into its own file (#2697) via
NachoSoto (@NachoSoto)
* Add `Sequence<AdditiveArithmetic>.sum()` (#2694) via NachoSoto
(@NachoSoto)
* Refactored `Data.asString` implementation (#2695) via NachoSoto
(@NachoSoto)
* `Diagnostics`: new `FileHandler` for abstracting file operations
(#2673) via NachoSoto (@NachoSoto)
NachoSoto added a commit that referenced this pull request Sep 6, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 6, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 7, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 7, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 7, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 7, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 7, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store an event: `PaywallEvent` + user ID
- `Created PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 7, 2023
### Changes:

- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store
an event: `PaywallEvent` + user ID
- Created `PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 8, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store
an event: `PaywallEvent` + user ID
- Created `PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 14, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store
an event: `PaywallEvent` + user ID
- Created `PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 14, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store
an event: `PaywallEvent` + user ID
- Created `PaywallEventSerializer`
NachoSoto added a commit that referenced this pull request Sep 15, 2023
- New `PaywallEventStore` using `FileHandler` (#2673)
- `MockFileHandler`
- `PaywallStoredEvent`: this contains the necessary information to store
an event: `PaywallEvent` + user ID
- Created `PaywallEventSerializer`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants