@@ -439,160 +439,81 @@ func (s *keyStore) size(ctx context.Context) (size int, err error) {
439
439
return
440
440
}
441
441
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 ) {
449
446
response := make (chan operationResponse , 1 )
450
447
select {
451
448
case s .requests <- operation {
452
- op : opPut ,
449
+ op : op ,
453
450
ctx : ctx ,
454
451
keys : keys ,
452
+ prefix : prefix ,
455
453
response : response ,
456
454
}:
457
455
case <- ctx .Done ():
458
- return nil , ctx .Err ()
456
+ return nil , 0 , false , ctx .Err ()
459
457
case <- s .close :
460
- return nil , ErrKeyStoreClosed
458
+ return nil , 0 , false , ErrKeyStoreClosed
461
459
}
462
460
463
461
select {
464
462
case resp := <- response :
465
- return resp .multihashes , resp .err
463
+ return resp .multihashes , resp .size , resp . found , resp . err
466
464
case <- ctx .Done ():
467
- return nil , ctx .Err ()
465
+ return nil , 0 , false , ctx .Err ()
468
466
}
469
467
}
470
468
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
+
471
480
// Get returns all keys whose bit256 representation matches the provided
472
481
// prefix.
473
482
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
494
485
}
495
486
496
487
// ContainsPrefix reports whether the KeyStore currently holds at least one
497
488
// multihash whose kademlia identifier (bit256.Key) starts with the provided
498
489
// bit-prefix.
499
490
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
520
493
}
521
494
522
495
// Empty deletes all entries under the datastore prefix.
523
496
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
543
499
}
544
500
545
501
// Delete removes the given keys from datastore.
546
502
func (s * keyStore ) Delete (ctx context.Context , keys ... mh.Multihash ) error {
547
503
if len (keys ) == 0 {
548
504
return nil
549
505
}
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
570
508
}
571
509
572
510
// Size returns the number of keys currently stored in the KeyStore.
573
511
//
574
512
// The size is obtained by iterating over all keys in the underlying
575
513
// datastore, so it may be expensive for large stores.
576
514
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
596
517
}
597
518
598
519
// Close shuts down the worker goroutine and releases resources.
0 commit comments