Skip to content

Commit 2f182c7

Browse files
deduplicate operation execution code
1 parent 38a707a commit 2f182c7

File tree

1 file changed

+31
-110
lines changed

1 file changed

+31
-110
lines changed

provider/datastore/keystore.go

Lines changed: 31 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -439,160 +439,81 @@ func (s *keyStore) size(ctx context.Context) (size int, err error) {
439439
return
440440
}
441441

442-
// Put stores the provided keys in the underlying datastore, grouping them by
443-
// the first prefixLen bits. It returns only the keys that were not previously
444-
// persisted in the datastore (i.e., newly added keys).
445-
func (s *keyStore) Put(ctx context.Context, keys ...mh.Multihash) ([]mh.Multihash, error) {
446-
if len(keys) == 0 {
447-
return nil, nil
448-
}
442+
// executeOperation sends an operation request to the worker goroutine and
443+
// waits for the response. It handles the communication protocol and returns
444+
// the results based on the operation type.
445+
func (s *keyStore) executeOperation(op opType, ctx context.Context, keys []mh.Multihash, prefix bitstr.Key) ([]mh.Multihash, int, bool, error) {
449446
response := make(chan operationResponse, 1)
450447
select {
451448
case s.requests <- operation{
452-
op: opPut,
449+
op: op,
453450
ctx: ctx,
454451
keys: keys,
452+
prefix: prefix,
455453
response: response,
456454
}:
457455
case <-ctx.Done():
458-
return nil, ctx.Err()
456+
return nil, 0, false, ctx.Err()
459457
case <-s.close:
460-
return nil, ErrKeyStoreClosed
458+
return nil, 0, false, ErrKeyStoreClosed
461459
}
462460

463461
select {
464462
case resp := <-response:
465-
return resp.multihashes, resp.err
463+
return resp.multihashes, resp.size, resp.found, resp.err
466464
case <-ctx.Done():
467-
return nil, ctx.Err()
465+
return nil, 0, false, ctx.Err()
468466
}
469467
}
470468

469+
// Put stores the provided keys in the underlying datastore, grouping them by
470+
// the first prefixLen bits. It returns only the keys that were not previously
471+
// persisted in the datastore (i.e., newly added keys).
472+
func (s *keyStore) Put(ctx context.Context, keys ...mh.Multihash) ([]mh.Multihash, error) {
473+
if len(keys) == 0 {
474+
return nil, nil
475+
}
476+
newKeys, _, _, err := s.executeOperation(opPut, ctx, keys, "")
477+
return newKeys, err
478+
}
479+
471480
// Get returns all keys whose bit256 representation matches the provided
472481
// prefix.
473482
func (s *keyStore) Get(ctx context.Context, prefix bitstr.Key) ([]mh.Multihash, error) {
474-
response := make(chan operationResponse, 1)
475-
select {
476-
case s.requests <- operation{
477-
op: opGet,
478-
ctx: ctx,
479-
prefix: prefix,
480-
response: response,
481-
}:
482-
case <-ctx.Done():
483-
return nil, ctx.Err()
484-
case <-s.close:
485-
return nil, ErrKeyStoreClosed
486-
}
487-
488-
select {
489-
case resp := <-response:
490-
return resp.multihashes, resp.err
491-
case <-ctx.Done():
492-
return nil, ctx.Err()
493-
}
483+
keys, _, _, err := s.executeOperation(opGet, ctx, nil, prefix)
484+
return keys, err
494485
}
495486

496487
// ContainsPrefix reports whether the KeyStore currently holds at least one
497488
// multihash whose kademlia identifier (bit256.Key) starts with the provided
498489
// bit-prefix.
499490
func (s *keyStore) ContainsPrefix(ctx context.Context, prefix bitstr.Key) (bool, error) {
500-
response := make(chan operationResponse, 1)
501-
select {
502-
case s.requests <- operation{
503-
op: opContainsPrefix,
504-
ctx: ctx,
505-
prefix: prefix,
506-
response: response,
507-
}:
508-
case <-ctx.Done():
509-
return false, ctx.Err()
510-
case <-s.close:
511-
return false, ErrKeyStoreClosed
512-
}
513-
514-
select {
515-
case resp := <-response:
516-
return resp.found, resp.err
517-
case <-ctx.Done():
518-
return false, ctx.Err()
519-
}
491+
_, _, found, err := s.executeOperation(opContainsPrefix, ctx, nil, prefix)
492+
return found, err
520493
}
521494

522495
// Empty deletes all entries under the datastore prefix.
523496
func (s *keyStore) Empty(ctx context.Context) error {
524-
response := make(chan operationResponse, 1)
525-
select {
526-
case s.requests <- operation{
527-
op: opEmpty,
528-
ctx: ctx,
529-
response: response,
530-
}:
531-
case <-ctx.Done():
532-
return ctx.Err()
533-
case <-s.close:
534-
return ErrKeyStoreClosed
535-
}
536-
537-
select {
538-
case resp := <-response:
539-
return resp.err
540-
case <-ctx.Done():
541-
return ctx.Err()
542-
}
497+
_, _, _, err := s.executeOperation(opEmpty, ctx, nil, "")
498+
return err
543499
}
544500

545501
// Delete removes the given keys from datastore.
546502
func (s *keyStore) Delete(ctx context.Context, keys ...mh.Multihash) error {
547503
if len(keys) == 0 {
548504
return nil
549505
}
550-
response := make(chan operationResponse, 1)
551-
select {
552-
case s.requests <- operation{
553-
op: opDelete,
554-
ctx: ctx,
555-
keys: keys,
556-
response: response,
557-
}:
558-
case <-ctx.Done():
559-
return ctx.Err()
560-
case <-s.close:
561-
return ErrKeyStoreClosed
562-
}
563-
564-
select {
565-
case resp := <-response:
566-
return resp.err
567-
case <-ctx.Done():
568-
return ctx.Err()
569-
}
506+
_, _, _, err := s.executeOperation(opDelete, ctx, keys, "")
507+
return err
570508
}
571509

572510
// Size returns the number of keys currently stored in the KeyStore.
573511
//
574512
// The size is obtained by iterating over all keys in the underlying
575513
// datastore, so it may be expensive for large stores.
576514
func (s *keyStore) Size(ctx context.Context) (int, error) {
577-
response := make(chan operationResponse, 1)
578-
select {
579-
case s.requests <- operation{
580-
op: opSize,
581-
ctx: ctx,
582-
response: response,
583-
}:
584-
case <-ctx.Done():
585-
return 0, ctx.Err()
586-
case <-s.close:
587-
return 0, ErrKeyStoreClosed
588-
}
589-
590-
select {
591-
case resp := <-response:
592-
return resp.size, resp.err
593-
case <-ctx.Done():
594-
return 0, ctx.Err()
595-
}
515+
_, size, _, err := s.executeOperation(opSize, ctx, nil, "")
516+
return size, err
596517
}
597518

598519
// Close shuts down the worker goroutine and releases resources.

0 commit comments

Comments
 (0)