OrientDB use cluster position as an identifier - orientdb

I would like to have an auto-increment identifier on my persisted orientdb entities.
The id in an orient database is the RecordID. It is composed of two parts (cluster:position)
cluster, is the cluster id.
position, is the absolute position of the record inside a cluster.
I saw in the documentation that by default OrientDB creates 1 cluster per class.
Since there is one entity class per cluster.
Is it possible to use only the RecordID position as an identifier (unique for a class) for my objects ?

Absolutely yes. The RID is never recycled, even on record deletion and represents the record as unique object in a database.

Related

Best Practice To Manage One To Many Insertions on Entity Framework Core

We're currently developing a .Net Core console application which consumes a web service to fetch data and inserts it to a database asynchronously.
Web service returns an object - to make the problem simple let's call it Orders - has a Country, Date and Ordered By fields. In our database we have a Countries and Orders table with 1 to Many relationship.
When the app starts it stores the country list in a static List property. As a new Order from web service is returned, country field is checked if exist in the current List property to get Id field from it. If it doesn't exist, a new record is inserted to the database and List property is updated with a helper method. This scenario works without any problem in single thread. However when we make the web service requests and update the database asyncronously, each async Task updates List property independently and that causes multiple country values with the same name.
Is there a best practice to keep country list updated only when a new record is inserted and prevent inserting the same values by different Tasks?
Kind regards
Is there a best practice to keep country list updated only when a new record is inserted and prevent inserting the same values by different Tasks?
Yes. Put a unique index on the Country entity's Name property. Then when you attempt to add a duplicate Country, it will fail and you can retrieve the ID of the existing country instead.

The difference between referenced relation and foreign key?

I think that referenced relation is one record has a property which value is the record id of the other record, at the same time, foreign key is that one record has the primary key of the other record. Why the doc2.1.x emphasize that the referenced relation avoid the costly join operation?
OrientDB manages relations as physical links to records, assigned only once when the edge is created. OrientDB not use JOIN. Instead, use the links that has a relationship managed by storing the RID target in the record source. It 'very similar to store a pointer between two objects in memory. An edge connects two vertices and must have: a unique identifier, links vertex incoming, outgoing link vertex and label that defines the type of connection.
This is a little example:
Hope it helps

Do classes necessarily have to exist prior to record insertion?

I'm coming from Neo4j and evaluating OrientDB, and I had a quick question about classes - do they have to exist prior to inserting a record? That is, in Neo4j there's the 'MERGE' command, which will update or create a node if it doesn't exist. Classes seem roughly equivalent to Neo4j's labels, and if a label doesn't exist when performing a MERGE, it will be created. Is there similar functionality in OrientDB? Currently when I try to insert a record whose class doesn't exist, OrientDB throws an exception, "Class SOME_CLASS not found in database".
I've been reading through the docs trying to get a handle on the various data models available, but I can't find anything explicit on this issue. One thing I tried was to add a cluster, and then insert a record with a class that does not exist. This worked, and in OrientDB Studio, under 'DB', I see the cluster with number of records equal to '1'; however, the class of that new record does not appear under 'Schema'.
If dynamic class creation of this sort isn't possible, is it acceptable to check if a class exists in the schema, and if not, create it, and then proceed with creating the record? A different question is, if it's acceptable, is it good to do this, or should I always define the schema manually?
If you use one of the CREATE commands, then the object is placed in a default class; for example:
CREATE VERTEX
Created vertex 'V#9:0 v1' in 0.047000 sec(s).
(In this case, the class is V.)
And of course if you use the INSERT INTO ... form, then you have to specify a class.
So perhaps your first question boils down to whether it is possible to change the class of an OrientDB vertex or edge.
It is possible to change the parent class of a vertex -- see
http://orientdb.com/docs/2.1/SQL-Move-Vertex.html -- but there are important caveats.
To test whether a class exists programmatically, see e.g. this SO entry:
Check if class exists or not in orientdb
This gives a Java example, but a similar approach is possible using other supported languages.
As to the wisdom of changing the class of an entity dynamically -- perhaps the safe answer is that if you can achieve whatever you want using a property label, then use labels.

Can a Orion Context Broker entity have two attributes with the same name but different type?

Can a Orion Context Broker entity have two attributes with the same name but different type?
If yes, is it controlled by Orion? Is an error returned when such an entity is created?
If no, what happens when a convenience operation tries to get the value of one of the 2 attributes (AFAIK, the attribute type is not passed in the operation).
From Orion 0.17.0 on, type is no longer used to identify an attribute. Thus, attributes are identified by name plus (optionally) metadata ID. I will assume version >=0.17.0 in the rest of this answer.
Orion doesn't control violation of that rule when processing operations to create entities or append attribute on existing entities. In those cases, only one instance of the attributes with the same identification is taken an stored in the DB, the others are ignored. It is not recommended at all that a client do such kind of operations (in the future, Orion may check that condition and return an error to the client).
Taking account the above paragraph and regarding what happens when a convenience operation tries to get the value of one of the 2 attributes (AFAIK, the attribute type is not passed in the operation)? note that situation cannot happend. I mean, at Orion DB will never store two attributes with the same identification associated to the same entity.
Some additional comment regarding metadata ID: I don't recommend the use of metadata ID as any potential ID can be included in the name and you will get your client much simpler, e.g. you don't need and attribute with name=temperature and id=outside if you use name=temperature::outside or any other namespacing technique.

Grails and MongoDb (persistent instance deletion)

If I have a Grails application which I have used to persist some domain objects to my database (MongoDb in this case but probably does not matter), and I modify my domain class, say I add some more properties or take some properties away. Now, will the modified version of the application with the newer version of the domain class still recognize the already persisted old version of the domain instances?
More importantly if I now restart the Grails application with the new version of the domain class, will it delete(or do anything to) the already persisted old versions of the domain objects? I am trying to chase a similar issue I am facing but I am on a team so I'm not sure if the application deleted the old objects from the db (which I don't think so) or did some human system user.
The way in which Grails handles the database depends on the value of the dbCreate property in DataSource.groovy.
If you declare
dataSource {
dbCreate = "update"
}
then any additional properties you add to your domain class will be added as columns in your database. Note that this will not delete any columns that already exist. So say you had a property called x and wanted to replace it with a property called y, this would create a y column in the database but the x column would still remain.
Here a list of the possible values and behaviors for dbCreate
create - Drops the existing schema. Creates the schema on startup, dropping existing tables, indexes, etc. first.
create-drop - Same as create, but also drops the tables when the application shuts down cleanly.
update - Creates missing tables and indexes, and updates the current schema without dropping any tables or data. Note that this can't properly handle many schema changes like column renames (you're left with the old column containing the existing data).
validate - Makes no changes to your database. Compares the configuration with the existing database schema and reports warnings.
any other value - does nothing
Hope this helps