Skip to content

Commit

Permalink
Merge pull request #3146 from nieqiurong/shared-ambiguity
Browse files Browse the repository at this point in the history
Shared ambiguity instance
  • Loading branch information
epochcoder authored Jan 3, 2025
2 parents 8ac3920 + ad8c4d0 commit 34fd2bd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
49 changes: 19 additions & 30 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,20 +15,6 @@
*/
package org.apache.ibatis.session;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;

import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.builder.CacheRefResolver;
import org.apache.ibatis.builder.IncompleteElementException;
Expand Down Expand Up @@ -97,6 +83,20 @@
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;

/**
* @author Clinton Begin
*/
Expand Down Expand Up @@ -1113,6 +1113,7 @@ protected static class StrictMap<V> extends ConcurrentHashMap<String, V> {
private static final long serialVersionUID = -4950446264854982944L;
private final String name;
private BiFunction<V, V, String> conflictMessageProducer;
private static final Object AMBIGUITY_INSTANCE = new Object();

public StrictMap(String name, int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
Expand Down Expand Up @@ -1162,7 +1163,7 @@ public V put(String key, V value) {
if (super.get(shortKey) == null) {
super.put(shortKey, value);
} else {
super.put(shortKey, (V) new Ambiguity(shortKey));
super.put(shortKey, (V) AMBIGUITY_INSTANCE);
}
}
return super.put(key, value);
Expand All @@ -1183,25 +1184,13 @@ public V get(Object key) {
if (value == null) {
throw new IllegalArgumentException(name + " does not contain value for " + key);
}
if (value instanceof Ambiguity) {
throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name
if (AMBIGUITY_INSTANCE == value) {
throw new IllegalArgumentException(key + " is ambiguous in " + name
+ " (try using the full name including the namespace, or rename one of the entries)");
}
return value;
}

protected static class Ambiguity {
private final String subject;

public Ambiguity(String subject) {
this.subject = subject;
}

public String getSubject() {
return subject;
}
}

private String getShortName(String key) {
final String[] keyParts = key.split("\\.");
return keyParts[keyParts.length - 1];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,12 +15,11 @@
*/
package org.apache.ibatis.submitted.global_variables_defaults;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
Expand All @@ -29,6 +28,10 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

class ConfigurationTest {

@Test
Expand Down Expand Up @@ -79,4 +82,27 @@ void applyPropertyValueOnXmlConfiguration() throws IOException {

}

@Test
void testAmbiguityCache() {
Configuration configuration = new Configuration();

configuration.addMappedStatement(
new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.selectById",
new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build());
configuration
.addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.test",
new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build());
configuration
.addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper2.test",
new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build());

Assertions.assertThat(configuration.getMappedStatement("selectById")).isNotNull();
Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper1.test")).isNotNull();
Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper2.test")).isNotNull();

Assertions.assertThatThrownBy(() -> configuration.getMappedStatement("test"))
.isInstanceOf(IllegalArgumentException.class).hasMessage(
"test is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)");
}

}

0 comments on commit 34fd2bd

Please sign in to comment.