diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-14 18:21:10 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-14 18:21:10 +0200 |
commit | f6fe29c80d11662a169806dcf413ecdedcb28fa3 (patch) | |
tree | 9958034c27521f720f8c5d83c12120ada196f48a /examples | |
parent | 91bf16b2336e6f80f0742be729582fe5fbbada83 (diff) | |
download | go-git-f6fe29c80d11662a169806dcf413ecdedcb28fa3.tar.gz |
core: Storage, removing err returning *Storages
Diffstat (limited to 'examples')
-rw-r--r-- | examples/object_storage/main.go | 19 | ||||
-rw-r--r-- | examples/object_storage/storage.go | 127 |
2 files changed, 132 insertions, 14 deletions
diff --git a/examples/object_storage/main.go b/examples/object_storage/main.go index 5cba6e6..22fa426 100644 --- a/examples/object_storage/main.go +++ b/examples/object_storage/main.go @@ -8,26 +8,25 @@ import ( "github.com/aerospike/aerospike-client-go" - "gopkg.in/src-d/go-git.v3" + "gopkg.in/src-d/go-git.v4" ) func main() { url := os.Args[2] - r, err := git.NewRepository(url, nil) + client, err := aerospike.NewClient("127.0.0.1", 3000) if err != nil { panic(err) } - client, err := aerospike.NewClient("127.0.0.1", 3000) + s := NewAerospikeStorage(url, client) + r, err := git.NewRepository(s) if err != nil { panic(err) } - r.Storage = NewAerospikeObjectStorage(url, client) - switch os.Args[1] { - case "pull": - pull(r) + case "clone": + clone(r, url) case "list": list(r) default: @@ -35,11 +34,11 @@ func main() { } } -func pull(r *git.Repository) { - fmt.Printf("Retrieving %q ...\n", os.Args[2]) +func clone(r *git.Repository, url string) { + fmt.Printf("Cloning %q ...\n", os.Args[2]) start := time.Now() - if err := r.PullDefault(); err != nil { + if err := r.Clone(&git.CloneOptions{URL: url}); err != nil { panic(err) } diff --git a/examples/object_storage/storage.go b/examples/object_storage/storage.go index c1e06a0..5868591 100644 --- a/examples/object_storage/storage.go +++ b/examples/object_storage/storage.go @@ -3,9 +3,9 @@ package main import ( "fmt" "io" + "io/ioutil" - "gopkg.in/src-d/go-git.v3/core" - "gopkg.in/src-d/go-git.v3/storage/memory" + "gopkg.in/src-d/go-git.v4/core" "github.com/aerospike/aerospike-client-go" ) @@ -13,6 +13,33 @@ import ( // CREATE INDEX commits ON test.commit (url) STRING; // CREATE INDEX blobs ON test.blob (url) STRING; +type AerospikeStorage struct { + url string + client *aerospike.Client + os *AerospikeObjectStorage + rs *AerospikeReferenceStorage +} + +func NewAerospikeStorage(url string, client *aerospike.Client) *AerospikeStorage { + return &AerospikeStorage{url: url, client: client} +} + +func (s *AerospikeStorage) ObjectStorage() core.ObjectStorage { + if s.os == nil { + s.os = NewAerospikeObjectStorage(s.url, s.client) + } + + return s.os +} + +func (s *AerospikeStorage) ReferenceStorage() core.ReferenceStorage { + if s.rs == nil { + s.rs = NewAerospikeReferenceStorage(s.url, s.client) + } + + return s.rs +} + type AerospikeObjectStorage struct { url string client *aerospike.Client @@ -22,17 +49,31 @@ func NewAerospikeObjectStorage(url string, c *aerospike.Client) *AerospikeObject return &AerospikeObjectStorage{url, c} } +func (s *AerospikeObjectStorage) NewObject() core.Object { + return &core.MemoryObject{} +} + func (o *AerospikeObjectStorage) Set(obj core.Object) (core.Hash, error) { key, err := aerospike.NewKey("test", obj.Type().String(), obj.Hash().String()) if err != nil { return obj.Hash(), err } + r, err := obj.Reader() + if err != nil { + return obj.Hash(), err + } + + c, err := ioutil.ReadAll(r) + if err != nil { + return obj.Hash(), err + } + bins := aerospike.BinMap{ "url": o.url, "hash": obj.Hash().String(), "type": obj.Type().String(), - "blob": obj.Content(), + "blob": c, } err = o.client.Put(nil, key, bins) @@ -83,7 +124,85 @@ func (i *AerospikeObjectIter) Next() (core.Object, error) { } content := r.Bins["blob"].([]byte) - return memory.NewObject(i.t, int64(len(content)), content), nil + + o := &core.MemoryObject{} + o.SetType(i.t) + o.SetSize(int64(len(content))) + + _, err := o.Write(content) + if err != nil { + return nil, err + } + + return o, nil } func (i *AerospikeObjectIter) Close() {} + +type AerospikeReferenceStorage struct { + url string + client *aerospike.Client +} + +func NewAerospikeReferenceStorage(url string, c *aerospike.Client) *AerospikeReferenceStorage { + return &AerospikeReferenceStorage{url, c} +} + +// Set stores a reference. +func (s *AerospikeReferenceStorage) Set(ref *core.Reference) error { + key, err := aerospike.NewKey("test", "references", ref.Name().String()) + if err != nil { + return err + } + + raw := ref.Strings() + bins := aerospike.BinMap{ + "url": s.url, + "name": raw[0], + "target": raw[1], + } + + return s.client.Put(nil, key, bins) +} + +// Get returns a stored reference with the given name +func (s *AerospikeReferenceStorage) Get(n core.ReferenceName) (*core.Reference, error) { + key, err := aerospike.NewKey("test", "references", n.String()) + if err != nil { + return nil, err + } + + rec, err := s.client.Get(nil, key) + if err != nil { + return nil, err + } + + return core.NewReferenceFromStrings( + rec.Bins["name"].(string), + rec.Bins["target"].(string), + ), nil +} + +// Iter returns a core.ReferenceIter +func (s *AerospikeReferenceStorage) Iter() (core.ReferenceIter, error) { + stmnt := aerospike.NewStatement("test", "references") + err := stmnt.Addfilter(aerospike.NewEqualFilter("url", s.url)) + if err != nil { + return nil, err + } + + rs, err := s.client.Query(nil, stmnt) + if err != nil { + return nil, err + } + + var refs []*core.Reference + for r := range rs.Records { + refs = append(refs, core.NewReferenceFromStrings( + r.Bins["name"].(string), + r.Bins["target"].(string), + )) + } + + return core.NewReferenceSliceIter(refs), nil +} |