String inverseJoinColumn = joinTableData.getInverseJoinColumnName();
String joinColumn = joinTableData.getJoinColumnName();
Map<Object, Set<Object>> joinTableRecords = joinTableData.getJoinTableRecords();
Object connection = null;
Pipeline pipeline = null;
/**
* Example: join table : PERSON_ADDRESS join column : PERSON_ID (1_p)
* inverse join column : ADDRESS_ID (1_a) store in REDIS:
* PERSON_ADDRESS:1_p_1_a PERSON_ID 1_p ADDRESS_ID 1_a
*/
// String rowKey =
try
{
connection = getConnection();
if (isBoundTransaction())
{
pipeline = ((Jedis) connection).pipelined();
}
Set<Object> joinKeys = joinTableRecords.keySet();
for (Object joinKey : joinKeys)
{
String joinKeyAsStr = PropertyAccessorHelper.getString(joinKey);
Set<Object> inverseKeys = joinTableRecords.get(joinKey);
for (Object inverseKey : inverseKeys)
{
Map<byte[], byte[]> redisFields = new HashMap<byte[], byte[]>(1);
String inverseJoinKeyAsStr = PropertyAccessorHelper.getString(inverseKey);
String redisKey = getHashKey(tableName, joinKeyAsStr + "_" + inverseJoinKeyAsStr);
redisFields.put(getEncodedBytes(joinColumn), getEncodedBytes(joinKeyAsStr)); // put
// join
// column
// field.
redisFields.put(getEncodedBytes(inverseJoinColumn), getEncodedBytes(inverseJoinKeyAsStr)); // put
// inverse
// join
// column
// field
// add to hash table.
if (resource != null && resource.isActive())
{
((Transaction) connection).hmset(getEncodedBytes(redisKey), redisFields);
// add index
((Transaction) connection).zadd(getHashKey(tableName, inverseJoinKeyAsStr),
getDouble(inverseJoinKeyAsStr), redisKey);
((Transaction) connection).zadd(getHashKey(tableName, joinKeyAsStr), getDouble(joinKeyAsStr),
redisKey);
}
else
{
((Jedis) connection).hmset(getEncodedBytes(redisKey), redisFields);
// add index
((Jedis) connection).zadd(getHashKey(tableName, inverseJoinKeyAsStr),
getDouble(inverseJoinKeyAsStr), redisKey);
((Jedis) connection).zadd(getHashKey(tableName, joinKeyAsStr), getDouble(joinKeyAsStr),
redisKey);
}
redisFields.clear();
}
}
}
finally
{
if (pipeline != null)
{
pipeline.sync();
}
onCleanup(connection);
}
}