-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add chunking functionality to main.go
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,66 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/i5heu/ouroboros-db/pkg/buzhashChunker" | ||
) | ||
|
||
func main() { | ||
|
||
// check if the user has provided the file path | ||
if len(os.Args) < 2 { | ||
fmt.Println("Please provide the file path") | ||
os.Exit(1) | ||
} | ||
|
||
// get the input from the user | ||
filePath := os.Args[1] | ||
|
||
// read the file | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer file.Close() | ||
|
||
// create a new chunker | ||
chunks, metas, err := buzhashChunker.ChunkReaderSynchronously(file) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// create folder to store the chunks besides the original file | ||
err = os.Mkdir(filePath+"_chunks", 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// write the chunks to the folder | ||
for i, chunk := range chunks { | ||
chunkFile, err := os.Create(filePath + "_chunks/chunk_" + fmt.Sprintf("%d", i)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer chunkFile.Close() | ||
chunkFile.Write(chunk.Data) | ||
} | ||
|
||
// write the meta into a file in the folder | ||
metaFile, err := os.Create(filePath + "_chunks/meta") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer metaFile.Close() | ||
// store as json | ||
metaJSON, err := json.Marshal(metas) | ||
if err != nil { | ||
panic(err) | ||
} | ||
metaFile.Write(metaJSON) | ||
|
||
fmt.Println("Chunks and meta data are stored in the folder: ", filePath+"_chunks") | ||
|
||
} |