Ejemplo n.º 1
0
 def query_table_meta(cls, filter_fields, query_fields=None):
     filters = []
     querys = []
     for f_n, f_v in filter_fields.items():
         attr_name = 'f_%s' % f_n
         if hasattr(StorageTableMetaModel, attr_name):
             filters.append(
                 operator.attrgetter('f_%s' %
                                     f_n)(StorageTableMetaModel) == f_v)
     if query_fields:
         for f_n in query_fields:
             attr_name = 'f_%s' % f_n
             if hasattr(StorageTableMetaModel, attr_name):
                 querys.append(
                     operator.attrgetter('f_%s' %
                                         f_n)(StorageTableMetaModel))
     if filters:
         if querys:
             tables_meta = StorageTableMetaModel.select(querys).where(
                 *filters)
         else:
             tables_meta = StorageTableMetaModel.select().where(*filters)
         return [table_meta for table_meta in tables_meta]
     else:
         # not allow query all table
         return []
Ejemplo n.º 2
0
 def update_metas(self,
                  schema=None,
                  count=None,
                  part_of_data=None,
                  description=None,
                  partitions=None,
                  in_serialized=None,
                  **kwargs):
     meta_info = {}
     for k, v in locals().items():
         if k not in ["self", "kwargs", "meta_info"] and v is not None:
             meta_info[k] = v
     meta_info.update(kwargs)
     meta_info["name"] = meta_info.get("name", self.name)
     meta_info["namespace"] = meta_info.get("namespace", self.namespace)
     update_filters = []
     primary_keys = StorageTableMetaModel._meta.primary_key.field_names
     for p_k in primary_keys:
         update_filters.append(
             operator.attrgetter(p_k)(StorageTableMetaModel) == meta_info[
                 p_k.lstrip("f_")])
     table_meta = StorageTableMetaModel()
     update_fields = {}
     for k, v in meta_info.items():
         attr_name = 'f_%s' % k
         if hasattr(StorageTableMetaModel,
                    attr_name) and attr_name not in primary_keys:
             if k == "part_of_data":
                 if len(v) < 100:
                     tmp = v
                 else:
                     tmp = v[:100]
                 update_fields[operator.attrgetter(attr_name)(
                     StorageTableMetaModel)] = tmp
             else:
                 update_fields[operator.attrgetter(attr_name)(
                     StorageTableMetaModel)] = v
     if update_filters:
         operate = table_meta.update(update_fields).where(*update_filters)
     else:
         operate = table_meta.update(update_fields)
     if count:
         self.count = count
     _return = operate.execute()
     _meta = StorageTableMeta(name=self.name, namespace=self.namespace)
     return _return > 0, _meta
Ejemplo n.º 3
0
 def get_storage_info(cls, name, namespace):
     metas = StorageTableMetaModel.select().where(
         StorageTableMetaModel.f_name == name,
         StorageTableMetaModel.f_namespace == namespace)
     if metas:
         meta = metas[0]
         engine = meta.f_engine
         address_dict = meta.f_address
         address = StorageTableMeta.create_address(
             storage_engine=engine, address_dict=address_dict)
         partitions = meta.f_partitions
         return engine, address, partitions
     else:
         return None, None, None
Ejemplo n.º 4
0
 def create(self):
     table_meta = StorageTableMetaModel()
     table_meta.f_create_time = current_timestamp()
     table_meta.f_schema = {}
     table_meta.f_part_of_data = []
     for k, v in self.to_dict().items():
         attr_name = 'f_%s' % k
         if hasattr(StorageTableMetaModel, attr_name):
             setattr(
                 table_meta, attr_name,
                 v if not issubclass(type(v), AddressABC) else v.__dict__)
     try:
         rows = table_meta.save(force_insert=True)
         if rows != 1:
             raise Exception("create table meta failed")
     except peewee.IntegrityError as e:
         if e.args[0] == 1062:
             # warning
             pass
         else:
             raise e
     except Exception as e:
         raise e