@Override public boolean hasValidKeys( KeysFetchingLocally keys, ObjectContainer container, ClientContext context) { if (persistent) { container.activate(this, 1); container.activate(blockNums, 1); container.activate(segment, 1); } boolean hasSet = false; boolean retval = false; synchronized (segment) { for (int i = 0; i < 10; i++) { Integer ret; int x; if (blockNums.isEmpty()) { break; } x = context.random.nextInt(blockNums.size()); ret = blockNums.get(x); int block = ret; Key key = segment.getBlockNodeKey(block, container); if (key == null) { if (segment.isFinishing(container) || segment.isFinished(container)) return false; if (segment.haveBlock(block, container)) Logger.error( this, "Already have block " + ret + " but was in blockNums on " + this + " in hasValidKeys"); else Logger.error( this, "Key is null for block " + ret + " for " + this + " in hasValidKeys"); blockNums.remove(x); if (persistent) { container.delete(ret); if (!hasSet) { hasSet = true; container.store(blockNums); } } continue; } if (keys.hasKey(key)) { continue; } retval = true; break; } } if (persistent) { container.deactivate(blockNums, 5); container.deactivate(segment, 1); } return retval; }
private SendableRequestItem getRandomBlockNum( KeysFetchingLocally keys, ClientContext context, ObjectContainer container) { if (persistent) { container.activate(this, 1); container.activate(blockNums, 1); container.activate(segment, 1); } logMINOR = Logger.shouldLog(Logger.MINOR, this); synchronized (segment) { if (blockNums.isEmpty()) { if (logMINOR) Logger.minor(this, "No blocks to remove"); return null; } for (int i = 0; i < 10; i++) { Integer ret; int x; if (blockNums.size() == 0) return null; x = context.random.nextInt(blockNums.size()); ret = blockNums.get(x); int num = ret; Key key = segment.getBlockNodeKey(num, container); if (key == null) { if (segment.isFinishing(container) || segment.isFinished(container)) return null; if (segment.haveBlock(num, container)) Logger.error(this, "Already have block " + ret + " but was in blockNums on " + this); else Logger.error(this, "Key is null for block " + ret + " for " + this); continue; } if (keys.hasKey(key)) { continue; } if (logMINOR) Logger.minor( this, "Removing block " + x + " of " + (blockNums.size() + 1) + " : " + ret + " on " + this); return new MySendableRequestItem(num); } return null; } }