Java Code Examples for com.google.appengine.api.datastore.Entity#setUnindexedProperty()

The following examples show how to use com.google.appengine.api.datastore.Entity#setUnindexedProperty() . 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: AppEngineDataStoreFactory.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
@Override
public AppEngineDataStore<V> set(String key, V value) throws IOException {
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);
  lock.lock();
  try {
    Entity entity = new Entity(getId(), key);
    entity.setUnindexedProperty(FIELD_VALUE, new Blob(IOUtils.serialize(value)));
    dataStoreService.put(entity);
    if (memcache != null) {
      memcache.put(key, value, memcacheExpiration);
    }
  } finally {
    lock.unlock();
  }
  return this;
}
 
Example 2
Source File: Slot.java    From appengine-pipelines with Apache License 2.0 6 votes vote down vote up
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setUnindexedProperty(FILLED_PROPERTY, filled);
  if (null != fillTime) {
    entity.setUnindexedProperty(FILL_TIME_PROPERTY, fillTime);
  }
  if (null != sourceJobKey) {
    entity.setProperty(SOURCE_JOB_KEY_PROPERTY, sourceJobKey);
  }
  entity.setProperty(WAITING_ON_ME_PROPERTY, waitingOnMeKeys);
  if (serializedVersion != null) {
    entity.setUnindexedProperty(VALUE_PROPERTY, serializedVersion);
  } else {
    try {
      entity.setUnindexedProperty(VALUE_PROPERTY,
          PipelineManager.getBackEnd().serializeValue(this, value));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  return entity;
}
 
Example 3
Source File: TransactionTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleDefault() throws EntityNotFoundException, InterruptedException {
    clearData(kindName);
    Transaction tx = service.beginTransaction();
    Entity newRec = new Entity(kindName);
    newRec.setProperty("check", "4100331");
    newRec.setProperty("step", "added");
    Key key = service.put(tx, newRec);
    tx.commit();
    Entity qRec = service.get(key);
    assertEquals("4100331", qRec.getProperty("check"));

    tx = service.beginTransaction();
    qRec = service.get(key);
    qRec.setUnindexedProperty("step", "update");
    service.put(tx, newRec);
    tx.rollback();
    qRec = service.get(key);
    assertEquals("added", qRec.getProperty("step"));
}
 
Example 4
Source File: TestMetadataServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private void populate(DatastoreService ds, String namespace) {
  NamespaceManager.set(namespace);
  Entity e = new Entity("Fun");
  e.setProperty("me", "yes");
  e.setProperty("you", 23);
  e.setUnindexedProperty("haha", 0);
  ds.put(e);
  Entity s = new Entity("Strange");
  ArrayList nowhereList = new ArrayList<Integer>();
  nowhereList.add(1);
  nowhereList.add(2);
  nowhereList.add(3);
  s.setProperty("nowhere", nowhereList);
  ds.put(s);
  Entity s2 = new Entity("Stranger");
  s2.setProperty("missing", new ArrayList<Integer>());
  ds.put(s2);
}
 
Example 5
Source File: UnindexedPropertiesTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnindexedProperties() throws Exception {
    Entity entity = new Entity(UNINDEXED_ENTITY);
    entity.setUnindexedProperty("unindexedString", "unindexedString");
    entity.setUnindexedProperty("unindexedList", new ArrayList<String>(Arrays.asList("listElement1", "listElement2", "listElement3")));
    entity.setUnindexedProperty("unindexedText", new Text("unindexedText"));
    entity.setUnindexedProperty("unindexedBlob", new Blob("unindexedBlob".getBytes()));
    entity.setProperty("text", new Text("text"));
    entity.setProperty("blob", new Blob("blob".getBytes()));

    Key key = service.put(entity);
    sync(3000);  // Not using ancestor queries, so pause before doing queries below.
    Entity entity2 = service.get(key);

    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedString")));
    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedList")));
    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedText")));
    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedBlob")));
    assertTrue(isUnindexed(getRawProperty(entity2, "text")));
    assertTrue(isUnindexed(getRawProperty(entity2, "blob")));

    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("unindexedString", EQUAL, "unindexedString"))));
    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("unindexedList", EQUAL, "listElement1"))));
    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("unindexedText", EQUAL, "unindexedText"))));
    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("text", EQUAL, "text"))));

    service.delete(key);
}
 
