采用左右值编码来存储无限分级树形结构
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,参考文档:https://blog.csdn.net/comiunknown/article/details/1586020python实现:from app.cmdb.config import d
千家信息网最后更新 2026年11月01日采用左右值编码来存储无限分级树形结构
参考文档:
https://blog.csdn.net/comiunknown/article/details/1586020
python实现:
from app.cmdb.config import dbconfig, cmdbsql, modelconfig, balantsqlfrom app.cmdb.utils.dbutil import MysqlConnectbalant_connect = MysqlConnect(db_params=dbconfig.balant_db_params_sit)# cmdb_connect = MysqlConnect(db_params=dbconfig.cmdb_db_params)class CatalogTemplate(object): def __init__(self, tag_id, catalog_id): """ :param tag_id: 基准节点 :param catalog_id: 基准目录 """ self.tag_id = tag_id self.catalog_id = catalog_id (self.lft, self.rht) = self.get_lft_rht() def get_tag(self): """ 获取节点id :return: 0 或 id """ try: catalog_tag_id = balant_connect.query_execute( """select id from mon_catalog_tag where tag_id = %d and catalog_id = %d""" % (self.tag_id, self.catalog_id)) if catalog_tag_id: return catalog_tag_id[0][0] else: return 0 except: return 0 def get_lft_rht(self): """ 获取该节点的左右id :return:列表[lft, rht] """ try: if self.get_tag(): result = balant_connect.query_execute("""select lft, rht from mon_catalog_tag where tag_id = %d and catalog_id = %d limit 1""" % (self.tag_id, self.catalog_id)) return result[0] else: return 0, 0 except Exception as e: raise Exception("记录不存在,info %s" % e) def get_layer(self): """ 获取节点层级 :return: 层数 """ try: if self.get_tag(): result = balant_connect.query_execute("""select count(*) from mon_catalog_tag where lft <= '%d' and rht >= %d and catalog_id = %d""" % (self.lft, self.rht, self.catalog_id)) return result[0][0] else: return 0 except Exception as e: raise Exception("记录不存在,info %s" % e) def get_tree_list(self): """ 获取该节点及其子孙节点 :return:节点列表 """ if self.get_tag(): try: return balant_connect.query_execute("""select * from (SELECT tag_id, lft, rht, %d as layer FROM mon_catalog_tag where catalog_id = %d ORDER BY lft) AS TreeList where lft between %d and %d order by lft asc""" % (self.get_layer(), self.catalog_id, self.lft, self.rht)) except Exception as e: raise Exception("记录不存在,info %s" % e) def add_sub_node(self, tag_name, catalog_name): """ 插入子节点, 只能添加末节点 :param tag_name:待插入节点的名称 :param catalog_name:待插入目录的名称 :return: 无 """ tag_id = balant_connect.query_execute("""select id from mon_tag where name = '%s'""" % tag_name) catalog_id = balant_connect.query_execute("""select id from mon_catalog where name = '%s'""" % catalog_name) insert_tag_id = balant_connect.query_execute( """select id from mon_catalog_tag where tag_id = %d and catalog_id = %d""" % (tag_id[0][0], catalog_id[0][0])) if self.get_tag() and not insert_tag_id: rht = balant_connect.query_execute("""select rht from mon_catalog_tag where tag_id = %d""" % self.tag_id) balant_connect.dml_execute("""update mon_catalog_tag set rht=rht+2 where rht>=%d""" % rht[0][0]) balant_connect.dml_execute("""update mon_catalog_tag set lft=lft+2 where lft>=%d""" % rht[0][0]) balant_connect.dml_execute("""insert into mon_catalog_tag (parent_id, lft, rht, tag_id, catalog_id) values (%d,%d,%d,%d,%d)""" % (self.get_tag(), rht[0][0], int(rht[0][0])+1, tag_id[0][0], catalog_id[0][0])) else: print("插入节点错误,请检查参数和层级关系!") return def del_node(self, tag_name, catalog_name): """ 删除节点 :param tag_name:待插入节点的名称 :param catalog_name:待插入目录的名称 :return: 无 """ tag_id = balant_connect.query_execute("""select id from mon_tag where name = '%s'""" % tag_name) catalog_id = balant_connect.query_execute("""select id from mon_catalog where name = '%s'""" % catalog_name) del_tag_id = balant_connect.query_execute( """select id from mon_catalog_tag where tag_id = %d and catalog_id = %d""" % ( tag_id[0][0], catalog_id[0][0])) lft_rht = balant_connect.query_execute("""select lft, rht from mon_catalog_tag where tag_id = %d and catalog_id = %d limit 1""" % (tag_id[0][0], catalog_id[0][0])) lft_id = lft_rht[0][0] rht_id = lft_rht[0][1] if self.get_tag() and del_tag_id: balant_connect.dml_execute("""delete from mon_catalog_tag where lft>=%d and rht<=%d""" % (lft_id, rht_id)) balant_connect.dml_execute("""update mon_catalog_tag set lft=lft-(%d-%d+1) where lft>%d""" % (rht_id, lft_id, lft_id)) balant_connect.dml_execute("""update mon_catalog_tag set rht=rht-(%d-%d+1) where rht>%d""" % (rht_id, lft_id, rht_id)) else: print("插入节点错误,请检查参数和层级关系!") return def move_node(self): passdef target_add_tag(): """ 查询所有tag信息,更新tag表和目录表 :return: """ data_mw = dict() sql_cmdb = cmdbsql.cmdb_select_app_tag # result = cmdb_connect.query_execute(sql_cmdb, db_params=dbconfig.cmdb_db_params) result = [('ASURA-BAR', 'ASU_BAR_CNSZ17_WAS', 'IBM WAS 6.x', 'ASU_BAR_CNSZ17_WAS_01', '生产')] # sysCode | vclName | soft | hostname | env # [('ASURA-BAR', 'ASU_BAR_CNSZ17_WAS', 'IBM WAS 6.x', 'ASU_BAR_CNSZ17_WAS_01', '生产')] for i in result: cur = balant_connect.query_execute(balantsql.balant_select_tag % i[3]) print(cur) # [(None, 'ASU_BAR_CNSZ17_WAS_01', 'middleware', 7092)] try: # 更新tag if cur[0][0] is None: balant_connect.dml_execute(balantsql.balant_update_tag % (i[0], i[0])) balant_connect.dml_execute(balantsql.balant_update_target_tag % (i[3], i[0], i[3], i[0])) balant_connect.dml_execute(balantsql.balant_update_tag % (i[1], i[1])) balant_connect.dml_execute(balantsql.balant_update_target_tag % (i[3], i[1], i[3], i[1])) balant_connect.dml_execute(balantsql.balant_update_tag % (i[4], i[4])) balant_connect.dml_execute(balantsql.balant_update_target_tag % (i[3], i[4], i[3], i[4])) elif i[0] not in cur[0][0]: # 判断cmdb是否存在tag balant_connect.dml_execute(balantsql.balant_update_tag % (i[0], i[0])) balant_connect.dml_execute(balantsql.balant_update_target_tag % (i[3], i[0], i[3], i[0])) elif i[1] not in cur[0][0]: # 判断cmdb是否存在tag balant_connect.dml_execute(balantsql.balant_update_tag % (i[1], i[1])) balant_connect.dml_execute(balantsql.balant_update_target_tag % (i[3], i[1], i[3], i[1])) elif i[4] not in cur[0][0]: # 判断cmdb是否存在tag balant_connect.dml_execute(balantsql.balant_update_tag % (i[4], i[4])) balant_connect.dml_execute(balantsql.balant_update_target_tag % (i[3], i[4], i[3], i[4])) # 更新catalog sysCode_tag_id = balant_connect.query_execute("""select id from mon_tag where name = '%s'""" % i[0]) catalog_id = balant_connect.query_execute("""select id from mon_catalog where name = '应用'""") catalog = CatalogTemplate(0, catalog_id[0][0]) catalog.add_sub_node(i[0], "应用") catalog = CatalogTemplate(sysCode_tag_id[0][0], catalog_id[0][0]) catalog.add_sub_node(i[1], "应用") except Exception as e: raise Exception("Error info: %s" % e) return data_mw
节点
名称
目录
层级
应用
更新
参数
基准
错误
检查
生产
信息
文档
参考
查询
树形
结构
编码
存储
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技有限公司
深圳九邦互联网科技有限公司
中国互联网科技股
互联网与科技发展
猪兼强互联网科技
猫创互联网科技
互联网科技大会案例
太原新华互联网科技学校怎么样
互联网科技公司的市场部门
阿里巴巴算科技企业还是互联网
彤创互联网科技有限公司
中科明德北京互联网科技
电子商务与互联网科技的关系
普陀区互联网教育科技管理办法
福仕德教育科技互联网
众联互联网络科技
互联网科技大会大佬
保山浩联互联网科技有限公司
重庆科技产业互联网有哪些
恒生科技互联网指数十大成分股
科技制造互联网公司
智网互联网科技
杭州互联网科技公司
奇士达互联网科技
互联网科技logo
滨湖区互联网智能科技产品供应
乌苏新华互联网科技做什么的
陕西云淼互联网科技公司
云南互联网科技学费
张家口科技产业互联网平台
- 上一篇
k8s1.14环境的搭建
本篇内容主要讲解"k8s1.14环境的搭建",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"k8s1.14环境的搭建"吧!1. 4台虚拟机 (CentOS L
- 下一篇
'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
Django运行时出现'url' requires a non-empty first argument的解决办法:参考了stackoverflow里面的一个帖子I started using Dja