create UUID from MostSignificantBits and LeastSignificantBits - java

As we know that we can get MostSignificantBits of UUID using method getMostSignificantBits() and LeastSignificantBits of UUID using method getLeastSignificantBits(). But , How to get the Original UUID(reverse process) if MostSignificantBits and LeastSignificantBits are known ?

you can use the UUID Constructor
UUID(long mostSigBits, long leastSigBits)
which constructs a new UUID using the specified data.

Related

How do I use Jooq insert ... returning in MySQL without code generation?

I've been trying to use insert...returning in MySQL with the DSL-based table definition (I'm not using the code generation) and my returned record is always null. Based on reading, I need to specify the identify column in the table definition, but I have no idea how!
Record recordKey = create.insertInto(table("modulerecords"),
field("id"),
field("module_id"),
field("created_date"),
field("created_by"),
field("state"),
field("tag_id"),
field("start_time",Timestamp.class),
field("kill_time", Timestamp.class),
field("feed_guid")
)
.values(null, moduleId, currentTimestamp(),
userId, state, tagId,
new Timestamp(startTime),
new Timestamp(killTime), feedGuid)
.returning(field("id"))
.fetchOne();
The field "id" is auto_increment primary key in the database, but recordKey is always null.
As of jOOQ 3.14, this is possible by specifying the field's datatype as being an identity, which can be done using SQLDataType.INTEGER.identity(true).
So for example, if you had a table with an auto-generating integer id and a string name, you would call:
int id = DSL.using(connection, MYSQL_5_7)
.insertInto(
table("myTable"),
field("name", String.class))
.values("John Smith")
.returning(field("id", SQLDataType.INTEGER.identity(true)))
.fetchAny(field("id", Integer.class))
So for your example, you would do
Record recordKey = create.insertInto(table("modulerecords"),
field("id"),
field("module_id"),
field("created_date"),
field("created_by"),
field("state"),
field("tag_id"),
field("start_time",Timestamp.class),
field("kill_time", Timestamp.class),
field("feed_guid")
)
.values(null, moduleId, currentTimestamp(),
userId, state, tagId,
new Timestamp(startTime),
new Timestamp(killTime), feedGuid)
.returning(field("id", SQLDataType.INTEGER.identity(true)))
.fetchOne();
See this Github comment for more background.
It is highly recommended you use the code generator to provide all the meta information to the DSL API. You can, of course, not use the code generator and still use the internal APIs that the code generator would otherwise use. Instead of creaating your table and field references using the plain SQL API, you'd have to create a TableImpl subclass and override / implement all the relevant methods.
Or, you just use the code generator.

How to create a UUID from a value

In my database, I have a uuid 67b26616-943e-49e0-a70c-8e9ddcba1baa. In my code, I want to query the database and want to pass the above uuid. How could I create a type UUID from a value? I saw the following function but I dont know which bits of my value goes where.
public UUID(long mostSigBits,
long leastSigBits)
You can create UUID this way:-
UUID uid = UUID.fromString("67b26616-943e-49e0-a70c-8e9ddcba1baa");
System.out.println("UUID value is: "+uid);

MongoiDB use a String for _id

I'd like to use a unique string per collection to id a doc. I'm using Scala and Casbah but can also use Java if needed.
I know I should use Casbah collection.createIndex but I don't understand the scaladocs.
If my case class is :
case class GroupParams (
_id: String,
//groupId: String,
testPeriodStart: DateTime, // ISO8601 date
variants: Seq[String], //["17","18"]
testPeriodEnd: Option[DateTime])
and I will always use the _id to reference a particular document (no need for _id: ObjectId).
I don't care about sorting/ordering since these will only be accessed as individual docs, never cursored through. There seems no reason to have the overhead of another index on the default _id: ObjectId.
How to I create the index on the collection with _id: String using Casbah? If I should create a new index and leave the default alone can you show how to do this?
Mongo automatically creates index for _id field for all types (ObjectId, String or whatever you want) - mongo indexes
Mongo automatically generate a _id to your index in the collection,
if you want to insert String to the _id you can convert a String to objectId like this : ObjectId.Parse(myString))
See more in the MongoDB API

How to generate a random string for ID/Name of an Entity instead of a number?

I'm working with the low-level datastore API. I've created an entity like this:
Entity entity = new Entity("Error");
entity.setProperty("description", "foo");
In the datastore viewer, I ses this:
Key Write Ops ID/Name description
----------------------------------------------
ahN0c... 4 259 foo
So the ID/Name field will be generated for me automatically since I'm not supplying anything in the Entity constructor. It generates an "ID" instead of a "Name", which is a number rather than an opaque string (like the "Key" value).
Is there a way to have the datastore generate a random "Name" instead of an "ID" for the Entity's "ID/Name" field?
I ask because if I share this ID with third parties, they could start to figure out roughly how many Error instances I have in my system. I'd rather give them an opaque string for the lookup ID, similar to what's in the auto-generated "Key" field. But I don't see a way to do this.
Thanks
For a similar task I used UUID to create a random string.
String uuid = UUID.randomUUID().toString();
You can use com.google.appengine.api.datastore.KeyFactory, combining the answer from #Devolus, it would look like
final Key key = KeyFactory.createKey("Error", UUID.randomUUID().toString());
final Entity e = new Entity(key);
You could even pass around the String representation of your Entitie's key via KeyFactory.keyToString(key) , may be after an encrypting depending on your security needs.

JPA timestamp column and net.sf.json result from query

Inside my entity class I have a column of type timestamp:
#Column(name = "TESTD")
#Temporal(TemporalType.TIMESTAMP)
private Date test_date;
Inside my session bean I'm creating a select query which return a resultList, and then I'm creating from it a json object:
Query query = em.createNamedQuery("findAllTest");
List<entityClass> results = query.getResultList();
JSONSerializer.toJSON((List)results ,jsonConfig);
when creating the json object I want the timestamp column to be formatted (and not to return as object). How can this be done? how can I cast/format the timestamp column according to the date format I want? what is the best way to do this?
I guess you're using json-lib, based on the code sample, and I've never used it, but the javadoc shows that JsonConfig provides the following method:
public void registerJsonValueProcessor(Class propertyType,
JsonValueProcessor jsonValueProcessor)
Registers a JsonValueProcessor.
[Java -> JSON]
So I guess you could use that method, and register a processor for Calendar.class that would transform the Calendar object into a String using the format you want..
I create a second transient getter that returns the date as a String formatted how I want. It's a bit if a hack but works.

Categories

Resources