Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 Google Inc. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.google.api.services.datastore.client;
 
 
 import java.io.File;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
Helper methods for Datastore.
 
 public class DatastoreHelper {
   private static final Logger logger = Logger.getLogger(DatastoreHelper.class.getName());

  
The property used in the Datastore to give us a random distribution.
 
   public static final String SCATTER_PROPERTY_NAME = "__scatter__";

  
The property used in the Datastore to get the key of the entity.
 
   public static final String KEY_PROPERTY_NAME = "__key__";

  
Comparator for Keys
 
   private static final class KeyComparator implements Comparator<Key> {
 
     static final KeyComparator INSTANCE = new KeyComparator();
 
     private int comparePathElement(PathElement thisElementPathElement otherElement) {
       int result = thisElement.getKind().compareTo(otherElement.getKind());
       if (result != 0) {
         return result;
       }
       if (thisElement.hasId()) {
         if (!otherElement.hasId()) {
           return -1;
         }
         return Long.valueOf(thisElement.getId()).compareTo(otherElement.getId());
       }
       if (otherElement.hasId()) {
         return 1;
       }
 
       return thisElement.getName().compareTo(otherElement.getName());
     }
 
     @Override
     public int compare(Key thisKeyKey otherKey) {
       if (!thisKey.getPartitionId().equals(otherKey.getPartitionId())) {
         throw new IllegalArgumentException("Cannot compare keys with different partition ids.");
       }
 
       Iterator<PathElementthisPath = thisKey.getPathElementList().iterator();
       Iterator<PathElementotherPath = otherKey.getPathElementList().iterator();
       while (thisPath.hasNext()) {
        if (!otherPath.hasNext()) {
          return 1;
        }
        int result = comparePathElement(thisPath.next(), otherPath.next());
        if (result != 0) {
          return result;
        }
      }
      return otherPath.hasNext() ? -1 : 0;
    }
  }
  private DatastoreHelper() {}

  
Attempts to get credentials from Google Compute Engine.

Returns:
valid credentials or null
      throws GeneralSecurityExceptionIOException {
    NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    try {
      // Try to connect using Google Compute Engine service account credentials.
      ComputeCredential credential = new ComputeCredential(transportnew JacksonFactory());
      // Force token refresh to detect if we are running on Google Compute Engine.
      credential.refreshToken();
      return credential;
    } catch (IOException e) {
      return null;
    }
  }

  
Constructs credentials for the given account and key.

Parameters:
account the account to use.
privateKeyFile the file name from which to get the private key.
Returns:
valid credentials or null
  public static Credential getServiceAccountCredential(String accountString privateKeyFile)
      throws GeneralSecurityExceptionIOException {
    NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    return new GoogleCredential.Builder()
        .setTransport(transport)
        .setJsonFactory(jsonFactory)
        .setServiceAccountId(account)
        .setServiceAccountPrivateKeyFromP12File(new File(privateKeyFile))
        .build();
  }

  
Uses the following enviorment variables to construct a Datastore: DATASTORE_DATASET - the datastore dataset id DATASTORE_HOST - the host to use to access the datastore e.g: https://www.googleapis.com/datastore/v1/datasets/{dataset} DATASTORE_SERVICE_ACCOUNT - (optional) service account name DATASTORE_PRIVATE_KEY_FILE - (optional) service account private key file Preference of credentials is: - ComputeEngine - Service Account (specified by DATASTORE_SERVICE_ACCOUNT and DATASTORE_PRIVATE_KEY_FILE) - no-credentials (for local development environment)
      throws GeneralSecurityExceptionIOException {
    options.dataset(System.getenv("DATASTORE_DATASET"));
    options.host(System.getenv("DATASTORE_HOST"));
    Credential credential = getComputeEngineCredential();
    if (credential != null) {
      .info("Using Compute Engine credential.");
    } else if (System.getenv("DATASTORE_SERVICE_ACCOUNT") != null &&
        System.getenv("DATASTORE_PRIVATE_KEY_FILE") != null) {
      credential = getServiceAccountCredential(System.getenv("DATASTORE_SERVICE_ACCOUNT"),
          System.getenv("DATASTORE_PRIVATE_KEY_FILE"));
      .info("Using JWT Service Account credential.");
    }
    options.credential(credential);
    return options;
  }

  
    return DatastoreFactory.get().create(getOptionsfromEnv().build());
  }

  
