public Builder addAllocation(String treatmentName, int offset, int size) { allocations.add(new Allocation(getTreatment(treatmentName), offset, size)); return this; }
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; }
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));
if (pieceSize > 0) { sizeLeft -= pieceSize; pieces.add(new Allocation(treatment, offset, pieceSize)); pieces.add(new Allocation(treatment, offset, sizeLeft)); sizeLeft = 0;
/** * 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(); }
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; }