protected static Object getAssociationRow(Tuple row, AssociationKey associationKey) { String[] columnsToPersist = associationKey.getMetadata() .getColumnsWithoutKeyColumns( row.getColumnNames() ); // return value itself if there is only a single column to store if ( columnsToPersist.length == 1 ) { return row.get( columnsToPersist[0] ); } Entity rowObject = new Entity(); String prefix = getColumnSharedPrefixOfAssociatedEntityLink( associationKey ); for ( String column : columnsToPersist ) { Object value = row.get( column ); if ( value != null ) { String columnName = column.startsWith( prefix ) ? column.substring( prefix.length() ) : column; rowObject.set( columnName, value ); } } return rowObject.getPropertiesAsHierarchy(); }
/** * Retrieve association from a Redis List or Redis Set, depending on the association type. * * @param key the association key * * @return the association */ protected org.hibernate.ogm.datastore.redis.dialect.value.Association getAssociation(AssociationKey key) { String associationId = associationId( key ); Collection<String> rows; if ( key.getMetadata().getAssociationType() == AssociationType.SET ) { rows = connection.smembers( associationId ); } else { rows = connection.lrange( associationId, 0, -1 ); } org.hibernate.ogm.datastore.redis.dialect.value.Association association = new org.hibernate.ogm.datastore.redis.dialect.value.Association(); for ( String item : rows ) { association.getRows().add( strategy.deserialize( item, Object.class ) ); } return association; }
public IgniteEmbeddedAssociationSnapshot(AssociationKey associationKey, Tuple tuple) { this.associationMetadata = associationKey.getMetadata(); this.tuple = tuple; BinaryObject obj = ( (IgniteTupleSnapshot) tuple.getSnapshot() ).getCacheValue(); Object objects[] = obj != null ? (Object[]) obj.field( StringHelper.realColumnName( associationMetadata.getCollectionRole() ) ) : null; rows = new HashMap<>(); if ( objects != null ) { String rowKeyColumnNames[] = new String[ associationMetadata.getRowKeyColumnNames().length ]; for ( int i = 0; i < rowKeyColumnNames.length; i++ ) { rowKeyColumnNames[i] = StringHelper.stringAfterPoint( associationMetadata.getRowKeyColumnNames()[i] ); } for ( int i = 0; i < objects.length; i++ ) { BinaryObject itemObject = (BinaryObject) objects[i]; Object rowKeyColumnValues[] = new Object[rowKeyColumnNames.length]; for ( int j = 0; j < rowKeyColumnNames.length; j++ ) { rowKeyColumnValues[j] = itemObject.field( rowKeyColumnNames[j] ); } RowKey rowKey = new RowKey( associationMetadata.getRowKeyColumnNames(), rowKeyColumnValues ); this.rows.put( rowKey, new IgniteTupleSnapshot( null, itemObject, associationMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata() ) ); } } }
private String createAssociationQuery(AssociationKey key, boolean selectObjects) { StringBuilder sb = new StringBuilder(); if ( selectObjects ) { sb.append( "SELECT _KEY, _VAL FROM " ); } else { sb.append( "SELECT _KEY FROM " ); } sb.append( key.getMetadata().getTable() ).append( " WHERE " ); boolean first = true; for ( String columnName : key.getColumnNames() ) { if ( !first ) { sb.append( " AND " ); } else { first = false; } sb.append( StringHelper.realColumnName( columnName ) ).append( "=?" ); } return sb.toString(); }
protected void removeAssociations(List<AssociationKey> keys) { if ( keys.isEmpty() ) { return; } String[] ids = new String[keys.size()]; int i = 0; for ( AssociationKey key : keys ) { ids[i] = associationId( key ); i++; } connection.del( ids ); }
/** * Store an association to a Redis List or Redis Set, depending on the association type. * * @param key the association key * @param association the association document */ protected void storeAssociation( AssociationKey key, org.hibernate.ogm.datastore.redis.dialect.value.Association association) { String associationId = associationId( key ); connection.del( associationId ); List<Object> rows = association.getRows(); if ( rows.isEmpty() ) { return; } String[] serializedRows = new String[rows.size()]; int i = 0; for ( Object row : rows ) { serializedRows[i] = strategy.serialize( row ); i++; } if ( key.getMetadata().getAssociationType() == AssociationType.SET ) { connection.sadd( associationId, serializedRows ); } else { connection.rpush( associationId, serializedRows ); } }
/** * Retrieve entity that contains the association, do not enhance with entity key */ protected TuplePointer getEmbeddingEntityTuplePointer(AssociationKey key, AssociationContext associationContext) { TuplePointer tuplePointer = associationContext.getEntityTuplePointer(); if ( tuplePointer.getTuple() == null ) { tuplePointer.setTuple( getTuple( key.getEntityKey(), associationContext ) ); } return tuplePointer; }
@Override public org.hibernate.ogm.model.spi.Association getAssociation(AssociationKey key, AssociationContext associationContext) { RedisAssociation redisAssociation = null; if ( isStoredInEntityStructure( key.getMetadata(), associationContext.getAssociationTypeContext() ) ) { TuplePointer tuplePointer = getEmbeddingEntityTuplePointer( key, associationContext ); if ( tuplePointer == null ) { // The entity associated with this association has already been removed // see ManyToOneTest#testRemovalOfTransientEntityWithAssociation return null; } Entity owningEntity = getEntityFromTuple( tuplePointer.getTuple() ); if ( owningEntity != null && DotPatternMapHelpers.hasField( owningEntity.getPropertiesAsHierarchy(), key.getMetadata().getCollectionRole() ) ) { redisAssociation = RedisAssociation.fromEmbeddedAssociation( tuplePointer, key.getMetadata() ); } } else { Association association = getAssociation( key ); if ( association != null ) { redisAssociation = RedisAssociation.fromAssociationDocument( association ); } } return redisAssociation != null ? new org.hibernate.ogm.model.spi.Association( new RedisAssociationSnapshot( redisAssociation, key ) ) : null; }
@Override public org.hibernate.ogm.model.spi.Association createAssociation( AssociationKey key, AssociationContext associationContext) { RedisAssociation redisAssociation; if ( isStoredInEntityStructure( key.getMetadata(), associationContext.getAssociationTypeContext() ) ) { TuplePointer tuplePointer = getEmbeddingEntityTuplePointer( key, associationContext ); Entity owningEntity = getEntityFromTuple( tuplePointer.getTuple() ); if ( owningEntity == null ) { owningEntity = new Entity(); storeEntity( key.getEntityKey(), owningEntity, associationContext.getAssociationTypeContext().getHostingEntityOptionsContext() ); tuplePointer.setTuple( new Tuple( new RedisJsonTupleSnapshot( owningEntity ), SnapshotType.UPDATE ) ); } redisAssociation = RedisAssociation.fromEmbeddedAssociation( tuplePointer, key.getMetadata() ); } else { redisAssociation = RedisAssociation.fromAssociationDocument( new Association() ); } org.hibernate.ogm.model.spi.Association association = new org.hibernate.ogm.model.spi.Association( new RedisAssociationSnapshot( redisAssociation, key ) ); // in the case of an association stored in the entity structure, we might end up with rows present in the current snapshot of the entity // while we want an empty association here. So, in this case, we clear the snapshot to be sure the association created is empty. if ( !association.isEmpty() ) { association.clear(); } return association; }
@Override public Association getAssociation( AssociationKey key, AssociationContext associationContext) { RedisAssociation redisAssociation = null; if ( isStoredInEntityStructure( key.getMetadata(), associationContext.getAssociationTypeContext() ) ) { TuplePointer tuplePointer = getEmbeddingEntityTuplePointer( key, associationContext ); if ( tuplePointer == null ) { // The entity associated with this association has already been removed // see ManyToOneTest#testRemovalOfTransientEntityWithAssociation return null; } HashEntity owningEntity = getEntityFromTuple( tuplePointer.getTuple() ); if ( owningEntity != null && owningEntity.has( key.getMetadata().getCollectionRole() ) ) { redisAssociation = RedisAssociation.fromHashEmbeddedAssociation( tuplePointer, key.getMetadata() ); } } else { org.hibernate.ogm.datastore.redis.dialect.value.Association association = getAssociation( key ); if ( association == null ) { return null; } redisAssociation = RedisAssociation.fromAssociationDocument( association ); } return redisAssociation != null ? new org.hibernate.ogm.model.spi.Association( new RedisAssociationSnapshot( redisAssociation, key ) ) : null; }
@Override public Association createAssociation( AssociationKey key, AssociationContext associationContext) { RedisAssociation redisAssociation; if ( isStoredInEntityStructure( key.getMetadata(), associationContext.getAssociationTypeContext() ) ) { TuplePointer tuplePointer = getEmbeddingEntityTuplePointer( key, associationContext ); HashEntity owningEntity = getEntityFromTuple( tuplePointer.getTuple() ); if ( owningEntity == null ) { owningEntity = new HashEntity( new HashMap<String, String>() ); storeEntity( key.getEntityKey(), owningEntity, associationContext.getAssociationTypeContext().getHostingEntityOptionsContext() ); tuplePointer.setTuple( new Tuple( new RedisHashTupleSnapshot( owningEntity ), SnapshotType.UPDATE ) ); } redisAssociation = RedisAssociation.fromHashEmbeddedAssociation( tuplePointer, key.getMetadata() ); } else { redisAssociation = RedisAssociation.fromAssociationDocument( new org.hibernate.ogm.datastore.redis.dialect.value.Association() ); } return new org.hibernate.ogm.model.spi.Association( new RedisAssociationSnapshot( redisAssociation, key ) ); }
private Object getAssociationRows( Association association, AssociationKey key) { List<Object> rows = new ArrayList<>( association.size() ); for ( RowKey rowKey : association.getKeys() ) { rows.add( getAssociationRow( association.get( rowKey ), key ) ); } return rows; }
/** * Converting association key to cache key * * @param key - association key * @return string key */ public Object createParentKeyObject(AssociationKey key) { Object result = null; if ( key.getColumnValues().length == 1 ) { result = key.getColumnValues()[0]; } else { throw new UnsupportedOperationException( "Not implemented yet" ); } return result; }
public IgniteAssociationSnapshot(AssociationKey associationKey, Map<Object, BinaryObject> associationMap) { this.rows = CollectionHelper.newHashMap( associationMap.size() ); for ( Map.Entry<Object, BinaryObject> entry : associationMap.entrySet() ) { IgniteAssociationRowSnapshot snapshot = new IgniteAssociationRowSnapshot( entry.getKey(), entry.getValue(), associationKey.getMetadata() ); String rowKeyColumnNames[] = associationKey.getMetadata().getRowKeyColumnNames(); Object rowKeyColumnValues[] = new Object[rowKeyColumnNames.length]; for ( int i = 0; i < rowKeyColumnNames.length; i++ ) { String columnName = rowKeyColumnNames[i]; rowKeyColumnValues[i] = snapshot.get( columnName ); } RowKey rowKey = new RowKey( rowKeyColumnNames, rowKeyColumnValues ); this.rows.put( rowKey, snapshot ); } }
@Override public Association createAssociation(AssociationKey key, AssociationContext associationContext) { if ( key.getMetadata().getAssociationKind() == AssociationKind.ASSOCIATION ) { return new Association( new IgniteAssociationSnapshot( key ) ); } else if ( key.getMetadata().getAssociationKind() == AssociationKind.EMBEDDED_COLLECTION ) { return new Association( new IgniteEmbeddedAssociationSnapshot( key, associationContext.getEntityTuplePointer().getTuple() ) ); } else { throw new UnsupportedOperationException( "Unknown association kind " + key.getMetadata().getAssociationKind() ); } }
public RedisAssociationSnapshot(RedisAssociation association, AssociationKey key) { super( key, MapAssociationRowsHelpers.getRows( association.getRows(), key ), RedisAssociationRowFactory.INSTANCE ); this.redisAssociation = association; }
public IgniteAssociationSnapshot(AssociationKey associationKey) { rows = Collections.emptyMap(); }
@Override public Association getAssociation(AssociationKey key, AssociationContext associationContext) { throw new UnsupportedOperationException("not yet supported"); }
@Override public Association createAssociation(AssociationKey key, AssociationContext associationContext) { throw new UnsupportedOperationException("not yet supported"); }
@Override public void insertOrUpdateAssociation(AssociationKey key, Association association, AssociationContext associationContext) { throw new UnsupportedOperationException("not yet supported"); }
@Override public void removeAssociation(AssociationKey key, AssociationContext associationContext) { throw new UnsupportedOperationException("not yet supported"); }
/** * Create a String representation of the entity key in the format of {@code Association:(table name):(columnId)}. * {@link #ASSOCIATIONS} * * @param key Key of the association * * @return byte array containing the key */ protected String associationId(AssociationKey key) { String prefix = ASSOCIATIONS + ":" + key.getTable() + ":"; String entityId = keyToString( key.getColumnNames(), key.getColumnValues() ) + ":" + key.getMetadata() .getCollectionRole(); return prefix + entityId; }