Skip to content

Commit

Permalink
Add Copy, IncrementLong, and Composition
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kielbus committed Jun 12, 2014
1 parent 3f54292 commit 3369fd0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 6 deletions.
Expand Up @@ -4,8 +4,10 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import com.liveramp.megadesk.core.state.Variable;
import com.liveramp.megadesk.core.transaction.Dependency;
Expand Down Expand Up @@ -107,7 +109,7 @@ public Builder reads(Variable[]... dependencies) {
return reads(concatenate(dependencies));
}

public Builder reads(List<Variable> dependencies) {
public Builder reads(Collection<Variable> dependencies) {
this.reads = Lists.newArrayList(dependencies);
return this;
}
Expand All @@ -120,7 +122,7 @@ public Builder writes(Variable[]... dependencies) {
return writes(concatenate(dependencies));
}

public Builder writes(List<Variable> dependencies) {
public Builder writes(Collection<Variable> dependencies) {
this.writes = Lists.newArrayList(dependencies);
return this;
}
Expand Down Expand Up @@ -151,11 +153,14 @@ public static Builder builder() {
}

public static BaseDependency merge(Dependency... dependencies) {
Builder builder = BaseDependency.builder();
List<VariableDependency> all = Lists.newArrayList();
Set<Variable> reads = Sets.newHashSet();
Set<Variable> writes = Sets.newHashSet();
for (Dependency dependency : dependencies) {
all.addAll(dependency.all());
reads.addAll(dependency.reads());
writes.addAll(dependency.writes());
}
return builder.all(all).build();
// Promote reads to writes
reads.removeAll(Sets.intersection(reads, writes));
return BaseDependency.builder().reads(reads).writes(writes).build();
}
}
@@ -0,0 +1,41 @@
/**
* Copyright 2014 LiveRamp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.liveramp.megadesk.recipes.transaction;

import com.liveramp.megadesk.base.transaction.BaseDependency;
import com.liveramp.megadesk.base.transaction.BaseTransaction;
import com.liveramp.megadesk.core.state.Variable;
import com.liveramp.megadesk.core.transaction.Context;

public class Copy<VALUE> extends BaseTransaction<VALUE> {

private final Variable<VALUE> source;
private final Variable<VALUE> destination;

public Copy(Variable<VALUE> source, Variable<VALUE> destination) {
super(BaseDependency.builder().reads(source).writes(destination).build());
this.source = source;
this.destination = destination;
}

@Override
public VALUE run(Context context) throws Exception {
VALUE value = context.read(source);
context.write(destination, value);
return value;
}
}
@@ -0,0 +1,39 @@
/**
* Copyright 2014 LiveRamp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.liveramp.megadesk.recipes.transaction;

import com.liveramp.megadesk.core.state.Variable;
import com.liveramp.megadesk.core.transaction.Transaction;

public class IncrementLong extends Alter<Long> implements Transaction<Long> {

private final long increment;

public IncrementLong(Variable<Long> variable) {
this(variable, 1L);
}

public IncrementLong(Variable<Long> variable, long increment) {
super(variable);
this.increment = increment;
}

@Override
protected Long alter(Long value) {
return value + increment;
}
}
@@ -0,0 +1,51 @@
/**
* Copyright 2014 LiveRamp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.liveramp.megadesk.recipes.transaction;

import org.junit.Test;

import com.liveramp.megadesk.base.state.InMemoryLocal;
import com.liveramp.megadesk.base.transaction.BaseTransactionExecutor;
import com.liveramp.megadesk.core.state.Variable;
import com.liveramp.megadesk.core.transaction.TransactionExecutor;
import com.liveramp.megadesk.test.BaseTestCase;

import static org.junit.Assert.assertEquals;

public class TestComposition extends BaseTestCase {

@Test
public void testMain() throws Exception {
TransactionExecutor executor = new BaseTransactionExecutor();

Variable<Long> v1 = new InMemoryLocal<Long>(0L);
Variable<Long> v2 = new InMemoryLocal<Long>(0L);
Variable<Long> v3 = new InMemoryLocal<Long>(0L);

executor.execute(new Composition(
new Write<Long>(v1, 1L),
new Copy<Long>(v1, v2),
new IncrementLong(v2, 2),
new Copy<Long>(v2, v3),
new IncrementLong(v3, 3)
));

assertEquals(1, (long)executor.execute(new Read<Long>(v1)));
assertEquals(3, (long)executor.execute(new Read<Long>(v2)));
assertEquals(6, (long)executor.execute(new Read<Long>(v3)));
}
}

0 comments on commit 3369fd0

Please sign in to comment.