Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OGM-887 Don't map elements within embeddable collections using dot names
This change also removes common prefixes from the JSON representation that is stored within Redis
- Loading branch information
Showing
6 changed files
with
173 additions
and
29 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
core/src/main/java/org/hibernate/ogm/dialect/impl/DotPatternMapHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.ogm.dialect.impl; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Provides functionality for dealing with (nested) fields of Map documents. | ||
* | ||
* @author Alan Fitton <alan at eth0.org.uk> | ||
* @author Emmanuel Bernard <emmanuel@hibernate.org> | ||
* @author Gunnar Morling | ||
*/ | ||
public class DotPatternMapHelpers { | ||
|
||
private static final Pattern DOT_SEPARATOR_PATTERN = Pattern.compile( "\\." ); | ||
|
||
/** | ||
* Remove a column from the Map | ||
* | ||
* @param entity the {@link Map} with the column | ||
* @param column the column to remove | ||
*/ | ||
public static void resetValue(Map<?,?> entity, String column) { | ||
// fast path for non-embedded case | ||
if ( !column.contains( "." ) ) { | ||
entity.remove( column ); | ||
} | ||
else { | ||
String[] path = DOT_SEPARATOR_PATTERN.split( column ); | ||
Object field = entity; | ||
int size = path.length; | ||
for (int index = 0 ; index < size ; index++) { | ||
String node = path[index]; | ||
Map parent = (Map) field; | ||
field = parent.get( node ); | ||
if ( field == null && index < size - 1 ) { | ||
//TODO clean up the hierarchy of empty containers | ||
// no way to reach the leaf, nothing to do | ||
return; | ||
} | ||
if ( index == size - 1 ) { | ||
parent.remove( node ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static boolean hasField(Map entity, String dotPath) { | ||
return getValueOrNull( entity, dotPath ) != null; | ||
} | ||
|
||
public static <T> T getValueOrNull(Map entity, String dotPath, Class<T> type) { | ||
Object value = getValueOrNull( entity, dotPath ); | ||
return type.isInstance( value ) ? type.cast( value ) : null; | ||
} | ||
|
||
public static Object getValueOrNull(Map entity, String dotPath) { | ||
// fast path for simple properties | ||
if ( !dotPath.contains( "." ) ) { | ||
return entity.get( dotPath ); | ||
} | ||
|
||
String[] path = DOT_SEPARATOR_PATTERN.split( dotPath ); | ||
int size = path.length; | ||
|
||
for (int index = 0 ; index < size - 1; index++) { | ||
Object next = entity.get( path[index] ); | ||
if ( next == null || !( next instanceof Map ) ) { | ||
return null; | ||
} | ||
entity = (Map) next; | ||
} | ||
|
||
String field = path[size - 1]; | ||
return entity.get( field ); | ||
} | ||
|
||
/** | ||
* Links the two field names into a single left.right field name. | ||
* If the left field is empty, right is returned | ||
* | ||
* @param left one field name | ||
* @param right the other field name | ||
* @return left.right or right if left is an empty string | ||
*/ | ||
public static String flatten(String left, String right) { | ||
return left == null || left.isEmpty() ? right : left + "." + right; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters