com.google.cloud.datastore.ListValue Java Examples

The following examples show how to use com.google.cloud.datastore.ListValue. 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: DefaultDatastoreEntityConverter.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private Value setExcludeFromIndexes(Value convertedVal) {
	// ListValues must have its contents individually excluded instead.
	// the entire list must NOT be excluded or there will be an exception.
	// Same for maps and embedded entities which are stored as EntityValue.
	if (convertedVal.getClass().equals(EntityValue.class)) {
		FullEntity.Builder<IncompleteKey> builder = FullEntity.newBuilder();
		((EntityValue) convertedVal).get().getProperties()
						.forEach((key, value) -> builder.set(key, setExcludeFromIndexes(value)));
		return EntityValue.of(builder.build());
	}
	else if (convertedVal.getClass().equals(ListValue.class)) {
		return ListValue.of((List) ((ListValue) convertedVal).get().stream()
						.map(this::setExcludeFromIndexes).collect(Collectors.toList()));
	}
	else {
		return convertedVal.toBuilder().setExcludeFromIndexes(true).build();
	}
}
 
Example #2
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrongTypeReadExceptionList() {
	this.thrown.expect(DatastoreDataException.class);
	this.thrown.expectMessage(
			"Unable to read " +
					"org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItem entity");
	this.thrown.expectMessage("Unable to read property boolField");
	this.thrown.expectMessage(
			"Unable to convert class " +
			"com.google.common.collect.SingletonImmutableList to class java.lang.Boolean");

	Entity entity = getEntityBuilder()
			.set("stringField", "string value")
			.set("boolField", ListValue.of(true))
			.build();

	ENTITY_CONVERTER.read(TestDatastoreItem.class, entity);
}
 
Example #3
Source File: LowerCaseStringListIndexer.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Value<?> index(Value<?> input) {
  if (input.getType() == ValueType.NULL) {
    return NullValue.of();
  }
  try {
    List<? extends Value<?>> list = ((ListValue) input).get();
    ListValue.Builder builder = ListValue.newBuilder();
    for (Value<?> item : list) {
      builder.addValue(ITEM_INDEXER.index(item));
    }
    return builder.build();
  } catch (Exception exp) {
    throw new IndexingException(exp);
  }
}
 
Example #4
Source File: UpperCaseStringListIndexer.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Value<?> index(Value<?> input) {
  if (input.getType() == ValueType.NULL) {
    return NullValue.of();
  }
  try {
    ListValue.Builder builder = ListValue.newBuilder();
    List<? extends Value<?>> list = ((ListValue) input).get();
    for (Value<?> item : list) {
      builder.addValue(ITEM_INDEXER.index(item));
    }
    return builder.build();
  } catch (Exception exp) {
    throw new IndexingException(exp);
  }
}
 
Example #5
Source File: SetMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  List<? extends Value<?>> list = ((ListValue) input).get();
  Set<Object> output;
  if (Modifier.isAbstract(setClass.getModifiers())) {
    if (SortedSet.class.isAssignableFrom(setClass)) {
      output = new TreeSet<>();
    } else {
      output = new HashSet<>();
    }
  } else {
    output = (Set<Object>) IntrospectionUtils.instantiateObject(setClass);
  }
  for (Value<?> item : list) {
    output.add(itemMapper.toModel(item));
  }
  return output;
}
 
Example #6
Source File: ListMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  List<? extends Value<?>> list = ((ListValue) input).get();
  List<Object> output;
  if (Modifier.isAbstract(listClass.getModifiers())) {
    output = new ArrayList<>();
  } else {
    output = (List<Object>) IntrospectionUtils.instantiateObject(listClass);
  }
  for (Value<?> item : list) {
    output.add(itemMapper.toModel(item));
  }
  return output;
}
 
Example #7
Source File: SetMapper.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();
  }
  Set<?> set = (Set<?>) input;
  ListValue.Builder listValueBuilder = ListValue.newBuilder();
  for (Object item : set) {
    listValueBuilder
        .addValue(itemMapper.toDatastore(item).setExcludeFromIndexes(!indexed).build());
  }
  return listValueBuilder;
}
 
