Skip to content

Commit 0f51d9e

Browse files
gselzerctrueden
authored andcommitted
Convert OpCache to Map<String, List<OpInfo>>
This is simpler than the PrefixTree and the Map of aliases Also, fix broken tests
1 parent b1423fd commit 0f51d9e

2 files changed

Lines changed: 16 additions & 36 deletions

File tree

src/main/java/org/scijava/ops/OpService.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.List;
4343
import java.util.Map;
4444
import java.util.Map.Entry;
45+
import java.util.stream.Collectors;
4546

4647
import org.scijava.InstantiableException;
4748
import org.scijava.log.LogService;
@@ -129,13 +130,13 @@ public class OpService extends AbstractService implements SciJavaService, OpEnvi
129130
/**
130131
* Prefix tree to cache and quickly find {@link OpInfo}s.
131132
*/
132-
private PrefixTree<OpInfo> opCache;
133+
// private PrefixTree<OpInfo> opCache;
133134

134135
/**
135136
* Map to collect all aliases for a specific op. All aliases will map to one
136137
* canonical name of the op which is defined as the first one.
137138
*/
138-
private Map<String, String> opAliases = new HashMap<>();
139+
private Map<String, List<OpInfo>> opCache;
139140

140141
private static Map<Class<?>, Class<?>> wrappers = wrappers();
141142

@@ -178,7 +179,7 @@ private static Map<Class<?>, Class<?>> wrappers() {
178179
}
179180

180181
public void initOpCache() {
181-
opCache = new PrefixTree<>();
182+
opCache = new HashMap<>();
182183

183184
// Add regular Ops
184185
for (final PluginInfo<Op> pluginInfo : pluginService.getPluginsOfType(Op.class)) {
@@ -222,16 +223,21 @@ private void addToCache(OpInfo opInfo, String opNames) {
222223
+ opInfo.getValidityException().getMessage());
223224
return;
224225
}
225-
addAliases(parsedOpNames, opInfo.implementationName());
226-
opCache.add(new PrefixQuery(parsedOpNames[0]), opInfo);
226+
for(String opName: parsedOpNames) {
227+
if (!opCache.containsKey(opName))
228+
opCache.put(opName, new ArrayList<>());
229+
opCache.get(opName).add(opInfo);
230+
}
227231
}
228232

229233
@Override
230234
public Iterable<OpInfo> infos() {
231235
if (opCache == null) {
232236
initOpCache();
233237
}
234-
return opCache.getAndBelow(PrefixQuery.all());
238+
return opCache.values().stream()
239+
.flatMap(list -> list.stream())
240+
.collect(Collectors.toList());
235241
}
236242

237243
@Override
@@ -242,11 +248,10 @@ public Iterable<OpInfo> infos(String name) {
242248
if (name == null || name.isEmpty()) {
243249
return infos();
244250
}
245-
String opName = opAliases.get(OpUtils.getCanonicalOpName(name));
246-
if (opName == null) {
251+
Iterable<OpInfo> infos = opCache.get(name);
252+
if (infos == null)
247253
throw new IllegalArgumentException("No op infos with name: " + name + " available.");
248-
}
249-
return opCache.getAndBelow(new PrefixQuery(opName));
254+
return infos;
250255
}
251256

252257
@Override
@@ -465,31 +470,6 @@ private OpRef inferOpRef(Type type, String name, Map<TypeVariable<?>, Type> type
465470
return new OpRef(name, new Type[] { type }, mappedOutputs[0], mappedInputs);
466471
}
467472

468-
/**
469-
* Updates alias map using the specified String list. The first String in the
470-
* list is assumed to be the canonical name of the op. After this method
471-
* returns, all String in the specified list will map to the canonical name.
472-
*
473-
* @param opNames
474-
*/
475-
private void addAliases(String[] opNames, String opImpl) {
476-
String opName = opNames[0];
477-
for (String alias : opNames) {
478-
if (alias == null || alias.isEmpty()) {
479-
continue;
480-
}
481-
if (opAliases.containsKey(alias)) {
482-
if (!opAliases.get(alias).equals(opName)) {
483-
// log.warn("Possible naming clash for op '" + opImpl + "' detected. Attempting to add alias '" + alias
484-
// + "' for op name '" + opName + "'. However the alias '" + alias + "' is already "
485-
// + "associated with op name '" + opAliases.get(alias) + "'.");
486-
}
487-
continue;
488-
}
489-
opAliases.put(alias, opName);
490-
}
491-
}
492-
493473
/**
494474
* Constructs a string with the specified number of tabs '\t'.
495475
*

src/test/java/org/scijava/ops/types/AnyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void setValue(String value) {
123123
}
124124
}
125125

126-
@Plugin(type = Op.class, name = "create.mutableNotAny")
126+
@Plugin(type = Op.class, name = "create, create.mutableNotAny")
127127
@Parameter(key = "mutableNotAny", type = ItemIO.OUTPUT)
128128
class MutableNotAnyCreator implements Source<MutableNotAny> {
129129

0 commit comments

Comments
 (0)