I need to find a way that a set of values will always generate the same ID and it needs to be unique.
The main reason for this is to speed up queries where before I needed to do a lot of joins, to only one comparision. I know my domain and sadly the values are not closed (i.e., they can be about anything).
I tryied with hash, but by definition, hash functions don’t guarantee uniquiness. Is there a way?
EDIT: Some more context
I am working in a system that correlate Events coming from different set of devices.
The correlation is calculated through some of the Events attributes:
- From where it came from.
- What kind of event it is.
- Values that come from it and that differs the same kind of events in different groups.
For example, think of something like this. Imagine my device is a refrigerator. It sends events about the temperature and food that are inside of it. So, imagine we receive events in this order:
Event1: {type: temperature, values: [{temperature: -1]}
Event2: {type: food, values: [{group: vegetable, name: brocollis, quantity: 2, weight: 0.1]}
Event3: {type: food, values: [{group: vegetable, name: lettuce, quantity: 1, weight: 0.1]}
Event4: {type: temperature, values: [{temperature: -5]}
All events of type temperature must be correlated, but of type food the correlation is also given from the its name and group. I.e., the number of correlation identifiers is variable.
This correlations are saved in a structure like this
@Entity
public class EventCorrelation {
@Id @GeneratedValue
@Audit
private Long id;
@ElementCollection
@CollectionTable(
name = "evt_corr_extra_id",
joinColumns = @JoinColumn(name = "correlation_id"))
@Column(name = "extra_id")
@LazyCollection(LazyCollectionOption.TRUE)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Collection<String> identifiers;
// getters/setters
}
It’s inside the identifiers array that I save the values that identify the correlation. I want to speed up the query that search for an event correlation and my first idea was to create an unique id from the correlation identifiers.
I have a knowledge database that knows that from a given event, certain attribute is a correlation identifier.
Any tips?
One possible solution is to convert your values to Strings and concatenate them using some kind of reserved delimiter, making sure to preserve ordering. The String becomes your unique id.
For example, the values 1, “Bob”, 7383.234, “{asdf}”, and 2013-01-08 would have the following unique id:
This is a kind of memoization technique.