com.google.cloud.datastore.Blob Java Examples

The following examples show how to use com.google.cloud.datastore.Blob. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleCloudUtils.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an Entity Builder for the given key and properties. Converts the objects to the proper
 * datastore values
 */
static Entity.Builder createEntityBuilder(Key key, Map<String, Object> data) throws IOException {
  Entity.Builder builder = Entity.newBuilder(key);

  for (Map.Entry<String, Object> entry : data.entrySet()) {
    if (entry.getValue() instanceof String) {
      builder.set(entry.getKey(), (String) entry.getValue()); // StringValue
    } else if (entry.getValue() instanceof Integer) {
      builder.set(entry.getKey(), (Integer) entry.getValue()); // LongValue
    } else if (entry.getValue() instanceof Double) {
      builder.set(entry.getKey(), (Double) entry.getValue()); // DoubleValue
    } else if (entry.getValue() instanceof Boolean) {
      builder.set(entry.getKey(), (Boolean) entry.getValue()); // BooleanValue
    } else if (entry.getValue() instanceof Timestamp) {
      builder.set(entry.getKey(), (Timestamp) entry.getValue()); // TimestampValue
    } else {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(entry.getValue());
      }
      builder.set(entry.getKey(), Blob.copyFrom(bos.toByteArray())); // BlobValue
    }
  }
  return builder;
}
 
Example #2
Source File: DatastoreSessionTest.java    From tomcat-runtime with Apache License 2.0 6 votes vote down vote up
@Test
public void testAttributesDeserialization() throws Exception {
  Entity metadata = mock(Entity.class);
  when(metadata.getBoolean(any())).thenReturn(true);
  when(sessionKey.getName()).thenReturn("count");
  when(metadata.getKey()).thenReturn(sessionKey);
  int count = 5;
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(bos);
  oos.writeObject(count);

  Entity valueEntity = Entity.newBuilder(
      (Key)when(mock(Key.class).getName()).thenReturn("count").getMock())
      .set("value", Blob.copyFrom(bos.toByteArray()))
      .build();

  DatastoreSession session = new DatastoreSession(sessionManager);
  session.restoreFromEntities(sessionKey, Arrays.asList(metadata, valueEntity));

  assertEquals(count, session.getAttribute("count"));
}
 
Example #3
Source File: DatastoreSession.java    From tomcat-runtime with Apache License 2.0 6 votes vote down vote up
/**
 * Serialize an attribute an embed it into an entity whose key is generated by the provided
 * KeyFactory.
 * @param attributeKeyFactory The KeyFactory to use to create the key for the entity.
 * @param name The name of the attribute to serialize.
 * @return An Entity containing the serialized attribute.
 */
private Entity serializeAttribute(KeyFactory attributeKeyFactory, String name) {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
    oos.writeObject(getAttribute(name));
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }

  return Entity.newBuilder(attributeKeyFactory.newKey(name))
      .set(SessionMetadata.ATTRIBUTE_VALUE_NAME,
          BlobValue.newBuilder(Blob.copyFrom(bos.toByteArray()))
              .setExcludeFromIndexes(true)
              .build())
      .build();
}
 
Example #4
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void writeNullTest() {
	byte[] bytes = { 1, 2, 3 };
	TestDatastoreItem item = new TestDatastoreItem();
	item.setStringField(null);
	item.setBoolField(true);
	item.setDoubleField(3.1415D);
	item.setLongField(123L);
	item.setLatLngField(LatLng.of(10, 20));
	item.setTimestampField(Timestamp.ofTimeSecondsAndNanos(30, 40));
	item.setBlobField(Blob.copyFrom(bytes));

	Entity.Builder builder = getEntityBuilder();
	ENTITY_CONVERTER.write(item, builder);

	Entity entity = builder.build();

	assertThat(entity.getValue("stringField").equals(new NullValue()))
			.as("validate null field").isTrue();
}
 
Example #5
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readNullTest() {
	byte[] bytes = { 1, 2, 3 };
	Entity entity = getEntityBuilder()
			.set("durationField", "PT24H")
			.set("stringField", new NullValue())
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.set("intField", 99)
			.set("enumField", "BLACK")
			.build();
	TestDatastoreItem item = ENTITY_CONVERTER.read(TestDatastoreItem.class, entity);

	assertThat(item.getStringField()).as("validate null field").isNull();
}
 
Example #6
Source File: TestEntity.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public TestEntity(Long id, String color, Long size, Shape shape, Blob blobField, EmbeddedEntity embeddedEntity) {
	this.id = id;
	this.color = color;
	this.size = size;
	this.shape = shape;
	this.blobField = blobField;
	this.embeddedEntity = embeddedEntity;
}
 
