我试验了在OrientDB图表中存储DNS层次结构的想法,并且无法找到为给定域添加新顶点的适当位置。
我从这个结构开始:
CREATE CLASS Zone EXTENDS V
CREATE CLASS Subdomain_of EXTENDS E
CREATE VERTEX Zone CONTENT {name: '.'} #12:0
CREATE VERTEX Zone CONTENT {name: 'com'} #12:1
CREATE VERTEX Zone CONTENT {name: 'net'} #12:2
CREATE VERTEX Zone CONTENT {name: 'org'} #12:3
CREATE VERTEX Zone CONTENT {name: 'example'} #12:4
CREATE VERTEX Zone CONTENT {name: 'www'} #12:5
CREATE EDGE Subdomain FROM #12:1 TO #12:0
CREATE EDGE Subdomain FROM #12:2 TO #12:0
CREATE EDGE Subdomain FROM #12:3 TO #12:0
CREATE EDGE Subdomain FROM #12:4 TO #12:1
CREATE EDGE Subdomain FROM #12:5 TO #12:4
我想创建一个保存的函数,它将域名字符串作为输入,并将相关的子域添加到图表中。
想象一下,我想添加域" mail.example.com"。该函数需要先搜索以查看路径.com.example.mail是否存在 - 如果不存在,则应检查.com.example并为" mail"添加新的顶点。具有子域边缘的"示例"顶点。
有没有办法根据特定路径搜索叶子节点?类似于Neo4j / Cypher语法的东西:
MATCH
(:Zone {name: ‘.’})<-[Subdomain]-(:Zone {name: ‘com’})<-[:Subdomain]-(a:Zone {name: ‘example’})
RETURN (ID(a))
谢谢,
查尔斯
答案 0 :(得分:1)
使用此Java函数应添加新顶点(如果尚未存在)。
public class DomainInternet {
static final String REMOTE = "remote:localhost/";
static final String NOMEDB = "domain"; // 2.1.9 community
static final String CURRENTPATH = REMOTE + NOMEDB;
public static void main(String[] args) {
OrientGraphNoTx g = new OrientGraphFactory(CURRENTPATH).getNoTx();
addDomain(g, "mail.example.com");
System.out.println("End!");
}
public static void addDomain(OrientGraphNoTx g, String newDomain) {
// ---------------------check if path is already present
// put in a list the single word
List<String> listDomain = new ArrayList<String>(Arrays.asList(newDomain.split("([.])")));
//reverse = "com; example; mail"
Collections.reverse(listDomain);
//set variable used by query to find if domain is already present
Iterable<Vertex> level = null;
List<OrientVertex> listaVertex = new ArrayList<OrientVertex>();
String queryFirst = "select expand(in('Subdomain_of')) from Zone where name = ";
String name = "";
boolean finded = false;
//set variable used by add new domain
OrientVertex addVertex;
String nodePrec = ".";
for(int i=0; i<listDomain.size(); i++) {
//from "."
level = g.command(new OSQLSynchQuery<Vertex>(queryFirst+"'"+nodePrec+"'")).execute();
CollectionUtils.addAll(listaVertex, level.iterator());
for (int ind = 0; ind<listaVertex.size(); ind++) {
name = listaVertex.get(ind).getProperty("name");
if(name.equals(listDomain.get(i))){
finded = true;
break;
}
}
// if not finded
if (!finded) {
//add vertex
addVertex = g.addVertex("class:Zone");
addVertex.setProperties("name", listDomain.get(i));
//add edge
Map<String, Vertex> vertices = new HashMap<String, Vertex>();
for (Vertex v : g.getVertices()){
vertices.put(v.getProperty("name").toString(), v);
}
g.addEdge("class:Subdomain_of", vertices.get(listDomain.get(i)), vertices.get(nodePrec), "Subdomain_of");
}
//set new name for next link
nodePrec = listDomain.get(i);
listaVertex.clear();
finded = false;
}
g.shutdown();
}
}
编辑1
与JAVASCRIPT相同的功能
添加名为&#39; nameNewDomain&#39;;
的参数 var gdb = orient.getGraphNoTx();
print("Insert new domain: '" + nameNewDomain + "'");
// ---------------------check if path is already present
var listDomain = nameNewDomain.split('.');
listDomain.reverse();
var listaVertex;
var queryFirst = "select expand(in('Subdomain_of')) from Zone where name = ";
var name = "";
var finded = false;
var nodePrec = ".";
for (i = 0; i < listDomain.length; i++) {
// from "."
listaVertex = gdb.command("sql", queryFirst + "'" + nodePrec + "'");
for (ind = 0; ind < listaVertex.length; ind++) {
name = listaVertex[ind].getProperty("name");
if (name.equals(listDomain[i])) {
finded = true;
break;
}
}
// if not finded
if (!finded) {
// add vertex
gdb.command("sql", "insert into Zone (name) values('" + listDomain[i] + "')");
// add edge
gdb.command("sql", "create edge Subdomain_of from (select from Zone where name = '" + listDomain[i]
+ "') to (select from Zone where name = '" + nodePrec + "')");
}
// set new name for next link
nodePrec = listDomain[i];
listaVertex = "";
finded = false;
}
在标签中浏览&#39;在工作室里,回想一下这个功能:
选择AddDomain(&#34; mail3.example2.com&#34;)