Skip to content

Commit e83129d

Browse files
authored
Merge pull request #40 from libp2p/doc
doc: document all types
2 parents 0fba523 + 8aad33e commit e83129d

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

composed.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ func (cr *Compose) Provide(ctx context.Context, c cid.Cid, local bool) error {
6363
return cr.ContentRouting.Provide(ctx, c, local)
6464
}
6565

66-
// FindProvidersAsync searches for peers who are able to provide a given key
66+
// FindProvidersAsync searches for peers who are able to provide a given key.
67+
//
68+
// If count > 0, it returns at most count providers. If count == 0, it returns
69+
// an unbounded number of providers.
6770
func (cr *Compose) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
6871
if cr.ContentRouting == nil {
6972
ch := make(chan peer.AddrInfo)

limited.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type LimitedValueStore struct {
1616
Namespaces []string
1717
}
1818

19-
// GetPublicKey returns the public key for the given peer.
19+
// GetPublicKey returns the public key for the given peer, if and only if this
20+
// router supports the /pk namespace. Otherwise, it returns routing.ErrNotFound.
2021
func (lvs *LimitedValueStore) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
2122
for _, ns := range lvs.Namespaces {
2223
if ns == "pk" {
@@ -26,7 +27,8 @@ func (lvs *LimitedValueStore) GetPublicKey(ctx context.Context, p peer.ID) (ci.P
2627
return nil, routing.ErrNotFound
2728
}
2829

29-
// PutValue returns ErrNotSupported
30+
// PutValue puts the given key in the underlying value store if the namespace
31+
// is supported. Otherwise, it returns routing.ErrNotSupported.
3032
func (lvs *LimitedValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
3133
if !lvs.KeySupported(key) {
3234
return routing.ErrNotSupported
@@ -51,16 +53,19 @@ func (lvs *LimitedValueStore) KeySupported(key string) bool {
5153
return false
5254
}
5355

54-
// GetValue returns routing.ErrNotFound if key isn't supported
56+
// GetValue retrieves the given key from the underlying value store if the namespace
57+
// is supported. Otherwise, it returns routing.ErrNotFound.
5558
func (lvs *LimitedValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
5659
if !lvs.KeySupported(key) {
5760
return nil, routing.ErrNotFound
5861
}
5962
return lvs.ValueStore.GetValue(ctx, key, opts...)
6063
}
6164

62-
// SearchValue returns empty channel if key isn't supported or calls SearchValue
63-
// on the underlying ValueStore
65+
// SearchValue searches the underlying value store for the given key if the
66+
// namespace is supported, returning results in monotonically increasing
67+
// "freshness". Otherwise, it returns an empty, closed channel to indicate that
68+
// the value wasn't found.
6469
func (lvs *LimitedValueStore) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
6570
if !lvs.KeySupported(key) {
6671
out := make(chan []byte)
@@ -70,13 +75,17 @@ func (lvs *LimitedValueStore) SearchValue(ctx context.Context, key string, opts
7075
return lvs.ValueStore.SearchValue(ctx, key, opts...)
7176
}
7277

78+
// Bootstrap signals the underlying value store to get into the "bootstrapped"
79+
// state, if it implements the Bootstrap interface.
7380
func (lvs *LimitedValueStore) Bootstrap(ctx context.Context) error {
7481
if bs, ok := lvs.ValueStore.(Bootstrap); ok {
7582
return bs.Bootstrap(ctx)
7683
}
7784
return nil
7885
}
7986

87+
// Close closest the underlying value store if it implements the io.Closer
88+
// interface.
8089
func (lvs *LimitedValueStore) Close() error {
8190
if closer, ok := lvs.ValueStore.(io.Closer); ok {
8291
return closer.Close()

parallel.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,17 @@ func (r Parallel) forKey(key string) Parallel {
289289
})
290290
}
291291

292+
// PutValue puts the given key to all sub-routers in parallel. It succeeds as
293+
// long as putting to at least one sub-router succeeds, but it waits for all
294+
// puts to terminate.
292295
func (r Parallel) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
293296
return r.forKey(key).put(func(ri routing.Routing) error {
294297
return ri.PutValue(ctx, key, value, opts...)
295298
})
296299
}
297300

301+
// GetValue searches all sub-routers for the given key, returning the result
302+
// from the first sub-router to complete the query.
298303
func (r Parallel) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
299304
vInt, err := r.forKey(key).get(ctx, func(ri routing.Routing) (interface{}, error) {
300305
return ri.GetValue(ctx, key, opts...)
@@ -303,6 +308,9 @@ func (r Parallel) GetValue(ctx context.Context, key string, opts ...routing.Opti
303308
return val, err
304309
}
305310

311+
// SearchValue searches all sub-routers for the given key in parallel,
312+
// returning results in monotonically increasing "freshness" from all
313+
// sub-routers.
306314
func (r Parallel) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
307315
resCh, err := r.forKey(key).search(ctx, func(ri routing.Routing) (<-chan []byte, error) {
308316
return ri.SearchValue(ctx, key, opts...)
@@ -342,6 +350,8 @@ func (r Parallel) SearchValue(ctx context.Context, key string, opts ...routing.O
342350
return valid, err
343351
}
344352

353+
// GetPublicKey retrieves the public key from all sub-routers in parallel,
354+
// returning the first result.
345355
func (r Parallel) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
346356
vInt, err := r.
347357
forKey(routing.KeyForPublicKey(p)).
@@ -352,6 +362,8 @@ func (r Parallel) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error
352362
return val, err
353363
}
354364

365+
// FindPeer finds the given peer in all sub-routers in parallel, returning the
366+
// first result.
355367
func (r Parallel) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
356368
vInt, err := r.filter(func(ri routing.Routing) bool {
357369
return supportsPeer(ri)
@@ -362,6 +374,13 @@ func (r Parallel) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error
362374
return pi, err
363375
}
364376

377+
// Provide announces that this peer provides the content in question to all
378+
// sub-routers in parallel. Provide returns success as long as a single
379+
// sub-router succeeds, but still waits for all sub-routers to finish before
380+
// returning.
381+
//
382+
// If count > 0, it returns at most count providers. If count == 0, it returns
383+
// an unbounded number of providers.
365384
func (r Parallel) Provide(ctx context.Context, c cid.Cid, local bool) error {
366385
return r.filter(func(ri routing.Routing) bool {
367386
return supportsContent(ri)
@@ -370,6 +389,11 @@ func (r Parallel) Provide(ctx context.Context, c cid.Cid, local bool) error {
370389
})
371390
}
372391

392+
// FindProvidersAsync searches all sub-routers in parallel for peers who are
393+
// able to provide a given key.
394+
//
395+
// If count > 0, it returns at most count providers. If count == 0, it returns
396+
// an unbounded number of providers.
373397
func (r Parallel) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
374398
routers := r.filter(func(ri routing.Routing) bool {
375399
return supportsContent(ri)
@@ -507,6 +531,7 @@ func fewProviders(ctx context.Context, out chan<- peer.AddrInfo, in []<-chan pee
507531
}
508532
}
509533

534+
// Bootstrap signals all the sub-routers to bootstrap.
510535
func (r Parallel) Bootstrap(ctx context.Context) error {
511536
var me multierror.Error
512537
for _, b := range r.Routers {
@@ -517,6 +542,7 @@ func (r Parallel) Bootstrap(ctx context.Context) error {
517542
return me.ErrorOrNil()
518543
}
519544

545+
// Close closes all sub-routers that implement the io.Closer interface.
520546
func (r Parallel) Close() error {
521547
var me multierror.Error
522548
for _, router := range r.Routers {

tiered.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type Tiered struct {
2020
Validator record.Validator
2121
}
2222

23+
// PutValue puts the given key to all sub-routers in parallel. It succeeds as
24+
// long as putting to at least one sub-router succeeds, but it waits for all
25+
// puts to terminate.
2326
func (r Tiered) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
2427
return Parallel{Routers: r.Routers}.PutValue(ctx, key, value, opts...)
2528
}
@@ -49,6 +52,8 @@ func (r Tiered) get(ctx context.Context, do func(routing.Routing) (interface{},
4952
}
5053
}
5154

55+
// GetValue sequentially searches each sub-router for the given key, returning
56+
// the value from the first sub-router to complete the query.
5257
func (r Tiered) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
5358
valInt, err := r.get(ctx, func(ri routing.Routing) (interface{}, error) {
5459
return ri.GetValue(ctx, key, opts...)
@@ -57,10 +62,15 @@ func (r Tiered) GetValue(ctx context.Context, key string, opts ...routing.Option
5762
return val, err
5863
}
5964

65+
// SearchValue searches all sub-routers for the given key in parallel,
66+
// returning results in monotonically increasing "freshness" from all
67+
// sub-routers.
6068
func (r Tiered) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
6169
return Parallel{Routers: r.Routers, Validator: r.Validator}.SearchValue(ctx, key, opts...)
6270
}
6371

72+
// GetPublicKey sequentially searches each sub-router for the the public key,
73+
// returning the first result.
6474
func (r Tiered) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
6575
vInt, err := r.get(ctx, func(ri routing.Routing) (interface{}, error) {
6676
return routing.GetPublicKey(ri, ctx, p)
@@ -69,14 +79,25 @@ func (r Tiered) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error)
6979
return val, err
7080
}
7181

82+
// Provide announces that this peer provides the content in question to all
83+
// sub-routers in parallel. Provide returns success as long as a single
84+
// sub-router succeeds, but still waits for all sub-routers to finish before
85+
// returning.
7286
func (r Tiered) Provide(ctx context.Context, c cid.Cid, local bool) error {
7387
return Parallel{Routers: r.Routers}.Provide(ctx, c, local)
7488
}
7589

90+
// FindProvidersAsync searches all sub-routers in parallel for peers who are
91+
// able to provide a given key.
92+
//
93+
// If count > 0, it returns at most count providers. If count == 0, it returns
94+
// an unbounded number of providers.
7695
func (r Tiered) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
7796
return Parallel{Routers: r.Routers}.FindProvidersAsync(ctx, c, count)
7897
}
7998

99+
// FindPeer sequentially searches for given peer using each sub-router,
100+
// returning the first result.
80101
func (r Tiered) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
81102
valInt, err := r.get(ctx, func(ri routing.Routing) (interface{}, error) {
82103
return ri.FindPeer(ctx, p)
@@ -85,10 +106,12 @@ func (r Tiered) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error)
85106
return val, err
86107
}
87108

109+
// Bootstrap signals all the sub-routers to bootstrap.
88110
func (r Tiered) Bootstrap(ctx context.Context) error {
89111
return Parallel{Routers: r.Routers}.Bootstrap(ctx)
90112
}
91113

114+
// Close closes all sub-routers that implement the io.Closer interface.
92115
func (r Tiered) Close() error {
93116
var me multierror.Error
94117
for _, router := range r.Routers {

0 commit comments

Comments
 (0)