灯泡:仅在尚未存在时创建顶点

时间:2014-06-10 08:38:24

标签: python titan bulbs rexster

我正在使用灯泡来创建顶点。我已经编写了下面给出的3个函数来做同样的事情。只是为了帮助你理解我在每个函数中调试行输出下面列出的顶点的属性。 / p>

DEBUG LINE:
       self.logger.info('%s',self.switches)
OUTPUT:
    00:00:00:00:00:00:02:01
    00:00:00:00:00:00:02:02
    00:00:00:00:00:00:02:03
    00:00:00:00:00:00:02:04
    00:00:00:00:00:00:02:05
    00:00:00:00:00:00:02:06

DEBUG LINE:
    self.logger.info('%s',self.ports)
OUTPUT:
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:011', 'desc': 's1-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:012', 'desc': 's1-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:021', 'desc': 's2-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:022', 'desc': 's2-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:031', 'desc': 's3-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:032', 'desc': 's3-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:041', 'desc': 's4-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:042', 'desc': 's4-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:051', 'desc': 's5-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:052', 'desc': 's5-eth2'}
    {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:061', 'desc': 's6-eth1'}
    {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:062', 'desc': 's6-eth2'}

DEBUG LINE:
    self.logger.info('%s',self.devices)
OUTPUT:    
    {'dl_addr': '00:00:00:00:00:04', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:01', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:03', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:05', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:02', 'state': 'active', 'type': 'device'}
    {'dl_addr': '00:00:00:00:00:06', 'state': 'active', 'type': 'device'}

       def create_switch_vertices(self):
        for index,switch in enumerate(self.switch_list):
               dpid_str = dpid_to_str(switch.dp.id)
               dpid_str = ':'.join([dpid_str[i:i + 2] for i in range(0, len(dpid_str), 2)])
               self.switches = {'state':'active','dpid':dpid_str,'type':'switch'}
               self.logger.info('%s',dpid_str)
               self.sw_vertices.append(self.g.vertices.create({'state':'active','dpid':dpid_str,'type':'switch'}))


    def create_port_vertices(self):
         for index,switch in enumerate(self.switch_list):
                for port in switch.ports:
                        desc = port.name
                        hw_addr = port.hw_addr
                        state = 'active'
                        port_state = 1
                        number = port.port_no
                        dpid_str = dpid_to_str(port.dpid)
                        dpid_s = ':'.join([dpid_str[i:i + 2] for i in range(0, len(dpid_str), 2)])
                        port_id = dpid_s + str(number)
                        self.ports = {'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}
                        self.logger.info('%s',self.ports)
                        self.port_vertices.append(self.g.vertices.create({'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}))

    def create_host_vertices(self):
        for value in switch_port_to_host.values():
                self.devices = {'state':'active','dl_addr':value,'type':'device'}
                self.logger.info('%s',self.devices)
                self.host_vertices.append(self.g.vertices.create({'state':'active','dl_addr':value,'type':'device'}))

我的问题如下。

1)运行此代码后,我打开了一个打开的rexster shell。 如果我做

g = rexster.getGraph("graph")
 ==>titangraph[embeddedcassandra:null]

 g.V('type', 'device').map       
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:05}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:06}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:04}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:03}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:02}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:05}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:06}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:04}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:01}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:03}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:01}
 ==>{state=active, type=device, dl_addr=00:00:00:00:00:02}

g.V('type', 'switch').map     
 ==>{state=active, dpid=00:00:00:00:00:00:02:06, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:05, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:02, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:04, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:06, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:03, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:01, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:04, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:01, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:05, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:02, type=switch}
 ==>{state=active, dpid=00:00:00:00:00:00:02:03, type=switch}

  g.V('type', 'port').map       
  ==>{desc=s5-eth1, port_id=00:00:00:00:00:00:02:051, state=active, port_state=1, number=1, type=port}
  ==>{desc=s5-eth1, port_id=00:00:00:00:00:00:02:051, state=active, port_state=1, number=1, type=port}
  ==>{desc=s1-eth1, port_id=00:00:00:00:00:00:02:011, state=active, port_state=1, number=1, type=port} 
  ==>{desc=s2-eth1, port_id=00:00:00:00:00:00:02:021, state=active, port_state=1, number=1, type=port}
  ==>{desc=s6-eth1, port_id=00:00:00:00:00:00:02:061, state=active, port_state=1, number=1, type=port}
  ==>{desc=s3-eth1, port_id=00:00:00:00:00:00:02:031, state=active, port_state=1, number=1, type=port}
  ==>{desc=s3-eth2, port_id=00:00:00:00:00:00:02:032, state=active, port_state=1, number=2, type=port}
  ==>{desc=s1-eth2, port_id=00:00:00:00:00:00:02:012, state=active, port_state=1, number=2, type=port}
  ==>{desc=s3-eth1, port_id=00:00:00:00:00:00:02:031, state=active, port_state=1, number=1, type=port}
  ==>{desc=s4-eth1, port_id=00:00:00:00:00:00:02:041, state=active, port_state=1, number=1, type=port}
  ==>{desc=s6-eth2, port_id=00:00:00:00:00:00:02:062, state=active, port_state=1, number=2, type=port}
  ==>{desc=s1-eth2, port_id=00:00:00:00:00:00:02:012, state=active, port_state=1, number=2, type=port}
  ==>{desc=s3-eth2, port_id=00:00:00:00:00:00:02:032, state=active, port_state=1, number=2, type=port}
  ==>{desc=s6-eth2, port_id=00:00:00:00:00:00:02:062, state=active, port_state=1, number=2, type=port}
  ==>{desc=s4-eth2, port_id=00:00:00:00:00:00:02:042, state=active, port_state=1, number=2, type=port}
  ==>{desc=s5-eth2, port_id=00:00:00:00:00:00:02:052, state=active, port_state=1, number=2, type=port}
  ==>{desc=s1-eth1, port_id=00:00:00:00:00:00:02:011, state=active, port_state=1, number=1, type=port}
  ==>{desc=s4-eth1, port_id=00:00:00:00:00:00:02:041, state=active, port_state=1, number=1, type=port}
  ==>{desc=s5-eth2, port_id=00:00:00:00:00:00:02:052, state=active, port_state=1, number=2, type=port}
  ==>{desc=s2-eth2, port_id=00:00:00:00:00:00:02:022, state=active, port_state=1, number=2, type=port}
  ==>{desc=s6-eth1, port_id=00:00:00:00:00:00:02:061, state=active, port_state=1, number=1, type=port}
  ==>{desc=s2-eth2, port_id=00:00:00:00:00:00:02:022, state=active, port_state=1, number=2, type=port}
  ==>{desc=s4-eth2, port_id=00:00:00:00:00:00:02:042, state=active, port_state=1, number=2, type=port}
  ==>{desc=s2-eth1, port_id=00:00:00:00:00:00:02:021, state=active, port_state=1, number=1, type=port}

你可以看到在代码多次运行时创建的相同顶点的多个副本。我想避免这种情况。我希望顶点只有在它不存在的情况下才能创建。我该如何实现。

1 个答案:

答案 0 :(得分:1)

gremlin-users邮件列表中回答了这个问题:

https://groups.google.com/forum/#!topic/gremlin-users/5rVEiX744-c

从线程中,可以:

  1. 在gremlin中编写自己的getOrCreate方法
  2. 考虑使用灯泡native function