Package com.google.appengine.api.datastore

Examples of com.google.appengine.api.datastore.DatastoreService


     * results, but this method can return the results quickly.
     *
     * @return a number of entities
     */
    public int countQuickly() {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery pq =
            txSet ? DatastoreUtil.prepare(ds, tx, query) : DatastoreUtil
                .prepare(ds, query);
        return DatastoreUtil.countEntities(pq);
    }
View Full Code Here


     * Returns entities as {@link Iterable}.
     *
     * @return entities as {@link Iterable}
     */
    protected Iterable<Entity> asIterableEntities() {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery pq =
            txSet ? DatastoreUtil.prepare(ds, tx, query) : DatastoreUtil
                .prepare(ds, query);
        return DatastoreUtil.asIterable(pq, fetchOptions);
    }
View Full Code Here

   * Gets or creates an empty CounterNamesShard with the given key
   * @param key
   * @return - the stored entity, or a newly stored entity
   */
  public static Entity getOrInsertCounterNamesShard(Key key) {
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
   
    try {
      Entity entity = datastoreService.get(key);
      _logger.info("Got entity: " + entity);
      return entity;
    } catch (EntityNotFoundException e) {
      //if the entity doesn't exist, create it
      _logger.info("Entity doesn't exist, creating and inserting");
      Entity entity = new Entity(key);
     
      entity.setUnindexedProperty("names", new ArrayList<String>());
      entity.setProperty("timestamp", new Date());
     
      datastoreService.put(entity);
     
      return entity;
    }
  }
View Full Code Here

  public static ResultType addNames(long id, Set<String> names) {
    Key key = KeyFactory.createKey(COUNTER_NAMES_SHARD, id);
   
    _logger.info("Key to add names for: " + key);
   
    DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();

    Transaction txn = datastoreService.beginTransaction();
   
    try {
      Entity entity = datastoreService.get(txn, key);
     
      @SuppressWarnings("unchecked")
      List<String> localNames = (List<String>) entity.getProperty("names");
     
      if (null == localNames) {
        _logger.info("names property is null. Creating new names property list");
        localNames = new ArrayList<String>();
      }
     
      Set<String> localNamesSet = new HashSet<String>(localNames);
      for (String name : names) {
        if (!localNamesSet.contains(name)) {
          localNamesSet.add(name);
        }
      }
     
      localNames = new ArrayList<String>(localNamesSet);
     
      try {
        entity.setProperty("names", localNames);
       
        datastoreService.put(txn, entity);
       
        txn.commit();

        return ResultType.ADD_SUCCESS;
      } catch (Exception e) {
View Full Code Here

    NamespaceManager.set(NAMESPACE);

    try { //put in a try so that we can have a finally block which resets namespaces
      Key key = KeyFactory.createKey("AuthKey", "master");

      DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
      MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();

      // look for the auth key in the datastore
      Entity authEntity = (Entity) memcacheService.get(KeyFactory.keyToString(key));
      if (authEntity == null) {
        try {
          authEntity = datastoreService.get(key);
          memcacheService.put(KeyFactory.keyToString(key), authEntity);
        } catch (EntityNotFoundException e) {
          _logger.info("Authentication entity not found in memcache or datastore");
          authEntity = null;
        }
      }

      if (null != updateAuth) {
        //if we're updating the auth
        if (null == authEntity || !authEntity.getProperty("secret").equals(updateAuth)) {
          //if there is no auth key, or the auth key doesn't match the one provided
          try {
            URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
            String host = URLEncoder.encode(req.getHeader("Host"), "UTF-8");
            HTTPResponse response = fetchService.fetch(new URL(SECURE_HOST + "/auth/?site=" + host + "&auth=" + updateAuth));

            if (response.getResponseCode() == 200) {
              //if successfully received a response, save the new auth key
              String content = new String(response.getContent());
              if (content.equals(new String("OK"))) {
                authEntity = new Entity(key);
                authEntity.setProperty("secret", updateAuth);

                datastoreService.put(authEntity);
                memcacheService.put(KeyFactory.keyToString(key), authEntity);
              }
            }
          } catch (Exception e) {
            _logger.severe("Exception while updating key: " + e);
View Full Code Here

     * Begins a transaction.
     *
     * @return a begun transaction
     */
    public static Transaction beginTransaction() {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        DatastoreTimeoutException dte = null;
        long wait = INITIAL_WAIT_MS;
        for (int i = 0; i < MAX_RETRY; i++) {
            try {
                return ds.beginTransaction();
            } catch (DatastoreTimeoutException e) {
                dte = e;
                logger.log(Level.INFO, "RETRY["
                    + i
                    + "] This message is just INFORMATION["
View Full Code Here

    public static KeyRange allocateIds(String kind, long num)
            throws NullPointerException {
        if (kind == null) {
            throw new NullPointerException("The kind parameter is null.");
        }
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        DatastoreTimeoutException dte = null;
        long wait = INITIAL_WAIT_MS;
        for (int i = 0; i < MAX_RETRY; i++) {
            try {
                return ds.allocateIds(kind, num);
            } catch (DatastoreTimeoutException e) {
                dte = e;
                logger.log(Level.INFO, "RETRY["
                    + i
                    + "] This message is just INFORMATION["
View Full Code Here

            throw new NullPointerException("The parentKey parameter is null.");
        }
        if (kind == null) {
            throw new NullPointerException("The kind parameter is null.");
        }
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        DatastoreTimeoutException dte = null;
        long wait = INITIAL_WAIT_MS;
        for (int i = 0; i < MAX_RETRY; i++) {
            try {
                return ds.allocateIds(parentKey, kind, num);
            } catch (DatastoreTimeoutException e) {
                dte = e;
                logger.log(Level.INFO, "RETRY["
                    + i
                    + "] This message is just INFORMATION["
View Full Code Here

            EntityNotFoundRuntimeException {
        if (key == null) {
            throw new NullPointerException(
                "The key parameter must not be null.");
        }
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        try {
            DatastoreTimeoutException dte = null;
            long wait = INITIAL_WAIT_MS;
            for (int i = 0; i < MAX_RETRY; i++) {
                try {
                    return ds.get(key);
                } catch (DatastoreTimeoutException e) {
                    dte = e;
                    logger.log(Level.INFO, "RETRY["
                        + i
                        + "] This message is just INFORMATION["
View Full Code Here

                "The key parameter must not be null.");
        }
        if (tx != null && !tx.isActive()) {
            throw new IllegalStateException("The transaction must be active.");
        }
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        try {
            DatastoreTimeoutException dte = null;
            long wait = INITIAL_WAIT_MS;
            for (int i = 0; i < MAX_RETRY; i++) {
                try {
                    return ds.get(tx, key);
                } catch (DatastoreTimeoutException e) {
                    dte = e;
                    logger.log(Level.INFO, "RETRY["
                        + i
                        + "] This message is just INFORMATION["
View Full Code Here

TOP

Related Classes of com.google.appengine.api.datastore.DatastoreService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.