Skip to content

Instantly share code, notes, and snippets.

@junjun-zhang
Created September 26, 2017 18:56
Show Gist options
  • Save junjun-zhang/8362c66d20f48073c644bd02062922c0 to your computer and use it in GitHub Desktop.
Save junjun-zhang/8362c66d20f48073c644bd02062922c0 to your computer and use it in GitHub Desktop.
POC: Implement Directed Acyclic Graph DB with Version Control using Elasticsearch
# 1.create_nodes_for_dag.1.sh
curl -XPUT 'localhost:9200/.d.dag.data.a/A/a1.1' -H 'Content-Type: application/json' -d'
{
"type": "A",
"id": "a1",
"p_id": [ ],
"a_id": [ ],
"data": { }
}
'
curl -XPUT 'localhost:9200/.d.dag.data.b/B/b1.1' -H 'Content-Type: application/json' -d'
{
"type": "B",
"id": "b1",
"p_id": [ "a1" ],
"a_id": [ "a1" ],
"data": { }
}
'
curl -XPUT 'localhost:9200/.d.dag.data.b/B/b2.1' -H 'Content-Type: application/json' -d'
{
"type": "B",
"id": "b2",
"p_id": [ "a1" ],
"a_id": [ "a1" ],
"data": { }
}
'
curl -XPUT 'localhost:9200/.d.dag.data.c/C/c1.1' -H 'Content-Type: application/json' -d'
{
"type": "C",
"id": "c1",
"p_id": [ "b1" ],
"a_id": [ "a1", "b1" ],
"data": { }
}
'
curl -XPUT 'localhost:9200/.d.dag.data.c/C/c2.1' -H 'Content-Type: application/json' -d'
{
"type": "C",
"id": "c2",
"p_id": [ "b2" ],
"a_id": [ "a1", "b2" ],
"data": { }
}
'
curl -XPUT 'localhost:9200/.d.dag.data.d/D/d1.1' -H 'Content-Type: application/json' -d'
{
"type": "D",
"id": "d1",
"p_id": [ "b1", "c1" ],
"a_id": [ "a1", "b1", "c1" ],
"data": { }
}
'
# 2.create_graph_member_lists_for_gdc.1.sh
curl -XPUT 'localhost:9200/.d.dag.rev.a/A/A.1' -H 'Content-Type: application/json' -d'
{
"m_ids": ["a1.1"]
}
'
curl -XPUT 'localhost:9200/.d.dag.rev.b/B/B.1' -H 'Content-Type: application/json' -d'
{
"m_ids": ["b1.1", "b2.1"]
}
'
curl -XPUT 'localhost:9200/.d.dag.rev.c/C/C.1' -H 'Content-Type: application/json' -d'
{
"m_ids": ["c1.1", "c2.1"]
}
'
curl -XPUT 'localhost:9200/.d.dag.rev.d/D/D.1' -H 'Content-Type: application/json' -d'
{
"m_ids": ["d1.1"]
}
'
# 3.create_alias_for_dag.1.sh
curl -XPOST 'localhost:9200/_aliases' -H 'Content-Type: application/json' -d '
{
"actions": [
{
"add": {
"index": ".d.dag.data.*",
"alias": ".d.dag.1",
"filter": {
"bool": { "minimum_should_match": 1,
"should": [
{ "terms": { "_id": { "index": ".d.dag.rev.a", "type": "A", "id": "A.1", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.b", "type": "B", "id": "B.1", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.c", "type": "C", "id": "C.1", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.d", "type": "D", "id": "D.1", "path": "m_ids" } } }
]
}
}
}
}
]
}
'
# 4.update_the_graph_to_version_2.sh
# update b1 from b1.1 to b1.2, just need to create b1.2 (we leave b1.1 unchanged)
curl -XPUT 'localhost:9200/.d.dag.data.b/B/b1.2' -H 'Content-Type: application/json' -d'
{
"type": "B",
"id": "b1",
"p_id": [ "a1" ],
"a_id": [ "a1" ],
"data": { "something": "changed in revsion 2 of b1 node" }
}
'
# update the member list for B node type by creating B.2
curl -XPUT 'localhost:9200/.d.dag.rev.b/B/B.2' -H 'Content-Type: application/json' -d'
{
"m_ids": ["b1.2", "b2.1"]
}
'
# create new c3 node
curl -XPUT 'localhost:9200/.d.dag.data.c/C/c3.1' -H 'Content-Type: application/json' -d'
{
"type": "C",
"id": "c3",
"p_id": [ "b1", "b2" ],
"a_id": [ "a1", "b1", "b2" ],
"data": { }
}
'
# update c1 from c1.1 to c1.2, just need to create c1.2 (we leave c1.1 unchanged)
curl -XPUT 'localhost:9200/.d.dag.data.c/C/c1.2' -H 'Content-Type: application/json' -d'
{
"type": "C",
"id": "c1",
"p_id": [ "b1" ],
"a_id": [ "a1", "b1" ],
"data": { "something": "changed in revsion 2 of c1 node" }
}
'
# update member list for C node type by create C.2
# note that c2.1 is excluded comparing what's in C.1, this is how deletion is handled
curl -XPUT 'localhost:9200/.d.dag.rev.c/C/C.2' -H 'Content-Type: application/json' -d'
{
"m_ids": [ "c1.2", "c3.1" ]
}
'
# create new d2 node
curl -XPUT 'localhost:9200/.d.dag.data.d/D/d2.1' -H 'Content-Type: application/json' -d'
{
"type": "D",
"id": "d2",
"p_id": [ "b2", "c3" ],
"a_id": [ "a1", "b1", "b2", "c3" ],
"data": { }
}
'
curl -XPUT 'localhost:9200/.d.dag.rev.d/D/D.2' -H 'Content-Type: application/json' -d'
{
"m_ids": [ "d1.1", "d2.1" ]
}
'
# 5.create_alias_for_dag.2_and_more.sh
curl -XPOST 'localhost:9200/_aliases' -H 'Content-Type: application/json' -d '
{
"actions": [
{
"add": {
"index": ".d.dag.data.*",
"alias": ".d.dag.2",
"filter": {
"bool": { "minimum_should_match": 1,
"should": [
{ "terms": { "_id": { "index": ".d.dag.rev.a", "type": "A", "id": "A.1", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.b", "type": "B", "id": "B.2", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.c", "type": "C", "id": "C.2", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.d", "type": "D", "id": "D.2", "path": "m_ids" } } }
]
}
}
}
}
]
}
'
# note that we can also create another alias called '.d.dag.latest'
curl -XPOST 'localhost:9200/_aliases' -H 'Content-Type: application/json' -d '
{
"actions": [
{
"add": {
"index": ".d.dag.data.*",
"alias": ".d.dag.latest",
"filter": {
"bool": { "minimum_should_match": 1,
"should": [
{ "terms": { "_id": { "index": ".d.dag.rev.a", "type": "A", "id": "A.1", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.b", "type": "B", "id": "B.2", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.c", "type": "C", "id": "C.2", "path": "m_ids" } } },
{ "terms": { "_id": { "index": ".d.dag.rev.d", "type": "D", "id": "D.2", "path": "m_ids" } } }
]
}
}
}
}
]
}
'
# to simultaneously access all nodes including their historic revisions, we can create
# an alias called '.d.dag.all' for which we will take out the 'filter'
curl -XPOST 'localhost:9200/_aliases' -H 'Content-Type: application/json' -d '
{
"actions": [
{
"add": {
"index": ".d.dag.data.*",
"alias": ".d.dag.all"
}
}
]
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment