Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 9fbfd39

Browse files
committed
example: improvements on the storage example
1 parent 7c43657 commit 9fbfd39

File tree

2 files changed

+54
-45
lines changed

2 files changed

+54
-45
lines changed

examples/storage/main.go

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,84 @@ package main
22

33
import (
44
"fmt"
5-
"io"
65
"os"
7-
"time"
6+
"strings"
87

98
"github.com/aerospike/aerospike-client-go"
9+
"github.com/fatih/color"
1010

1111
"gopkg.in/src-d/go-git.v4"
1212
)
1313

1414
func main() {
15+
checkArgs()
16+
action := os.Args[1]
1517
url := os.Args[2]
18+
19+
// Aerospike client to be used by the custom storage
1620
client, err := aerospike.NewClient("127.0.0.1", 3000)
17-
if err != nil {
18-
panic(err)
19-
}
21+
checkIfError(err)
2022

23+
// New instance of the custom aerospike storage, all the objects,
24+
// references and configuration is saved to aerospike
2125
s, err := NewAerospikeStorage(client, "test", url)
22-
if err != nil {
23-
panic(err)
24-
}
26+
checkIfError(err)
2527

28+
// A new repository instance using as storage the custom implementation
2629
r, err := git.NewRepository(s)
27-
if err != nil {
28-
panic(err)
29-
}
30+
checkIfError(err)
3031

31-
switch os.Args[1] {
32+
switch action {
3233
case "clone":
3334
clone(r, url)
34-
case "list":
35-
list(r)
35+
case "log":
36+
log(r)
3637
default:
3738
panic("unknown option")
3839
}
3940
}
4041

4142
func clone(r *git.Repository, url string) {
42-
fmt.Printf("Cloning %q ...\n", os.Args[2])
43-
start := time.Now()
43+
// Clone the given repository, all the objects, references and
44+
// configuration sush as remotes, are save into the Aerospike database.
45+
// > git clone <url>
46+
color.Blue("git clone %s", url)
47+
err := r.Clone(&git.CloneOptions{URL: url})
48+
checkIfError(err)
49+
}
4450

45-
if err := r.Clone(&git.CloneOptions{URL: url}); err != nil {
46-
panic(err)
51+
func log(r *git.Repository) {
52+
// Prints the history of the repository starting in the current HEAD, the
53+
// objects are retrieved from Aerospike database.
54+
// > git log --oneline
55+
color.Blue("git log --oneline")
56+
57+
ref, err := r.Head()
58+
checkIfError(err)
59+
commit, err := r.Commit(ref.Hash())
60+
checkIfError(err)
61+
commits, err := commit.History()
62+
checkIfError(err)
63+
64+
for _, c := range commits {
65+
hash := c.Hash.String()
66+
line := strings.Split(c.Message, "\n")
67+
fmt.Println(hash[:7], line[0])
4768
}
48-
49-
fmt.Printf("Time elapsed %s\n", time.Since(start))
5069
}
5170

52-
func list(r *git.Repository) {
53-
fmt.Printf("Listing commits from %q ...\n", os.Args[1])
54-
55-
iter, err := r.Commits()
56-
if err != nil {
57-
panic(err)
71+
func checkIfError(err error) {
72+
if err == nil {
73+
return
5874
}
59-
defer iter.Close()
60-
61-
var count int
62-
for {
63-
//the commits are not shorted in any special order
64-
commit, err := iter.Next()
65-
if err != nil {
66-
if err == io.EOF {
67-
break
68-
}
6975

70-
panic(err)
71-
}
76+
color.Red("error: %s", err)
77+
os.Exit(1)
78+
}
7279

73-
count++
74-
fmt.Println(commit)
80+
func checkArgs() {
81+
if len(os.Args) < 3 {
82+
color.Cyan("Usage: %s <clone|log> <url>", os.Args[0])
83+
os.Exit(1)
7584
}
76-
77-
fmt.Printf("Total number of commits %d\n", count)
7885
}

examples/storage/storage.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io"
76
"io/ioutil"
87

@@ -108,7 +107,11 @@ func (s *AerospikeObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Objec
108107
return nil, err
109108
}
110109

111-
return nil, core.ErrObjectNotFound
110+
if rec == nil {
111+
return nil, core.ErrObjectNotFound
112+
}
113+
114+
return objectFromRecord(rec, t)
112115
}
113116

114117
func (s *AerospikeObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) {
@@ -125,7 +128,6 @@ func (s *AerospikeObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error
125128

126129
func (s *AerospikeObjectStorage) keyFromObject(h core.Hash, t core.ObjectType,
127130
) (*aerospike.Key, error) {
128-
fmt.Println(t.String())
129131
return aerospike.NewKey(s.ns, t.String(), h.String())
130132
}
131133

0 commit comments

Comments
 (0)