Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resource: Defer SHA-256 calculation into the future
The osgi.content capability must hold the SHA-256 value of the resource. But this value is rarely used. So, when building a resource from a file, we defer the SHA-256 calculation until actually needed. The can help performance of FileSetRepository used by bndtools m2e. However, we do need the hashCode of the SHA-256 value early, so use a substitute hashCode value based upon the File object. This is not perfect since two files can hold identical content and the hashCode of their SHA-256 values should be identical. But in practice, this should work when creating a resource from a file since the early need for the hashCode of the SHA-256 value is in the computing of the hashCode of the capability so it can be inserted in a set. But creating a resource from a file only creates a single osgi.content capability. Fixes #5322 Signed-off-by: BJ Hargrave <bj@hargrave.dev>
- Loading branch information
1 parent
86b426d
commit 8d9ac47
Showing
8 changed files
with
384 additions
and
14 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
19 changes: 19 additions & 0 deletions
19
biz.aQute.bndlib/src/aQute/bnd/osgi/resource/DeferredComparableValue.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,19 @@ | ||
package aQute.bnd.osgi.resource; | ||
|
||
import java.util.function.Supplier; | ||
|
||
class DeferredComparableValue<T extends Comparable<T>> extends DeferredValue<T> implements Comparable<T> { | ||
|
||
DeferredComparableValue(Class<T> type, Supplier<? extends T> supplier, int hashCode) { | ||
super(type, supplier, hashCode); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public int compareTo(T o) { | ||
if (o instanceof DeferredComparableValue) { | ||
o = ((DeferredComparableValue<T>) o).get(); | ||
} | ||
return get().compareTo(o); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
biz.aQute.bndlib/src/aQute/bnd/osgi/resource/DeferredValue.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,50 @@ | ||
package aQute.bnd.osgi.resource; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Supplier; | ||
|
||
class DeferredValue<T> implements Supplier<T> { | ||
private final Class<T> type; | ||
private final Supplier<? extends T> supplier; | ||
private final int hashCode; | ||
private T value; | ||
|
||
DeferredValue(Class<T> type, Supplier<? extends T> supplier, int hashCode) { | ||
this.type = requireNonNull(type); | ||
this.supplier = requireNonNull(supplier); | ||
this.hashCode = hashCode; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
T v = value; | ||
if (v == null) { | ||
return value = supplier.get(); | ||
} | ||
return v; | ||
} | ||
|
||
Class<T> type() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof DeferredValue) { | ||
obj = ((DeferredValue<T>) obj).get(); | ||
} | ||
return get().equals(obj); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(get()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
biz.aQute.bndlib/src/aQute/bnd/osgi/resource/DeferredValueEntry.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,57 @@ | ||
package aQute.bnd.osgi.resource; | ||
|
||
import java.util.Map.Entry; | ||
|
||
class DeferredValueEntry<K, V> implements Entry<K, V> { | ||
private final K key; | ||
private final DeferredValue<V> value; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param key Must not be {@code null}. | ||
* @param value Must not be {@code null}. | ||
*/ | ||
DeferredValueEntry(K key, DeferredValue<V> value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public K getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public V getValue() { | ||
return value.get(); | ||
} | ||
|
||
DeferredValue<V> getDeferredValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public V setValue(V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return key.hashCode() ^ value.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Entry)) { | ||
return false; | ||
} | ||
Entry<?, ?> entry = (Entry<?, ?>) obj; | ||
return key.equals(entry.getKey()) && value.equals(entry.getValue()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return key + "=" + value; | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
biz.aQute.bndlib/src/aQute/bnd/osgi/resource/DeferredValueMap.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,181 @@ | ||
package aQute.bnd.osgi.resource; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.AbstractSet; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
class DeferredValueMap<K, V> extends AbstractMap<K, V> implements Map<K, V> { | ||
private final Map<K, V> map; | ||
|
||
DeferredValueMap(Map<K, V> map) { | ||
this.map = map; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return map.size(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
return map.containsKey(key); | ||
} | ||
|
||
@Override | ||
public boolean containsValue(Object value) { | ||
return map.containsValue(value); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public V get(Object key) { | ||
V v = map.get(key); | ||
if (v instanceof DeferredValue) { | ||
v = ((DeferredValue<V>) v).get(); | ||
} | ||
return v; | ||
} | ||
|
||
/** | ||
* This get method will not unwrap a DeferredValue. | ||
* | ||
* @param key The map key. | ||
* @return The DeferredValue or value. | ||
*/ | ||
Object getDeferred(Object key) { | ||
Object v = map.get(key); | ||
return v; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return map.equals(o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return map.hashCode(); | ||
} | ||
|
||
@Override | ||
public Set<Entry<K, V>> entrySet() { | ||
return new EntrySet<>(map); | ||
} | ||
|
||
@Override | ||
public Set<K> keySet() { | ||
return map.keySet(); | ||
} | ||
|
||
final static class EntrySet<K, V> extends AbstractSet<Entry<K, V>> { | ||
private final Set<Entry<K, V>> entries; | ||
|
||
EntrySet(Map<K, V> map) { | ||
this.entries = map.entrySet(); | ||
} | ||
|
||
@Override | ||
public Iterator<Entry<K, V>> iterator() { | ||
return new EntryIterator<>(entries); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return entries.size(); | ||
} | ||
} | ||
|
||
final static class EntryIterator<K, V> implements Iterator<Entry<K, V>> { | ||
private final Iterator<Entry<K, V>> iterator; | ||
|
||
EntryIterator(Set<Entry<K, V>> entries) { | ||
this.iterator = entries.iterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public Entry<K, V> next() { | ||
Entry<K, V> entry = iterator.next(); | ||
V v = entry.getValue(); | ||
if (v instanceof DeferredValue) { | ||
@SuppressWarnings("unchecked") | ||
DeferredValue<V> deferred = (DeferredValue<V>) v; | ||
return new DeferredValueEntry<>(entry.getKey(), deferred); | ||
} | ||
return entry; | ||
} | ||
} | ||
|
||
@Override | ||
public V put(K key, V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V remove(Object key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void putAll(Map<? extends K, ? extends V> map) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V putIfAbsent(K key, V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object key, Object value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean replace(K key, V oldValue, V newValue) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V replace(K key, V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Oops, something went wrong.