Example #7
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void readTest() {
	byte[] bytes = { 1, 2, 3 };
	Key otherKey = Key.newBuilder("testproject", "test_kind", "test_name").build();
	Entity entity = getEntityBuilder()
			.set("durationField", "PT24H")
			.set("stringField", "string value")
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.set("intField", 99)
			.set("enumField", "WHITE")
			.set("keyField", otherKey)
			.build();
	TestDatastoreItem item = ENTITY_CONVERTER.read(TestDatastoreItem.class, entity);

	assertThat(item.getDurationField()).as("validate duration field").isEqualTo(Duration.ofDays(1));
	assertThat(item.getStringField()).as("validate string field").isEqualTo("string value");
	assertThat(item.getBoolField()).as("validate boolean field").isTrue();
	assertThat(item.getDoubleField()).as("validate double field").isEqualTo(3.1415D);
	assertThat(item.getLongField()).as("validate long field").isEqualTo(123L);
	assertThat(item.getLatLngField()).as("validate latLng field")
			.isEqualTo(LatLng.of(10, 20));
	assertThat(item.getTimestampField()).as("validate timestamp field")
			.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
	assertThat(item.getBlobField()).as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
	assertThat(item.getIntField()).as("validate int field").isEqualTo(99);
	assertThat(item.getEnumField()).as("validate enum field").isEqualTo(TestDatastoreItem.Color.WHITE);
	assertThat(item.getKeyField()).as("validate key field").isEqualTo(otherKey);
}
 
Example #8
Source File: TestEntity.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
public TestEntity(Long id, String color, Long size, Shape shape, Blob blobField) {
	this.id = id;
	this.color = color;
	this.size = size;
	this.shape = shape;
	this.blobField = blobField;
}
 
Example #9
Source File: EntityPropertyValueProviderTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void getPropertyValue() {
	byte[] bytes = { 1, 2, 3 };
	Entity entity = Entity.newBuilder(this.datastore.newKeyFactory().setKind("aKind").newKey("1"))
			.set("stringField", "string value")
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.build();

	EntityPropertyValueProvider provider = new EntityPropertyValueProvider(entity, this.twoStepsConversion);

	assertThat((String) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("stringField")))
			.as("validate string field").isEqualTo("string value");
	assertThat((Boolean) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("boolField")))
			.as("validate boolean field").isTrue();
	assertThat((Double) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("doubleField")))
			.as("validate double field").isEqualTo(3.1415D);
	assertThat((Long) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("longField")))
			.as("validate long field").isEqualTo(123L);
	assertThat((LatLng) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("latLngField")))
			.as("validate latLng field").isEqualTo(LatLng.of(10, 20));
	assertThat((Timestamp) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("timestampField")))
			.as("validate timestamp field")
			.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
	assertThat((Blob) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("blobField")))
			.as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
}
 
Example #10
Source File: ByteArrayMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return BlobValue.newBuilder(Blob.copyFrom((byte[]) input));
}
 
Example #11
Source File: DatastoreSession.java    From tomcat-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize the content of each entity and add them as attribute of the session.
 * @param entities The entities containing the serialized attributes.
 * @throws IOException If an error occur during the deserialization
 * @throws ClassNotFoundException If the class being deserialized is not present in this program.
 */
private void restoreAttributesFromEntity(Iterable<Entity> entities) throws IOException,
    ClassNotFoundException {
  for (Entity entity : entities) {
    String name = entity.getKey().getName();
    Blob value = entity.getBlob(SessionMetadata.ATTRIBUTE_VALUE_NAME);
    try (InputStream fis = value.asInputStream();
        ObjectInputStream ois = new ObjectInputStream(fis)) {
      Object attribute = ois.readObject();
      setAttribute(name, attribute, false);
    }
  }
}
 
Example #12
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> getProperties(Entity entity)
    throws IOException, ClassNotFoundException {
  if (entity == null) {
    return null;
  }
  ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
  for (String property : entity.getNames()) {
    // builder.put(property, entity.getValue(property));
    if (entity.getValue(property) instanceof StringValue) {
      builder.put(property, (String) entity.getString(property));
    } else if (entity.getValue(property) instanceof LongValue) {
      // This conversion is safe because of integer to long conversion above
      builder.put(property, new Long(entity.getLong(property)).intValue());
    } else if (entity.getValue(property) instanceof DoubleValue) {
      builder.put(property, (Double) entity.getDouble(property));
    } else if (entity.getValue(property) instanceof BooleanValue) {
      builder.put(property, (Boolean) entity.getBoolean(property));
    } else if (entity.getValue(property) instanceof TimestampValue) {
      builder.put(property, (Timestamp) entity.getTimestamp(property));
    } else {
      Blob blob = entity.getBlob(property);
      Object obj = null;
      try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) {
        obj = in.readObject();
      }
      builder.put(property, obj); // BlobValue
    }
  }

  return builder.build();
}
 
