private void mergeAdjacentAllocations() { // merge all adjacent allocations for the same treatment if (allocations.size() < 2) { return; } final List<Allocation> newList = Lists.newArrayList(); int totalSize = allocations.get(0).getSize(); int offset = allocations.get(0).getOffset(); Treatment treatment = allocations.get(0).getTreatment(); for (int i=1; i<allocations.size(); i++) { final Allocation allocation = allocations.get(i); if (!allocation.getTreatment().equals(treatment)) { // different treatment newList.add(new Allocation(treatment, offset, totalSize)); totalSize = allocation.getSize(); offset = allocation.getOffset(); treatment = allocation.getTreatment(); } else { // same treatment, accumulate size totalSize += allocation.getSize(); } } newList.add(new Allocation(treatment, offset, totalSize)); allocations = newList; }
private AllocationEntity(Allocation allocation) { this.treatment = allocation.getTreatment().getName(); this.offset = allocation.getOffset(); this.size = allocation.getSize(); } }
if (index < allocations.size()) { final Allocation allocation = allocations.get(index); final int pieceSize = allocation.getOffset() - offset; if (pieceSize > 0) { sizeLeft -= pieceSize; pieces.add(new Allocation(treatment, offset, pieceSize)); offset = allocation.getOffset() + allocation.getSize(); index++; } else { pieces.add(new Allocation(treatment, offset, sizeLeft)); sizeLeft = 0;
public Builder addAllocation(String treatmentName, int offset, int size) { allocations.add(new Allocation(getTreatment(treatmentName), offset, size)); return this; }
@Override public AllocationDto toDto(Allocation allocation) { return new AllocationDto(allocation.getTreatment().getName(), allocation.getOffset(), allocation.getSize()); }
lastOffset = next.getOffset() + next.getSize(); if (!next.getTreatment().equals(source)) { continue; sizeLeft -= next.getSize(); iter.remove(); pieces.add(new Allocation(destination, next.getOffset(), next.getSize() + sizeLeft)); pieces.add(new Allocation(source, next.getOffset() + next.getSize() + sizeLeft, -sizeLeft)); } else { pieces.add(new Allocation(destination, next.getOffset(), next.getSize())); pieces.add(new Allocation(destination, lastOffset, sizeLeft));
private void rebuildAllocationTables() { final Map<Treatment, Integer> treatments = Maps.newHashMap(); int size = 0; allocationMap = new Byte[NUM_BINS]; for (Allocation allocation : allocations) { Integer treatment = treatments.get(allocation.getTreatment()); if (treatment == null) { treatment = treatments.size(); treatments.put(allocation.getTreatment(), treatment); } for (int i=allocation.getOffset(); i<allocation.getOffset() + allocation.getSize(); i++, size++) { Preconditions.checkState( allocationMap[i] == null, "overlapping allocations" ); allocationMap[i] = treatment.byteValue(); } } this.size = size; this.treatmentsMap = new Treatment[treatments.size()]; for (Entry<Treatment, Integer> entry : treatments.entrySet()) { this.treatmentsMap[entry.getValue()] = entry.getKey(); } }
/** * De-allocates bins from a treatment * @param treatment The treatment * @param size The number of bins */ public void deallocate(Treatment treatment, int size) { final Iterator<Allocation> iter = allocations.iterator(); int sizeLeft = size; while (iter.hasNext()) { final Allocation next = iter.next(); if (next.getTreatment().equals(treatment)) { sizeLeft -= next.getSize(); iter.remove(); } if (sizeLeft < 0) { // a piece could not be evenly split by the amount we wanted to deallocate, need to add back in allocations.add(new Allocation(treatment, next.getOffset() + next.getSize() + sizeLeft, -sizeLeft)); break; } } sortAllocations(); mergeAdjacentAllocations(); rebuildAllocationTables(); }
println( "treatment: %s, offset: %d, size: %d", allocation.getTreatment().getName(), allocation.getOffset(), allocation.getSize() );
private Experiment(Experiment toCopy) { this.owner = toCopy.owner; this.name = toCopy.name; this.treatments = Maps.newConcurrentMap(); for (final Treatment treatment : toCopy.getTreatments()) { this.treatments.put(treatment.getName(), new Treatment(treatment.getName(), treatment.getDescription())); } final List<Allocation> allocations = Lists.newArrayList(); for (final Allocation allocation : toCopy.getAllocations()) { final Treatment treatment = this.treatments.get(allocation.getTreatment().getName()); allocations.add(new Allocation(treatment, allocation.getOffset(), allocation.getSize())); } this.allocations = new Allocations(allocations); this.overrides = Maps.newConcurrentMap(); for (final TreatmentOverride override : toCopy.getOverrides()) { final Treatment treatment = this.treatments.get(override.getTreatment().getName()); final TreatmentOverride newOverride = new TreatmentOverride(override.getName(), override.getFilter(), treatment); overrides.put(override.getName(), newOverride); } this.seed = toCopy.seed; this.description = toCopy.description; this.filter = toCopy.filter; this.hashAttributes = Sets.newLinkedHashSet(toCopy.getHashAttributes()); this.active = toCopy.active; this.created = toCopy.created; this.modified = toCopy.modified; this.activated = toCopy.activated; this.deactivated = toCopy.deactivated; }