/** * Allocates bins to a treatment * @param treatmentName The treatment * @param size The number of bins */ public Experiment allocate(String treatmentName, int size) { allocations.allocate(treatment(treatmentName), size); return this; }
/** * Removes all allocations */ public Experiment deallocateAll() { allocations.clear(); return this; }
/** * De-allocates bins from a treatment * @param treatmentName The treatment * @param size The number of bins */ public Experiment deallocate(String treatmentName, int size) { allocations.deallocate(treatment(treatmentName), size); return this; }
public Allocations(Iterable<Allocation> allocations) { this.allocations = Lists.newArrayList(allocations); sortAllocations(); rebuildAllocationTables(); }
getUnallocatedSize() >= size, "not enough free bins to allocate treatment %s with size %s given %s unallocated bin(s)", treatment.getName(), size, getUnallocatedSize() ); sortAllocations(); mergeAdjacentAllocations(); rebuildAllocationTables();
/** * 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(); }
protected Experiment(Experiments owner, String name) { this.owner = owner; this.name = name; this.filter = FilterExpression.alwaysTrue(); this.hashAttributes = EMPTY_SET; this.allocations = new Allocations(); this.treatments = Maps.newConcurrentMap(); this.overrides = Maps.newConcurrentMap(); this.seed = (int) IdentityBuilder.seed(0).putString(name).hash(); }
/** * Gets all allocations defined on this experiment */ public List<Allocation> getAllocations() { return Collections.unmodifiableList(allocations.getAllocations()); }
public void clear() { allocations.clear(); rebuildAllocationTables(); } }
/** * Returns treatment for an identity * @param identity The identity * @return the treatment assigned to given identity */ public Treatment getTreatment(Identity identity, AttributesMap attributes) { return allocations.getTreatment(identityToBin(identity, attributes)); }
/** * Reallocates bins from one treatment to another * @param sourceTreatmentName The source treatment * @param destinationTreatmentName The destination treatment * @param size The number of bins */ public Experiment reallocate(String sourceTreatmentName, String destinationTreatmentName, int size) { allocations.reallocate( treatment(sourceTreatmentName), treatment(destinationTreatmentName), size ); return this; }
this.treatments = new ConcurrentHashMap<>(treatments); this.overrides = new ConcurrentHashMap<>(Maps.uniqueIndex(overrides, TREATMENT_INDEXER)); this.allocations = new Allocations(allocations); this.seed = seed;
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; }
/** * Removes a treatment * @param name The treatment */ public Experiment removeTreatment(String name) { final Treatment treatment = treatments.get(name); if (treatment == null) { return this; } removeOverrides(name); allocations.deallocate(treatment, Allocations.NUM_BINS); treatments.remove(name); return this; }