List of usage examples for java.time Instant MIN
Instant MIN
To view the source code for java.time Instant MIN.
Click Source Link
From source file:com.offbynull.voip.kademlia.model.RouteTree.java
/** * Updates the appropriate k-bucket in this route tree by touching it. When the Kademlia node that this route tree is for receives a * request or response from some other node in the network, this method should be called. * <p>/* w w w . j a v a2s. co m*/ * See {@link KBucket#touch(java.time.Instant, com.offbynull.voip.kademlia.model.Node) } for more information. * @param time time which request or response came in * @param node node which issued the request or response * @return changes to collection of stored nodes and replacement cache of the k-bucket effected * @throws NullPointerException if any argument is {@code null} * @throws IdLengthMismatchException if the bitlength of {@code node}'s ID doesn't match the bitlength of the owning node's ID (the ID * of the node this route tree is for) * @throws BaseIdMatchException if {@code node}'s ID is the same as the owning node's ID (the ID of the node this route tree is for) * @throws BackwardTimeException if {@code time} is less than the time used in the previous invocation of this method * @throws LinkMismatchException if this route tree already contains a node with {@code node}'s ID but with a different link (SPECIAL * CASE: If the contained node is marked as stale, this exception will not be thrown. Since the node is marked as stale, it means it * should have been replaced but the replacement cache was empty. As such, this case is treated as if this were a new node replacing * a stale node, not a stale node being reverted to normal status -- the fact that the IDs are the same but the links don't match * doesn't matter) * @see KBucket#touch(java.time.Instant, com.offbynull.voip.kademlia.model.Node) */ public RouteTreeChangeSet touch(Instant time, Node node) { Validate.notNull(time); Validate.notNull(node); Id id = node.getId(); InternalValidate.matchesLength(baseId.getBitLength(), id); InternalValidate.notMatchesBase(baseId, id); InternalValidate.forwardTime(lastTouchTime, time); // time must be >= lastUpdatedTime lastTouchTime = time; KBucket bucket = root.getBucketFor(node.getId()); // because we use this method to find the appropriate kbucket, // IdPrefixMismatchException never occurs KBucketChangeSet kBucketChangeSet = bucket.touch(time, node); BitString kBucketPrefix = bucket.getPrefix(); // insert last bucket activity time in to bucket update times... it may be null if bucket has never been accessed, in which case // we insert MIN instead Instant lastBucketActivityTime = bucket.getLatestBucketActivityTime(); if (lastBucketActivityTime == null) { lastBucketActivityTime = Instant.MIN; } bucketUpdateTimes.remove(kBucketPrefix); bucketUpdateTimes.insert(lastBucketActivityTime, kBucketPrefix); return new RouteTreeChangeSet(kBucketPrefix, kBucketChangeSet); }
From source file:com.offbynull.voip.kademlia.model.RouteTree.java
/** * Marks a node within this route tree as stale (meaning that you're no longer able to communicate with it), evicting it and replacing * it with the most recent node in the effected k-bucket's replacement cache. * <p>/* w ww . j a va2s.c om*/ * See {@link KBucket#stale(com.offbynull.voip.kademlia.model.Node) } for more information. * @param node node to mark as stale * @return changes to collection of stored nodes and replacement cache of the k-bucket effected * @throws NullPointerException if any argument is {@code null} * @throws IdLengthMismatchException if the bitlength of {@code node}'s ID doesn't match the bitlength of the owning node's ID (the ID * of the node this route tree is for) * @throws BaseIdMatchException if {@code node}'s ID is the same as the owning node's ID (the ID of the node this route tree is for) * @throws NodeNotFoundException if this route tree doesn't contain {@code node} * @throws LinkMismatchException if this route tree contains a node with {@code node}'s ID but with a different link * @throws BadNodeStateException if this route tree contains {@code node} but {@code node} is marked as locked * @see KBucket#stale(com.offbynull.voip.kademlia.model.Node) */ public RouteTreeChangeSet stale(Node node) { Validate.notNull(node); Id id = node.getId(); InternalValidate.matchesLength(baseId.getBitLength(), id); InternalValidate.notMatchesBase(baseId, id); KBucket bucket = root.getBucketFor(node.getId()); // because we use this method to find the appropriate kbucket, // IdPrefixMismatchException never occurs KBucketChangeSet kBucketChangeSet = bucket.stale(node); BitString kBucketPrefix = bucket.getPrefix(); // insert last bucket activity time in to bucket update times... it may be null if bucket has never been accessed, in which // case we insert MIN instead // // note that marking a node as stale may have replaced it in the bucket with another node in the cache. That cache node // could have an older time than the stale node, meaning that bucketUpdateTimes may actually be older after the replacement! Instant lastBucketActivityTime = bucket.getLatestBucketActivityTime(); if (lastBucketActivityTime == null) { lastBucketActivityTime = Instant.MIN; } bucketUpdateTimes.remove(kBucketPrefix); bucketUpdateTimes.insert(lastBucketActivityTime, kBucketPrefix); return new RouteTreeChangeSet(kBucketPrefix, kBucketChangeSet); }
From source file:com.offbynull.voip.kademlia.model.RouteTree.java
private RouteTreeNode createRoot(RouteTreeBranchStrategy branchStrategy, RouteTreeBucketStrategy bucketStrategy) { Validate.notNull(branchStrategy);/*from ww w. j a va2 s. c o m*/ Validate.notNull(bucketStrategy); // Get number of branches/buckets to create for root int numOfBuckets = branchStrategy.getBranchCount(EMPTY); Validate.isTrue(numOfBuckets >= 0, "Branch cannot be negative, was %d", numOfBuckets); Validate.isTrue(numOfBuckets != 0, "Root of tree must contain at least 1 branch, was %d", numOfBuckets); Validate.isTrue(Integer.bitCount(numOfBuckets) == 1, "Branch count must be power of 2"); int suffixBitCount = Integer.bitCount(numOfBuckets - 1); // num of bits e.g. 8 --> 1000 - 1 = 0111, bitcount(0111) = 3 Validate.isTrue(suffixBitCount <= baseId.getBitLength(), "Attempting to branch too far (in root) %d bits extends past %d bits", suffixBitCount, baseId.getBitLength()); // Create buckets by creating a 0-sized top bucket and splitting it + resizing each split KBucket[] newBuckets = new KBucket(baseId, EMPTY, 0, 0).split(suffixBitCount); for (int i = 0; i < newBuckets.length; i++) { BucketParameters bucketParams = bucketStrategy.getBucketParameters(newBuckets[i].getPrefix()); int bucketSize = bucketParams.getBucketSize(); int cacheSize = bucketParams.getCacheSize(); newBuckets[i].resizeBucket(bucketSize); newBuckets[i].resizeCache(cacheSize); // insert last bucket activity time in to bucket update times... it may be null if bucket has never been accessed, in which case // we insert MIN instead Instant lastBucketActivityTime = newBuckets[i].getLatestBucketActivityTime(); if (lastBucketActivityTime == null) { lastBucketActivityTime = Instant.MIN; } bucketUpdateTimes.insert(lastBucketActivityTime, newBuckets[i].getPrefix()); } // Create root return new RouteTreeNode(EMPTY, suffixBitCount, newBuckets); }
From source file:com.offbynull.voip.kademlia.model.RouteTree.java
private RouteTreeNode growParent(RouteTreeNode parent, RouteTreeBranchStrategy branchStrategy, RouteTreeBucketStrategy bucketStrategy) { Validate.notNull(parent);/* ww w . j a v a 2 s . c om*/ Validate.notNull(branchStrategy); Validate.notNull(bucketStrategy); // Calculate which bucket from parent to split int parentNumOfBuckets = parent.getBranchCount(); Validate.validState(Integer.bitCount(parentNumOfBuckets) == 1); // sanity check numofbuckets is pow of 2 int parentPrefixBitLen = parent.getPrefix().getBitLength(); // num of bits in parent's prefix int parentSuffixBitCount = Integer.bitCount(parentNumOfBuckets - 1); // num of bits in parent's suffix // e.g. 8 --> 1000 - 1 = 0111, bitcount(0111) = 3 if (parentPrefixBitLen + parentSuffixBitCount >= baseId.getBitLength()) { // should never be >, only ==, but just in case // The parents prefix length + the number of bits the parent used for buckets > baseId's length. As such, it isn't possible to // grow any further, so don't even try. return null; } int splitBucketIdx = (int) baseId.getBitString().getBitsAsLong(parentPrefixBitLen, parentSuffixBitCount); KBucket splitBucket = parent.getBranch(splitBucketIdx).getItem(); BitString splitBucketPrefix = splitBucket.getPrefix(); // Get number of buckets to create for new level int numOfBuckets = branchStrategy.getBranchCount(splitBucketPrefix); Validate.isTrue(numOfBuckets >= 2, "Branch count must be atleast 2, was %d", numOfBuckets); Validate.isTrue(Integer.bitCount(numOfBuckets) == 1, "Branch count must be power of 2"); int suffixBitCount = Integer.bitCount(numOfBuckets - 1); // num of bits e.g. 8 (1000) -- 1000 - 1 = 0111, bitcount(0111) = 3 Validate.isTrue(splitBucketPrefix.getBitLength() + suffixBitCount <= baseId.getBitLength(), "Attempting to branch too far %s with %d bits extends past %d bits", splitBucketPrefix, suffixBitCount, baseId.getBitLength()); // Split parent bucket at that branch index BitString newPrefix = baseId.getBitString().getBits(0, parentPrefixBitLen + suffixBitCount); KBucket[] newBuckets = splitBucket.split(suffixBitCount); for (int i = 0; i < newBuckets.length; i++) { BucketParameters bucketParams = bucketStrategy.getBucketParameters(newBuckets[i].getPrefix()); int bucketSize = bucketParams.getBucketSize(); int cacheSize = bucketParams.getCacheSize(); newBuckets[i].resizeBucket(bucketSize); newBuckets[i].resizeCache(cacheSize); Instant lastBucketActivityTime = newBuckets[i].getLatestBucketActivityTime(); if (lastBucketActivityTime == null) { lastBucketActivityTime = Instant.MIN; } bucketUpdateTimes.insert(lastBucketActivityTime, newBuckets[i].getPrefix()); } // Get rid of parent bucket we just split. It branches down at that point, and any nodes that were contained within will be in the // newly created buckets bucketUpdateTimes.remove(splitBucketPrefix); // Create new level and set as child RouteTreeNode newNode = new RouteTreeNode(newPrefix, suffixBitCount, newBuckets); parent.setBranch(splitBucketIdx, new RouteTreeNodeBranch(newNode)); return newNode; }