Example #8
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayValue() {
  // [START datastore_array_value]
  Entity task = Entity.newBuilder(taskKey)
      .set("tags", "fun", "programming")
      .set("collaborators", ListValue.of("alice", "bob"))
      .build();
  // [END datastore_array_value]
  assertValidEntity(task);
}
 
Example #9
Source File: LowerCaseStringListIndexerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test(expected = IndexingException.class)
public void testIndex_5() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of(), LongValue.of(5L) };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  try {
    ListValue output = (ListValue) indexer.index(input);
  } catch (Exception exp) {
    LOGGER.log(Level.INFO, exp.toString());
    throw exp;
  }
}
 
Example #10
Source File: LowerCaseStringListIndexerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_3() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of() };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<? extends Value> inputList = input.get();
  List<? extends Value> outputList = output.get();
  assertEquals(((StringValue) inputList.get(0)).get().toLowerCase(Locale.ENGLISH),
      outputList.get(0).get());
  assertEquals(inputList.get(1).get(), outputList.get(1).get());
}
 
Example #11
Source File: LowerCaseStringListIndexerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_2() {
  Value<?>[] inputArray = { StringValue.of("ONE"), StringValue.of("Two"),
      StringValue.of("thRee") };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<StringValue> inputList = (List<StringValue>) input.get();
  List<StringValue> outputList = (List<StringValue>) output.get();
  assertEquals(inputList.get(0).get().toLowerCase(Locale.ENGLISH), outputList.get(0).get());
  assertEquals(inputList.get(1).get().toLowerCase(Locale.ENGLISH), outputList.get(1).get());
  assertEquals(inputList.get(2).get().toLowerCase(Locale.ENGLISH), outputList.get(2).get());
}
 
Example #12
Source File: UpperCaseStringListIndexerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test(expected = IndexingException.class)
public void testIndex_5() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of(), LongValue.of(5L) };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  try {
    ListValue output = (ListValue) indexer.index(input);
  } catch (Exception exp) {
    LOGGER.log(Level.INFO, exp.toString());
    throw exp;
  }
}
 
Example #13
Source File: UpperCaseStringListIndexerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_3() {
  Value<?>[] inputArray = { StringValue.of("Hello"), NullValue.of() };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<? extends Value> inputList = input.get();
  List<? extends Value> outputList = output.get();
  assertEquals(((StringValue) inputList.get(0)).get().toUpperCase(Locale.ENGLISH),
      outputList.get(0).get());
  assertEquals(inputList.get(1).get(), outputList.get(1).get());
}
 
Example #14
Source File: UpperCaseStringListIndexerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndex_2() {
  Value<?>[] inputArray = { StringValue.of("ONE"), StringValue.of("Two"),
      StringValue.of("thRee") };
  ListValue input = ListValue.of(Arrays.asList(inputArray));
  ListValue output = (ListValue) indexer.index(input);
  List<StringValue> inputList = (List<StringValue>) input.get();
  List<StringValue> outputList = (List<StringValue>) output.get();
  assertEquals(inputList.get(0).get().toUpperCase(Locale.ENGLISH), outputList.get(0).get());
  assertEquals(inputList.get(1).get().toUpperCase(Locale.ENGLISH), outputList.get(1).get());
  assertEquals(inputList.get(2).get().toUpperCase(Locale.ENGLISH), outputList.get(2).get());
}
 
Example #15
Source File: ListMapper.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();
  }
  List<?> list = (List<?>) input;
  ListValue.Builder listValueBuilder = ListValue.newBuilder();
  for (Object item : list) {
    listValueBuilder
        .addValue(itemMapper.toDatastore(item).setExcludeFromIndexes(!indexed).build());
  }
  return listValueBuilder;
}
 
