Commit f00fad26 authored by Matija Obreza's avatar Matija Obreza
Browse files

@Cacheable on selected entities

parent aaa9dd4a
......@@ -58,10 +58,11 @@ public class Accession extends VersionedAuditedModel {
@Column(name = "acceNumb", nullable = false, length = 128)
private String accessionName;
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "taxonomyId")
private Taxonomy taxonomy1;
// @ManyToOne(cascade = {}, optional = true)
// @JoinColumn(name = "taxonomyId")
// private Taxonomy taxonomy1;
// @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "taxonomyId2")
private Taxonomy2 taxonomy;
......@@ -156,10 +157,6 @@ public class Accession extends VersionedAuditedModel {
this.accessionName = accessionName;
}
public Taxonomy getTaxonomy1() {
return this.taxonomy1;
}
public Taxonomy2 getTaxonomy() {
return this.taxonomy;
}
......@@ -265,10 +262,6 @@ public class Accession extends VersionedAuditedModel {
this.taxonomy = taxonomy2;
}
public void setTaxonomy1(Taxonomy taxonomy1) {
this.taxonomy1 = taxonomy1;
}
public List<Integer> getStoRage() {
return stoRage;
}
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.model.genesys;
import java.text.MessageFormat;
import java.util.List;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
......@@ -36,6 +37,7 @@ import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Cacheable
@Entity
// Add index on all fields
@Table(name = "taxonomy2", uniqueConstraints = { @UniqueConstraint(columnNames = { "genus", "species", "spAuthor", "subtaxa", "subtAuthor" }) })
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.model.impl;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
......@@ -28,6 +29,7 @@ import org.genesys2.server.model.BusinessModel;
* @author mobreza
*
*/
@Cacheable
@Entity
@Table(name = "classname")
public class ClassPK extends BusinessModel {
......
......@@ -23,6 +23,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
......@@ -39,6 +40,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Cacheable
@Entity
@Table(name = "country")
@JsonSerialize(using = CountrySerializer.class)
......
......@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
......@@ -43,6 +44,7 @@ import org.genesys2.server.servlet.controller.rest.serialization.FaoInstituteSer
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Cacheable
@Entity
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }), indexes = { @Index(columnList = "code", name = "code_FAOINSTITUTE") })
@JsonSerialize(using = FaoInstituteSerializer.class)
......
......@@ -22,6 +22,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Cacheable;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
......@@ -41,9 +42,12 @@ import net.sf.oval.constraint.NotNull;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.servlet.controller.rest.serialization.UserSerializer;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Cacheable
@Entity
@Table(name = "user")
@JsonSerialize(using = UserSerializer.class)
......@@ -80,6 +84,7 @@ public class User extends BusinessModel {
private String name;
// validation
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@ElementCollection
@Enumerated(EnumType.STRING)
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
......
......@@ -103,6 +103,7 @@ public class UserServiceImpl implements UserService {
if (user == null) {
return null;
}
System.out.println("*** getUserDetails ***");
final boolean enabled = user.isEnabled();
final boolean accountNonExpired = !user.isAccountExpired();
final boolean credentialsNonExpired = !user.isPasswordExpired();
......@@ -131,6 +132,7 @@ public class UserServiceImpl implements UserService {
}
private Collection<? extends GrantedAuthority> getGrantedAuthorities(User user) {
System.out.println("*** getGrantedAuthorities ***");
final List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
for (final UserRole userRole : user.getRoles()) {
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed 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.genesys2.server.model.impl;
import java.io.IOException;
import java.util.Set;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.genesys2.server.aspect.AsAdminAspect;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.AclService;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.UserService;
import org.genesys2.server.service.impl.AclServiceImpl;
import org.genesys2.server.service.impl.BatchRESTServiceImpl;
import org.genesys2.server.service.impl.ContentServiceImpl;
import org.genesys2.server.service.impl.CropServiceImpl;
import org.genesys2.server.service.impl.GenesysServiceImpl;
import org.genesys2.server.service.impl.GeoServiceImpl;
import org.genesys2.server.service.impl.InstituteServiceImpl;
import org.genesys2.server.service.impl.OWASPSanitizer;
import org.genesys2.server.service.impl.OrganizationServiceImpl;
import org.genesys2.server.service.impl.TaxonomyServiceImpl;
import org.genesys2.server.service.impl.UserServiceImpl;
import org.genesys2.server.test.JpaRealDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.monitor.LocalMapStats;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AccessionCacheTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
public class AccessionCacheTest {
@Import({ JpaRealDataConfig.class, HazelcastConfig.class })
@ComponentScan(basePackages = { "org.genesys2.server.persistence.domain" })
public static class Config {
@Bean
public AsAdminAspect asAdminAspect() {
return new AsAdminAspect();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public AclService aclService() {
return new AclServiceImpl();
}
@Bean
public TaxonomyService taxonomyService() {
return new TaxonomyServiceImpl();
}
@Bean
public BatchRESTService batchRESTService() {
return new BatchRESTServiceImpl();
}
@Bean
public CropService cropService() {
return new CropServiceImpl();
}
@Bean
public GenesysService genesysService() {
return new GenesysServiceImpl();
}
@Bean
public CacheManager cacheManager() {
return new NoOpCacheManager();
}
@Bean
public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer();
}
@Bean
public GeoService geoService() {
return new GeoServiceImpl();
}
@Bean
public ContentService contentService() {
return new ContentServiceImpl();
}
@Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException {
final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean();
return vf.createVelocityEngine();
}
@Bean
public OrganizationService organizationService() {
return new OrganizationServiceImpl();
}
@Bean
public InstituteService instituteService() {
return new InstituteServiceImpl();
}
}
@Autowired
private GenesysService genesysService;
@Autowired
private InstituteService instituteService;
@Test
public void test1() {
FaoInstitute iita = instituteService.getInstitute("NGA039");
System.out.println("IITA: " + iita);
System.err.println("See caching of country");
for (int i = 0; i < 10; i++) {
iita = instituteService.getInstitute("NGA039");
System.out.println("Country: " + iita.getCountry());
}
printCacheStats();
}
@Test
public void test2() {
System.out.println("Find 1st");
Accession accession = genesysService.getAccession(3446120);
System.out.println("Acn: " + accession);
System.out.println(accession.getCountryOfOrigin());
System.out.println(accession.getInstitute());
System.out.println(accession.getTaxonomy());
System.out.println("See caching of stuff");
for (int i = 0; i < 10; i++) {
System.out.println("Find");
accession = genesysService.getAccession(3446120);
System.out.println("Acn: " + accession);
System.out.println(accession.getCountryOfOrigin());
System.out.println(accession.getInstitute());
System.out.println(accession.getTaxonomy());
}
printCacheStats();
}
public static void printCacheStats() {
Set<HazelcastInstance> instances = Hazelcast.getAllHazelcastInstances();
for (HazelcastInstance hz : instances) {
System.out.println("\n\nCache stats Instance: " + hz.getName());
for (DistributedObject o : hz.getDistributedObjects()) {
if (o instanceof IMap) {
IMap imap = (IMap) o;
System.out.println(imap.getServiceName() + ": " + imap.getName() + " " + imap.getPartitionKey());
LocalMapStats localMapStats = imap.getLocalMapStats();
System.out.println("created: " + localMapStats.getCreationTime());
System.out.println("owned entries: " + localMapStats.getOwnedEntryCount());
System.out.println("backup entries: " + localMapStats.getBackupEntryCount());
System.out.println("locked entries: " + localMapStats.getLockedEntryCount());
System.out.println("dirty entries: " + localMapStats.getDirtyEntryCount());
System.out.println("hits: " + localMapStats.getHits());
System.out.println("puts: " + localMapStats.getPutOperationCount());
System.out.println("last update: " + localMapStats.getLastUpdateTime());
System.out.println("last access:" + localMapStats.getLastAccessTime());
} else {
System.out.println(o.getClass() + " " + o);
}
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment