mirror of https://github.com/apache/openjpa.git
OPENJPA-1790: Added Proxy for concurrent maps.
Submitted By: Heath Thomann, ported from Jody's patch for trunk git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@1034584 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9e389d2d28
commit
35740819fe
|
@ -0,0 +1,55 @@
|
||||||
|
/*
|
||||||
|
* 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.openjpa.util;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility methods used by concurrent map proxies.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ProxyConcurrentMaps extends ProxyMaps {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call before invoking {@link Map#remove} on super.
|
||||||
|
*/
|
||||||
|
public static boolean beforeRemove(ProxyMap map, Object key, Object value) {
|
||||||
|
dirty(map, false);
|
||||||
|
return map.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call after invoking {@link Map#remove} on super.
|
||||||
|
*
|
||||||
|
* @param ret the return value from the super's method
|
||||||
|
* @param before the return value from {@link #beforeRemove}
|
||||||
|
* @return the value to return from {@link Map#remove}
|
||||||
|
*/
|
||||||
|
public static boolean afterRemove(ProxyMap map, Object key, Object value, boolean ret,
|
||||||
|
boolean before) {
|
||||||
|
if (before) {
|
||||||
|
if (map.getChangeTracker() != null) {
|
||||||
|
((MapChangeTracker) map.getChangeTracker()).removed(key, ret);
|
||||||
|
}
|
||||||
|
removed(map, key, true);
|
||||||
|
removed(map, ret, false);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* 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.openjpa.persistence.relations;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class ConcurrentEntityLeft {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private String strData;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name="right_id", referencedColumnName="id")
|
||||||
|
private ConcurrentEntityRight rightEntity;
|
||||||
|
|
||||||
|
public ConcurrentEntityLeft() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStrData() {
|
||||||
|
return strData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStrData(String strData) {
|
||||||
|
this.strData = strData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConcurrentEntityRight getRightEntity() {
|
||||||
|
return rightEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRightEntity(ConcurrentEntityRight rightEntity) {
|
||||||
|
this.rightEntity = rightEntity;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
* 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.openjpa.persistence.relations;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.MapKey;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class ConcurrentEntityRight {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private String strData;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy="rightEntity", targetEntity=ConcurrentEntityLeft.class,
|
||||||
|
cascade={javax.persistence.CascadeType.ALL})
|
||||||
|
@MapKey(name="strData")
|
||||||
|
private Map<String, ConcurrentEntityLeft> leftEntityMap;
|
||||||
|
|
||||||
|
public ConcurrentEntityRight() {
|
||||||
|
leftEntityMap = new ConcurrentHashMap<String, ConcurrentEntityLeft>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStrData() {
|
||||||
|
return strData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStrData(String strData) {
|
||||||
|
this.strData = strData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, ConcurrentEntityLeft> getLeftEntityMap() {
|
||||||
|
return leftEntityMap;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* 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.openjpa.persistence.relations;
|
||||||
|
|
||||||
|
import org.apache.openjpa.persistence.test.SingleEMTestCase;
|
||||||
|
|
||||||
|
public class TestConcurrentMap extends SingleEMTestCase {
|
||||||
|
public void setUp() {
|
||||||
|
setUp(ConcurrentEntityLeft.class, ConcurrentEntityRight.class,
|
||||||
|
CLEAR_TABLES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testConcurrentMap001() {
|
||||||
|
em.getTransaction().begin();
|
||||||
|
|
||||||
|
ConcurrentEntityLeft left = new ConcurrentEntityLeft();
|
||||||
|
left.setId(1);
|
||||||
|
left.setStrData("Lefty");
|
||||||
|
|
||||||
|
ConcurrentEntityRight right = new ConcurrentEntityRight();
|
||||||
|
right.setId(1);
|
||||||
|
right.setStrData("Poncho");
|
||||||
|
|
||||||
|
em.persist(left);
|
||||||
|
em.persist(right);
|
||||||
|
|
||||||
|
left.setRightEntity(right);
|
||||||
|
right.getLeftEntityMap().put(left.getStrData(), left);
|
||||||
|
|
||||||
|
em.getTransaction().commit();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue