From c4c95813b79a2e4b9c9bb530f29ac818720af925 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 31 Aug 2011 19:41:04 +0200 Subject: [PATCH] =?UTF-8?q?DATADOC-199=20-=20Added=20caching=20for=20Mongo?= =?UTF-8?q?PersistentProperty.isIdProperty=20and=20=E2=80=A6.getFieldName.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Continuous field and annotation lookups in those methods have turned out to be some hotspots in performance tests. Added a CachingMongoPersistentProperty that delegates to the actual implementation once and caching it. --- .../CachingMongoPersistentProperty.java | 73 +++++++++++++++++++ .../core/mapping/MongoMappingContext.java | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java new file mode 100644 index 000000000..a732a635b --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2011 by the original author(s). + * + * 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.springframework.data.mongodb.core.mapping; + +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; + +import org.springframework.data.mapping.model.SimpleTypeHolder; + +/** + * {@link MongoPersistentProperty} caching access to {@link #isIdProperty()} and {@link #getFieldName()}. + * + * @author Oliver Gierke + */ +public class CachingMongoPersistentProperty extends BasicMongoPersistentProperty { + + private Boolean isIdProperty; + private String fieldName; + + /** + * Creates a new {@link CachingMongoPersistentProperty}. + * + * @param field + * @param propertyDescriptor + * @param owner + * @param simpleTypeHolder + */ + public CachingMongoPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, + MongoPersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { + super(field, propertyDescriptor, owner, simpleTypeHolder); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#isIdProperty() + */ + @Override + public boolean isIdProperty() { + + if (this.isIdProperty == null) { + this.isIdProperty = super.isIdProperty(); + } + + return this.isIdProperty; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#getFieldName() + */ + @Override + public String getFieldName() { + + if (this.fieldName == null) { + this.fieldName = super.getFieldName(); + } + + return super.getFieldName(); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java index d5906625a..f09e7ea07 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java @@ -43,7 +43,7 @@ public class MongoMappingContext extends AbstractMappingContext owner, SimpleTypeHolder simpleTypeHolder) { - return new BasicMongoPersistentProperty(field, descriptor, owner, simpleTypeHolder); + return new CachingMongoPersistentProperty(field, descriptor, owner, simpleTypeHolder); } /* (non-Javadoc)