Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Copy, IncrementLong, and Composition
- Loading branch information
1 parent
3f54292
commit 3369fd0
Showing
4 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
megadesk-recipes/src/main/java/com/liveramp/megadesk/recipes/transaction/Copy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
megadesk-recipes/src/main/java/com/liveramp/megadesk/recipes/transaction/IncrementLong.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...desk-recipes/src/test/java/com/liveramp/megadesk/recipes/transaction/TestComposition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
} |