Skip to content

Commit

Permalink
Major performance issue in case of many new entities - backport from …
Browse files Browse the repository at this point in the history
…master (#1860)

* Issue 1596: use a hash-based collection to lookup objects in the cache
instead of a linear search

Signed-off-by: Patrick Schmitt <Patrick.Schmitt@cpb-software.com>
  • Loading branch information
Zuplyx committed Apr 13, 2023
1 parent 5d60217 commit cbec1c7
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -16,11 +16,14 @@

import java.util.Vector;

import org.eclipse.persistence.queries.*;
import org.eclipse.persistence.expressions.*;
import org.eclipse.persistence.mappings.*;
import org.eclipse.persistence.testing.framework.*;
import org.eclipse.persistence.testing.models.employee.domain.*;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.mappings.AttributeAccessor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ReadObjectQuery;
import org.eclipse.persistence.testing.framework.TestErrorException;
import org.eclipse.persistence.testing.models.employee.domain.Employee;

/**
* <b>Purpose</b>: Test for bug 2782991: Find by Primary Key with
Expand Down Expand Up @@ -93,6 +96,7 @@ public ConformResultsWithPrimaryKeyExpressionTest(int testCase, boolean checkCac
}
}

@Override
public void buildConformQuery() {
conformedQuery = new ReadObjectQuery(Employee.class);
ExpressionBuilder emp = new ExpressionBuilder();
Expand Down Expand Up @@ -132,6 +136,7 @@ protected Employee findWorstCaseEmployee() {
/**
* prepareTest method comment.
*/
@Override
public void prepareTest() {
ReadAllQuery query = new ReadAllQuery(Employee.class);
Vector employees = (Vector)getSession().executeQuery(query);
Expand All @@ -156,7 +161,7 @@ public void prepareTest() {
case CASE_NEW: {
selectionObject = newEmployee;
if (shouldCheckCacheByExactPrimaryKey()) {
expectedGetIdCallCount = 1;
expectedGetIdCallCount = 0;
} else {
expectedGetIdCallCount = n + 1;
}
Expand Down Expand Up @@ -190,7 +195,7 @@ public void prepareTest() {
// S.M. This went from 5 calls to 4, which is good.
// When checking the one new object + registration +
// building clone + building backup clone.
expectedGetIdCallCount = 3;
expectedGetIdCallCount = 2;
} else {
expectedGetIdCallCount = n + 4;
}
Expand All @@ -199,6 +204,7 @@ public void prepareTest() {
}
}

@Override
public void setup() {
// Change how the primary key attribute 'id' in Employee is accessed.
// Now everytime TopLink extracts the primary key from an Employee object it
Expand All @@ -211,6 +217,7 @@ public void setup() {
super.setup();
}

@Override
public void reset() {
DatabaseMapping mapping = getSession().getDescriptor(Employee.class).getMappingForAttributeName("id");
mapping.setAttributeAccessor(overwrittenAccessor);
Expand All @@ -220,6 +227,7 @@ public void reset() {
/**
* Override test to count the calls to Employee.getId just for the query.
*/
@Override
public void test() {
int initialCount = Employee.getGetIdCallCount();
result = unitOfWork.executeQuery(conformedQuery);
Expand All @@ -230,6 +238,7 @@ public void test() {
/**
* verify method comment.
*/
@Override
public void verify() {
if ((result == null) && (testCase != CASE_DELETED)) {
throw new TestErrorException("object existed in unit of work but not returned in query.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -31,10 +31,12 @@ public DoubleNestedUnitOfWorkDeleteConformedNestedNewObjectTest() {
setDescription("Test designed to verify that a new object registered in a nested unit of work is unregistered when deleted in same unit of work.");
}

@Override
public void setup() {
getAbstractSession().beginTransaction();
}

@Override
public void test() {

UnitOfWork uow = getSession().acquireUnitOfWork();
Expand Down Expand Up @@ -63,8 +65,13 @@ public void test() {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

if (!((UnitOfWorkImpl)uow).getPrimaryKeyToNewObjects().isEmpty()) {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

}

@Override
public void reset() {
getAbstractSession().commitTransaction();
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -63,6 +63,10 @@ public void test() {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

if (!((UnitOfWorkImpl)uow).getPrimaryKeyToNewObjects().isEmpty()) {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -15,7 +15,6 @@
package org.eclipse.persistence.testing.tests.unitofwork;

import java.math.BigDecimal;

import java.util.Vector;

import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
Expand All @@ -33,10 +32,12 @@ public NestedUnitOfWorkDeleteConformedNestedNewObjectTest() {
setDescription("Test designed to verify that a new object registered in a nested unit of work is unregistered when deleted in same unit of work.");
}

@Override
public void setup() {
getAbstractSession().beginTransaction();
}

@Override
public void test() {

UnitOfWork uow = getSession().acquireUnitOfWork();
Expand Down Expand Up @@ -65,8 +66,13 @@ public void test() {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

if (!((UnitOfWorkImpl)uow).getPrimaryKeyToNewObjects().isEmpty()) {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

}

@Override
public void reset() {
getAbstractSession().commitTransaction();
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -47,6 +47,10 @@ public void test() {
throw new TestErrorException("Failed to unregister the Object in the nested unit of work");
}

if (((UnitOfWorkImpl)uow).getPrimaryKeyToNewObjects().values().stream().anyMatch(c -> c.contains(employee))) {
throw new TestErrorException("Failed to move the deleted new object into the parent UOW");
}

}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -42,6 +42,10 @@ public void test() {
throw new TestErrorException("Failed to move the deleted new object into the parent UOW");
}

if (((UnitOfWorkImpl)uow).getPrimaryKeyToNewObjects().values().stream().anyMatch(c -> c.contains(original))) {
throw new TestErrorException("Failed to move the deleted new object into the parent UOW");
}

}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -28,6 +28,7 @@ public UnregisterUnitOfWorkTest() {
setDescription("Check the SQL to see that no inserts are issued.");
}

@Override
protected void test() {

/*******************/
Expand All @@ -54,6 +55,10 @@ protected void test() {
throw new TestErrorException("Deep unregister object did not work");
}

if (!uow.getPrimaryKeyToNewObjects().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}

uow.commit();

uow = (UnitOfWorkImpl)getSession().acquireUnitOfWork();
Expand All @@ -75,6 +80,10 @@ protected void test() {
throw new TestErrorException("Deep unregister object did not work");
}

if (!uow.getPrimaryKeyToNewObjects().isEmpty()) {
throw new TestErrorException("Deep unregister object did not work");
}

uow.commit();

/*******************/
Expand Down

0 comments on commit cbec1c7

Please sign in to comment.