aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-08-12 02:01:21 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-12 02:01:21 +0200
commit89797d3e3c56a954b84dc69aaea8a37a736baf04 (patch)
tree61a3d23fba7bf56fece353bf0c38297c5dcb9555
parent805cc777b083fee68a8834ebe1823d0b9dc6a00f (diff)
downloadgo-git-89797d3e3c56a954b84dc69aaea8a37a736baf04.tar.gz
examples: object_storage example
-rw-r--r--examples/object_storage/main.go75
-rw-r--r--examples/object_storage/storage.go89
2 files changed, 164 insertions, 0 deletions
diff --git a/examples/object_storage/main.go b/examples/object_storage/main.go
new file mode 100644
index 0000000..5cba6e6
--- /dev/null
+++ b/examples/object_storage/main.go
@@ -0,0 +1,75 @@
+package main
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "time"
+
+ "github.com/aerospike/aerospike-client-go"
+
+ "gopkg.in/src-d/go-git.v3"
+)
+
+func main() {
+ url := os.Args[2]
+ r, err := git.NewRepository(url, nil)
+ if err != nil {
+ panic(err)
+ }
+
+ client, err := aerospike.NewClient("127.0.0.1", 3000)
+ if err != nil {
+ panic(err)
+ }
+
+ r.Storage = NewAerospikeObjectStorage(url, client)
+
+ switch os.Args[1] {
+ case "pull":
+ pull(r)
+ case "list":
+ list(r)
+ default:
+ panic("unknown option")
+ }
+}
+
+func pull(r *git.Repository) {
+ fmt.Printf("Retrieving %q ...\n", os.Args[2])
+ start := time.Now()
+
+ if err := r.PullDefault(); err != nil {
+ panic(err)
+ }
+
+ fmt.Printf("Time elapsed %s\n", time.Since(start))
+}
+
+func list(r *git.Repository) {
+ fmt.Printf("Listing commits from %q ...\n", os.Args[1])
+
+ iter, err := r.Commits()
+ if err != nil {
+ panic(err)
+ }
+ defer iter.Close()
+
+ var count int
+ for {
+ //the commits are not shorted in any special order
+ commit, err := iter.Next()
+ if err != nil {
+ if err == io.EOF {
+ break
+ }
+
+ panic(err)
+ }
+
+ count++
+ fmt.Println(commit)
+ }
+
+ fmt.Printf("Total number of commits %d\n", count)
+}
diff --git a/examples/object_storage/storage.go b/examples/object_storage/storage.go
new file mode 100644
index 0000000..c1e06a0
--- /dev/null
+++ b/examples/object_storage/storage.go
@@ -0,0 +1,89 @@
+package main
+
+import (
+ "fmt"
+ "io"
+
+ "gopkg.in/src-d/go-git.v3/core"
+ "gopkg.in/src-d/go-git.v3/storage/memory"
+
+ "github.com/aerospike/aerospike-client-go"
+)
+
+// CREATE INDEX commits ON test.commit (url) STRING;
+// CREATE INDEX blobs ON test.blob (url) STRING;
+
+type AerospikeObjectStorage struct {
+ url string
+ client *aerospike.Client
+}
+
+func NewAerospikeObjectStorage(url string, c *aerospike.Client) *AerospikeObjectStorage {
+ return &AerospikeObjectStorage{url, c}
+}
+
+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
+ }
+
+ bins := aerospike.BinMap{
+ "url": o.url,
+ "hash": obj.Hash().String(),
+ "type": obj.Type().String(),
+ "blob": obj.Content(),
+ }
+
+ err = o.client.Put(nil, key, bins)
+ fmt.Println(err, key)
+ return obj.Hash(), err
+}
+
+func (o *AerospikeObjectStorage) Get(h core.Hash) (core.Object, error) {
+ key, err := keyFromObject(h)
+ if err != nil {
+ return nil, err
+ }
+
+ rec, err := o.client.Get(nil, key)
+ if err != nil {
+ return nil, err
+ }
+
+ fmt.Println(rec.Bins)
+ return nil, core.ErrObjectNotFound
+}
+
+func (o *AerospikeObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) {
+ s := aerospike.NewStatement("test", t.String())
+ err := s.Addfilter(aerospike.NewEqualFilter("url", o.url))
+
+ rs, err := o.client.Query(nil, s)
+ if err != nil {
+ return nil, err
+ }
+
+ return &AerospikeObjectIter{t, rs.Records}, nil
+}
+
+func keyFromObject(h core.Hash) (*aerospike.Key, error) {
+ return aerospike.NewKey("test", "objects", h.String())
+}
+
+type AerospikeObjectIter struct {
+ t core.ObjectType
+ ch chan *aerospike.Record
+}
+
+func (i *AerospikeObjectIter) Next() (core.Object, error) {
+ r := <-i.ch
+ if r == nil {
+ return nil, io.EOF
+ }
+
+ content := r.Bins["blob"].([]byte)
+ return memory.NewObject(i.t, int64(len(content)), content), nil
+}
+
+func (i *AerospikeObjectIter) Close() {}