-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(local): add directory size support #624
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5aeafd6
feat(local): add directory size support
LmanTW c84ade2
Merge branch 'OpenListTeam:main' into main
LmanTW 2867868
fix(local): fix and improve directory size calculation
LmanTW f37d560
style(local): fix code style
LmanTW f0f5ff1
style(local): fix code style
LmanTW 392ce6b
style(local): fix code style
LmanTW 156cd3b
fix(local): refresh directory size when force refresh
dezhishen 99af56a
fix:(local): Avoid traversing the parent's parent, which leads to an …
dezhishen a5afb0f
fix(local:) refresh dir size only enabled
dezhishen 87f6c5e
fix(local): logical error && add RecalculateDirSize && cleaner code f…
dezhishen 62404ac
Merge branch 'main' into main
dezhishen 84c3250
feat(local): add Benchmark for CalculateDirSize
dezhishen 5190bd6
refactor(local): 优化移动中对于错误的判断。
dezhishen 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
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,92 @@ | ||
| package local | ||
|
|
||
| // TestDirCalculateSize tests the directory size calculation | ||
| // It should be run with the local driver enabled and directory size calculation set to true | ||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/OpenListTeam/OpenList/v4/internal/driver" | ||
| ) | ||
|
|
||
| func generatedTestDir(dir string, dep, filecount int) { | ||
| if dep == 0 { | ||
| return | ||
| } | ||
| for i := 0; i < dep; i++ { | ||
| subDir := dir + "/dir" + strconv.Itoa(i) | ||
| os.Mkdir(subDir, 0755) | ||
| generatedTestDir(subDir, dep-1, filecount) | ||
| generatedFiles(subDir, filecount) | ||
| } | ||
| } | ||
|
|
||
| func generatedFiles(path string, count int) error { | ||
| for i := 0; i < count; i++ { | ||
| filePath := filepath.Join(path, "file"+strconv.Itoa(i)+".txt") | ||
| file, err := os.Create(filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // 使用随机ascii字符填充文件 | ||
| content := make([]byte, 1024) // 1KB file | ||
| for j := range content { | ||
| content[j] = byte('a' + j%26) // Fill with 'a' to 'z' | ||
| } | ||
| _, err = file.Write(content) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| file.Close() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // performance tests for directory size calculation | ||
| func BenchmarkCalculateDirSize(t *testing.B) { | ||
| // 初始化t的日志 | ||
| t.Logf("Starting performance test for directory size calculation") | ||
| // 确保测试目录存在 | ||
| if testing.Short() { | ||
| t.Skip("Skipping performance test in short mode") | ||
| } | ||
| // 创建tmp directory for testing | ||
| testTempDir := t.TempDir() | ||
| err := os.MkdirAll(testTempDir, 0755) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create test directory: %v", err) | ||
| } | ||
| defer os.RemoveAll(testTempDir) // Clean up after test | ||
| // 构建一个深度为5,每层10个文件和10个目录的目录结构 | ||
| generatedTestDir(testTempDir, 5, 10) | ||
| // Initialize the local driver with directory size calculation enabled | ||
| d := &Local{ | ||
| directoryMap: DirectoryMap{ | ||
| root: testTempDir, | ||
| }, | ||
| Addition: Addition{ | ||
| DirectorySize: true, | ||
| RootPath: driver.RootPath{ | ||
| RootFolderPath: testTempDir, | ||
| }, | ||
| }, | ||
| } | ||
| //record the start time | ||
| t.StartTimer() | ||
| // Calculate the directory size | ||
| err = d.directoryMap.RecalculateDirSize() | ||
| if err != nil { | ||
| t.Fatalf("Failed to calculate directory size: %v", err) | ||
| } | ||
| //record the end time | ||
| t.StopTimer() | ||
| // Print the size and duration | ||
| node, ok := d.directoryMap.Get(d.directoryMap.root) | ||
| if !ok { | ||
| t.Fatalf("Failed to get root node from directory map") | ||
| } | ||
| t.Logf("Directory size: %d bytes", node.fileSum+node.directorySum) | ||
| t.Logf("Performance test completed successfully") | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.