54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package partitioner
|
|
|
|
import (
|
|
"fmt"
|
|
"forever-files/db"
|
|
"forever-files/types"
|
|
"github.com/dustin/go-humanize"
|
|
)
|
|
|
|
func CalculatePartitions(store db.DB, targetSize int64) (partitions [][]types.FileMetadata, err error) {
|
|
totalSize, err := store.GetTotalSize()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error getting total size: %w", err)
|
|
}
|
|
if targetSize <= 0 {
|
|
targetSize = totalSize / 2
|
|
}
|
|
fmt.Printf("Total Size: %v\n", totalSize)
|
|
fmt.Printf("Target Size: %v\n", targetSize)
|
|
|
|
files, err := store.GetFiles()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error getting files: %w", err)
|
|
}
|
|
partitions = make([][]types.FileMetadata, 0)
|
|
partitionSize := int64(0)
|
|
partitionFiles := make([]types.FileMetadata, 0)
|
|
leftOverFiles := make([]types.FileMetadata, 0)
|
|
leftOverSize := int64(0)
|
|
for _, file := range files {
|
|
if partitionSize+file.Size > targetSize {
|
|
fmt.Printf("Partition Size: %v\n", humanize.Bytes(uint64(partitionSize)))
|
|
partitions = append(partitions, partitionFiles)
|
|
partitionFiles = make([]types.FileMetadata, 0)
|
|
partitionSize = 0
|
|
}
|
|
if partitionSize < targetSize && partitionSize+file.Size < targetSize {
|
|
partitionFiles = append(partitionFiles, file)
|
|
partitionSize += file.Size
|
|
} else {
|
|
leftOverFiles = append(leftOverFiles, file)
|
|
leftOverSize += file.Size
|
|
}
|
|
}
|
|
|
|
for _, partition := range partitions {
|
|
fmt.Printf("Partition File Count: %v\n", len(partition))
|
|
}
|
|
fmt.Printf("Left Over File Count: %v\n", len(leftOverFiles))
|
|
fmt.Printf("Left Over Size: %v\n", humanize.Bytes(uint64(leftOverSize)))
|
|
|
|
return partitions, nil
|
|
}
|