Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding maximum length of an AppEngine key-name in the Java API

I am trying to figure out what the maximum length for an AppEngine key name is in the Java API.

This question has been asked in much less depth before:

How long (max characters) can a datastore entity key_name be? Is it bad to haver very long key_names?

and received two conflicting answers (with the one that seems less credible to me being the accepted one...)

@ryan was able to provide links to the relevant Python API source in his answer and I've been trying to find something similar in the Java API.

But neither Key.java, nor KeyFactory.java, nor KeyTranslator.java seem to enforce any limits on the name property of a key. So, if there is a limit, it is implemented elsewhere. KeyTranslator calls com.google.storage.onestore.v3.OnestoreEntity.Path.Element.setName(), which could be the place where the limit is implemented, but unfortunately I can't find the source for this class anywhere...

Specifically, I would like to know:

  • Is the 500 character limit a hard limit specifically imposed for key names somewhere in the backend or is it simply a recommendation that should be sufficient to make sure the full key-string never exceeds the 1500-byte limit of a short text property (long text properties with more bytes cannot be indexed, if I understand correctly).
  • If it is a hard limit:

    • Is it 500 characters or 500 bytes (i.e. the length after some encoding)?
    • Are the full 500 bytes/characters available for the name of the key or do the other key-components (kind, parent, app-id, ...) deduct from this number?
  • If it is a recommendation:

    • Is it sufficient in all cases?
    • What is the maximum I can use if all keys are located in the root of my application and the kind is only one letter long? In other words: Is there a formula I can use to calculate the real limit given the other key components?
  • Lastly, if I simply try to measure this limit by attempting to store keys of increasing length until I get some exception, will I be able to rely on the limit that I find if I only create keys with identical ancestor paths and same-length kinds in the same application? Or are there other variable-length components to a key that might get added and reduce the available key-name-length in some cases? Should it be the same for the Development and the Production Servers?

like image 550
Markus A. Avatar asked Mar 14 '23 12:03

Markus A.


1 Answers

The Datastore implements all of its validation in the backend (because it prevents successful operations in one client to fail in another). Datastore keys have the following restrictions:

  • A key can have at most 100 path elements (these are kind, name/id pairs)
  • Each kind can be at most 1500 bytes.
  • Each name can be at most 1500 bytes.

The 500 character limit has been converted into a 1500 byte limit. So places you've seen a 500 character limit before (like @ryan's answer in the linked question) are now 1500 bytes. Strings are encoded using UTF-8.

Importantly to answer some specifics from your question:

Are the full 500 bytes/characters available for the name of the key or do the other key-components (kind, parent, app-id, ...) deduct from this number?

No, the 1500 byte limit is per field.

like image 76
Patrick Costello Avatar answered Mar 17 '23 00:03

Patrick Costello