This repository was archived by the owner on Aug 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
feat: rotate cache folder #494
Merged
marandaneto
merged 5 commits into
getsentry:3.0.0
from
marandaneto:feat/rotate-cache-folder
Jul 28, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c677596
feat: rotate cache folder
marandaneto cfd4cb0
ref tests
marandaneto 2807010
Update sentry-core/src/main/java/io/sentry/core/cache/CacheStrategy.java
marandaneto a2f1fac
code review
marandaneto 696da07
make fields final
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
sentry-core/src/main/java/io/sentry/core/cache/CacheStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package io.sentry.core.cache; | ||
|
|
||
| import static io.sentry.core.SentryLevel.ERROR; | ||
|
|
||
| import io.sentry.core.ISerializer; | ||
| import io.sentry.core.SentryLevel; | ||
| import io.sentry.core.SentryOptions; | ||
| import io.sentry.core.util.Objects; | ||
| import java.io.File; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Arrays; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| abstract class CacheStrategy { | ||
|
|
||
| @SuppressWarnings("CharsetObjectCanBeUsed") | ||
| protected static final Charset UTF_8 = Charset.forName("UTF-8"); | ||
|
|
||
| protected final @NotNull SentryOptions options; | ||
| protected final @NotNull ISerializer serializer; | ||
| protected final @NotNull File directory; | ||
| private final int maxSize; | ||
|
|
||
| CacheStrategy( | ||
| final @NotNull SentryOptions options, | ||
| final @NotNull String directoryPath, | ||
| final int maxSize) { | ||
| Objects.requireNonNull(directoryPath, "Directory is required."); | ||
| this.options = Objects.requireNonNull(options, "SentryOptions is required."); | ||
|
|
||
| this.serializer = options.getSerializer(); | ||
| this.directory = new File(directoryPath); | ||
|
|
||
| this.maxSize = maxSize; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a dir. is valid and have write and read permission | ||
| * | ||
| * @return true if valid and has permissions or false otherwise | ||
| */ | ||
| protected boolean isDirectoryValid() { | ||
| if (!directory.isDirectory() || !directory.canWrite() || !directory.canRead()) { | ||
| options | ||
| .getLogger() | ||
| .log( | ||
| ERROR, | ||
| "The directory for caching files is inaccessible.: %s", | ||
| directory.getAbsolutePath()); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Sort files from oldest to the newest using the lastModified method | ||
| * | ||
| * @param files the Files | ||
| */ | ||
| private void sortFilesOldestToNewest(@NotNull File[] files) { | ||
| // just sort it if more than 1 file | ||
| if (files.length > 1) { | ||
| Arrays.sort(files, (f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified())); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Rotates the caching folder if full, deleting the oldest files first | ||
| * | ||
| * @param files the Files | ||
| */ | ||
| protected void rotateCacheIfNeeded(final @NotNull File[] files) { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final int length = files.length; | ||
| if (length >= maxSize) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.WARNING, "Cache folder if full (respecting maxSize). Rotating files"); | ||
| final int totalToBeDeleted = (length - maxSize) + 1; | ||
|
|
||
| sortFilesOldestToNewest(files); | ||
|
|
||
| // delete files from the top of the Array as its sorted by the oldest to the newest | ||
| for (int i = 0; i < totalToBeDeleted; i++) { | ||
| final File file = files[i]; | ||
| // sanity check if the file actually exists. | ||
| if (!file.delete()) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.WARNING, "File can't be deleted: %s", file.getAbsolutePath()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: I think we could split up this class into two classes. For me, we have two different responsibilities in here. We have some base functionality that bothDiskCacheandSessionCacheneed. And we have the sorting and deleting of the files. What about renamingCacheStrategyto something likeSentryCacheorBaseCache, although I'm not a fan of naming something base, ... and extractingsortFilesOldestToNewestandrotateCacheIfNeededto a class calledFileCacheStrategyor something similar. I'm a huge fan of composition over inheritance. So with the extracted class, we wouldn't have to createCustomCachein theCacheStrategyTestto be able to test the deleting and sorting of the files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if it's worth creating another extra class to achieve this, actually only
we wouldn't have to create CustomCache in the CacheStrategyTestis not enough argument as this is not added to the classpath, only on tests.I still would need a class + an interface, ideally
DiskCacheandSessionCachewill be merged very soon (when events become purely envelopes) and this class might lose its meaning, I mean, it'll be a single class anywayopinions @bruno-garcia ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also a huge fan of composition over inheritence but IMHO this is a good use of inheritance.
Also, this stuff is internal so no need to over engineer this and break it apart. If it grows in the upcoming changes, we can refactor then.
That said, IMHO LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already stated my opinion and still with your arguments for keeping as it is I'm in favor of splitting it up, but I have no strong opinion on this as it's just a small class. I'm fine with keeping as it is. No need for further discussion IMO.