Example 6
Source File: IndexesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
  // [START unindexed_properties_1]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Key acmeKey = KeyFactory.createKey("Company", "Acme");

  Entity tom = new Entity("Person", "Tom", acmeKey);
  tom.setProperty("name", "Tom");
  tom.setProperty("age", 32);
  datastore.put(tom);

  Entity lucy = new Entity("Person", "Lucy", acmeKey);
  lucy.setProperty("name", "Lucy");
  lucy.setUnindexedProperty("age", 29);
  datastore.put(lucy);

  Filter ageFilter = new FilterPredicate("age", FilterOperator.GREATER_THAN, 25);

  Query q = new Query("Person").setAncestor(acmeKey).setFilter(ageFilter);

  // Returns tom but not lucy, because her age is unindexed
  List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
  // [END unindexed_properties_1]

  assertWithMessage("query results").that(results).containsExactly(tom);
}
 
Example 7
Source File: Barrier.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setProperty(JOB_KEY_PROPERTY, jobKey);
  entity.setUnindexedProperty(TYPE_PROPERTY, type.toString());
  entity.setUnindexedProperty(RELEASED_PROPERTY, released);
  entity.setUnindexedProperty(WAITING_ON_KEYS_PROPERTY, waitingOnKeys);
  entity.setUnindexedProperty(WAITING_ON_GROUP_SIZES_PROPERTY, waitingOnGroupSizes);
  return entity;
}
 
Example 8
Source File: JobInstanceRecord.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setProperty(JOB_KEY_PROPERTY, jobKey);
  entity.setProperty(JOB_CLASS_NAME_PROPERTY, jobClassName);
  entity.setUnindexedProperty(INSTANCE_VALUE_PROPERTY, value);
  entity.setUnindexedProperty(JOB_DISPLAY_NAME_PROPERTY, jobDisplayName);
  return entity;
}
 
Example 9
Source File: ShardedValue.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setUnindexedProperty(SHARD_ID_PROPERTY, shardId);
  entity.setUnindexedProperty(VALUE_PROPERTY, new Blob(value));
  return entity;
}
 
Example 10
Source File: PipelineModelObject.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
protected Entity toProtoEntity() {
  Entity entity = new Entity(key);
  entity.setProperty(ROOT_JOB_KEY_PROPERTY, rootJobKey);
  if (generatorJobKey != null) {
    entity.setProperty(GENERATOR_JOB_PROPERTY, generatorJobKey);
  }
  if (graphGUID != null) {
    entity.setUnindexedProperty(GRAPH_GUID_PROPERTY, graphGUID);
  }
  return entity;
}
 
Example 11
Source File: ExceptionRecord.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public Entity toEntity() {
  try {
    Entity entity = toProtoEntity();
    byte[] serializedException = SerializationUtils.serialize(exception);
    entity.setUnindexedProperty(EXCEPTION_PROPERTY, new Blob(serializedException));
    return entity;
  } catch (IOException e) {
    throw new RuntimeException("Failed to serialize exception for " + getKey(), e);
  }
}
 
Example 12
Source File: FanoutTaskRecord.java    From appengine-pipelines with Apache License 2.0 4 votes vote down vote up
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setUnindexedProperty(PAYLOAD_PROPERTY, new Blob(payload));
  return entity;
}
 
Example 13
Source File: LocalRawGcsService.java    From appengine-gcs-client with Apache License 2.0 4 votes vote down vote up
@Override
public void finishObjectCreation(RawGcsCreationToken token, ByteBuffer chunk, long timeoutMillis)
    throws IOException {
  ensureInitialized();
  Token t = append(token, chunk);

  int totalBytes = 0;

  BlobKey blobKey = getBlobKeyForFilename(t.filename);
  try (WritableByteChannel outputChannel = Channels.newChannel(blobStorage.storeBlob(blobKey))) {
    for (ByteBuffer buffer : inMemoryData.get(t.filename)){
      totalBytes += buffer.remaining();
      outputChannel.write(buffer);
    }
    inMemoryData.remove(t.filename);
  }

  String mimeType = t.options.getMimeType();
  if (Strings.isNullOrEmpty(mimeType)) {
    mimeType = "application/octet-stream";
  }

  BlobInfo blobInfo = new BlobInfo(
      blobKey, mimeType, new Date(), getPathForGcsFilename(t.filename),
      totalBytes);

  String namespace = NamespaceManager.get();
  try {
    NamespaceManager.set("");
    String blobKeyString = blobInfo.getBlobKey().getKeyString();
    Entity blobInfoEntity =
        new Entity(GOOGLE_STORAGE_FILE_KIND, blobKeyString);
    blobInfoEntity.setProperty(BlobInfoFactory.CONTENT_TYPE, blobInfo.getContentType());
    blobInfoEntity.setProperty(BlobInfoFactory.CREATION, blobInfo.getCreation());
    blobInfoEntity.setProperty(BlobInfoFactory.FILENAME, blobInfo.getFilename());
    blobInfoEntity.setProperty(BlobInfoFactory.SIZE, blobInfo.getSize());
    datastore.put(blobInfoEntity);
  } finally {
    NamespaceManager.set(namespace);
  }

  Entity e = new Entity(makeKey(t.filename));
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  try (ObjectOutputStream oout = new ObjectOutputStream(bout)) {
    oout.writeObject(t.options);
  }
  e.setUnindexedProperty(OPTIONS_PROP, new Blob(bout.toByteArray()));
  e.setUnindexedProperty(CREATION_TIME_PROP, System.currentTimeMillis());
  e.setUnindexedProperty(FILE_LENGTH_PROP, totalBytes);
  datastore.put(null, e);
}
 
