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

Diagnostics: new FileHandler for abstracting file operations #2673

Merged
merged 2 commits into from
Jun 22, 2023

Conversation

NachoSoto
Copy link
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
}

/// Deletes the first N lines from the file, without loading the entire file in memory.
func removeFirstLines(_ count: Int) async throws {
Copy link
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.

Copy link
Contributor

@tonidero tonidero left a comment

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
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
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
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
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
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
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
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
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 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