Example #13
Source File: TestEntity.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public Blob getBlobField() {
	return this.blobField;
}
 
Example #14
Source File: TestEntity.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public void setBlobField(Blob blobField) {
	this.blobField = blobField;
}
 
Example #15
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void writeTest() {
	byte[] bytesForBlob = { 1, 2, 3 };
	byte[] bytes = { 1, 2, 3 };
	Key otherKey = Key.newBuilder("testproject", "test_kind", "test_name").build();
	TestDatastoreItem item = new TestDatastoreItem();
	item.setDurationField(Duration.ofDays(1));
	item.setStringField("string value");
	item.setBoolField(true);
	item.setDoubleField(3.1415D);
	item.setLongField(123L);
	item.setLatLngField(LatLng.of(10, 20));
	item.setTimestampField(Timestamp.ofTimeSecondsAndNanos(30, 40));
	item.setBlobField(Blob.copyFrom(bytesForBlob));
	item.setIntField(99);
	item.setEnumField(TestDatastoreItem.Color.BLACK);
	item.setByteArrayField(bytes);
	item.setKeyField(otherKey);

	Entity.Builder builder = getEntityBuilder();
	ENTITY_CONVERTER.write(item, builder);

	Entity entity = builder.build();

	assertThat(entity.getString("durationField")).as("validate duration field")
			.isEqualTo("PT24H");
	assertThat(entity.getString("stringField")).as("validate string field")
			.isEqualTo("string value");
	assertThat(entity.getBoolean("boolField")).as("validate boolean field").isTrue();
	assertThat(entity.getDouble("doubleField")).as("validate double field").isEqualTo(3.1415D);
	assertThat(entity.getLong("longField")).as("validate long field").isEqualTo(123L);
	assertThat(entity.getLatLng("latLngField")).as("validate latLng field")
			.isEqualTo(LatLng.of(10, 20));
	assertThat(entity.getTimestamp("timestampField")).as("validate timestamp field")
			.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
	assertThat(entity.getBlob("blobField")).as("validate blob field")
			.isEqualTo(Blob.copyFrom(bytesForBlob));
	assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(99L);
	assertThat(entity.getString("enumField")).as("validate enum field").isEqualTo("BLACK");
	assertThat(entity.getBlob("byteArrayField")).as("validate blob field")
			.isEqualTo(Blob.copyFrom(bytes));
	assertThat(entity.getKey("keyField")).as("validate key field")
			.isEqualTo(otherKey);
}
 
Example #16
Source File: TwoStepsConversions.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
public Blob convert(byte[] source) {
	return Blob.copyFrom(source);
}
 
Example #17
Source File: TestDatastoreItem.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public Blob getBlobField() {
	return this.blobField;
}
 
Example #18
Source File: TestDatastoreItem.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
public void setBlobField(Blob blobField) {
	this.blobField = blobField;
}
 
Example #19
Source File: QueryUtils.java    From catatumbo with Apache License 2.0 4 votes vote down vote up
/**
 * Adds the given binding to the given query builder's to the list of positional bindings.
 * 
 * @param queryBuilder
 *          the query builder
 * @param binding
 *          the positional binding to add
 */
