use of com.google.storage.onestore.v3.OnestoreEntity.Reference in project appengine-java-standard by GoogleCloudPlatform.
the class DataTypeTranslator method toKey.
static Key toKey(KeyOrBuilder proto) {
// Check that the reference contains elements first.
if (proto.getPathCount() == 0) {
throw new IllegalArgumentException("Invalid Key PB: no elements.");
}
AppIdNamespace appIdNamespace = toAppIdNamespace(proto.getPartitionId());
Key key = null;
for (PathElement e : proto.getPathList()) {
String kind = e.getKind();
key = new Key(kind, key, e.getId(), e.getIdTypeCase() == IdTypeCase.NAME ? e.getName() : null, appIdNamespace);
}
return requireNonNull(key);
}
use of com.google.storage.onestore.v3.OnestoreEntity.Reference in project appengine-java-standard by GoogleCloudPlatform.
the class QueryTranslator method convertToPb.
@SuppressWarnings("deprecation")
public static DatastoreV3Pb.Query convertToPb(Query query, FetchOptions fetchOptions) {
Key ancestor = query.getAncestor();
List<Query.SortPredicate> sortPredicates = query.getSortPredicates();
DatastoreV3Pb.Query proto = new DatastoreV3Pb.Query();
if (query.getKind() != null) {
proto.setKind(query.getKind());
}
proto.setApp(query.getAppIdNamespace().getAppId());
String nameSpace = query.getAppIdNamespace().getNamespace();
if (nameSpace.length() != 0) {
proto.setNameSpace(nameSpace);
}
if (fetchOptions.getOffset() != null) {
proto.setOffset(fetchOptions.getOffset());
}
if (fetchOptions.getLimit() != null) {
proto.setLimit(fetchOptions.getLimit());
}
if (fetchOptions.getPrefetchSize() != null) {
proto.setCount(fetchOptions.getPrefetchSize());
} else if (fetchOptions.getChunkSize() != null) {
proto.setCount(fetchOptions.getChunkSize());
}
if (fetchOptions.getStartCursor() != null) {
if (!proto.getMutableCompiledCursor().parseFrom(fetchOptions.getStartCursor().toByteString())) {
throw new IllegalArgumentException("Invalid cursor");
}
}
if (fetchOptions.getEndCursor() != null) {
if (!proto.getMutableEndCompiledCursor().parseFrom(fetchOptions.getEndCursor().toByteString())) {
throw new IllegalArgumentException("Invalid cursor");
}
}
if (fetchOptions.getCompile() != null) {
proto.setCompile(fetchOptions.getCompile());
}
if (ancestor != null) {
Reference ref = KeyTranslator.convertToPb(ancestor);
if (!ref.getApp().equals(proto.getApp())) {
throw new IllegalArgumentException("Query and ancestor appid/namespace mismatch");
}
proto.setAncestor(ref);
}
if (query.getDistinct()) {
if (query.getProjections().isEmpty()) {
throw new IllegalArgumentException("Projected properties must be set to " + "allow for distinct projections");
}
for (Projection projection : query.getProjections()) {
proto.addGroupByPropertyName(projection.getPropertyName());
}
}
proto.setKeysOnly(query.isKeysOnly());
Query.Filter filter = query.getFilter();
if (filter != null) {
// At this point, all non-geo queries have had their filters
// converted to sets of FilterPredicate objects; so this must be
// a geo query.
copyGeoFilterToPb(filter, proto);
} else {
for (Query.FilterPredicate filterPredicate : query.getFilterPredicates()) {
Filter filterPb = proto.addFilter();
filterPb.copyFrom(convertFilterPredicateToPb(filterPredicate));
}
}
for (Query.SortPredicate sortPredicate : sortPredicates) {
Order order = proto.addOrder();
order.copyFrom(convertSortPredicateToPb(sortPredicate));
}
for (Projection projection : query.getProjections()) {
proto.addPropertyName(projection.getPropertyName());
}
return proto;
}
use of com.google.storage.onestore.v3.OnestoreEntity.Reference in project appengine-java-standard by GoogleCloudPlatform.
the class KeyFactory method stringToKey.
/**
* Converts a {@code String}-representation of a {@code Key} into the {@code Key} instance it
* represents. Note that <code>
* str.equals(KeyFactory.keyToString(KeyFactory.stringToKey(str))
* </code> should evaluate to {@code true} for all strings returned by {@link
* KeyFactory#keyToString}.
*
* @param encoded The {@code String} representation of a {@code Key}.
* @return The {@code Key} that the given {@code String} represents.
* @throws IllegalArgumentException If the string cannot be parsed.
*/
public static Key stringToKey(String encoded) {
// Note: This must be consistent across languages. If it changes,
// make sure that the similar logic in Python changes as well.
// There are unit tests which also need to be changed.
int modulo = encoded.length() % 4;
if (modulo != 0) {
// Pad the string to a 4-byte boundary.
encoded += "====".substring(modulo);
}
byte[] decodedBytes;
try {
decodedBytes = base64Url().decode(CharMatcher.whitespace().removeFrom(encoded));
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Cannot parse: " + encoded, ex);
}
Reference reference = new Reference();
boolean parsed = reference.parseFrom(decodedBytes);
if (!parsed) {
throw new IllegalArgumentException("Could not parse Reference");
}
// an isInitialized check.
return KeyTranslator.createFromPb(reference);
}
use of com.google.storage.onestore.v3.OnestoreEntity.Reference in project appengine-java-standard by GoogleCloudPlatform.
the class KeyTranslator method createFromPb.
public static Key createFromPb(Reference reference) {
// Check that the reference contains elements first.
Key parentKey = null;
Path path = reference.getPath();
List<Element> elements = path.elements();
if (elements.isEmpty()) {
throw new IllegalArgumentException("Invalid Key PB: no elements.");
}
AppIdNamespace appIdNamespace = new AppIdNamespace(reference.getApp(), reference.hasNameSpace() ? reference.getNameSpace() : "");
for (Element e : elements) {
String kind = e.getType();
if (e.hasName() && e.hasId()) {
throw new IllegalArgumentException("Invalid Key PB: both id and name are set.");
} else if (e.hasName()) {
parentKey = new Key(kind, parentKey, Key.NOT_ASSIGNED, e.getName(), appIdNamespace);
} else if (e.hasId()) {
parentKey = new Key(kind, parentKey, e.getId(), null, appIdNamespace);
} else {
// Incomplete key. Neither id nor name are set.
//
// TODO: consider throwing an exception if this is not the last
// path element.
parentKey = new Key(kind, parentKey, Key.NOT_ASSIGNED, null, appIdNamespace);
}
}
return parentKey;
}
use of com.google.storage.onestore.v3.OnestoreEntity.Reference in project appengine-java-standard by GoogleCloudPlatform.
the class KeyTranslator method updateKey.
public static void updateKey(Reference reference, Key key) {
// Can only have id or name, not both.
if (key.getName() == null) {
Path path = reference.getPath();
key.setId(path.getElement(path.elementSize() - 1).getId());
}
}
Aggregations