Example #16
Source File: DatastoreTemplate.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
	DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext
			.getPersistentEntity(entity.getClass());
	List<Entity> entitiesToSave = new ArrayList<>();
	datastorePersistentEntity.doWithAssociations((AssociationHandler) (association) -> {
		PersistentProperty persistentProperty = association.getInverse();
		PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
		Object val = accessor.getProperty(persistentProperty);
		if (val == null) {
			return;
		}
		Value<?> value;
		if (LazyUtil.isLazyAndNotLoaded(val)) {
			value = LazyUtil.getKeys(val);
		}
		else if (persistentProperty.isCollectionLike()) {
			Iterable<?> iterableVal = (Iterable<?>) ValueUtil.toListIfArray(val);
			entitiesToSave.addAll(getEntitiesForSave(iterableVal, persistedEntities));
			List<KeyValue> keyValues = StreamSupport.stream((iterableVal).spliterator(), false)
					.map((o) -> KeyValue.of(this.getKey(o, false)))
					.collect(Collectors.toList());
			value = ListValue.of(keyValues);

		}
		else {
			entitiesToSave.addAll(getEntitiesForSave(Collections.singletonList(val), persistedEntities));
			Key key = getKey(val, false);
			value = KeyValue.of(key);
		}
		builder.set(((DatastorePersistentProperty) persistentProperty).getFieldName(), value);
	});
	return entitiesToSave;
}
 
Example #17
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void saveTestNotInterfaceLazy() {
	ArrayList<ChildEntity> arrayList = new ArrayList();
	arrayList.add(this.childEntity7);
	this.ob1.lazyMultipleReference = LazyUtil.wrapSimpleLazyProxy(
			() -> arrayList, ArrayList.class,
			ListValue.of(KeyValue.of(this.childKey7)));
	saveTestCommon(this.ob1, true);
}
 
Example #18
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void saveTestCollectionLazy() {
	this.ob1.lazyMultipleReference = LazyUtil.wrapSimpleLazyProxy(
			() -> Collections.singletonList(this.childEntity7), List.class,
			ListValue.of(KeyValue.of(this.childKey7)));
	saveTestCommon(this.ob1, true);
}
 
Example #19
Source File: TwoStepsConversions.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private Value convertOnWrite(Object proppertyVal, EmbeddedType embeddedType,
		String fieldName, TypeInformation typeInformation) {
	Object val = proppertyVal;

	Function<Object, Value> writeConverter = this::convertOnWriteSingle;
	if (proppertyVal != null) {
		switch (embeddedType) {
		case EMBEDDED_MAP:
			writeConverter = (x) -> convertOnWriteSingleEmbeddedMap(x, fieldName,
					(TypeInformation) typeInformation.getMapValueType());
			break;
		case EMBEDDED_ENTITY:
			writeConverter = (x) -> convertOnWriteSingleEmbedded(x, fieldName);
			break;
		case NOT_EMBEDDED:
			writeConverter = this::convertOnWriteSingle;
			break;
		default:
			throw new DatastoreDataException(
					"Unexpected property embedded type: " + embeddedType);
		}
	}

	val = ValueUtil.toListIfArray(val);

	if (val instanceof Iterable) {
		List<Value<?>> values = new ArrayList<>();
		for (Object propEltValue : (Iterable) val) {
			values.add(writeConverter.apply(propEltValue));
		}
		return ListValue.of(values);
	}
	return writeConverter.apply(val);
}
 