Gets a QuerySplitter. The returned QuerySplitter.getSplits(com.google.api.services.datastore.DatastoreV1.Query,int,com.google.api.services.datastore.client.Datastore) cannot accept a query that contains inequality filters, a sort filter, or a missing kind.
  public static QuerySplitter getQuerySplitter() {
  }
  public static Comparator<KeygetKeyComparator() {
    return .;
  }

  
Make a sort order for use in a query.
  public static PropertyOrder.Builder makeOrder(String property,
      PropertyOrder.Direction direction) {
    return PropertyOrder.newBuilder()
        .setProperty(makePropertyReference(property))
        .setDirection(direction);
  }

  
Make a filter on a property for use in a query.
  public static Filter.Builder makeFilter(String propertyPropertyFilter.Operator operator,
      Value value) {
    return Filter.newBuilder()
        .setPropertyFilter(PropertyFilter.newBuilder()
            .setProperty(makePropertyReference(property))
            .setOperator(operator)
            .setValue(value));
  }

  
Make a filter on a property for use in a query.
  public static Filter.Builder makeFilter(String propertyPropertyFilter.Operator operator,
      Value.Builder value) {
    return makeFilter(propertyoperatorvalue.build());
  }

  
Make a composite filter from the given sub-filters. Uses AND to combine filters.
  public static Filter.Builder makeFilter(Filter ...subfilters) {
    return makeCompositeFilter(Arrays.asList(subfilters));
  }

  
Make a composite filter from the given sub-filters. Uses AND to combine filters.
  public static Filter.Builder makeCompositeFilter(Iterable<Filtersubfilters) {
    return Filter.newBuilder()
        .setCompositeFilter(CompositeFilter.newBuilder()
            .addAllFilter(subfilters)
            .setOperator(..));
  }

  
Make an entity property with the specified value.
  public static Property.Builder makeProperty(String nameValue value) {
    return Property.newBuilder().setName(name).addValue(value);
  }

  
Make an entity property with the specified value.
  public static Property.Builder makeProperty(String nameValue.Builder value) {
    return makeProperty(namevalue.build());
  }

  
Make an entity property with the specified values.
  public static Property.Builder makeProperty(String nameIterable<Valuevalues) {
    return Property.newBuilder().setName(name).setMulti(true).addAllValue(values);
  }

  
Make an entity property with the specified values.
  public static Property.Builder makeProperty(String nameValue ...values) {
    return makeProperty(name, Arrays.asList(values));
  }

  
Make an entity property with the specified values.
  public static Property.Builder makeProperty(String nameValue.Builder ...builders) {
    Property.Builder prop = makeProperty(name, Collections.<Value>emptyList());
    for (Value.Builder builder : builders) {
      prop.addValue(builder);
    }
    return prop;
  }

  
Make a property reference for use in a query.
  public static PropertyReference.Builder makePropertyReference(String propertyName) {
    return PropertyReference.newBuilder().setName(propertyName);
  }

  
Make a key value.
  public static Value.Builder makeValue(Key key) {
    return Value.newBuilder().setKeyValue(key);
  }

  
Make a key value.
  public static Value.Builder makeValue(Key.Builder key) {
    return makeValue(key.build());
  }

  
Make an integer value.
  public static Value.Builder makeValue(long key) {
    return Value.newBuilder().setIntegerValue(key);
  }

  
Make a floating point value.
  public static Value.Builder makeValue(double value) {
    return Value.newBuilder().setDoubleValue(value);
  }

  
Make a floating point value.
  public static Value.Builder makeValue(boolean value) {
    return Value.newBuilder().setBooleanValue(value);
  }

  
Make a string value.
  public static Value.Builder makeValue(String value) {
    return Value.newBuilder().setStringValue(value);
  }

  
