@Override public Builder newBuilder(String value) { return StringValue.newBuilder(value); }
@SuppressWarnings("deprecation") @Test public void testBuilder() throws Exception { StringValue.Builder builder = StringValue.newBuilder(CONTENT); StringValue value = builder.setMeaning(1).setExcludeFromIndexes(true).build(); assertEquals(CONTENT, value.get()); assertEquals(1, value.getMeaning()); assertTrue(value.excludeFromIndexes()); } }
@Test public void testOf() throws Exception { StringValue value = StringValue.of(CONTENT); assertEquals(CONTENT, value.get()); assertFalse(value.excludeFromIndexes()); }
@Test public void testNewTransactionRollback() { Transaction transaction = datastore.newTransaction(); transaction.add(ENTITY3); Entity entity2 = Entity.newBuilder(ENTITY2) .clear() .setNull("bla") .set("list3", StringValue.of("bla"), StringValue.newBuilder("bla").build()) .build(); transaction.update(entity2); transaction.delete(KEY1); transaction.rollback(); transaction.rollback(); // should be safe to repeat rollback calls try { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException ex) { // expected to fail } verifyNotUsable(transaction); List<Entity> list = datastore.fetch(KEY1, KEY2, KEY3); assertEquals(ENTITY1, list.get(0)); assertEquals(ENTITY2, list.get(1)); assertNull(list.get(2)); assertEquals(3, list.size()); }
@Override protected void setValue(StringValue from, com.google.datastore.v1.Value.Builder to) { to.setStringValue(from.get()); } };
@Override public StringValue build() { return new StringValue(this); } }
@Test public void testNewTransactionRollback() { Transaction transaction = DATASTORE.newTransaction(); transaction.add(ENTITY3); Entity entity2 = Entity.newBuilder(ENTITY2) .clear() .setNull("bla") .set("list3", StringValue.of("bla"), StringValue.newBuilder("bla").build()) .build(); transaction.update(entity2); transaction.delete(KEY1); transaction.rollback(); transaction.rollback(); // should be safe to repeat rollback calls try { transaction.commit(); fail("Expecting a failure"); } catch (DatastoreException expected) { assertEquals("FAILED_PRECONDITION", expected.getReason()); } List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3); assertEquals(ENTITY1, list.get(0)); assertEquals(ENTITY2, list.get(1)); assertNull(list.get(2)); assertEquals(3, list.size()); }
@Test public void testGetBlob() throws Exception { assertArrayEquals(STRING_INDEX_VALUE.get().getBytes(), ENTITY2.getBlob("a").toByteArray()); assertEquals(BLOB_VALUE.get(), ENTITY2.getBlob("b")); }
public static StringValue of(String value) { return new StringValue(value); }
public StringValue(String value) { this(newBuilder(value)); }
/** * Sets a property of type {@link StringValue}. * * @param name name of the property * @param value value associated with the property */ public B set(String name, String value) { properties.put(name, of(value)); return self(); }
@Override public Object toModel(Value<?> input) { if (input instanceof NullValue) { return null; } return ((StringValue) input).get(); }
@Override public StringValue build() { return new StringValue(this); } }
@Override public Value<FullEntity<?>> saveSafe(final P pojo, final boolean index, final SaveContext ctx, final Path path) throws SkipException { // check if we need to redirect to a different translator if (pojo.getClass() != declaredClass) { // Sometimes generics are more of a hindrance than a help @SuppressWarnings("unchecked") final ClassTranslator<P> translator = (ClassTranslator<P>)byClass.get(pojo.getClass()); if (translator == null) throw new IllegalStateException("Class '" + pojo.getClass() + "' is not a registered @Subclass"); else return translator.save(pojo, index, ctx, path); } else { // This is a normal save final FullEntity.Builder<IncompleteKey> into = FullEntity.newBuilder(); populator.save(pojo, index, ctx, path, into); if (discriminator != null) { into.set(DISCRIMINATOR_PROPERTY, StringValue.newBuilder(discriminator).setExcludeFromIndexes(true).build()); if (!indexedDiscriminators.isEmpty()) into.set(DISCRIMINATOR_INDEX_PROPERTY, ListValue.of(indexedDiscriminators)); } // The question of whether to index this is weird. In order for subthings to be indexed, the entity needs // to be indexed. But then lists with index-heterogeous values (say, nulls) get reordered (!) // by the datastore. So we always index EntityValues and force all the list translators homogenize their lists. // Gross but seems to be the only way. return EntityValue.of(into.build()); } }
public static PropertyFilter gt(String property, String value) { return new PropertyFilter(property, Operator.GREATER_THAN, of(value)); }
@Override public Object toModel(Value<?> input) { if (input instanceof NullValue) { return null; } return ((StringValue) input).get().toCharArray(); }