model.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. # coding=utf-8
  2. # Copyright 2014 Baidu, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. # the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. # specific language governing permissions and limitations under the License.
  12. """
  13. This module defines some Argument classes for BTS
  14. """
  15. class CreateInstanceArgs(object):
  16. """
  17. Create Instance Args
  18. :param storage_type instance's storage type. eg.CommonPerformance
  19. :type storage_type string
  20. """
  21. def __init__(self, storage_type=None):
  22. self.storage_type = storage_type
  23. def create_instance_args_2_dict(args):
  24. """
  25. change create_instance_args to dict
  26. :param args: create instance args
  27. :type args: CreateInstanceArgs
  28. :return:
  29. :rtype dict
  30. """
  31. return {
  32. 'storageType': args.storage_type
  33. }
  34. class CreateTableArgs(object):
  35. """
  36. Create Table Args
  37. :param table_version table's version
  38. :type table_version int64
  39. :param compress_type table's compress type. eg.SNAPPY_ALL
  40. :type compress_type string
  41. :param ttl time to live
  42. :type ttl int
  43. :param storage_type instance's storage type. eg.CommonPerformance
  44. :type storage_type string
  45. :param max_versions table's max data versions.
  46. :type max_versions int
  47. """
  48. def __init__(self, table_version=0, compress_type=None, ttl=0, storage_type=None, max_versions=1):
  49. self.table_version = table_version
  50. self.compress_type = compress_type
  51. self.ttl = ttl
  52. self.storage_type = storage_type
  53. self.max_versions = max_versions
  54. def create_table_args_2_dict(args):
  55. """
  56. change create_table_args to dict
  57. :param args: create table args
  58. :type args: CreateTableArgs
  59. :return:
  60. :rtype dict
  61. """
  62. return {
  63. 'tableVersion': args.table_version,
  64. 'compressType': args.compress_type,
  65. 'ttl': args.ttl,
  66. 'storageType': args.storage_type,
  67. 'maxVersions': args.max_versions
  68. }
  69. class UpdateTableArgs(object):
  70. """
  71. Update Table Args
  72. :param table_version table's version
  73. :type table_version int64
  74. :param compress_type table's compress type. eg.SNAPPY_ALL
  75. :type compress_type string
  76. :param ttl time to live
  77. :type ttl int
  78. :param max_versions table's max data versions.
  79. :type max_versions int
  80. """
  81. # 不能将table_version初始化为None,否则后端会认为是创建表
  82. def __init__(self, table_version=1, compress_type=None, ttl=None, max_versions=None):
  83. self.table_version = table_version
  84. self.compress_type = compress_type
  85. self.ttl = ttl
  86. self.max_versions = max_versions
  87. def update_table_args_2_dict(args):
  88. """
  89. change update_table_args to dict
  90. :param args: update table args
  91. :type args: UpdateTableArgs
  92. :return:
  93. :rtype dict
  94. """
  95. return {
  96. 'tableVersion': args.table_version,
  97. 'compressType': args.compress_type,
  98. 'ttl': args.ttl,
  99. 'maxVersions': args.max_versions
  100. }
  101. class Cell(object):
  102. """
  103. Cell
  104. :param column
  105. :type column string
  106. :param value
  107. :type value string
  108. """
  109. def __init__(self, column="", value=""):
  110. self.column = column
  111. self.value = value
  112. def to_dict(self):
  113. """
  114. Convert the Cell instance into a dictionary with keys for column and value.
  115. :return: A dictionary with 'column' and 'value' as keys, representing
  116. the cell's data.
  117. :rtype: dict
  118. """
  119. return {
  120. 'column': self.column,
  121. 'value': self.value
  122. }
  123. class Row(object):
  124. """
  125. Row
  126. :param rowkey
  127. :type rowkey string
  128. :param cells
  129. :type cells []
  130. """
  131. def __init__(self, rowkey=""):
  132. self.rowkey = rowkey
  133. self.cells = []
  134. def append_cell(self, cell):
  135. """
  136. append cell
  137. :param cell: column & value
  138. :type cell: Cell
  139. :return:
  140. :rtype
  141. """
  142. self.cells.append(cell)
  143. def get_cell(self):
  144. """
  145. get cell
  146. :return cells:
  147. :rtype Cell[]
  148. """
  149. return self.cells
  150. def to_dict(self):
  151. """
  152. Convert the Row instance into a dictionary suitable for serialization.
  153. :return: A dictionary with 'rowkey' as the unique identifier and 'cells'
  154. containing a list of cell data in dictionary form.
  155. :rtype: dict
  156. """
  157. cells = []
  158. for cell in self.cells:
  159. if isinstance(cell, Cell):
  160. cells.append(cell.to_dict())
  161. elif isinstance(cell, dict):
  162. cells.append(cell)
  163. return {
  164. 'rowkey': self.rowkey,
  165. 'cells': cells
  166. }
  167. class BatchPutRowArgs(object):
  168. """
  169. Batch Put Row Args
  170. :param rows
  171. :type rows []
  172. """
  173. def __init__(self):
  174. self.rows = []
  175. def append_row(self, row):
  176. """
  177. append row
  178. :param row:
  179. :type row: Row
  180. :return:
  181. :rtype
  182. """
  183. self.rows.append(row)
  184. def get_row(self):
  185. """
  186. get row
  187. :return rows:
  188. :rtype Row[]
  189. """
  190. return self.rows
  191. class QueryCell(object):
  192. """
  193. Query Cell
  194. :param column
  195. :type column string
  196. """
  197. def __init__(self, column=""):
  198. self.column = column
  199. def to_dict(self):
  200. """
  201. Convert the QueryCell instance into a dictionary
  202. :return: A dictionary representation of the QueryCell, with 'column' as the key.
  203. :rtype: dict
  204. """
  205. return {
  206. 'column': self.column,
  207. }
  208. class QueryRowArgs(object):
  209. """
  210. Query Row Arg
  211. :param rowkey
  212. :type rowkey string
  213. :param max_versions
  214. :type max_versions int
  215. :param cells
  216. :type cells []
  217. """
  218. def __init__(self, rowkey="", max_versions=0):
  219. self.rowkey = rowkey
  220. self.max_versions = max_versions
  221. self.cells = []
  222. def append_cell(self, cell):
  223. """
  224. append cell
  225. :param cell: column & value
  226. :type cell: Cell
  227. :return:
  228. :rtype
  229. """
  230. self.cells.append(cell)
  231. def get_cell(self):
  232. """
  233. get cell
  234. :return cells:
  235. :rtype Cell[]
  236. """
  237. return self.cells
  238. def to_dict(self):
  239. """
  240. Convert the QueryRowArgs instance into a dictionary suitable for serialization.
  241. :return: A dictionary representation of the QueryRowArgs, including the rowkey and cells.
  242. :rtype: dict
  243. """
  244. cells_dict = [cell.to_dict() if hasattr(cell, 'to_dict') else cell for cell in self.cells]
  245. return {
  246. 'rowkey': self.rowkey,
  247. 'cells': cells_dict,
  248. }
  249. def query_row_args_2_dict(args):
  250. """
  251. change query_row_args to dict
  252. :param args: query row args
  253. :type args: QueryRowArgs
  254. :return:
  255. :rtype dict
  256. """
  257. return {
  258. 'rowkey': args.rowkey,
  259. 'maxVersions': args.max_versions,
  260. 'cells': args.cells
  261. }
  262. class BatchQueryRowArgs(object):
  263. """
  264. Batch Query Row Args
  265. :param rows
  266. :type rows []
  267. :param max_versions
  268. :type max_versions int
  269. """
  270. def __init__(self, max_versions=0):
  271. self.rows = []
  272. self.max_versions = max_versions
  273. def append_row(self, row):
  274. """
  275. append row
  276. :param row:
  277. :type row: Row
  278. :return:
  279. :rtype
  280. """
  281. self.rows.append(row)
  282. def get_rows(self):
  283. """
  284. get row
  285. :return rows:
  286. :rtype Row[]
  287. """
  288. return self.rows
  289. def to_dict(self):
  290. """
  291. Convert the BatchQueryRowArgs instance to a dictionary for JSON serialization.
  292. :return: A dictionary representation of the BatchQueryRowArgs instance with
  293. 'rows' key containing a list of rows and 'max_versions' key
  294. containing the maximum number of versions.
  295. :rtype: dict
  296. """
  297. rows_data = []
  298. for row_arg in self.rows:
  299. if isinstance(row_arg, QueryRowArgs):
  300. # Assuming QueryRowArgs has a to_dict method
  301. row_data = row_arg.to_dict()
  302. elif isinstance(row_arg, dict):
  303. row_data = row_arg
  304. rows_data.append(row_data)
  305. return {
  306. 'rows': rows_data,
  307. 'max_versions': self.max_versions
  308. }
  309. def batch_query_row_args_2_dict(args):
  310. """
  311. change batch_query_row_args to dict
  312. :param args: batch query row args
  313. :type args: BatchQueryRowArgs
  314. :return:
  315. :rtype dict
  316. """
  317. return {
  318. 'maxVersions': args.max_versions,
  319. 'rows': args.rows
  320. }
  321. class ScanArgs(object):
  322. """
  323. Scan Args
  324. :param start_rowkey
  325. :type start_rowkey string
  326. :param include_start
  327. :type include_start bool
  328. :param stop_rowkey
  329. :type stop_rowkey string
  330. :param include_stop
  331. :type include_stop bool
  332. :param limit
  333. :type limit int
  334. :param max_versions
  335. :type max_versions int
  336. :param selector
  337. :type selector []
  338. """
  339. def __init__(self, start_rowkey="", include_start=True, stop_rowkey="",
  340. include_stop=False, limit=0, max_versions=0):
  341. self.start_rowkey = start_rowkey
  342. self.include_start = include_start
  343. self.stop_rowkey = stop_rowkey
  344. self.include_stop = include_stop
  345. self.limit = limit
  346. self.max_versions = max_versions
  347. self.selector = []
  348. def append_selector(self, query_cell):
  349. """
  350. append selector
  351. :param query_cell:
  352. :type query_cell: QueryCell
  353. :return:
  354. :rtype
  355. """
  356. self.selector.append(query_cell)
  357. def get_selector(self):
  358. """
  359. get selector
  360. :return selector:
  361. :rtype query_cell[]
  362. """
  363. return self.selector
  364. def to_dict(self):
  365. """
  366. Convert the ScanArgs instance into a dictionary
  367. :return: A dictionary representation of the scan arguments.
  368. :rtype: dict
  369. """
  370. selector = []
  371. for item in self.selector:
  372. if isinstance(item, QueryCell):
  373. selector.append({'column': item.column})
  374. elif isinstance(item, dict):
  375. selector.append(item)
  376. return {
  377. 'startRowkey': self.start_rowkey,
  378. 'includeStart': self.include_start,
  379. 'stopRowkey': self.stop_rowkey,
  380. 'includeStop': self.include_stop,
  381. 'selector': selector,
  382. 'maxVersions': self.max_versions,
  383. 'limit': self.limit
  384. }
  385. def scan_args_2_dict(args):
  386. """
  387. change scan_args to dict
  388. :param args: scan row args
  389. :type args: ScanArgs
  390. :return:
  391. :rtype dict
  392. """
  393. return {
  394. 'startRowkey': args.start_rowkey,
  395. 'includeStart': args.include_start,
  396. 'stopRowkey': args.stop_rowkey,
  397. 'includeStop': args.include_stop,
  398. 'selector': args.selector,
  399. 'limit': args.limit,
  400. 'maxVersions': args.max_versions
  401. }