Make a key value.
  public static Value.Builder makeValue(Entity entity) {
    return Value.newBuilder().setEntityValue(entity);
  }

  
Make a entity value.
  public static Value.Builder makeValue(Entity.Builder entity) {
    return makeValue(entity.build());
  }

  
Make a entity value.
  public static Value.Builder makeValue(ByteString blob) {
    return Value.newBuilder().setBlobValue(blob);
  }

  
Make a date value given a time in milliseconds.
  public static Value.Builder makeValue(Date date) {
    return Value.newBuilder().setTimestampMicrosecondsValue(date.getTime() * 1000L);
  }

  
Make a key from the specified path of kind/id-or-name pairs. The id-or-name values must be either Key, String, Long, Integer or Short. The last id-or-name value may be omitted, in which case an entity without an id is created (for use with automatic id allocation).
  public static Key.Builder makeKey(Object... elements) {
    Key.Builder key = Key.newBuilder();
    for (int pathIndex = 0; pathIndex < elements.lengthpathIndex += 2) {
      PathElement.Builder pathElement = PathElement.newBuilder();
      Object element =  elements[pathIndex];
      if (element instanceof Key) {
        key.addAllPathElement(((Keyelement).getPathElementList());
        // We increment by 2, but since we got a Key argument we're only consuming 1 element in this
        // iteration of the loop. Decrement the index so that when we jump by 2 we end up in the
        // right spot.
        pathIndex--;
      } else {
        String kind;
        try {
          kind = (Stringelement;
        } catch (ClassCastException e) {
          throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass());
        }
        pathElement.setKind(kind);
        if (pathIndex + 1 < elements.length) {
          Object value = elements[pathIndex + 1];
          if (value instanceof String) {
            pathElement.setName((Stringvalue);
          } else if (value instanceof Long) {
            pathElement.setId((Longvalue);
          } else if (value instanceof Integer) {
            pathElement.setId((Integervalue);
          } else if (value instanceof Short) {
            pathElement.setId((Shortvalue);
          } else {
            throw new IllegalArgumentException(
                "Expected string or integer, got: " + value.getClass());
          }
        }
        key.addPathElement(pathElement);
      }
    }
    return key;
  }

  
Return a map of property name to java object. Looses microseconds on timestamp values, meaning and indexing information.

Parameters:
entity the entity to search for the property.
Returns:
The property value, or null if it's not found.
  public static Map<StringObjectgetPropertyMap(EntityOrBuilder entity) {
    Map<StringObjectresult = new HashMap<StringObject>();
    for (PropertyOrBuilder property : entity.getPropertyList()) {
      Object value;
      if (property.getMulti()) {
        List<Objectvalues = new ArrayList<Object>(property.getValueCount());
        for (ValueOrBuilder subValue : property.getValueList()) {
          values.add(getValue(subValue));
        }
        value = values;
      } else {
        value = getValue(property.getValue(0));
      }
      result.put(property.getName(), value);
    }
    return Collections.unmodifiableMap(result);
  }

  
Convert a Value to a Java Object. Ignores meaning. Looses microseconds on timestamp values, meaning and indexing information.
  public static Object getValue(ValueOrBuilder value) {
    if (value.hasBooleanValue()) {
      return value.getBooleanValue();
    }
    if (value.hasIntegerValue()) {
      return value.getIntegerValue();
    }
    if (value.hasDoubleValue()) {
      return value.getDoubleValue();
    }
    if (value.hasTimestampMicrosecondsValue()) {
      return new Date(value.getTimestampMicrosecondsValue() / 1000L);
    }
    if (value.hasKeyValue()) {
      return value.getKeyValue();
    }
    if (value.hasBlobKeyValue()) {
      return value;  // Returning value directly to avoid loosing type info.
    }
    if (value.hasStringValue()) {
      return value.getStringValue();
    }
    if (value.hasBlobValue()) {
      return value.getBlobValue();
    }
    if (value.hasEntityValue()) {
      return value.getEntityValue();
    }
    return null;
  }
New to GrepCode? Check out our FAQ X