CosmicMind / Graph

Graph is a semantic database that is used to create data-driven applications.
http://cosmicmind.com
MIT License
873 stars 72 forks source link

Best way to search then insert or update #125

Closed lazarte closed 7 years ago

lazarte commented 7 years ago

Hi, what will be the best way to handle searching entity for specific key(unique identifier) if exist do the update for that entity if not then do the insert considering the asynchronous process so that UI will not be block.

Lets say for example I have 10000 documents from the api server each document have unique identifier and I want those documents to be inserted or updated on graph.

daniel-jonathan commented 7 years ago

Hey, this is a really good question. The easiest way to approach this code wise is to check for a given identifier before each operation, for example:

func insert(data: [String: Any]) {
    let graph = Graph()
    let search = Search<Entity>(graph: graph).for(types: "Document").where(properties: ("identifier", 1)

    guard 0 == search.sync().count else {
        return
    }

    // insert Entity ...
}

This approach is probably the most expensive approach as you are creating a new Graph instance and Search instance each time, as well as, searching against the datastore directly.

A better approach would be to do an initial search and store the identifier values in a locally cached object. Then while inserting, if the value doesn't exist in the cached identifiers, then add it there and insert a new Entity of type Document, otherwise, skip. You can save Graph in a transaction, which will ensure that you save everything or nothing, this can be done asynchronously. Are you familiar with an approach like this?

lazarte commented 7 years ago

Thank you @danieldahan. Yes the app is very stressful with the first approach, the second approach is much better.

daniel-jonathan commented 7 years ago

Feel free to share you solution if you want more tips :) Please reopen the issue if you need.