/
RedisTestHelper.java
293 lines (236 loc) · 9.05 KB
/
RedisTestHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* 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.datastore.redis.utils;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.ogm.OgmSessionFactory;
import org.hibernate.ogm.cfg.OgmProperties;
import org.hibernate.ogm.datastore.document.options.AssociationStorageType;
import org.hibernate.ogm.datastore.redis.AbstractRedisDialect;
import org.hibernate.ogm.datastore.redis.Redis;
import org.hibernate.ogm.datastore.redis.RedisHashDialect;
import org.hibernate.ogm.datastore.redis.RedisJsonDialect;
import org.hibernate.ogm.datastore.redis.RedisProperties;
import org.hibernate.ogm.datastore.redis.dialect.value.Entity;
import org.hibernate.ogm.datastore.redis.impl.RedisDatastoreProvider;
import org.hibernate.ogm.datastore.spi.DatastoreConfiguration;
import org.hibernate.ogm.datastore.spi.DatastoreProvider;
import org.hibernate.ogm.model.key.spi.EntityKey;
import org.hibernate.ogm.utils.GridDialectType;
import org.hibernate.ogm.utils.TestHelper;
import org.hibernate.ogm.utils.GridDialectTestHelper;
import com.lambdaworks.redis.api.sync.RedisKeyCommands;
import com.lambdaworks.redis.cluster.api.sync.RedisClusterCommands;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class RedisTestHelper implements GridDialectTestHelper {
static {
// Read host and port from environment variable
// Maven's surefire plugin set it to the string 'null'
String redisHostName = System.getenv( "REDIS_HOSTNAME" );
if ( isNotNull( redisHostName ) ) {
System.getProperties().setProperty( OgmProperties.HOST, redisHostName );
}
String redisPort = System.getenv( "REDIS_PORT" );
if ( isNotNull( redisPort ) ) {
System.getProperties().setProperty( OgmProperties.PORT, redisPort );
}
String redisCluster = System.getenv( "REDIS_CLUSTER" );
if ( isNotNull( redisCluster ) ) {
System.getProperties().setProperty( RedisProperties.CLUSTER, redisCluster );
}
}
private static boolean isNotNull(String value) {
return value != null && value.length() > 0 && !"null".equals( value );
}
@Override
public Map<String, Object> extractEntityTuple(Session session, EntityKey key) {
RedisDatastoreProvider castProvider = getProvider( session.getSessionFactory() );
AbstractRedisDialect gridDialect = getGridDialect( castProvider );
if ( gridDialect instanceof RedisJsonDialect ) {
return extractFromJsonDialect( key, (RedisJsonDialect) gridDialect );
}
if ( gridDialect instanceof RedisHashDialect ) {
return extractFromHashDialect( session.getSessionFactory(), key, (RedisHashDialect) gridDialect );
}
throw new IllegalStateException( "Unsupported dialect " + gridDialect );
}
private Map<String, Object> extractFromJsonDialect(
EntityKey key,
RedisJsonDialect gridDialect
) {
Entity entity = gridDialect.getEntityStorageStrategy().getEntity(
gridDialect.entityId( key )
);
if ( entity == null ) {
return null;
}
for ( int i = 0; i < key.getColumnNames().length; i++ ) {
entity.set( key.getColumnNames()[i], key.getColumnValues()[i] );
}
return new HashMap<>( entity.getProperties() );
}
private Map<String, Object> extractFromHashDialect(
SessionFactory sessionFactory,
EntityKey key,
RedisHashDialect gridDialect) {
RedisClusterCommands<String, String> connection = getConnection( sessionFactory );
String entityId = gridDialect.entityId( key );
if ( !connection.exists( entityId ) ) {
return null;
}
Map<String, String> hgetall = connection.hgetall( entityId );
Map<String, Object> result = new HashMap<>();
result.putAll( hgetall );
for ( int i = 0; i < key.getColumnNames().length; i++ ) {
result.put( key.getColumnNames()[i], key.getColumnValues()[i] );
}
return result;
}
private static RedisDatastoreProvider getProvider(SessionFactory sessionFactory) {
DatastoreProvider provider = ( (SessionFactoryImplementor) sessionFactory ).getServiceRegistry().getService(
DatastoreProvider.class
);
if ( !( RedisDatastoreProvider.class.isInstance( provider ) ) ) {
throw new RuntimeException( "Not testing with RedisDatastoreProvider, cannot extract underlying map." );
}
return RedisDatastoreProvider.class.cast( provider );
}
@Override
public AbstractRedisDialect getGridDialect(DatastoreProvider datastoreProvider) {
return getDialect( (RedisDatastoreProvider) datastoreProvider );
}
public static AbstractRedisDialect getDialect(RedisDatastoreProvider datastoreProvider) {
if ( TestHelper.getCurrentDialectType() == GridDialectType.REDIS_HASH ) {
return new RedisHashDialect( datastoreProvider );
}
return new RedisJsonDialect( datastoreProvider );
}
@Override
public boolean backendSupportsTransactions() {
return false;
}
@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
RedisClusterCommands<String, String> connection = getConnection( sessionFactory );
connection.flushall();
}
private RedisClusterCommands<String, String> getConnection(SessionFactory sessionFactory) {
RedisDatastoreProvider castProvider = getProvider( sessionFactory );
return castProvider.getConnection();
}
@Override
public Map<String, String> getAdditionalConfigurationProperties() {
return Collections.emptyMap();
}
@Override
public long getNumberOfEntities(Session session) {
return getNumberOfEntities( session.getSessionFactory() );
}
@Override
public long getNumberOfEntities(SessionFactory sessionFactory) {
RedisClusterCommands<String, String> connection = getConnection( sessionFactory );
List<String> keys = connection.keys( "*" );
long result = 0;
for ( String key : keys ) {
if ( key.startsWith( AbstractRedisDialect.ASSOCIATIONS ) || key.startsWith( AbstractRedisDialect.IDENTIFIERS ) ) {
continue;
}
String type = connection.type( key );
if ( type.equals( "hash" ) || type.equals( "string" ) ) {
result++;
}
}
return result;
}
public static void assertDbObject(
OgmSessionFactory sessionFactory,
String hash,
String key,
String expectedDbObject) {
RedisDatastoreProvider provider = getProvider( sessionFactory );
try {
String actual = provider.getConnection().get( hash + ":" + key );
JSONAssert.assertEquals( expectedDbObject, actual, JSONCompareMode.LENIENT );
}
catch (JSONException e) {
throw new IllegalStateException( e );
}
}
@Override
public long getNumberOfAssociations(Session session) {
return getNumberOfAssociations( session.getSessionFactory() );
}
@Override
public long getNumberOfAssociations(SessionFactory sessionFactory) {
long associationCount = getNumberOfGlobalAssociations( sessionFactory );
associationCount += getNumberOfEmbeddedAssociations( sessionFactory );
return associationCount;
}
@Override
public long getNumberOfAssociations(SessionFactory sessionFactory, AssociationStorageType type) {
switch ( type ) {
case ASSOCIATION_DOCUMENT:
return getNumberOfGlobalAssociations( sessionFactory );
case IN_ENTITY:
return getNumberOfEmbeddedAssociations( sessionFactory );
default:
throw new IllegalArgumentException( "Unexpected association storaget type " + type );
}
}
public long getNumberOfGlobalAssociations(SessionFactory sessionFactory) {
RedisKeyCommands<String, String> connection = getConnection( sessionFactory );
return connection.keys( AbstractRedisDialect.ASSOCIATIONS + ":*" ).size();
}
public long getNumberOfEmbeddedAssociations(SessionFactory sessionFactory) {
RedisClusterCommands<String, String> connection = getConnection( sessionFactory );
long associationCount = 0;
List<String> keys = connection.keys( "*" );
for ( String key : keys ) {
if ( key.startsWith( AbstractRedisDialect.ASSOCIATIONS ) || key.startsWith( AbstractRedisDialect.IDENTIFIERS ) ) {
continue;
}
String type = connection.type( key );
if ( "string".equalsIgnoreCase( type ) ) {
String value = connection.get( key );
JsonNode jsonNode = fromJSON( value );
for ( JsonNode node : jsonNode ) {
if ( node.isArray() ) {
associationCount++;
}
}
}
}
return associationCount;
}
public static JsonNode fromJSON(String json) {
if ( json == null || json.length() == 0 ) {
return null;
}
try {
ObjectMapper objectMapper = new ObjectMapper().configure( JsonParser.Feature.ALLOW_SINGLE_QUOTES, true );
return objectMapper.reader().readTree( json );
}
catch (IOException e) {
throw new IllegalStateException( e );
}
}
@Override
public Class<? extends DatastoreConfiguration<?>> getDatastoreConfigurationType() {
return Redis.class;
}
}