Why are appengine database keys sorted differently than strings
I found this interesting, and am curious to know why db keys are ordered differently than strings ..
If y开发者_运维知识库ou test the following example in the Python development environment (true for server as well), you will see the following (somewhat unexpected) behaviour
str1 = 'agpyb21hbmNlYXBwchILEglVc2VyTW9kZWwY4-agBgw'
str2 = 'agpyb21hbmNlYXBwchILEglVc2VyTW9kZWwYkoWBBQw'
Result: str < str2
is TRUE
However, if we do the following
key1 = db.Key(str1)
key2 = db.Key(str2)
Result: key1 < key2
is FALSE
What you're comparing are string encoded keys, which have been passed through base64
in order to be safe for output as text. Base64 does not preserve string ordering. If you decode them using base64.urlsafe_b64decode
, they will compare in the order you expect.
They use a different collation in extraction operator. I guess the db.Key() has strong probabilities to bind to DB properties. So the < operator 'sees' different data, the first being (say) Ut8 source based, the second some weird db collation based, where the dash has specific meaning (unknown until we can get the specification). I've experienced some pain in the past feeding massive data across DB interfaces. But internationalization it's inherently complex.
精彩评论