Java Code Examples for com.googlecode.objectify.Objectify#find()

The following examples show how to use com.googlecode.objectify.Objectify#find() . 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: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
private void addUserFileContents(Objectify datastore, String userId, String fileName, byte[] content) {
  UserFileData ufd = datastore.find(userFileKey(userKey(userId), fileName));
  byte [] empty = new byte[] { (byte)0x5b, (byte)0x5d }; // "[]" in bytes
  if (ufd == null) {          // File doesn't exist
    if (fileName.equals(StorageUtil.USER_BACKPACK_FILENAME) &&
      Arrays.equals(empty, content)) {
      return;                 // Nothing to do
    }
    ufd = new UserFileData();
    ufd.fileName = fileName;
    ufd.userKey = userKey(userId);
  } else {
    if (fileName.equals(StorageUtil.USER_BACKPACK_FILENAME) &&
      Arrays.equals(empty, content)) {
      // Storing an empty backback, just delete the file
      datastore.delete(userFileKey(userKey(userId), fileName));
      return;
    }
  }
  ufd.content = content;
  datastore.put(ufd);
}
 
Example 2
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
private FileData createProjectFile(Objectify datastore, Key<ProjectData> projectKey,
    FileData.RoleEnum role, String fileName) {
  FileData fd = datastore.find(projectFileKey(projectKey, fileName));
  if (fd == null) {
    fd = new FileData();
    fd.fileName = fileName;
    fd.projectKey = projectKey;
    fd.role = role;
    return fd;
  } else if (!fd.role.equals(role)) {
    throw CrashReport.createAndLogError(LOG, null,
        collectProjectErrorInfo(null, projectKey.getId(), fileName),
        new IllegalStateException("File role change is not supported"));
  }
  return null;
}
 
Example 3
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
private void removeFilesFromProject(Objectify datastore, long projectId,
    FileData.RoleEnum role, boolean changeModDate, String... fileNames) {
  Key<ProjectData> projectKey = projectKey(projectId);
  List<Key<FileData>> filesToRemove = new ArrayList<Key<FileData>>();
  for (String fileName : fileNames) {
    Key<FileData> key = projectFileKey(projectKey, fileName);
    memcache.delete(key.getString()); // Remove it from memcache (if it is there)
    FileData fd = datastore.find(key);
    if (fd != null) {
      if (fd.role.equals(role)) {
        filesToRemove.add(projectFileKey(projectKey, fileName));
      } else {
        throw CrashReport.createAndLogError(LOG, null,
            collectProjectErrorInfo(null, projectId, fileName),
            new IllegalStateException("File role change is not supported"));
      }
    }
  }
  datastore.delete(filesToRemove);  // batch delete
  if (changeModDate) {
    updateProjectModDate(datastore, projectId, false);
  }
}
 
Example 4
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
private long updateProjectModDate(Objectify datastore, long projectId, boolean doingConversion) {
  long modDate = System.currentTimeMillis();
  ProjectData pd = datastore.find(projectKey(projectId));
  if (pd != null) {
    // Only update the ProjectData dateModified if it is more then a minute
    // in the future. Do this to avoid unnecessary datastore puts.
    // Also do not update modification time when doing conversion from
    // blobstore to GCS
    if ((modDate > (pd.dateModified + 1000*60)) && !doingConversion) {
      pd.dateModified = modDate;
      datastore.put(pd);
    } else {
      // return the (old) dateModified
      modDate = pd.dateModified;
    }
    return modDate;
  } else {
    throw CrashReport.createAndLogError(LOG, null, null,
        new IllegalArgumentException("project " + projectId + " doesn't exist"));
  }
}
 
Example 5
Source File: ObjectifyGalleryStorageIo.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a wrapped class which contains a list of featured gallery app
 * @param start start index
 * @param count count number
 * @return list of gallery app
 */
public GalleryAppListResult getFeaturedApp(int start, int count){
  final List<GalleryApp> apps = new ArrayList<GalleryApp>();
  Objectify datastore = ObjectifyService.begin();
  for (GalleryAppFeatureData appFeatureData:datastore.query(GalleryAppFeatureData.class).offset(start).limit(count)) {
    Long galleryId = appFeatureData.galleryKey.getId();
    GalleryApp gApp = new GalleryApp();
    GalleryAppData galleryAppData = datastore.find(galleryKey(galleryId));
    makeGalleryApp(galleryAppData, gApp);
    apps.add(gApp);
  }

  int totalCount = datastore.query(GalleryAppFeatureData.class).count();
  return new GalleryAppListResult(apps, totalCount);
}
 
