|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.grails.datastore.gorm.mongo.api |
| 20 | + |
| 21 | +import groovy.transform.CompileStatic |
| 22 | + |
| 23 | +import org.bson.Document |
| 24 | + |
| 25 | +import org.grails.datastore.gorm.GormInstanceApi |
| 26 | +import org.grails.datastore.mapping.core.Datastore |
| 27 | +import org.grails.datastore.mapping.core.Session |
| 28 | +import org.grails.datastore.mapping.core.SessionImplementor |
| 29 | +import org.grails.datastore.mapping.dirty.checking.DirtyCheckable |
| 30 | +import org.grails.datastore.mapping.engine.EntityAccess |
| 31 | +import org.grails.datastore.mapping.engine.EntityPersister |
| 32 | +import org.grails.datastore.mapping.model.PersistentEntity |
| 33 | +import org.grails.datastore.mapping.model.PersistentProperty |
| 34 | +import org.grails.datastore.mapping.model.config.GormProperties |
| 35 | +import org.grails.datastore.mapping.model.types.Basic |
| 36 | +import org.grails.datastore.mapping.model.types.Simple |
| 37 | +import org.grails.datastore.mapping.model.types.ToOne |
| 38 | +import com.mongodb.DBRef |
| 39 | + |
| 40 | +/** |
| 41 | + * Mongo-specific instance API that aligns isDirty semantics with Hibernate by |
| 42 | + * comparing the current state to the session snapshot instead of relying solely |
| 43 | + * on DirtyCheckable setters. |
| 44 | + */ |
| 45 | +@CompileStatic |
| 46 | +class MongoGormInstanceApi<D> extends GormInstanceApi<D> { |
| 47 | + |
| 48 | + MongoGormInstanceApi(Class<D> persistentClass, Datastore datastore) { |
| 49 | + super(persistentClass, datastore) |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + boolean isDirty(D instance) { |
| 54 | + // Delegate to the Session implementation which compares against cached entry |
| 55 | + execute({ Session session -> |
| 56 | + session.isDirty(instance) |
| 57 | + } as org.grails.datastore.mapping.core.SessionCallback<Boolean>) |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + boolean isDirty(D instance, String fieldName) { |
| 62 | + if (instance == null || fieldName == null) return false |
| 63 | + |
| 64 | + // Prefer session snapshot comparison like HibernateGormInstanceApi |
| 65 | + execute({ Session session -> |
| 66 | + final EntityPersister persister = (EntityPersister) session.getPersister(instance) |
| 67 | + if (persister == null) return false |
| 68 | + |
| 69 | + final Serializable id = persister.getObjectIdentifier(instance) |
| 70 | + if (id == null) return false |
| 71 | + |
| 72 | + final PersistentEntity entity = persister.getPersistentEntity() |
| 73 | + final PersistentProperty property = entity.getPropertyByName(fieldName) |
| 74 | + if (property == null) return false |
| 75 | + |
| 76 | + // Obtain the cached native entry (snapshot) from the session |
| 77 | + final SessionImplementor<Document> si = (SessionImplementor<Document>) session |
| 78 | + final Document cached = (Document) si.getCachedEntry(entity, id, true) |
| 79 | + if (cached == null) return false |
| 80 | + |
| 81 | + // Compute the key used in the native entry |
| 82 | + String key = property.getMapping()?.getMappedForm()?.getTargetName() |
| 83 | + if (key == null) key = property.getName() |
| 84 | + |
| 85 | + // Current value via EntityAccess to avoid proxies |
| 86 | + final EntityAccess access = session.getMappingContext().createEntityAccess(entity, instance) |
| 87 | + final Object currentValue = access.getProperty(fieldName) |
| 88 | + |
| 89 | + // Old value from cached native entry |
| 90 | + final Object oldValue = cached.get(key) |
| 91 | + |
| 92 | + return !valuesEqual(oldValue, currentValue, key) |
| 93 | + } as org.grails.datastore.mapping.core.SessionCallback<Boolean>) |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + List getDirtyPropertyNames(D instance) { |
| 98 | + if (instance == null) return Collections.emptyList() |
| 99 | + |
| 100 | + execute({ Session session -> |
| 101 | + final EntityPersister persister = (EntityPersister) session.getPersister(instance) |
| 102 | + if (persister == null) return Collections.<String>emptyList() |
| 103 | + |
| 104 | + final Serializable id = persister.getObjectIdentifier(instance) |
| 105 | + if (id == null) return Collections.<String>emptyList() |
| 106 | + |
| 107 | + final PersistentEntity entity = persister.getPersistentEntity() |
| 108 | + final SessionImplementor<Document> si = (SessionImplementor<Document>) session |
| 109 | + final Document cached = (Document) si.getCachedEntry(entity, id, true) |
| 110 | + if (cached == null) return Collections.<String>emptyList() |
| 111 | + |
| 112 | + final EntityAccess access = session.getMappingContext().createEntityAccess(entity, instance) |
| 113 | + List<String> dirty = [] |
| 114 | + |
| 115 | + for (PersistentProperty prop : entity.getPersistentProperties()) { |
| 116 | + // skip id |
| 117 | + if (prop.name == entity.identity?.name) continue |
| 118 | + |
| 119 | + String key = prop.getMapping()?.getMappedForm()?.getTargetName() |
| 120 | + if (key == null) key = prop.getName() |
| 121 | + |
| 122 | + Object currentValue |
| 123 | + Object oldValue = cached.get(key) |
| 124 | + |
| 125 | + if (prop instanceof ToOne) { |
| 126 | + def assocVal = access.getProperty(prop.name) |
| 127 | + def oldId = (oldValue instanceof DBRef) ? ((DBRef) oldValue).getId() : oldValue |
| 128 | + if (assocVal == null) { |
| 129 | + currentValue = null |
| 130 | + } else { |
| 131 | + def ae = ((ToOne) prop).associatedEntity |
| 132 | + def idVal = ae?.reflector?.getIdentifier(assocVal) |
| 133 | + currentValue = idVal |
| 134 | + } |
| 135 | + if (!valuesEqual(oldId, currentValue, key)) { |
| 136 | + dirty << prop.name |
| 137 | + } |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + if (prop instanceof Simple || prop instanceof Basic) { |
| 142 | + currentValue = access.getProperty(prop.name) |
| 143 | + if (!valuesEqual(oldValue, currentValue, key)) { |
| 144 | + dirty << prop.name |
| 145 | + } |
| 146 | + continue |
| 147 | + } |
| 148 | + // For other kinds (embedded/collections), fall back to DirtyCheckable if available |
| 149 | + def v = access.getProperty(prop.name) |
| 150 | + if (v instanceof org.grails.datastore.mapping.dirty.checking.DirtyCheckable) { |
| 151 | + if (((org.grails.datastore.mapping.dirty.checking.DirtyCheckable) v).hasChanged()) { |
| 152 | + dirty << prop.name |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return dirty |
| 158 | + } as org.grails.datastore.mapping.core.SessionCallback<List<String>>) |
| 159 | + } |
| 160 | + |
| 161 | + private static boolean valuesEqual(Object oldValue, Object currentValue, String propName) { |
| 162 | + if (oldValue === currentValue) return true |
| 163 | + if (oldValue == null || currentValue == null) return false |
| 164 | + |
| 165 | + if (GormProperties.VERSION.equals(propName)) { |
| 166 | + if (oldValue instanceof Number && currentValue instanceof Number) { |
| 167 | + return ((Number) oldValue).longValue() == ((Number) currentValue).longValue() |
| 168 | + } |
| 169 | + return oldValue.toString() == currentValue.toString() |
| 170 | + } |
| 171 | + |
| 172 | + if (oldValue instanceof Float && currentValue instanceof Float) { |
| 173 | + return Float.floatToIntBits((Float) oldValue) == Float.floatToIntBits((Float) currentValue) |
| 174 | + } |
| 175 | + if (oldValue instanceof Double && currentValue instanceof Double) { |
| 176 | + return Double.doubleToLongBits((Double) oldValue) == Double.doubleToLongBits((Double) currentValue) |
| 177 | + } |
| 178 | + |
| 179 | + // Basic equality fallback |
| 180 | + return oldValue == currentValue |
| 181 | + } |
| 182 | +} |
0 commit comments