Example 14
Source File: JobRecord.java    From appengine-pipelines with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs and returns a Data Store Entity that represents this model
 * object
 */
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setProperty(JOB_INSTANCE_PROPERTY, jobInstanceKey);
  entity.setProperty(FINALIZE_BARRIER_PROPERTY, finalizeBarrierKey);
  entity.setProperty(RUN_BARRIER_PROPERTY, runBarrierKey);
  entity.setProperty(OUTPUT_SLOT_PROPERTY, outputSlotKey);
  entity.setProperty(STATE_PROPERTY, state.toString());
  if (null != exceptionHandlingAncestorKey) {
    entity.setProperty(EXCEPTION_HANDLING_ANCESTOR_KEY_PROPERTY, exceptionHandlingAncestorKey);
  }
  if (exceptionHandlerSpecified) {
    entity.setProperty(EXCEPTION_HANDLER_SPECIFIED_PROPERTY, Boolean.TRUE);
  }
  if (null != exceptionHandlerJobKey) {
    entity.setProperty(EXCEPTION_HANDLER_JOB_KEY_PROPERTY, exceptionHandlerJobKey);
  }
  if (null != exceptionKey) {
    entity.setProperty(EXCEPTION_KEY_PROPERTY, exceptionKey);
  }
  if (null != exceptionHandlerJobGraphGuid) {
    entity.setUnindexedProperty(
        EXCEPTION_HANDLER_JOB_GRAPH_GUID_PROPERTY, new Text(exceptionHandlerJobGraphGuid));
  }
  entity.setUnindexedProperty(CALL_EXCEPTION_HANDLER_PROPERTY, callExceptionHandler);
  entity.setUnindexedProperty(IGNORE_EXCEPTION_PROPERTY, ignoreException);
  if (childGraphGuid != null) {
    entity.setUnindexedProperty(CHILD_GRAPH_GUID_PROPERTY, new Text(childGraphGuid));
  }
  entity.setProperty(START_TIME_PROPERTY, startTime);
  entity.setUnindexedProperty(END_TIME_PROPERTY, endTime);
  entity.setProperty(CHILD_KEYS_PROPERTY, childKeys);
  entity.setUnindexedProperty(ATTEMPT_NUM_PROPERTY, attemptNumber);
  entity.setUnindexedProperty(MAX_ATTEMPTS_PROPERTY, maxAttempts);
  entity.setUnindexedProperty(BACKOFF_SECONDS_PROPERTY, backoffSeconds);
  entity.setUnindexedProperty(BACKOFF_FACTOR_PROPERTY, backoffFactor);
  entity.setUnindexedProperty(ON_BACKEND_PROPERTY, queueSettings.getOnBackend());
  entity.setUnindexedProperty(ON_MODULE_PROPERTY, queueSettings.getOnModule());
  entity.setUnindexedProperty(MODULE_VERSION_PROPERTY, queueSettings.getModuleVersion());
  entity.setUnindexedProperty(ON_QUEUE_PROPERTY, queueSettings.getOnQueue());
  entity.setUnindexedProperty(STATUS_CONSOLE_URL, statusConsoleUrl);
  if (rootJobDisplayName != null) {
    entity.setProperty(ROOT_JOB_DISPLAY_NAME, rootJobDisplayName);
  }
  return entity;
}
 
Example 15
Source File: EntityBatchUpdater.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
/**
 * Processes the entities meeting the criteria defined by the query filter.
 * @return the string representation of the updated count
 */
public String processEntities() {
	if ( maxUpdates <= 0 )
		return getProcessedCountString();
	
	if ( batchSize == null )
		batchSize = 500;
	if ( maxUpdates < batchSize )
		batchSize = maxUpdates;
	
	final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
	
	final FetchOptions fetchOptions = FetchOptions.Builder.withLimit( batchSize );
	
	Cursor cursor = null;
	while ( true ) {
		if ( cursor != null )
			fetchOptions.startCursor( cursor );
		
		final QueryResultList< Entity > resultList = ds.prepare( query ).asQueryResultList( fetchOptions );
		
		for ( final Entity e : resultList ) {
			if ( entityProcessor != null )
				entityProcessor.processEntity( e );
			
			if ( makePropertiesUnindexed != null )
				for ( final String propertyName : makePropertiesUnindexed )
					e.setUnindexedProperty( propertyName, e.getProperty( propertyName ) );
			
			if ( newVersionToSet != null )
				e.setProperty( "v", newVersionToSet );
			
			if ( autoSave )
				ds.put( e );
			
			processedCount++;
		}
		
		if ( resultList.size() < batchSize || processedCount >= maxUpdates )
			return getProcessedCountString();
		
		cursor = resultList.getCursor();
	}
}
 
Example 16
Source File: TaskServlet.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
private void customTask( final HttpServletRequest request, final PersistenceManager pm ) throws IOException {
	LOGGER.fine( "Key: " + request.getParameter( "key" ) + ", file type: " + request.getParameter( "fileType" ) );
	
	final FileType fileType = FileType.fromString( request.getParameter( "fileType" ) );
	if ( fileType == null ) {
		LOGGER.severe( "Invalid File type!" );
		return;
	}
	
	final Key key = KeyFactory.stringToKey( request.getParameter( "key" ) );
	
	final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
       final Entity e;
	try {
        e = ds.get( key );
       } catch ( final EntityNotFoundException enfe ) {
		LOGGER.log( Level.WARNING, "Entity not found!", enfe );
        return;
       }
	
       if ( !e.getKind().equals( "Rep" ) && !e.getKind().equals( "Smpd" ) && !e.getKind().equals( "OtherFile" ) ) {
		LOGGER.severe( "Invalid Entity kind:" + e.getKind() );
		return;
       }
       
       if ( (Long) e.getProperty( "v" ) == 4 ) {
		LOGGER.warning( "Entity is already v4!" );
		return;
	}
       
       // Update common properties:
       // TODO
       final int size = ( (Long) e.getProperty( "size" ) ).intValue();
       if ( size < FileServlet.DATASTORE_CONTENT_STORE_LIMIT ) {
           final FileService fileService = FileServiceFactory.getFileService();
   		final AppEngineFile   appeFile = new AppEngineFile( FileSystem.BLOBSTORE, ( (BlobKey) e.getProperty( "blobKey" ) ).getKeyString() );
   		final FileReadChannel channel  = fileService.openReadChannel( appeFile, false );
   		final byte[]          content  = new byte[ size ];
   		final ByteBuffer      wrapper  = ByteBuffer.wrap( content );
   		while ( channel.read( wrapper ) > 0 )
   			;
   		channel.close();
   		
   		e.setProperty( "content", new Blob( content ) );
   		e.setProperty( "blobKey", null );
   		fileService.delete( appeFile );
       }
       e.setUnindexedProperty( "blobKey", e.getProperty( "blobKey" ) );
       e.setUnindexedProperty( "content", e.getProperty( "content" ) );
       
       switch ( fileType ) {
       case SC2REPLAY :
           e.setUnindexedProperty( "matchup", e.getProperty( "matchup" ) );
       	break;
       case MOUSE_PRINT :
       	break;
       case OTHER :
       	break;
       default:
       	throw new RuntimeException( "Invalid file type!" );
       }
       
       // UPGRADE COMPLETE!
	e.setProperty( "v", 4 );
	ds.put( e );
}
 
Example 17
Source File: BlobMetadata.java    From io2014-codelabs with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs a BlobMetadata for a new blob.
 *
 * @param gcsCanonicalizedResourcePath canonicalized resource path for this blob, e.g.,
 *        "/bucket/path/objectName".
 * @param accessMode access mode for this blob.
 * @param ownerId owner of this blob.
 */
public BlobMetadata(
    String gcsCanonicalizedResourcePath, BlobAccessMode accessMode, String ownerId) {
  entity = new Entity(ENTITY_KIND, gcsCanonicalizedResourcePath);
  entity.setProperty(ENTITY_OWNER_PROPERTY, ownerId);
  entity.setUnindexedProperty(ENTITY_ACCESS_MODE_PROPERTY, (long) accessMode.ordinal());
}
 
Example 18
Source File: BlobMetadata.java    From solutions-mobile-backend-starter-java with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs a BlobMetadata for a new blob.
 *
 * @param gcsCanonicalizedResourcePath canonicalized resource path for this blob, e.g.,
 *        "/bucket/path/objectName".
 * @param accessMode access mode for this blob.
 * @param ownerId owner of this blob.
 */
public BlobMetadata(
    String gcsCanonicalizedResourcePath, BlobAccessMode accessMode, String ownerId) {
  entity = new Entity(ENTITY_KIND, gcsCanonicalizedResourcePath);
  entity.setProperty(ENTITY_OWNER_PROPERTY, ownerId);
  entity.setUnindexedProperty(ENTITY_ACCESS_MODE_PROPERTY, (long) accessMode.ordinal());
}