Currently, I am working on the migration mentioned in the title line. Problem is application configuration that is kept in registry has a tree like structure, for example:
X
|->Y
|->Z
|->SomeKey someValue
W
|->AnotherKey anotherValue
and so on.
How can I model this structure in SQLite (or any other DB)? If you have experience in similar problems, please send posts. Thanks in advance.
Baris, this structure its similar to a directory/file structure.
You can model this with a simple parent<>child relationship on the directories and key value pairs relatade to the directory.
Something like
Directory:
id integer auto_increment;
name string not null;
parent_id integer not null default 0;
Property:
id integer auto_increment;
key string;
value string;
directory_id integer not null;
With this you can address the root directories searching for directories with parent_id=0, child directories by looking at WHERE parent_id=someid and for properties on that looking for directory_id=someid.
Hope this helps :)
Representing hierarchies in a relational database is pretty easy. You just use self-references. For example, you have a category table that has a field called ParentCategoryId that is either null ( for leaf categories) or the id of the parent category. You can even setup foreign keys to enforce valid relationships. This kind of structure is easy to traverse in code, usually via recursion, but a pain when it comes to writing sql queries.
One way around this for a registry clone is to use the registry key path as the key. That is, have an entry where Path is "X/Y/Z/SomeKey" and Value is "someValue". This will query easier but may not express the hierarchy the way you might like. That is, you will only have the values and not the overall structure of the hierarchy.
The bottom line is you have to compromise to map a hierarchy with an unknown number of levels onto a relational database structure.
Self-referencing tables mentioned by previous posters are nice when you want to store an hierarchy, they become less attractive when you start selecting leaves of the tree.
Could you clarify the use-case of retrieving data from the configuration?
Are you going to load the whole configuration at once or to retrieve each parameter separately?
How arbitrary will be the depth of the leaf nodes?
Related
I only found the way to get children entities by parent one.
But is there any way to retrieve the list of parent entities (by some parent entity's properties filter) with their children in single call?
(Pseudo query):
SELECT * FROM parents (with children) WHERE parent.property1=...
Result should be like this:
- (parent1, child1, child2)
- (parent2, child3, child4)
....
This is not possible if you store the parents and children as separate entities. However, if the children for a parent is expected to be small, then you can consider storing them as embedded struct slice. Then as you query the parent entities and retrieve them you will have access to their children.
The short answer is no.
Some background
Entities in a Datastore mode database form an ancestor path, which means a hierarchically structured space, similar to the directory structure of a file system.
Workarounds:
Depending on your needs there are a couple things that you could do:
You could retrieve each parent and their children individually in multiple calls and then combine all the parts together.
You could use Special query types, such as kindless queries, which is a query with no kind and no ancestor filter that retrieves all of the entities of an application from Datastore, so you can gather the needed information.
Not sure this qualifies as an answer but I have the same scenario and below are the two options I am considering.
The scenario I am in is an Employer kind and an Employee kind. The Employee is a child of Employer but the same Employee can be part of multiple Employers.
I need to find all Employers a specific Employee is part of.
Option 1 (the one I think I will go with)
Have a third entity for a reverse lookup. The key will be the Employee's last part of the key with an additional property to hold the list of Employers' keys.
Every time an Employer/Employee relationship changes I will also update the reverse lookup kind.
Option 2
Have the last part of the Employee's key saved separately in the same entity as an indexed property so that I can search by it (and then able to find each of those entities' parent key).
How to add multiple node to relations here is my query
MATCH (n:Customer{name:"motoM"})-[:RECENT {default:TRUE}]-(l:Location{name:"Adugodi"}) return l how to write a query to add one more "location" node to the relation "recent" if location node is not found and setting default to true to newly created realtion
What about this?
MATCH (n:Customer{name:"motoM"})-[:RECENT {default:TRUE}]-(l:Location{name:"Adugodi"})
MERGE (n)-[:RECENT]->(l2:Location)
ON CREATE SET l2.default = true
RETURN l, l2
The direction needs to be specified so I made it up, but it might need to go the other way.
Well, I don't know if I understood what you were looking for, but this might help you :)
Try with this query:
MATCH (n:Customer{name:"motoM"})-[r:RECENT {default:TRUE}]-(:Location{name:"Adugodi"})
CREATE (l2:Location{name:"Wherever You need"})
With r,n,l,l2
Set r.default = false
With n,l2
CREATE (n)-[r2:RECENT{default:TRUE}]->(l2)
I'm using Withto make the query easier to read, but you can do it in a single query.
In fact, I think your problem is your Graph model.
You should probably do something like a Customer node, related to Location nodes with a "VISITED" relation, and when you create your VISITED relation, you set date property to timestamp. Then, when you get your relations, you can simply compare timestamps to get the closest one, and you know which one is the one your need. Also, if you need a default property, set it on the node, it'll be easier to match.
Tell me if you need a code example for match, create and set data with this graph model.
We heavily use the tree doctrine extension in our zf2 project - with some big tree data structures. We know that inserts and updates in a nested set are expensive. We also know that the tree plugin uses the "root" column to find out which tree shall be updated.
Yesterday I read the tree documentation again and found:
"Support for multiple roots in nested-set"
What does it mean and how does it work? I couldn't find any documentation for this feature.
Our hope would be that we could define a second root item of a lower branch of a big tree so that inserts and updates into this lower branch will not affect the whole tree but only this branch. Is it possible?
yes it is possible, tree root branches will be separated by level 0 nodes, see mapping example of TreeRoot column there should be examples for all mapping types to map treeRoot column. The column must be of the same type as ID, it does not support a ManyToOne relation for now, but there is a plan someday to support it.
root1
child
root2
child
child2
When updating or inserting any child on root2 or root1 branch, it will affect only that certain branch. Also note that, tree is still not concurrently safe, you have to manage locking yourself, see documentation reference here.
The doc directory contains most of the information given here.
I am thinking about using this STL-like tree library for C++ http://tree.phi-sci.com/ to store hierarchical data (think organisation chart).
In my case the tree only contains the structure, the 'payload' of each node is stored elsewhere. So it will probably end up as a tree<int> or a tree<simple_class_containing_a_couple_of_ints>
I would like to find the best way to persist the tree. To be more specific I would like to find the best way to persist the tree to a SQL database so it can be loaded back into the application on startup.
So my question is: How can I persist a tree contained in a tree.hh container to a SQL database?
Note: It is not necessary to store it as a tree structure in the database (i.e. no need for nested set, adjacency list). There is no need to query the database as the whole tree will be loaded into memory.
UPDATE:
I have found this class as an alternative to tree.hh here: http://stlplus.sourceforge.net/stlplus3/docs/ntree.html
I cannot comment yet on any performance differences, but it mostly implements what I need and has a persistence class (sorry no link as not enough reputation) that I can dump to a BLOB. I haven't entered this as an answer yet because I am still interested in any alternative solutions.
I would persist each node in one SQL table (one row per node) and perhaps each node -> sibling relation in another table.
I am not sure SQL is the best way to persist. You could consider using JSON.
I write a small database project for handling payroll records. These payroll records will be put into a tableview object for viewing. How can I search a particular record in a TableView ? Any idea, please help me.
If you are using model / view paradigm, you may consider using a "match()" method, located in the QAbstractItemModel class. For example, see this code snippet:
model->match(model->index(0,0),
Qt::DisplayRole,
pattern, -1,
Qt::MatchContains | Qt::MatchRecursive );
This is a code, I use to locate a pattern string in the TreeView. The flags are set to locate those records, that have a "pattern" among their display role representation, the search is performed recursively through the tree (you don't need it in your TableView, I believe :) ).
A Proxy-Model can be plugged between your (source) model and the view(s) to filter the models data. Take a look at QSortFilterProxyModel which allows to sort models rows/columns. Providing it with the right regexp for the key it will match only one item if found. You can use the proxy-model like a usual model (e.g. check rowCount) so it will update automatically and can be used in other views.