Decode the name or namespace from a UUID - uuid

Is it possible to decode the namespace or the name from a given UUID?
My idea is to generate the uuid with a particular namespace or name later and later retrieve it to check whether 2 UUIDs belong to the same namespace or name. Is this possible?

As stated in RFC4122, UUID3 and UUID5 namespaces and names are hashed (with either MD5 or SHA1), which means there is no other way to “decode” the namespace or name from a given UUID than bruteforce (that is the whole point of hash functions).
Compute the hash of the name space ID concatenated with the name.
RFC422 - 4.3 - Algorithm for Creating a Name-Based UUID
However you can directly compare the hashed namespaces and name to detect whether two UUIDs belong to the same namespace and have the same name, indeed. Here is an example in Python (using the standard uuid module):
import uuid
name = 'stackoverflow.com'
a = uuid.uuid5(namespace=uuid.NAMESPACE_DNS, name=name)
b = uuid.uuid5(namespace=uuid.NAMESPACE_DNS, name=name)
assert a == b
print(a)
print(b)
cd84c40a-6019-50c7-87f7-178668ab9c8b
cd84c40a-6019-50c7-87f7-178668ab9c8b

Related

Avoiding database queries in Anylogic

I would like to avoid costly repeated data base queries in Anylogic. I have seen the following thread in Stack Overflow What is the fastest way to look up continuous data on Anylogic (Java, SQL) where a simple three step answer is provided but I'm not sure what the second point of the three actually means:
Save all rows as instances of that class at model start-up into a map - you can use Origin/Destination as the key (use Anylogic's Pair object) and the class instance as the value
I have created a class that takes as inputs the information from each column of my database. I would now like to save each row as an instance of that class - is there an easy way to do this? I may be missing something simple as I'm new to Anylogic.
I'm also unsure of how to create a mapping, if anyone could add more detail to point 2 above I'd be very grateful!
this is effectively the best advice, you created the class, which is a great step, but now, one element of that class, will be used as the key... for example the name... for instance if your class has firstName as one variable and lastName as another variable, you will use a string that is the concatenation of firstName and lastName as your key. Of course any key is fine, assuming that it is unique for all your table. Also an integer as an id is ok too.
create a collection of type LinkedHashMap
Create a class (you did that)
Your collection will take as the key a String (first + last name) and as the value of the elment the class...
now, when you read your database you will have something like this:
for(Tuple t : yourQueryResults){
YourClass yc=new YourClass(t.get(db.var1),t.get(db.var2));
String totalName=t.get(db.first_name)+"_"+t.get(db.last_name);
yourCollection.put(totalName,yc);
}
Now every time you want to find someone with the a name, for example "John Doe", instead of making a query, you will do
yourCollection.get("John_Doe").theVarYouWant;
if you use an id instead of the name, you can set an int as the key, and then you will just do yourCollection.get(theId).theVarYouWant

How NamespaceManager and Query by key works together in objectify

I have two organisation in my datastore inside their own namespace. Lets say organisation1 present inside namespace1 and organisation2 present inside namespace2. I am retrieving organisation by its web-safe-key. lets say that web-safe-key of organisation1 is orgWebSafeKey1 and web-safe-key of organisation2 is orgWebSafeKey2. I am using following code to get an organisation:
NamespaceManager.set("namespace1");
Organisation organisation = (Organisation) ofy().load().key(Key.create(orgWebSafeKey1)).now();
above code works as I expected because organisation1 is present inside namespace1 and I am trying get that organisation in its namespace.
But if I just change the websafekey of the organisation then according to my expectaion below query should result "null" organisation because there is no organisation with key orgWebSafeKey2 inside namespace1. But practically it is giving me organisation2.
NamespaceManager.set("namespace1");
Organisation organisation = (Organisation) ofy().load().key(Key.create(orgWebSafeKey2)).now();
If the above query result is correct and expected according to objectify and datastore then can I assume that query by key works globally , across all the namespaces?
I also want confirmation that in this case Key.create(orgWebSafeKey2) will not change the namespace of the key? and query is running according to the namespace of the key not by NamespaceManager.set("namespace1")?
A Datastore Key contains the following components:
Project/App ID
Namespace
Entity Path (Ancestor Kind + ID/Name(zero or more), Final Entity Kind + ID/Name)
Since namespace is part of the key, lookup of an entity by Key always finds the right entity regardless of the namespace set by the NamespaceManager. In other words, a Key is a GUID that uniquely identifies an entity across all apps/projects.
Refer to the below link for more details/answers for your questions:
https://cloud.google.com/appengine/docs/java/multitenancy/multitenancy#Java_Using_namespaces_with_the_Datastore

datomic attribute with a colon prefix

Have 2 queries about datomic attributes.
1. If I know the attribute name (String), how do I check if the attribute is already defined or not in the schema?
2. Based on my experimenting with datomic, I see that datomic treats attributes with colon prefix and without colon prefix same. i.e if we create attributes named "foo" and ":foo", they are one and the same. Is this true? Is this a limitation?
I am using datomic with groovy.Below is the code used to create the attribute. Along with name, other params are input.
static def createAttribute(String name, String type, String description, Connection connection) {
List schema = [[
':db/id': Peer.tempid(':db.part/db'),
':db/ident' : name,
':db/valueType': type,
':db/cardinality': ':db.cardinality/one',
':db/doc': description,
':db.install/_attribute': ':db.part/db'
]]
connection.transact(schema).get()
And the query I use to verify attribute presence is
def attributeFor(String attributeName, Database db) {
db.entity(attributeName).get(':db.install/_attribute')
}
If I call "createAttribute" with "foo" as attribute name and "attributeFor" method ":foo" as attribute name, I get a result. i.e "foo" and ":foo" are treated same.
How can I create and query for attributes with name that contain a colon prefix?
Datomic attribute names are not Strings, they are edn keywords. The prefix colon is required (and always stored, regardless of whether you specify it in code.) The optionality of the colon when working from languages that do not support a name literal (e.g. Java or Groovy) is intended as a convenience.

v5 UUID. What is difference between UUID of the namespace and name

I am trying to generate a v5 UUID by referring to the function (http://www.ietf.org/rfc/rfc4122.txt) :
/* uuid_create_sha1_from_name -- create a version 5 (SHA-1) UUID
using a "name" from a "name space" */
void uuid_create_sha1_from_name(
uuid_t *uuid, /* resulting UUID */
uuid_t nsid, /* UUID of the namespace */
void *name, /* the name from which to generate a UUID */
int namelen /* the length of the name */
);
I have read the help, but I am still not clear on what is the difference between the 2nd(uuid_t nsid) and 3rd (void *name) parameters of above function?
Could someone explain me the above with an example ?
I would also like to understand what the below means in the RFC4122 link and does it have any significance to the 2nd parameter ?
/* Name string is a URL */
uuid_t NameSpace_URL = { /* 6ba7b811-9dad-11d1-80b4-00c04fd430c8 */
0x6ba7b811,
0x9dad,
0x11d1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
};
The name is the key that is unique to whatever thing you're generating uuid's for
The namespace is a constant UUID that identifies the context in which you're generating UUIDs
If you look at the RFC, you'll see section 4.3 defines these characteristics of a name-baesed UUID:
The UUIDs generated at different times from the same name in the
same namespace MUST be equal.
The UUIDs generated from two different names in the same namespace
should be different (with very high probability).
The UUIDs generated from the same name in two different namespaces
should be different with (very high probability).
If two UUIDs that were generated from names are equal, then they
were generated from the same name in the same namespace (with very
high probability).
These are all important properties to have in a name-based UUID. For example, let's say you and I are implementing HR systems for our respective companies. The systems are completely unrelated to one another, but because UUIDs are awesome, we're both using name-based UUIDs to identify employees. And because it's a rather obvious thing to do, we use employee names as the name from which the UUIDs are generated.
Without namespaces we would both create the same UUID for anyone named "John Smith"... but that'd be Bad (tm) since our systems are unrelated and we're dealing with different John Smiths. "So what," you say! ... but what happens when our companies merge next year and we have to combine our HR databases? Well, at that point we find ourselves merging database records that have the same ID and pretty soon the paychecks for every John Smith in the company are crossing in the mail and HR is handing us our pink slips.
To prevent this sort of thing from happening, the RFC specifies that we each independently choose a UUID to use as our namespace. Namespaces will typically be fixed and associated with a specific system in which UUIDs are being generated, so we'll probably just hardcode this as a constant in some configuration file somewhere. Thus, within my namespace (e.g. 87c9cdf7-101d-4c05-a89d-c7aaff3a3fcf) I can trust that the UUID I generate for John Smith will always be the same. But I can also count on it being different from any UUID you create since you'll be using a different namespace. And so if/when our systems merge, there won't be any issues.
Quoting from section 4.3 of the RFC, there is this step:
Allocate a UUID to use as a "name space ID" for all UUIDs generated from names in that name space; see Appendix C for some pre-defined values.
As far as I understand this, the aim of this namespace is to ensure that using the same name with the same meaning will result in the same UUID, whereas using the same name with a different meaning will result in a different UUID. The namespace UUID is concatenated with the actual name, and both are hashed together.
The code snippet about the 6ba7b811-9dad-11d1-80b4-00c04fd430c8 UUID comes from that section C. So when you use that as a namespace, you should use a URL as a name. For example, you'd call the function as
uuid_t result_uuid;
const char* url = "http://www.example.com/document.txt";
uuid_create_sha1_from_name(&result_uuid, NameSpace_URL, url, strlen(url));
In contrast to this, a UUID for the whole example.com domain would be created using
uuid_t result_uuid;
const char* domain = "example.com";
uuid_create_sha1_from_name(&result_uuid, NameSpace_DNS, domain, strlen(domain));

get_by_id() will not return model instance

I have a Model called Version that looks like this:
from google.appengine.ext import db
import piece
class Version(db.Model):
"A particular version of a piece of writing."
parent_piece = db.ReferenceProperty(piece.Piece, collection_name='versions')
"The Piece to which this version belongs."
note = db.TextProperty()
"A note from the Author about this version."
content = db.TextProperty()
"The actual content of this version of the Piece."
published_on = db.DateProperty(auto_now_add=True)
"The date on which the version was published."
I would like to access instances of Version via their IDs, using Version.get_by_id(), but this call always returns None. I can see in the Datastore Viewer that they have ID values, and in the debugger, I can query for them but not use them:
>>> for each_ver in version.Version.all():
... print each_ver.key().id()
...
34
35
36
31
32
>>> a = version.Version.get_by_id(34)
>>> type(a)
<type 'NoneType'>
I see that there are plenty of questions here where people are able to use get_by_id() effectively just as I wish, and they do not see the results that I am seeing.
Could the problem be that each Version instance is a child in an Entity Group rather than a root of an Entity Group? Each Version lives in an Entity Group that looks like Member->Piece->Version. If that is the problem, is there a way that I can refer to Version entity without using its entire key? If that is not the problem, can anyone tell me what I can do to make get_by_id() work as expected?
Could the problem be that each Version
instance is a child in an Entity Group
rather than a root of an Entity Group?
Yes. An entity's key includes the keys of any parent entities.
If that is the problem, is there a
way that I can refer to Version entity
without using its entire key?
No. An entity is uniquely identified only by its entire key, which includes the keys of all the parent entities. If you know the kinds of its parent entities, though, you can use db.Key.from_path to construct the key from the chain of IDs or key names.
I had your same problem but in ndb.Model and I found that I need to convert the ID to an int. So maybe using version.Version.get_by_id(int(34)) can solve your problem.

Resources