HHH-9811 - Change EntityPersister#resolveAttributeIndexes to take String[]

This commit is contained in:
barreiro 2015-05-21 21:04:51 +01:00 committed by Steve Ebersole
parent 73c5d576e7
commit f795dfc2bc
11 changed files with 44 additions and 55 deletions

View File

@ -6,6 +6,11 @@
*/ */
package org.hibernate.bytecode.enhance.internal; package org.hibernate.bytecode.enhance.internal;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField; import javassist.CtField;
@ -20,11 +25,6 @@ import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.bytecode.enhance.spi.EnhancerConstants;
import org.hibernate.engine.spi.SelfDirtinessTracker; import org.hibernate.engine.spi.SelfDirtinessTracker;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
/** /**
* enhancer for regular entities * enhancer for regular entities
* *
@ -140,13 +140,18 @@ public class EntityEnhancer extends Enhancer {
MethodWriter.write( MethodWriter.write(
managedCtClass, "" + managedCtClass, "" +
"public java.util.Set %1$s() {%n" + "public String[] %1$s() {%n" +
" if (%2$s == null) { %2$s = new %4$s(); }%n" + " if(%3$s == null) {%n" +
" %3$s(%2$s);%n" + " return (%2$s == null) ? new String[0] : %2$s.get();%n" +
" return %2$s.asSet();%n" + " } else {%n" +
" if (%2$s == null) %2$s = new %5$s();%n" +
" %4$s(%2$s);%n" +
" return %2$s.get();%n" +
" }%n" +
"}", "}",
EnhancerConstants.TRACKER_GET_NAME, EnhancerConstants.TRACKER_GET_NAME,
EnhancerConstants.TRACKER_FIELD_NAME, EnhancerConstants.TRACKER_FIELD_NAME,
EnhancerConstants.TRACKER_COLLECTION_NAME,
EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME, EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME,
TRACKER_IMPL TRACKER_IMPL
); );

View File

@ -7,8 +7,6 @@
package org.hibernate.bytecode.enhance.internal.tracker; package org.hibernate.bytecode.enhance.internal.tracker;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/** /**
* small low memory class to keep track of changed fields * small low memory class to keep track of changed fields
@ -44,8 +42,8 @@ public final class SimpleDirtyTracker {
return names.length == 0; return names.length == 0;
} }
public Set<String> asSet() { public String[] get() {
return new CopyOnWriteArraySet<String>( Arrays.asList( names ) ); return names;
} }
} }

View File

@ -6,10 +6,6 @@
*/ */
package org.hibernate.bytecode.enhance.internal.tracker; package org.hibernate.bytecode.enhance.internal.tracker;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/** /**
* small low memory class to keep track of changed fields * small low memory class to keep track of changed fields
* *
@ -58,8 +54,8 @@ public final class SortedDirtyTracker {
return names.length == 0; return names.length == 0;
} }
public Set<String> asSet() { public String[] get() {
return new CopyOnWriteArraySet<String>( Arrays.asList( names ) ); return names;
} }
} }

View File

@ -6,8 +6,6 @@
*/ */
package org.hibernate.engine.spi; package org.hibernate.engine.spi;
import java.util.Set;
/** /**
* Contract for an entity to report that it tracks the dirtiness of its own state, * Contract for an entity to report that it tracks the dirtiness of its own state,
* as opposed to needing Hibernate to perform state-diff dirty calculations. * as opposed to needing Hibernate to perform state-diff dirty calculations.
@ -30,9 +28,9 @@ public interface SelfDirtinessTracker {
/** /**
* Retrieve the names of all the persistent attributes whose values have changed. * Retrieve the names of all the persistent attributes whose values have changed.
* *
* @return The set of changed persistent attribute names * @return An array of changed persistent attribute names
*/ */
Set<String> $$_hibernate_getDirtyAttributes(); String[] $$_hibernate_getDirtyAttributes();
/** /**
* Clear the stored dirty attributes * Clear the stored dirty attributes

View File

@ -1868,12 +1868,12 @@ public abstract class AbstractEntityPersister
} }
@Override @Override
public int[] resolveAttributeIndexes(Set<String> properties) { public int[] resolveAttributeIndexes(String[] attributeNames) {
Iterator<String> iter = properties.iterator(); int[] fields = new int[attributeNames.length];
int[] fields = new int[properties.size()];
int counter = 0; int counter = 0;
while ( iter.hasNext() ) {
Integer index = entityMetamodel.getPropertyIndexOrNull( iter.next() ); for ( String attribute : attributeNames ) {
Integer index = entityMetamodel.getPropertyIndexOrNull( attribute );
if ( index != null ) { if ( index != null ) {
fields[counter++] = index; fields[counter++] = index;
} }

View File

@ -8,7 +8,6 @@ package org.hibernate.persister.entity;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -784,7 +783,7 @@ public interface EntityPersister extends OptimisticCacheSource, EntityDefinition
public FilterAliasGenerator getFilterAliasGenerator(final String rootAlias); public FilterAliasGenerator getFilterAliasGenerator(final String rootAlias);
public int[] resolveAttributeIndexes(Set<String> properties); public int[] resolveAttributeIndexes(String[] attributeNames);
public boolean canUseReferenceCacheEntries(); public boolean canUseReferenceCacheEntries();
} }

View File

@ -9,7 +9,6 @@ package org.hibernate.test.bytecode.enhancement;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -203,9 +202,9 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) { public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) {
final SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance; final SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance;
assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() ); assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() );
Set<?> tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes(); String[] tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes();
assertEquals( dirtyFields.length, tracked.size() ); assertEquals( dirtyFields.length, tracked.length );
assertTrue( tracked.containsAll( Arrays.asList( dirtyFields ) ) ); assertTrue( Arrays.asList( tracked ).containsAll( Arrays.asList( dirtyFields ) ) );
} }
static EntityEntry makeEntityEntry() { static EntityEntry makeEntityEntry() {

View File

@ -6,11 +6,10 @@
*/ */
package org.hibernate.test.bytecode.enhancement.tracker; package org.hibernate.test.bytecode.enhancement.tracker;
import org.hibernate.bytecode.enhance.internal.tracker.SortedDirtyTracker;
import org.hibernate.bytecode.enhance.internal.tracker.SimpleDirtyTracker; import org.hibernate.bytecode.enhance.internal.tracker.SimpleDirtyTracker;
import org.junit.Test; import org.hibernate.bytecode.enhance.internal.tracker.SortedDirtyTracker;
import java.util.Set; import org.junit.Test;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -25,15 +24,15 @@ public class DirtyTrackerTest {
public void testSimpleTracker() { public void testSimpleTracker() {
SimpleDirtyTracker tracker = new SimpleDirtyTracker(); SimpleDirtyTracker tracker = new SimpleDirtyTracker();
assertTrue(tracker.isEmpty()); assertTrue(tracker.isEmpty());
assertTrue(tracker.asSet().isEmpty()); assertTrue(tracker.get().length == 0);
tracker.add("foo"); tracker.add("foo");
assertFalse(tracker.isEmpty()); assertFalse(tracker.isEmpty());
assertArrayEquals(tracker.asSet().toArray(), new String[]{"foo"}); assertArrayEquals(tracker.get(), new String[]{"foo"});
tracker.clear(); tracker.clear();
assertTrue(tracker.isEmpty()); assertTrue(tracker.isEmpty());
assertTrue(tracker.asSet().isEmpty()); assertTrue(tracker.get().length == 0);
tracker.add("foo"); tracker.add("foo");
tracker.add("bar"); tracker.add("bar");
@ -41,7 +40,7 @@ public class DirtyTrackerTest {
tracker.add("foo"); tracker.add("foo");
tracker.add("another.foo"); tracker.add("another.foo");
tracker.add("another.bar"); tracker.add("another.bar");
assertTrue(tracker.asSet().size() == 4); assertTrue(tracker.get().length == 4);
} }
@ -49,15 +48,15 @@ public class DirtyTrackerTest {
public void testSortedTracker() { public void testSortedTracker() {
SortedDirtyTracker tracker = new SortedDirtyTracker(); SortedDirtyTracker tracker = new SortedDirtyTracker();
assertTrue(tracker.isEmpty()); assertTrue(tracker.isEmpty());
assertTrue(tracker.asSet().isEmpty()); assertTrue(tracker.get().length == 0);
tracker.add("foo"); tracker.add("foo");
assertFalse(tracker.isEmpty()); assertFalse(tracker.isEmpty());
assertArrayEquals(tracker.asSet().toArray(), new String[]{"foo"}); assertArrayEquals(tracker.get(), new String[]{"foo"});
tracker.clear(); tracker.clear();
assertTrue(tracker.isEmpty()); assertTrue(tracker.isEmpty());
assertTrue(tracker.asSet().isEmpty()); assertTrue(tracker.get().length == 0);
tracker.add("foo"); tracker.add("foo");
tracker.add("bar"); tracker.add("bar");
@ -65,15 +64,13 @@ public class DirtyTrackerTest {
tracker.add("foo"); tracker.add("foo");
tracker.add("another.foo"); tracker.add("another.foo");
tracker.add("another.bar"); tracker.add("another.bar");
assertTrue(tracker.asSet().size() == 4); assertTrue(tracker.get().length == 4);
// we the algorithm for this implementation relies on the fact that the array is kept sorted, so let's check it really is // we the algorithm for this implementation relies on the fact that the array is kept sorted, so let's check it really is
assertTrue(isSorted(tracker.asSet())); assertTrue(isSorted(tracker.get()));
} }
private boolean isSorted(Set<String> set) { private boolean isSorted(String[] arr) {
String[] arr = new String[set.size()];
arr = set.toArray(arr);
for (int i = 1; i < arr.length; i++) { for (int i = 1; i < arr.length; i++) {
if (arr[i - 1].compareTo(arr[i]) > 0) { if (arr[i - 1].compareTo(arr[i]) > 0) {
return false; return false;

View File

@ -11,7 +11,6 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Comparator; import java.util.Comparator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -587,7 +586,7 @@ public class GoofyPersisterClassProvider implements PersisterClassResolver {
} }
@Override @Override
public int[] resolveAttributeIndexes(Set<String> attributes) { public int[] resolveAttributeIndexes(String[] attributeNames) {
return null; return null;
} }

View File

@ -10,7 +10,6 @@ import java.io.Serializable;
import java.util.Comparator; import java.util.Comparator;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -709,7 +708,7 @@ public class CustomPersister implements EntityPersister {
} }
@Override @Override
public int[] resolveAttributeIndexes(Set<String> attributes) { public int[] resolveAttributeIndexes(String[] attributeNames) {
return null; return null;
} }

View File

@ -10,7 +10,6 @@ import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
@ -592,7 +591,7 @@ public class PersisterClassProviderTest {
} }
@Override @Override
public int[] resolveAttributeIndexes(Set<String> properties) { public int[] resolveAttributeIndexes(String[] attributeNames) {
return new int[0]; return new int[0];
} }