aboutsummaryrefslogtreecommitdiffstats
path: root/examples/object_storage/storage.go
blob: bb9ff298e75f09333abf4ba84b3321e80b41a512 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main

import (
	"fmt"
	"io"
	"io/ioutil"

	"gopkg.in/src-d/go-git.v4/core"

	"github.com/aerospike/aerospike-client-go"
)

// 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
}

func NewAerospikeObjectStorage(url string, c *aerospike.Client) *AerospikeObjectStorage {
	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": c,
	}

	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)

	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) ForEach(cb func(obj core.Object) error) error {
	for {
		obj, err := i.Next()
		if err != nil {
			if err == io.EOF {
				return nil
			}

			return err
		}

		if err := cb(obj); err != nil {
			if err == core.ErrStop {
				return nil
			}

			return err
		}
	}
}

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
}