This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
/
pointers.go
222 lines (213 loc) · 4.89 KB
/
pointers.go
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
package db
import (
"database/sql"
"github.com/OpenBazaar/openbazaar-go/ipfs"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
ps "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
"strconv"
"sync"
"time"
)
type PointersDB struct {
db *sql.DB
lock sync.RWMutex
}
func (p *PointersDB) Put(pointer ipfs.Pointer) error {
p.lock.Lock()
defer p.lock.Unlock()
tx, err := p.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("insert into pointers(pointerID, key, address, cancelID, purpose, timestamp) values(?,?,?,?,?,?)")
if err != nil {
return err
}
defer stmt.Close()
var cancelID string
if pointer.CancelID != nil {
cancelID = pointer.CancelID.Pretty()
}
_, err = stmt.Exec(pointer.Value.ID.Pretty(), pointer.Cid.String(), pointer.Value.Addrs[0].String(), cancelID, pointer.Purpose, int(time.Now().Unix()))
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (p *PointersDB) Delete(id peer.ID) error {
p.lock.Lock()
defer p.lock.Unlock()
_, err := p.db.Exec("delete from pointers where pointerID=?", id.Pretty())
if err != nil {
return err
}
return nil
}
func (p *PointersDB) DeleteAll(purpose ipfs.Purpose) error {
p.lock.Lock()
defer p.lock.Unlock()
_, err := p.db.Exec("delete from pointers where purpose=?", purpose)
if err != nil {
return err
}
return nil
}
func (p *PointersDB) GetAll() ([]ipfs.Pointer, error) {
p.lock.RLock()
defer p.lock.RUnlock()
stm := "select * from pointers"
rows, err := p.db.Query(stm)
if err != nil {
return nil, err
}
defer rows.Close()
var ret []ipfs.Pointer
for rows.Next() {
var pointerID string
var key string
var address string
var purpose int
var timestamp int
var cancelID string
if err := rows.Scan(&pointerID, &key, &address, &cancelID, &purpose, ×tamp); err != nil {
return ret, err
}
maAddr, err := ma.NewMultiaddr(address)
if err != nil {
return ret, err
}
pid, err := peer.IDB58Decode(pointerID)
if err != nil {
return ret, err
}
k, err := cid.Decode(key)
if err != nil {
return ret, err
}
var canID *peer.ID
if cancelID != "" {
c, err := peer.IDB58Decode(cancelID)
if err != nil {
return ret, err
}
canID = &c
}
pointer := ipfs.Pointer{
Cid: k,
Value: ps.PeerInfo{
ID: pid,
Addrs: []ma.Multiaddr{maAddr},
},
CancelID: canID,
Purpose: ipfs.Purpose(purpose),
Timestamp: time.Unix(int64(timestamp), 0),
}
ret = append(ret, pointer)
}
return ret, nil
}
func (p *PointersDB) GetByPurpose(purpose ipfs.Purpose) ([]ipfs.Pointer, error) {
p.lock.RLock()
defer p.lock.RUnlock()
stm := "select * from pointers where purpose=" + strconv.Itoa(int(purpose))
rows, err := p.db.Query(stm)
if err != nil {
return nil, err
}
defer rows.Close()
var ret []ipfs.Pointer
for rows.Next() {
var pointerID string
var key string
var address string
var purpose int
var timestamp int
var cancelID string
if err := rows.Scan(&pointerID, &key, &address, &cancelID, &purpose, ×tamp); err != nil {
return ret, err
}
maAddr, err := ma.NewMultiaddr(address)
if err != nil {
return ret, err
}
pid, err := peer.IDB58Decode(pointerID)
if err != nil {
return ret, err
}
k, err := cid.Decode(key)
if err != nil {
return ret, err
}
var canID *peer.ID
if cancelID != "" {
c, err := peer.IDB58Decode(cancelID)
if err != nil {
return ret, err
}
canID = &c
}
pointer := ipfs.Pointer{
Cid: k,
Value: ps.PeerInfo{
ID: pid,
Addrs: []ma.Multiaddr{maAddr},
},
CancelID: canID,
Purpose: ipfs.Purpose(purpose),
Timestamp: time.Unix(int64(timestamp), 0),
}
ret = append(ret, pointer)
}
return ret, nil
}
func (p *PointersDB) Get(id peer.ID) (ipfs.Pointer, error) {
p.lock.RLock()
defer p.lock.RUnlock()
stm := "select * from pointers where pointerID=?"
row := p.db.QueryRow(stm, id.Pretty())
var pointer ipfs.Pointer
var pointerID string
var key string
var address string
var purpose int
var timestamp int
var cancelID string
if err := row.Scan(&pointerID, &key, &address, &cancelID, &purpose, ×tamp); err != nil {
return pointer, err
}
maAddr, err := ma.NewMultiaddr(address)
if err != nil {
return pointer, err
}
pid, err := peer.IDB58Decode(pointerID)
if err != nil {
return pointer, err
}
k, err := cid.Decode(key)
if err != nil {
return pointer, err
}
var canID *peer.ID
if cancelID != "" {
c, err := peer.IDB58Decode(cancelID)
if err != nil {
return pointer, err
}
canID = &c
}
pointer = ipfs.Pointer{
Cid: k,
Value: ps.PeerInfo{
ID: pid,
Addrs: []ma.Multiaddr{maAddr},
},
CancelID: canID,
Purpose: ipfs.Purpose(purpose),
Timestamp: time.Unix(int64(timestamp), 0),
}
return pointer, nil
}