Example 6
Source File: ObjectifyGalleryStorageIo.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a wrapped class which contains a list of tutorial gallery app
 * @param start start index
 * @param count count number
 * @return list of gallery app
 */
public GalleryAppListResult getTutorialApp(int start, int count){
  final List<GalleryApp> apps = new ArrayList<GalleryApp>();
  Objectify datastore = ObjectifyService.begin();
  for (GalleryAppTutorialData appTutorialData:datastore.query(GalleryAppTutorialData.class).offset(start).limit(count)) {
    Long galleryId = appTutorialData.galleryKey.getId();
    GalleryApp gApp = new GalleryApp();
    GalleryAppData galleryAppData = datastore.find(galleryKey(galleryId));
    makeGalleryApp(galleryAppData, gApp);
    apps.add(gApp);
  }

  int totalCount = datastore.query(GalleryAppTutorialData.class).count();
  return new GalleryAppListResult(apps, totalCount);
}
 
Example 7
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private UserFileData createUserFile(Objectify datastore, Key<UserData> userKey,
    String fileName) {
  UserFileData ufd = datastore.find(userFileKey(userKey, fileName));
  if (ufd == null) {
    ufd = new UserFileData();
    ufd.fileName = fileName;
    ufd.userKey = userKey;
    return ufd;
  }
  return null;
}
 
Example 8
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
boolean isGcsFile(long projectId, String fileName) {
  Objectify datastore = ObjectifyService.begin();
  Key<FileData> fileKey = projectFileKey(projectKey(projectId), fileName);
  FileData fd;
  fd = (FileData) memcache.get(fileKey.getString());
  if (fd == null) {
    fd = datastore.find(fileKey);
  }
  if (fd != null) {
    return isTrue(fd.isGCS);
  } else {
    return false;
  }
}
 
Example 9
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
public void checkUpgrade(String userId) {
  if (!conversionEnabled)     // Unless conversion is enabled...
    return;
  Objectify datastore = ObjectifyService.begin();
  UserData userData = datastore.find(userKey(userId));
  if ((userData.upgradedGCS && useGcs) ||
    (!userData.upgradedGCS && !useGcs))
    return;                   // All done.
  Queue queue = QueueFactory.getQueue("blobupgrade");
  queue.add(TaskOptions.Builder.withUrl("/convert").param("user", userId)
    .etaMillis(System.currentTimeMillis() + 60000));
  return;
}
 
Example 10
Source File: ObjectifyStorageIo.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
public void doUpgrade(String userId) {
  if (!conversionEnabled)     // Unless conversion is enabled...
    return;                   // shouldn't really ever happen but...
  Objectify datastore = ObjectifyService.begin();
  UserData userData = datastore.find(userKey(userId));
  if ((userData.upgradedGCS && useGcs) ||
    (!userData.upgradedGCS && !useGcs))
    return;                   // All done, another task did it!
  List<Long> projectIds = getProjects(userId);
  boolean anyFailed = false;
  for (long projectId : projectIds) {
    for (FileData fd : datastore.query(FileData.class).ancestor(projectKey(projectId))) {
      if (fd.isBlob) {
        if (useGcs) {         // Let's convert by just reading it!
          downloadRawFile(userId, projectId, fd.fileName);
        }
      } else if (isTrue(fd.isGCS)) {
        if (!useGcs) {        // Let's downgrade by just reading it!
          downloadRawFile(userId, projectId, fd.fileName);
        }
      }
    }
  }

  /*
   * If we are running low on time, we may have not moved all files
   * so exit now without marking the user as having been finished
   */
  if (ApiProxy.getCurrentEnvironment().getRemainingMillis() <= 5000)
    return;

  /* If anything failed, also return without marking user */
  if (anyFailed)
    return;

  datastore = ObjectifyService.beginTransaction();
  userData = datastore.find(userKey(userId));
  userData.upgradedGCS = useGcs;
  datastore.put(userData);
  datastore.getTxn().commit();
}