static void addPositionalBinding(GqlQuery.Builder<?> queryBuilder, Object binding) {
  if (binding == null) {
    throw new IllegalArgumentException("binding cannot be null. Use IS NULL in your query");
  }
  if (binding instanceof Short) {
    queryBuilder.addBinding((short) binding);
  } else if (binding instanceof Integer) {
    queryBuilder.addBinding((int) binding);
  } else if (binding instanceof Long) {
    queryBuilder.addBinding((long) binding);
  } else if (binding instanceof Float) {
    queryBuilder.addBinding((float) binding);
  } else if (binding instanceof Double) {
    queryBuilder.addBinding((double) binding);
  } else if (binding instanceof Boolean) {
    queryBuilder.addBinding((boolean) binding);
  } else if (binding instanceof Character) {
    queryBuilder.addBinding(String.valueOf((char) binding));
  } else if (binding instanceof String) {
    queryBuilder.addBinding((String) binding);
  } else if (binding instanceof Calendar) {
    queryBuilder.addBinding(toTimestamp((Calendar) binding));
  } else if (binding instanceof Date) {
    queryBuilder.addBinding(toTimestamp((Date) binding));
  } else if (binding instanceof LocalDate) {
    queryBuilder.addBinding(((LocalDate) binding).toString());
  } else if (binding instanceof LocalTime) {
    queryBuilder.addBinding(((LocalTime) binding).format(LocalTimeMapper.FORMATTER));
  } else if (binding instanceof LocalDateTime) {
    queryBuilder.addBinding(((LocalDateTime) binding).format(LocalDateTimeMapper.FORMATTER));
  } else if (binding instanceof OffsetDateTime) {
    queryBuilder.addBinding(toTimestamp((OffsetDateTime) binding));
  } else if (binding instanceof ZonedDateTime) {
    queryBuilder.addBinding(toTimestamp((ZonedDateTime) binding));
  } else if (binding instanceof byte[]) {
    queryBuilder.addBinding(Blob.copyFrom((byte[]) binding));
  } else if (binding instanceof DatastoreKey) {
    queryBuilder.addBinding(((DatastoreKey) binding).nativeKey());
  } else if (binding instanceof DatastoreCursor) {
    queryBuilder.addBinding(Cursor.fromUrlSafe(((DatastoreCursor) binding).getEncoded()));
  } else if (binding instanceof GeoLocation) {
    // TODO no support for GeoLocation in the gcloud API
  }
}
 
Example #20
Source File: QueryUtils.java    From catatumbo with Apache License 2.0 4 votes vote down vote up
/**
 * Applies the given positional bindings to the given query builder.
 * 
 * @param queryBuilder
 *          the query builder
 * @param namedBindings
 *          the named bindings to apply
 */
static void applyNamedBindings(GqlQuery.Builder<?> queryBuilder,
    Map<String, Object> namedBindings) {
  if (namedBindings != null) {
    for (Map.Entry<String, Object> entry : namedBindings.entrySet()) {
      String bindingName = entry.getKey();
      Object bindingValue = entry.getValue();
      if (bindingValue instanceof Short) {
        queryBuilder.setBinding(bindingName, (short) bindingValue);
      } else if (bindingValue instanceof Integer) {
        queryBuilder.setBinding(bindingName, (int) bindingValue);
      } else if (bindingValue instanceof Long) {
        queryBuilder.setBinding(bindingName, (long) bindingValue);
      } else if (bindingValue instanceof Float) {
        queryBuilder.setBinding(bindingName, (float) bindingValue);
      } else if (bindingValue instanceof Double) {
        queryBuilder.setBinding(bindingName, (double) bindingValue);
      } else if (bindingValue instanceof Boolean) {
        queryBuilder.setBinding(bindingName, (boolean) bindingValue);
      } else if (bindingValue instanceof String) {
        queryBuilder.setBinding(bindingName, (String) bindingValue);
      } else if (bindingValue instanceof Calendar) {
        queryBuilder.setBinding(bindingName, toTimestamp((Calendar) bindingValue));
      } else if (bindingValue instanceof Date) {
        queryBuilder.setBinding(bindingName, toTimestamp((Date) bindingValue));
      } else if (bindingValue instanceof LocalDate) {
        queryBuilder.setBinding(bindingName, ((LocalDate) bindingValue).toString());
      } else if (bindingValue instanceof LocalTime) {
        queryBuilder.setBinding(bindingName,
            ((LocalTime) bindingValue).format(LocalTimeMapper.FORMATTER));
      } else if (bindingValue instanceof LocalDateTime) {
        queryBuilder.setBinding(bindingName,
            ((LocalDateTime) bindingValue).format(LocalDateTimeMapper.FORMATTER));
      } else if (bindingValue instanceof OffsetDateTime) {
        queryBuilder.setBinding(bindingName, toTimestamp((OffsetDateTime) bindingValue));
      } else if (bindingValue instanceof ZonedDateTime) {
        queryBuilder.setBinding(bindingName, toTimestamp((ZonedDateTime) bindingValue));
      } else if (bindingValue instanceof byte[]) {
        queryBuilder.setBinding(bindingName, Blob.copyFrom((byte[]) bindingValue));
      } else if (bindingValue instanceof DatastoreKey) {
        queryBuilder.setBinding(bindingName, ((DatastoreKey) bindingValue).nativeKey());
      } else if (bindingValue instanceof DatastoreCursor) {
        queryBuilder.setBinding(bindingName,
            Cursor.fromUrlSafe(((DatastoreCursor) bindingValue).getEncoded()));
      } else if (bindingValue instanceof GeoLocation) {
        // TODO no support for GeoLocation in the gcloud API
      }
    }
  }
}
 
Example #21
Source File: TwoStepsConversions.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] convert(Blob source) {
	return source.toByteArray();
}