Coverage Summary for Class: Bucket (co.rsk.net.discovery.table)

Class Class, % Method, % Line, %
Bucket 0% (0/1) 0% (0/7) 0% (0/26)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.net.discovery.table; 20  21 import org.ethereum.net.rlpx.Node; 22  23 import java.util.*; 24 import java.util.concurrent.ConcurrentHashMap; 25  26 /** 27  * Created by mario on 21/02/17. 28  */ 29 public class Bucket { 30  private Map<String, BucketEntry> entries; 31  private final int bucketSize; 32  private final int id; 33  34  public Bucket(int bucketSize, int bucketId) { 35  this.bucketSize = bucketSize; 36  this.entries = new ConcurrentHashMap<>(); 37  this.id = bucketId; 38  } 39  40  public int getId() { 41  return id; 42  } 43  44  public synchronized OperationResult addNode(Node node) { 45  if (entries.size() == bucketSize) { 46  return new OperationResult(false, this.getOldestEntry()); 47  } else { 48  String nodeId = node.getHexId(); 49  BucketEntry entry = this.entries.get(nodeId); 50  if (entry == null) { 51  entry = new BucketEntry(node); 52  entries.put(nodeId, entry); 53  } 54  entry.updateTime(); 55  return new OperationResult(true, entry); 56  } 57  } 58  59  public synchronized OperationResult removeNode(Node node) { 60  BucketEntry toRemove = this.entries.remove(node.getHexId()); 61  62  if (toRemove != null) { 63  return new OperationResult(true, toRemove); 64  } else { 65  return new OperationResult(false, null); 66  } 67  } 68  69  public synchronized Set<BucketEntry> getEntries() { 70  return new HashSet<>(this.entries.values()); 71  } 72  73  public synchronized BucketEntry getOldestEntry() { 74  List<BucketEntry> bucketEntries = new ArrayList<>(); 75  bucketEntries.addAll(this.entries.values()); 76  Collections.sort(bucketEntries, new BucketEntryComparator()); 77  return bucketEntries.get(0); 78  } 79  80  public void updateEntry(Node node) { 81  BucketEntry entry = this.entries.get(node.getHexId()); 82  83  if (entry != null) { 84  entry.updateTime(); 85  } 86  } 87 }