Migrate existing code to LockBasedStorageManager.NO_LOCKS

This commit is contained in:
Andrey Breslav
2013-10-02 17:32:12 +04:00
parent aa90302d1b
commit db34868b6e
11 changed files with 113 additions and 283 deletions
@@ -1,44 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.storage;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
public abstract class MemoizedFunctionToNotNullImpl<K, V> extends MemoizedFunctionToNullableImpl<K, V> implements
MemoizedFunctionToNotNull<K, V> {
public MemoizedFunctionToNotNullImpl() {
}
public MemoizedFunctionToNotNullImpl(@NotNull Map<K, Object> map) {
super(map);
}
@NotNull
@Override
public V fun(K input) {
V result = super.fun(input);
assert result != null : "compute() returned null";
return result;
}
@NotNull
@Override
protected abstract V doCompute(K input);
}
@@ -1,54 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.storage.MemoizedFunctionToNullable;
import org.jetbrains.jet.utils.WrappedValues;
import java.util.HashMap;
import java.util.Map;
public abstract class MemoizedFunctionToNullableImpl<K, V> implements MemoizedFunctionToNullable<K, V> {
private final Map<K, Object> cache;
public MemoizedFunctionToNullableImpl() {
this(new HashMap<K, Object>());
}
public MemoizedFunctionToNullableImpl(@NotNull Map<K, Object> map) {
this.cache = map;
}
@Nullable
@Override
public V fun(K input) {
Object value = cache.get(input);
if (value != null) return WrappedValues.unescapeNull(value);
V typedValue = doCompute(input);
Object oldValue = cache.put(input, WrappedValues.escapeNull(typedValue));
assert oldValue == null : "Race condition detected";
return typedValue;
}
@Nullable
protected abstract V doCompute(K input);
}
@@ -1,34 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.storage;
import org.jetbrains.annotations.NotNull;
public abstract class NotNullLazyValueImpl<T> extends NullableLazyValueImpl<T> implements NotNullLazyValue<T> {
@NotNull
@Override
public T compute() {
T result = super.compute();
assert result != null : "compute() returned null";
return result;
}
@NotNull
@Override
protected abstract T doCompute();
}
@@ -1,17 +0,0 @@
package org.jetbrains.jet.storage;
import org.jetbrains.annotations.NotNull;
public abstract class NotNullLazyValueWithDefault<T> extends NotNullLazyValueImpl<T> {
private final T defaultValue;
protected NotNullLazyValueWithDefault(@NotNull T defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public Object recursionDetected(boolean firstTime) {
if (firstTime) return super.recursionDetected(firstTime);
return defaultValue;
}
}
@@ -1,85 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.storage;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.utils.ExceptionUtils;
import org.jetbrains.jet.utils.WrappedValues;
public abstract class NullableLazyValueImpl<T> implements NullableLazyValue<T> {
private enum State {
NOT_COMPUTED,
COMPUTING,
RECURSION_WAS_DETECTED
}
@Nullable
private Object value = State.NOT_COMPUTED;
@Override
public boolean isComputed() {
return value != State.NOT_COMPUTED && value != State.COMPUTING;
}
@Override
public T compute() {
if (!(value instanceof State)) {
// Computed already
return WrappedValues.unescapeThrowable(value);
}
if (value == State.COMPUTING) {
Object result = recursionDetected(true);
if (result != State.NOT_COMPUTED) {
value = State.RECURSION_WAS_DETECTED;
return WrappedValues.unescapeThrowable(result);
}
}
else if (value == State.RECURSION_WAS_DETECTED) {
Object result = recursionDetected(false);
if (result != State.NOT_COMPUTED) {
return WrappedValues.unescapeThrowable(result);
}
}
value = State.COMPUTING;
try {
T typedValue = doCompute();
value = typedValue;
postCompute(typedValue);
return typedValue;
}
catch (Throwable e) {
value = WrappedValues.escapeThrowable(e);
throw ExceptionUtils.rethrow(e);
}
}
/**
* @return {@code NOT_COMPUTED} to proceed, a value or wrapped exception otherwise, see WrappedValues
* @throws DO NOT throw exceptions from implementations of this method, instead return WrappedValues.escapeThrowable(exception)
*/
public Object recursionDetected(boolean firstTime) {
return WrappedValues.escapeThrowable(new ReenteringLazyValueComputationException());
}
protected abstract T doCompute();
protected void postCompute(T value) {
// Doing something in post-compute helps prevent infinite recursion
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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 org.jetbrains.jet.storage;
import com.intellij.openapi.util.Computable;
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.WrappedValues;
public class StorageUtil {
public static <T> NotNullLazyValue<T> createRecursionIntolerantLazyValueWithDefault(
@NotNull final T defaultValue,
@NotNull Computable<T> compute
) {
//noinspection unchecked
return LockBasedStorageManager.NO_LOCKS.createLazyValueWithPostCompute(
compute,
new Function<Boolean, Object>() {
@Override
public Object fun(Boolean firstTime) {
if (firstTime) return WrappedValues.escapeThrowable(new ReenteringLazyValueComputationException());
return defaultValue;
}
},
Consumer.EMPTY_CONSUMER
);
}
}