Example #20
Source File: DatastoreTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void findAllReferenceLoopTest() {

	Entity referenceTestDatastoreEntity = Entity.newBuilder(this.key1)
			.set("sibling", this.key1)
			.set("lazyChildren", ListValue.of(this.key2))
			.set("lazyChild", this.childKey2)
			.build();

	Entity child = Entity.newBuilder(this.key1).build();
	Entity child2 = Entity.newBuilder(this.childKey2).build();

	when(this.datastore.fetch(eq(this.key1)))
			.thenReturn(Collections.singletonList(referenceTestDatastoreEntity));
	when(this.datastore.fetch(eq(this.key2)))
			.thenReturn(Collections.singletonList(child));
	when(this.datastore.fetch(eq(this.childKey2)))
			.thenReturn(Collections.singletonList(child2));

	ReferenceTestEntity referenceTestEntity = new ReferenceTestEntity();
	ReferenceTestEntity childEntity = new ReferenceTestEntity();
	ReferenceTestEntity childEntity2 = new ReferenceTestEntity();

	when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(referenceTestDatastoreEntity)))
			.thenAnswer(invocationOnMock -> referenceTestEntity);

	when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child)))
			.thenAnswer(invocationOnMock -> childEntity);
	when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child2)))
			.thenAnswer(invocationOnMock -> childEntity2);

	verifyBeforeAndAfterEvents(null,
			new AfterFindByKeyEvent(Collections.singletonList(referenceTestEntity),
					Collections.singleton(this.key1)),
			() -> {
				ReferenceTestEntity readReferenceTestEntity = this.datastoreTemplate.findById(this.key1,
						ReferenceTestEntity.class);

				assertThat(readReferenceTestEntity.sibling).isSameAs(readReferenceTestEntity);
				verify(this.datastore, times(1)).fetch(any());

				assertThat(readReferenceTestEntity.lazyChildren.size()).isEqualTo(1);
				verify(this.datastore, times(2)).fetch(any());
				verify(this.datastore, times(1)).fetch(eq(this.key1));
				verify(this.datastore, times(1)).fetch(eq(this.key2));

				assertThat(readReferenceTestEntity.lazyChild.toString()).isNotNull();
				verify(this.datastore, times(3)).fetch(any());
				verify(this.datastore, times(1)).fetch(eq(this.childKey2));
			}, x -> {
			});
}
 
Example #21
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void testUnindexedField() {
	UnindexedTestDatastoreItem item = new UnindexedTestDatastoreItem();
	item.setIndexedField(1L);
	item.setUnindexedField(2L);
	item.setUnindexedStringListField(Arrays.asList("a", "b"));
	item.setUnindexedMapField(new MapBuilder<String, String>().put("c", "C").put("d", "D").build());
	item.setEmbeddedItem(new UnindexedTestDatastoreItem(2, new UnindexedTestDatastoreItem(3, null)));
	item.setUnindexedItems(Collections.singletonList(new UnindexedTestDatastoreItem(4, new UnindexedTestDatastoreItem(5, null))));

	DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(
					new DatastoreMappingContext(),
					new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
	Entity.Builder builder = getEntityBuilder();
	entityConverter.write(item, builder);
	Entity entity = builder.build();

	assertThat(entity.getLong("indexedField")).as("validate indexed field value")
			.isEqualTo(1L);

	assertThat(entity.getLong("unindexedField")).as("validate unindexed field value")
			.isEqualTo(2L);

	assertThat(entity.getValue("indexedField").excludeFromIndexes())
			.as("validate excludeFromIndexes on indexed field").isFalse();
	assertThat(entity.getValue("unindexedField").excludeFromIndexes())
			.as("validate excludeFromIndexes on unindexed field").isTrue();

	// the ListValue itself must NOT be unindexed or it will cause exception. the contents
	// individually must be. Same for map.
	assertThat(entity.getValue("unindexedStringListField").excludeFromIndexes()).isFalse();
	assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(0).excludeFromIndexes())
			.isTrue();
	assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(1).excludeFromIndexes())
			.isTrue();

	assertThat(entity.getValue("unindexedMapField").excludeFromIndexes()).isFalse();
	assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("c").excludeFromIndexes())
			.isTrue();
	assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("d").excludeFromIndexes())
			.isTrue();

	//Multi-level embedded entities - exclusion from indexes
	testMultiLevelEmbeddedEntityUnindexed(((EntityValue) entity.getValue("embeddedItem")).get());
	//Multi-level embedded entities in a list - exclusion from indexes
	testMultiLevelEmbeddedEntityUnindexed(
					((EntityValue) (
									(ListValue) entity.getValue("unindexedItems")).get().get(0)
					).get());
}