-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed many race conditions with Spark
- Loading branch information
Showing
110 changed files
with
1,135 additions
and
820 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
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
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
90 changes: 90 additions & 0 deletions
90
vtl-api/src/main/java/it/bancaditalia/oss/vtl/util/Holder.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,90 @@ | ||
/* | ||
* Copyright © 2020 Banca D'Italia | ||
* | ||
* Licensed under the EUPL, Version 1.2 (the "License"); | ||
* You may not use this work except in compliance with the | ||
* License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* https://joinup.ec.europa.eu/sites/default/files/custom-page/attachment/2020-03/EUPL-1.2%20EN.txt | ||
* | ||
* 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 it.bancaditalia.oss.vtl.util; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.io.Serializable; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.VarHandle; | ||
import java.util.function.BinaryOperator; | ||
|
||
/** | ||
* Simplified version of AtomicReference (to be used with Spark) | ||
* @param <V> | ||
*/ | ||
public class Holder<V> implements Serializable | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
private static final VarHandle VALUE; | ||
|
||
static { | ||
try | ||
{ | ||
MethodHandles.Lookup l = MethodHandles.lookup(); | ||
VALUE = l.findVarHandle(Holder.class, "value", Object.class); | ||
} | ||
catch (ReflectiveOperationException e) | ||
{ | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
public final Class<?> repr; | ||
private volatile V value; | ||
|
||
public Holder(Class<?> repr) | ||
{ | ||
this.repr = requireNonNull(repr); | ||
} | ||
|
||
public V get() | ||
{ | ||
return value; | ||
} | ||
|
||
public void set(V newValue) | ||
{ | ||
value = newValue; | ||
} | ||
|
||
public boolean compareAndSet(V expectedValue, V newValue) | ||
{ | ||
return VALUE.compareAndSet(this, expectedValue, newValue); | ||
} | ||
|
||
public V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction) | ||
{ | ||
V prev = get(), next = null; | ||
for (boolean haveNext = false;;) | ||
{ | ||
if (!haveNext) | ||
next = accumulatorFunction.apply(prev, x); | ||
if (VALUE.weakCompareAndSet(this, prev, next)) | ||
return next; | ||
haveNext = (prev == (prev = get())); | ||
} | ||
} | ||
|
||
public String toString() | ||
{ | ||
return String.valueOf(get()); | ||
} | ||
} |
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
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
Oops, something went wrong.