private static <V> Binding toBinding( Value.BuilderFactory<V, ?, ?> builderFactory, List<?> values) { List<Value<V>> list = new ArrayList<>(values.size()); for (Object object : values) { @SuppressWarnings("unchecked") V v = (V) object; list.add(builderFactory.newBuilder(v).build()); } Value<?> value; if (list.isEmpty()) { value = new NullValue(); } else if (list.size() == 1) { value = list.get(0); } else { value = new ListValue(list); } return new Binding(value); } }
static Value<?> fromPb(com.google.datastore.v1.Value proto) { ValueTypeCase descriptorId = proto.getValueTypeCase(); ValueType valueType = ValueType.getByDescriptorId(descriptorId.getNumber()); return valueType == null ? RawValue.MARSHALLER.fromProto(proto).build() : valueType.getMarshaller().fromProto(proto).build(); } }
@SuppressWarnings({"unchecked", "deprecation"}) @Test public void testToBuilder() throws Exception { Set<String> content = Collections.singleton("bla"); @SuppressWarnings("rawtypes") ValueBuilder builder = new TestBuilder(); builder.setMeaning(1).set(content).setExcludeFromIndexes(true); Value<?> value = builder.build(); builder = value.toBuilder(); assertEquals(1, value.getMeaning()); assertTrue(value.excludeFromIndexes()); assertEquals(ValueType.LIST, value.getType()); assertEquals(content, value.get()); assertEquals(value, builder.build()); } }
/** @return a value that has the specified index flag (possibly the original itself) */ @SuppressWarnings("unchecked") public static <D> Value<D> index(final Value<D> original, final boolean index) { if (original.excludeFromIndexes() == !index) { return original; } else { return (Value<D>)original.toBuilder().setExcludeFromIndexes(!index).build(); } }
private static <V> Binding toBinding( Value.BuilderFactory<V, ?, ?> builderFactory, List<?> values) { List<Value<V>> list = new ArrayList<>(values.size()); for (Object object : values) { @SuppressWarnings("unchecked") V v = (V) object; list.add(builderFactory.newBuilder(v).build()); } Value<?> value; if (list.isEmpty()) { value = new NullValue(); } else if (list.size() == 1) { value = list.get(0); } else { value = new ListValue(list); } return new Binding(value); } }
@SuppressWarnings("unchecked") @Override public ValueBuilder<?, ?, ?> toDatastore(Object input) { if (input == null) { return NullValue.newBuilder(); } Map<String, ?> map = (Map<String, ?>) input; FullEntity.Builder<IncompleteKey> entityBuilder = FullEntity.newBuilder(); for (Map.Entry<String, ?> entry : map.entrySet()) { String key = entry.getKey(); entityBuilder.set(key, valueMapper.toDatastore(entry.getValue()).build()); } return EntityValue.newBuilder(entityBuilder.build()); }
/** * Applies the given time, <code>millis</code>, to the property represented by the given metadata. * * @param propertyMetadata * the property metadata of the field * @param millis * the time in milliseconds */ private void applyAutoTimestamp(PropertyMetadata propertyMetadata, long millis) { Object timestamp = null; Class<?> fieldType = propertyMetadata.getDeclaredType(); if (Date.class.equals(fieldType)) { timestamp = new Date(millis); } else if (Calendar.class.equals(fieldType)) { Calendar calendar = new Calendar.Builder().setInstant(millis).build(); timestamp = calendar; } else if (Long.class.equals(fieldType) || long.class.equals(fieldType)) { timestamp = millis; } else if (OffsetDateTime.class.equals(fieldType)) { timestamp = OffsetDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault()); } else if (ZonedDateTime.class.equals(fieldType)) { timestamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault()); } ValueBuilder<?, ?, ?> valueBuilder = propertyMetadata.getMapper().toDatastore(timestamp); valueBuilder.setExcludeFromIndexes(!propertyMetadata.isIndexed()); entityBuilder.set(propertyMetadata.getMappedName(), valueBuilder.build()); }
@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; }
@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; }
static Value<?> fromPb(com.google.datastore.v1.Value proto) { ValueTypeCase descriptorId = proto.getValueTypeCase(); ValueType valueType = ValueType.getByDescriptorId(descriptorId.getNumber()); return valueType == null ? RawValue.MARSHALLER.fromProto(proto).build() : valueType.getMarshaller().fromProto(proto).build(); } }
/** * Marshals the embedded fields. */ private void marshalEmbeddedFields() { for (EmbeddedMetadata embeddedMetadata : entityMetadata.getEmbeddedMetadataCollection()) { if (embeddedMetadata.getStorageStrategy() == StorageStrategy.EXPLODED) { marshalWithExplodedStrategy(embeddedMetadata, entity); } else { ValueBuilder<?, ?, ?> embeddedEntityBuilder = marshalWithImplodedStrategy(embeddedMetadata, entity); if (embeddedEntityBuilder != null) { entityBuilder.set(embeddedMetadata.getMappedName(), embeddedEntityBuilder.build()); } } } }
for (Map.Entry<String, ?> entry : map.entrySet()) { String key = entry.getKey(); entityBuilder.set(key, toDatastore(entry.getValue()).build());
/** * Marshals the version field, if it exists. The version will be set to one more than the previous * value. */ private void marshalVersionField() { PropertyMetadata versionMetadata = entityMetadata.getVersionMetadata(); if (versionMetadata != null) { long version = (long) IntrospectionUtils.getFieldValue(versionMetadata, entity); ValueBuilder<?, ?, ?> valueBuilder = versionMetadata.getMapper().toDatastore(version + 1); valueBuilder.setExcludeFromIndexes(!versionMetadata.isIndexed()); entityBuilder.set(versionMetadata.getMappedName(), valueBuilder.build()); } }
@Override @SuppressWarnings("unchecked") public void write(Object source, BaseEntity.Builder sink) { DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(source.getClass()); PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source); persistentEntity.doWithColumnBackedProperties( (DatastorePersistentProperty persistentProperty) -> { try { Object val = accessor.getProperty(persistentProperty); Value convertedVal = this.conversions.convertOnWrite(val, persistentProperty); if (persistentProperty.isUnindexed()) { ValueBuilder valueBuilder = convertedVal.toBuilder(); valueBuilder.setExcludeFromIndexes(true); convertedVal = valueBuilder.build(); } sink.set(persistentProperty.getFieldName(), convertedVal); } catch (DatastoreDataException ex) { throw new DatastoreDataException( "Unable to write " + persistentEntity.kindName() + "." + persistentProperty.getFieldName(), ex); } }); } }
@Override @SuppressWarnings("unchecked") public void write(Object source, BaseEntity.Builder sink) { DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(source.getClass()); PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source); persistentEntity.doWithColumnBackedProperties( (DatastorePersistentProperty persistentProperty) -> { try { Object val = accessor.getProperty(persistentProperty); Value convertedVal = this.conversions.convertOnWrite(val, persistentProperty); if (persistentProperty.isUnindexed()) { ValueBuilder valueBuilder = convertedVal.toBuilder(); valueBuilder.setExcludeFromIndexes(true); convertedVal = valueBuilder.build(); } sink.set(persistentProperty.getFieldName(), convertedVal); } catch (DatastoreDataException ex) { throw new DatastoreDataException( "Unable to write " + persistentEntity.kindName() + "." + persistentProperty.getFieldName(), ex); } }); } }
/** * Marshals the field with the given property metadata. * * @param propertyMetadata * the metadata of the field to be marshaled. * @param target * the object in which the field is defined/accessible from * @param entityBuilder * the native entity on which the marshaled field should be set */ private static void marshalField(PropertyMetadata propertyMetadata, Object target, BaseEntity.Builder<?, ?> entityBuilder) { Object fieldValue = IntrospectionUtils.getFieldValue(propertyMetadata, target); if (fieldValue == null && propertyMetadata.isOptional()) { return; } ValueBuilder<?, ?, ?> valueBuilder = propertyMetadata.getMapper().toDatastore(fieldValue); // ListValues cannot have indexing turned off. Indexing is turned on by // default, so we don't touch excludeFromIndexes for ListValues. if (valueBuilder.getValueType() != ValueType.LIST) { valueBuilder.setExcludeFromIndexes(!propertyMetadata.isIndexed()); } Value<?> datastoreValue = valueBuilder.build(); entityBuilder.set(propertyMetadata.getMappedName(), datastoreValue); Indexer indexer = propertyMetadata.getSecondaryIndexer(); if (indexer != null) { entityBuilder.set(propertyMetadata.getSecondaryIndexName(), indexer.index(datastoreValue)); } }
if (embeddedEntityBuilder2 != null) { embeddedEntityBuilder.set(embeddedMetadata2.getMappedName(), embeddedEntityBuilder2.build());
valueBuilder.setExcludeFromIndexes(!propertyMetadata.isIndexed()); Value<?> value = valueBuilder.build(); entityBuilder.set(propertyMetadata.getMappedName(), value); Indexer indexer = propertyMetadata.getSecondaryIndexer();