Commit e45d3b24 authored by Masih H. Derkani's avatar Masih H. Derkani Committed by Masih H. Derkani

Remove `index.Save` API

It's two lines of code extra and we already have read from and write to
APIs for index.

Remove `hydrate` cli since the assumptions it makes about appending
index are no longer true. header needs updating for example. Removing to
be re-added when needed as part of a propper CARv2 CLI.
parent 64bb51b2
...@@ -25,7 +25,12 @@ func ExampleWrapV1File() { ...@@ -25,7 +25,12 @@ func ExampleWrapV1File() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer cr.Close() defer func() {
if err := cr.Close(); err != nil {
panic(err)
}
}()
roots, err := cr.Roots() roots, err := cr.Roots()
if err != nil { if err != nil {
panic(err) panic(err)
......
...@@ -2,6 +2,7 @@ package index_test ...@@ -2,6 +2,7 @@ package index_test
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"reflect" "reflect"
...@@ -44,16 +45,20 @@ func ExampleReadFrom() { ...@@ -44,16 +45,20 @@ func ExampleReadFrom() {
// Frame with CID bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy starts at offset 61 relative to CARv1 data payload. // Frame with CID bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy starts at offset 61 relative to CARv1 data payload.
} }
// ExampleSave unmarshalls an index from an indexed CARv2 file, and stores it as a separate // ExampleWriteTo unmarshalls an index from an indexed CARv2 file, and stores it as a separate
// file on disk. // file on disk.
func ExampleSave() { func ExampleWriteTo() {
// Open the CARv2 file // Open the CARv2 file
src := "../testdata/sample-wrapped-v2.car" src := "../testdata/sample-wrapped-v2.car"
cr, err := carv2.OpenReader(src) cr, err := carv2.OpenReader(src)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer cr.Close() defer func() {
if err := cr.Close(); err != nil {
panic(err)
}
}()
// Read and unmarshall index within CARv2 file. // Read and unmarshall index within CARv2 file.
idx, err := index.ReadFrom(cr.IndexReader()) idx, err := index.ReadFrom(cr.IndexReader())
...@@ -63,17 +68,25 @@ func ExampleSave() { ...@@ -63,17 +68,25 @@ func ExampleSave() {
// Store the index alone onto destination file. // Store the index alone onto destination file.
dest := "../testdata/sample-index.carindex" dest := "../testdata/sample-index.carindex"
err = index.Save(idx, dest) f, err := os.Create(dest)
if err != nil {
panic(err)
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
err = index.WriteTo(idx, f)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Open the destination file that contains the index only. // Seek to the beginning of tile to read it back.
f, err := os.Open(dest) _, err = f.Seek(0, io.SeekStart)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close()
// Read and unmarshall the destination file as a separate index instance. // Read and unmarshall the destination file as a separate index instance.
reReadIdx, err := index.ReadFrom(f) reReadIdx, err := index.ReadFrom(f)
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"os"
internalio "github.com/ipld/go-car/v2/internal/io" internalio "github.com/ipld/go-car/v2/internal/io"
...@@ -52,16 +51,6 @@ func New(codec multicodec.Code) (Index, error) { ...@@ -52,16 +51,6 @@ func New(codec multicodec.Code) (Index, error) {
} }
} }
// Save writes a generated index into the given `path` replacing the file if it exists.
func Save(idx Index, path string) error {
stream, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o640)
if err != nil {
return err
}
defer stream.Close()
return WriteTo(idx, stream)
}
// WriteTo writes the given idx into w. // WriteTo writes the given idx into w.
// The written bytes include the index encoding. // The written bytes include the index encoding.
// This can then be read back using index.ReadFrom // This can then be read back using index.ReadFrom
......
...@@ -128,33 +128,6 @@ func TestWriteTo(t *testing.T) { ...@@ -128,33 +128,6 @@ func TestWriteTo(t *testing.T) {
require.Equal(t, wantIdx, gotIdx) require.Equal(t, wantIdx, gotIdx)
} }
func TestSave(t *testing.T) {
// Read sample index on file
idxf, err := os.Open("../testdata/sample-index.carindex")
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, idxf.Close()) })
// Unmarshall to get expected index
wantIdx, err := ReadFrom(idxf)
require.NoError(t, err)
// Save the same index at destination
dest := filepath.Join(t.TempDir(), "index-write-to-test.carindex")
require.NoError(t, Save(wantIdx, dest))
// Open the saved file
destF, err := os.Open(dest)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, destF.Close()) })
// Read the written index back
gotIdx, err := ReadFrom(destF)
require.NoError(t, err)
// Assert they are equal
require.Equal(t, wantIdx, gotIdx)
}
func TestMarshalledIndexStartsWithCodec(t *testing.T) { func TestMarshalledIndexStartsWithCodec(t *testing.T) {
// Read sample index on file // Read sample index on file
idxf, err := os.Open("../testdata/sample-index.carindex") idxf, err := os.Open("../testdata/sample-index.carindex")
......
// Package carbs provides a read-only blockstore interface directly reading out of a car file.
// TODO to be refactored
package carbs
package main
import (
"fmt"
"os"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/index"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: hydrate <file.car>\n")
return
}
db := os.Args[1]
idx, err := carv2.GenerateIndexFromFile(db)
if err != nil {
fmt.Printf("Error generating index: %v\n", err)
return
}
fmt.Printf("Saving...\n")
if err := index.Save(idx, db); err != nil {
fmt.Printf("Error saving : %v\n", err)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment