Skip to content

Commit

Permalink
Provide optimized resource / filter / capability / requirement / capa…
Browse files Browse the repository at this point in the history
…bility set

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1829639 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
gnodet committed Apr 20, 2018
1 parent eeb35d4 commit 8cb2d55
Show file tree
Hide file tree
Showing 21 changed files with 3,186 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public List<Capability> getCapabilities(String namespace)

if (namespace == null || namespace.equals(IdentityNamespace.IDENTITY_NAMESPACE))
{
CapabilityImpl c = OSGiRepositoryImpl.newOSGiIdentityCapability(resource, this);
CapabilityImpl c = OSGiRepositoryImpl.newOSGiIdentityCapability(this, resource);
result.add(c);
}
if (namespace == null || namespace.equals(ContentNamespace.CONTENT_NAMESPACE))
{
CapabilityImpl c = OSGiRepositoryImpl.newOSGiContentCapability(resource, this);
CapabilityImpl c = OSGiRepositoryImpl.newOSGiContentCapability(this, resource);
result.add(c);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.felix.bundlerepository.impl;

import java.util.Map;

import org.apache.felix.utils.collections.StringArrayMap;

/**
* A map that can delay the computation of certain values up until the moment that they
* are actually needed. Useful for expensive to compute values such as the SHA-256.
* This map does <b>not</b> support {@code null} values.
*/
@SuppressWarnings("serial")
public class LazyStringMap<V> extends StringArrayMap<V>
{
public LazyStringMap(Map<String, ? extends V> map) {
super(map);
}

public LazyStringMap() {
}

public LazyStringMap(int capacity) {
super(capacity);
}

@Override
@SuppressWarnings("unchecked")
public V get(Object key)
{
V val = super.get(key);
if (val instanceof LazyValue) {
val = ((LazyValue<V>) val).compute();
if (val == null) {
throw new NullPointerException("Lazy computed values may not be null");
}
put((String) key, val);
}
return val;
}

public void putLazy(String key, LazyValue<V> lazy) {
super.doPut(key, lazy);
}

public interface LazyValue<V>
{
V compute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.apache.felix.bundlerepository.impl.LazyHashMap.LazyValue;
import org.apache.felix.bundlerepository.Resource;
import org.apache.felix.utils.resource.CapabilityImpl;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.resource.Capability;
import org.osgi.resource.Namespace;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.service.repository.ContentNamespace;
import org.osgi.service.repository.Repository;

Expand Down Expand Up @@ -120,8 +118,7 @@ private void addResourceForIdentity(final org.apache.felix.bundlerepository.Reso
caps.add(idCap);
}

static CapabilityImpl newOSGiIdentityCapability(org.apache.felix.bundlerepository.Resource res,
org.osgi.resource.Resource targetResource)
static CapabilityImpl newOSGiIdentityCapability(org.osgi.resource.Resource or, org.apache.felix.bundlerepository.Resource res)
{
@SuppressWarnings("unchecked")
Map<String, Object> idAttrs = new HashMap<String, Object>(res.getProperties());
Expand All @@ -132,32 +129,34 @@ static CapabilityImpl newOSGiIdentityCapability(org.apache.felix.bundlerepositor
if (idAttrs.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE) == null)
idAttrs.put(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE, IdentityNamespace.TYPE_BUNDLE);

return new CapabilityImpl(IdentityNamespace.IDENTITY_NAMESPACE, idAttrs, Collections.<String, String> emptyMap(), targetResource);
return new CapabilityImpl(or, IdentityNamespace.IDENTITY_NAMESPACE, Collections.<String, String> emptyMap(), idAttrs);
}

static CapabilityImpl newOSGiContentCapability(org.apache.felix.bundlerepository.Resource resource,
org.osgi.resource.Resource targetResource)
static CapabilityImpl newOSGiContentCapability(org.osgi.resource.Resource or, Resource resource)
{
final String uri = resource.getURI();
LazyValue<String, Object> lazyValue =
new LazyValue<String, Object>(ContentNamespace.CONTENT_NAMESPACE, new Callable<Object>()
{
public Object call() throws Exception
{
// This is expensive to do, so only compute it when actually obtained...
LazyStringMap.LazyValue<String> content = new LazyStringMap.LazyValue<String>() {
public String compute() {
// This is expensive to do, so only compute it when actually obtained...
try {
return OSGiRepositoryImpl.getSHA256(uri);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
});

}
};
Object mime = resource.getProperties().get("mime");
if (mime == null)
mime = "application/vnd.osgi.bundle";

Map<String, Object> contentAttrs = new LazyHashMap<String, Object>(Collections.singleton(lazyValue));
Map<String, Object> contentAttrs = new LazyStringMap<Object>(4);
contentAttrs.put(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE, mime);
contentAttrs.put(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE, resource.getSize());
contentAttrs.put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, uri);
return new ContentCapabilityImpl(contentAttrs, targetResource);
contentAttrs.put(ContentNamespace.CONTENT_NAMESPACE, content);
return new CapabilityImpl(or, ContentNamespace.CONTENT_NAMESPACE, Collections.<String, String> emptyMap(), contentAttrs);
}

static String getSHA256(String uri) throws IOException, NoSuchAlgorithmException // TODO find a good place for this
Expand All @@ -183,19 +182,4 @@ static String getSHA256(String uri) throws IOException, NoSuchAlgorithmException
return sb.toString();
}

// This capability variant does not take a private copy of the capabilities so that it
// can lazily compute the content hash.
private static class ContentCapabilityImpl extends CapabilityImpl implements Capability {
private final Map<String, Object> contentAttributes;

public ContentCapabilityImpl(Map<String, Object> contentAttrs, Resource targetResource) {
super(ContentNamespace.CONTENT_NAMESPACE, null, null, targetResource);
contentAttributes = Collections.unmodifiableMap(contentAttrs);
}

@Override
public Map<String, Object> getAttributes() {
return contentAttributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,39 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.TestCase;
import org.apache.felix.bundlerepository.impl.LazyStringMap.LazyValue;

import org.apache.felix.bundlerepository.impl.LazyHashMap.LazyValue;

public class LazyHashMapTest extends TestCase
public class LazyStringMapTest extends TestCase
{
public void testLazyHashMap() {
final AtomicInteger lv1Computed = new AtomicInteger(0);
LazyValue<String, Long> lv1 = new LazyValue<String, Long>("42", new Callable<Long>()
{
public Long call() throws Exception
{
LazyValue<Long> lv1 = new LazyValue<Long>() {
public Long compute() {
lv1Computed.incrementAndGet();
return 24L;
}
});
};

final AtomicInteger lv2Computed = new AtomicInteger(0);
LazyValue<String, Long> lv2 = new LazyValue<String, Long>("zero", new Callable<Long>()
{
public Long call() throws Exception
{
LazyValue<Long> lv2 = new LazyValue<Long>() {
public Long compute() {
lv2Computed.incrementAndGet();
return 0L;
}
});
};

Collection<LazyValue<String, Long>> lazyValues = new ArrayList<LazyHashMap.LazyValue<String,Long>>();
Collection<LazyValue<Long>> lazyValues = new ArrayList<LazyValue<Long>>();
lazyValues.add(lv1);
lazyValues.add(lv2);
HashMap<String, Long> lhm = new LazyHashMap<String, Long>(lazyValues);
LazyStringMap<Long> lhm = new LazyStringMap<Long>();
lhm.put("1", 2L);
lhm.putLazy("42", lv1);
lhm.putLazy("zero", lv2);

assertEquals(new Long(2L), lhm.get("1"));
assertEquals("No computation should have happened yet", 0, lv1Computed.get());
Expand Down
Loading

0 comments on commit 8cb2d55

Please sign in to comment.