aboutsummaryrefslogtreecommitdiffstats
path: root/storage
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-09-08 23:58:41 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-09-08 23:58:41 +0200
commit3b1baea2dd9353f42b3a9d93f6bc92ecbe9f4f01 (patch)
treed255b7e6edbcbaba1f98cf0c666a82c0f9750019 /storage
parent2293a3dbda0a96f5cbac851bb73e1e675417e4f3 (diff)
downloadgo-git-3b1baea2dd9353f42b3a9d93f6bc92ecbe9f4f01.tar.gz
format: packfile based on ObjectStorage and CRC32 calculation
Diffstat (limited to 'storage')
-rw-r--r--storage/filesystem/internal/dotgit/dotgit.go49
-rw-r--r--storage/filesystem/internal/index/index.go12
-rw-r--r--storage/filesystem/object.go4
-rw-r--r--storage/memory/storage.go10
4 files changed, 48 insertions, 27 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go
index 3a985d5..d21b754 100644
--- a/storage/filesystem/internal/dotgit/dotgit.go
+++ b/storage/filesystem/internal/dotgit/dotgit.go
@@ -194,34 +194,34 @@ func isHexAlpha(b byte) bool {
}
type PackWriter struct {
- fs fs.Filesystem
- file fs.File
- writer io.Writer
- pipeReader io.ReadCloser
- pipeWriter io.WriteCloser
- hash core.Hash
- index index.Index
- result chan error
+ fs fs.Filesystem
+ sr io.ReadCloser
+ sw io.WriteCloser
+ fw fs.File
+ mw io.Writer
+ hash core.Hash
+ index index.Index
+ result chan error
}
func newPackWrite(fs fs.Filesystem) (*PackWriter, error) {
- r, w := io.Pipe()
-
temp := sha1.Sum([]byte(time.Now().String()))
filename := fmt.Sprintf(".%x", temp)
- file, err := fs.Create(fs.Join(objectsPath, packPath, filename))
+ fw, err := fs.Create(fs.Join(objectsPath, packPath, filename))
if err != nil {
return nil, err
}
+ sr, sw := io.Pipe()
+
writer := &PackWriter{
- fs: fs,
- file: file,
- writer: io.MultiWriter(w, file),
- pipeReader: r,
- pipeWriter: w,
- result: make(chan error),
+ fs: fs,
+ fw: fw,
+ sr: sr,
+ sw: sw,
+ mw: io.MultiWriter(sw, fw),
+ result: make(chan error),
}
go writer.buildIndex()
@@ -229,16 +229,19 @@ func newPackWrite(fs fs.Filesystem) (*PackWriter, error) {
}
func (w *PackWriter) buildIndex() {
- defer w.pipeReader.Close()
- index, hash, err := index.NewFromPackfile(w.pipeReader)
+ defer w.sr.Close()
+ index, hash, err := index.NewFromPackfile(w.sr)
+
w.index = index
w.hash = hash
+ fmt.Println(hash, w.index)
+
w.result <- err
}
func (w *PackWriter) Write(p []byte) (int, error) {
- return w.writer.Write(p)
+ return w.mw.Write(p)
}
func (w *PackWriter) Close() error {
@@ -246,11 +249,11 @@ func (w *PackWriter) Close() error {
close(w.result)
}()
- if err := w.file.Close(); err != nil {
+ if err := w.fw.Close(); err != nil {
return err
}
- if err := w.pipeWriter.Close(); err != nil {
+ if err := w.sw.Close(); err != nil {
return err
}
@@ -266,5 +269,5 @@ func (w *PackWriter) save() error {
//idx, err := w.fs.Create(fmt.Sprintf("%s.idx", base))
- return w.fs.Rename(w.file.Filename(), fmt.Sprintf("%s.pack", base))
+ return w.fs.Rename(w.fw.Filename(), fmt.Sprintf("%s.pack", base))
}
diff --git a/storage/filesystem/internal/index/index.go b/storage/filesystem/internal/index/index.go
index 2fd2a55..412b78f 100644
--- a/storage/filesystem/internal/index/index.go
+++ b/storage/filesystem/internal/index/index.go
@@ -1,11 +1,13 @@
package index
import (
+ "fmt"
"io"
"gopkg.in/src-d/go-git.v4/core"
"gopkg.in/src-d/go-git.v4/formats/idxfile"
"gopkg.in/src-d/go-git.v4/formats/packfile"
+ "gopkg.in/src-d/go-git.v4/storage/memory"
)
// Index is a database of objects and their offset in a packfile.
@@ -25,6 +27,7 @@ func (i *Index) Decode(r io.Reader) error {
}
for _, e := range idx.Entries {
+ fmt.Println(e.CRC32)
(*i)[e.Hash] = int64(e.Offset)
}
@@ -33,16 +36,17 @@ func (i *Index) Decode(r io.Reader) error {
// NewFrompackfile returns a new index from a packfile reader.
func NewFromPackfile(r io.Reader) (Index, core.Hash, error) {
- p := packfile.NewScannerFromReader(r)
- d := packfile.NewDecoder(p, nil)
+ o := memory.NewStorage().ObjectStorage()
+ s := packfile.NewScannerFromReader(r)
+ d := packfile.NewDecoder(s, o)
checksum, err := d.Decode()
if err != nil {
return nil, core.ZeroHash, err
}
- index := Index(d.Index())
- return index, checksum, p.Close()
+ index := Index(d.Offsets())
+ return index, checksum, d.Close()
}
// Get returns the offset that an object has the packfile.
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go
index c6d5359..65fc6e0 100644
--- a/storage/filesystem/object.go
+++ b/storage/filesystem/object.go
@@ -238,6 +238,10 @@ func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) {
return core.ZeroHash, fmt.Errorf("not implemented yet")
}
+func (tx *TxObjectStorage) Get(core.ObjectType, core.Hash) (core.Object, error) {
+ return nil, fmt.Errorf("not implemented yet")
+}
+
func (tx *TxObjectStorage) Commit() error {
return fmt.Errorf("not implemented yet")
}
diff --git a/storage/memory/storage.go b/storage/memory/storage.go
index 1bf99fc..6a56d6a 100644
--- a/storage/memory/storage.go
+++ b/storage/memory/storage.go
@@ -143,6 +143,7 @@ func (o *ObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error)
if !ok || (core.AnyObject != t && obj.Type() != t) {
return nil, core.ErrObjectNotFound
}
+
return obj, nil
}
@@ -192,6 +193,15 @@ func (tx *TxObjectStorage) Set(obj core.Object) (core.Hash, error) {
return h, nil
}
+func (tx *TxObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Object, error) {
+ obj, ok := tx.Objects[h]
+ if !ok || (core.AnyObject != t && obj.Type() != t) {
+ return nil, core.ErrObjectNotFound
+ }
+
+ return obj, nil
+}
+
func (tx *TxObjectStorage) Commit() error {
for h, obj := range tx.Objects {
delete(tx.Objects, h)