@Override public Tuple getTuple(EntityKey key, OperationContext operationContext) { String entityIdString = entityId( key ); if ( !connection.exists( entityIdString ) ) { return null; } Map<String, String> objects; if ( operationContext.getTupleTypeContext().getSelectableColumns().isEmpty() ) { objects = connection.hgetall( entityIdString ); } else { List<String> hmget = connection.hmget( entityIdString, getFields( operationContext.getTupleTypeContext() ) ); objects = toEntity( operationContext.getTupleTypeContext(), hmget ); } return createTuple( objects ); }
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; }
/** * Converting entity key to cache key * * @param key entity key * @return string key */ public Object createKeyObject(EntityKey key) { Object result = null; if ( key.getColumnValues().length == 1 ) { IgniteCache<Object, BinaryObject> entityCache = getEntityCache( key.getMetadata() ); CacheConfiguration cacheConfig = entityCache.getConfiguration( CacheConfiguration.class ); result = toValidKeyObject( key.getColumnValues()[0], cacheConfig.getKeyType() ); } else { BinaryObjectBuilder builder = createBinaryObjectBuilder( findKeyType( key.getMetadata() ) ); for ( int i = 0; i < key.getColumnNames().length; i++ ) { builder.setField( StringHelper.stringAfterPoint( key.getColumnNames()[i] ), key.getColumnValues()[i] ); } result = builder.build(); } return result; }
@Override public Map<String, Object> extractEntityTuple(Session session, EntityKey key) { SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) session.getSessionFactory(); IgniteCache<Object, BinaryObject> cache = getEntityCache( sessionFactory, key.getMetadata() ); Object cacheKey = getProvider( sessionFactory ).createKeyObject( key ); Map<String, Object> result = new HashMap<>(); BinaryObject po = cache.get( cacheKey ); TupleSnapshot snapshot = new IgniteTupleSnapshot( cacheKey, po, key.getMetadata() ); for ( String fieldName : snapshot.getColumnNames() ) { result.put( fieldName, snapshot.get( fieldName ) ); } return result; }
private ObjectNode fetchObject(EntityKey key, TupleContext tupleContext) { LightblueEntityMetadataId entityId = LightblueEntityMetadataId.extractEntityInfo(key.getMetadata()); DataFindRequest request = new DataFindRequest(entityId.entityName, entityId.entityVersion); request.where(new ValueQuery("_id", ExpressionOperation.EQUALS, key.getColumnValues()[0].toString())); List<Projection> projections = new ArrayList<Projection>(Arrays.asList(projectionsFromColumns(tupleContext.getSelectableColumns()))); projections.add(new FieldProjection("_id", true, true)); request.select(projections); try { LightblueResponse response = provider.getLightblueClient().data(request); if (response.hasError()) { throw new RuntimeException("Error returned in response: " + response.getText()); } return (ObjectNode) response.getJson().get("processed").get(0); } catch (LightblueException e) { throw new RuntimeException("Unable to communicate with lightblue.", e); } }
@Override public Tuple getTuple(EntityKey key, OperationContext operationContext) { Entity entity = entityStorageStrategy.getEntity( entityId( key ) ); if ( entity != null ) { return new Tuple( new RedisJsonTupleSnapshot( entity ), SnapshotType.UPDATE ); } else if ( isInTheInsertionQueue( key, operationContext ) ) { return createTuple( key, operationContext ); } else { return null; } }
private void storeEntity(EntityKey key, Entity entity, OptionsContext optionsContext) { String entityId = entityId( key ); Long currentTtl = getObjectTTL( entityId, optionsContext ); entityStorageStrategy.storeEntity( entityId, entity ); setObjectTTL( entityId, currentTtl ); }
private void storeEntity(EntityKey key, HashEntity entity, OptionsContext optionsContext) { String entityId = entityId( key ); if ( !entity.isEmpty() ) { Long currentTtl = getObjectTTL( entityId, optionsContext ); connection.hmset( entityId, entity.getProperties() ); setObjectTTL( entityId, currentTtl ); } }
@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 ); }
@Override public Tuple getTuple(EntityKey key, OperationContext operationContext) { IgniteCache<Object, BinaryObject> entityCache = provider.getEntityCache( key.getMetadata() ); if ( entityCache == null ) { throw log.cacheNotFound( key.getMetadata().getTable() ); } Object id = provider.createKeyObject( key ); BinaryObject po = entityCache.get( id ); if ( po != null ) { return new Tuple( new IgniteTupleSnapshot( id, po, key.getMetadata() ), SnapshotType.UPDATE ); } else { return null; } }
@Override public Tuple createTuple(EntityKey key, OperationContext operationContext) { IgniteCache<Object, BinaryObject> entityCache = provider.getEntityCache( key.getMetadata() ); if ( entityCache == null ) { throw log.cacheNotFound( key.getMetadata().getTable() ); } Object id = provider.createKeyObject( key ); return new Tuple( new IgniteTupleSnapshot( id, null, key.getMetadata() ), SnapshotType.INSERT ); }
@Override public void insertOrUpdateTuple(EntityKey key, TuplePointer tuplePointer, TupleContext tupleContext) throws TupleAlreadyExistsException { IgniteCache<Object, BinaryObject> entityCache = provider.getEntityCache( key.getMetadata() ); Tuple tuple = tuplePointer.getTuple(); Object keyObject = null; BinaryObjectBuilder builder = null; IgniteTupleSnapshot tupleSnapshot = (IgniteTupleSnapshot) tuple.getSnapshot(); keyObject = tupleSnapshot.getCacheKey(); if ( tuple.getSnapshotType() == SnapshotType.UPDATE ) { builder = provider.createBinaryObjectBuilder( tupleSnapshot.getCacheValue() ); } else { builder = provider.createBinaryObjectBuilder( provider.getEntityTypeName( key.getMetadata().getTable() ) ); } for ( String columnName : tuple.getColumnNames() ) { Object value = tuple.get( columnName ); if ( value != null ) { builder.setField( StringHelper.realColumnName( columnName ), value ); } else { builder.removeField( StringHelper.realColumnName( columnName ) ); } } BinaryObject valueObject = builder.build(); entityCache.put( keyObject, valueObject ); tuplePointer.setTuple( new Tuple( new IgniteTupleSnapshot( keyObject, valueObject, key.getMetadata() ), SnapshotType.UPDATE ) ); }
@Override public Tuple getTuple(EntityKey key, TupleContext tupleContext) { ObjectNode object = fetchObject(key, tupleContext); if (object != null) { return new Tuple(new LightblueTupleSnapshot(object, key.getMetadata(), OperationType.UPDATE)); } if (isInQueue(key, tupleContext)) { return createTuple(key, tupleContext); } return null; }
@Override public void removeTuple(EntityKey key, TupleContext tupleContext) { LightblueEntityMetadataId entityId = LightblueEntityMetadataId.extractEntityInfo(key.getMetadata()); DataDeleteRequest request = new DataDeleteRequest(entityId.entityName, entityId.entityVersion); request.where(new ValueQuery("_id", ExpressionOperation.EQUALS, key.getColumnValues()[0].toString())); try { LightblueResponse data = provider.getLightblueClient().data(request); System.out.println(data.getJson()); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void removeTuple(EntityKey key, TupleContext tupleContext) { remove( key ); }
protected void remove(EntityKey key) { connection.del( entityId( key ) ); }
@Override public Tuple createTuple(EntityKey key, OperationContext operationContext) { return new Tuple( new RedisJsonTupleSnapshot( new Entity() ), SnapshotType.INSERT ); }
@Override public Tuple createTuple(EntityKey key, OperationContext operationContext) { return new Tuple( new RedisHashTupleSnapshot( new HashEntity( new HashMap<String, String>() ) ), SnapshotType.INSERT ); }
@Override public void removeTuple(EntityKey key, TupleContext tupleContext) { IgniteCache<Object, BinaryObject> entityCache = provider.getEntityCache( key.getMetadata() ); entityCache.remove( provider.createKeyObject( key ) ); }
private boolean isInQueue(EntityKey key, TupleContext tupleContext) { return tupleContext.getOperationsQueue() != null && tupleContext.getOperationsQueue().contains(key); }
@Override public Tuple createTuple(EntityKey key, TupleContext tupleContext) { return new Tuple(new LightblueTupleSnapshot(mapper.createObjectNode(), key.getMetadata(), OperationType.INSERT)); }