A semantic search engine for source code https://bitshift.benkurtovic.com/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

216 rader
9.1 KiB

  1. """
  2. Subpackage with classes and functions to handle communication with the MySQL
  3. database backend, which manages the search index.
  4. """
  5. import os
  6. import mmh3
  7. import oursql
  8. from .migration import VERSION, MIGRATIONS
  9. from ..codelet import Codelet
  10. from ..query.nodes import (String, Regex, Text, Language, Author, Date, Symbol,
  11. BinaryOp, UnaryOp)
  12. __all__ = ["Database"]
  13. class Database(object):
  14. """Represents the MySQL database."""
  15. def __init__(self, migrate=False):
  16. self._conn = self._connect()
  17. self._check_version(migrate)
  18. def _connect(self):
  19. """Establish a connection to the database."""
  20. root = os.path.dirname(os.path.abspath(__file__))
  21. default_file = os.path.join(root, ".my.cnf")
  22. return oursql.connect(db="bitshift", read_default_file=default_file,
  23. autoping=True, autoreconnect=True)
  24. def _migrate(self, cursor, current):
  25. """Migrate the database to the latest schema version."""
  26. for version in xrange(current, VERSION):
  27. print "Migrating to %d..." % version + 1
  28. for query in MIGRATIONS[version - 1]:
  29. cursor.execute(query)
  30. cursor.execute("UPDATE version SET version = ?", (version + 1,))
  31. def _check_version(self, migrate):
  32. """Check the database schema version and respond accordingly.
  33. If the schema is out of date, migrate if *migrate* is True, else raise
  34. an exception.
  35. """
  36. with self._conn.cursor() as cursor:
  37. cursor.execute("SELECT version FROM version")
  38. version = cursor.fetchone()[0]
  39. if version < VERSION:
  40. if migrate:
  41. self._migrate(cursor, version)
  42. else:
  43. err = "Database schema out of date. " \
  44. "Run `python -m bitshift.database.migration`."
  45. raise RuntimeError(err)
  46. def _search_with_query(self, cursor, tree, page):
  47. """Execute an SQL query based on a query tree, and return results.
  48. The returned data is a 2-tuple of (list of codelet IDs, estimated
  49. number of total results).
  50. """
  51. query, args = tree.build_query(page)
  52. cursor.execute(query, args)
  53. ids = [id for id, _ in cursor.fetchall()]
  54. num_results = 0 # TODO: NotImplemented
  55. return ids, num_results
  56. def _get_authors_for_codelet(self, cursor, codelet_id):
  57. """Return a list of authors for a given codelet."""
  58. query = """SELECT author_name, author_url
  59. FROM authors
  60. WHERE author_codelet = ?"""
  61. cursor.execute(query, (codelet_id,))
  62. return cursor.fetchall()
  63. def _get_symbols_for_code(self, cursor, code_id):
  64. """Return a list of symbols for a given codelet."""
  65. query = """SELECT symbol_type, symbol_name, sloc_type, sloc_row,
  66. sloc_col, sloc_end_row, sloc_end_col
  67. FROM symbols
  68. INNER JOIN symbol_locations ON sloc_symbol = symbol_id
  69. WHERE symbol_code = ?"""
  70. symbols = {type_: {} for type_ in Symbol.TYPES_INV}
  71. cursor.execute(query, (code_id,))
  72. for type_, name, loc_type, row, col, erow, ecol in cursor.fetchall():
  73. sdict = symbols[Symbol.TYPES_INV[type_]]
  74. if name not in sdict:
  75. sdict[name] = ((), ())
  76. sdict[name][loc_type].append((row, col, erow, ecol))
  77. for type_, sdict in symbols.items():
  78. symbols[type_] = [(n, d, u) for n, (d, u) in sdict.iteritems()]
  79. return symbols
  80. def _get_codelets_from_ids(self, cursor, ids):
  81. """Return a list of Codelet objects given a list of codelet IDs."""
  82. query = """SELECT *
  83. FROM codelets
  84. INNER JOIN code ON codelet_code_id = code_id
  85. INNER JOIN origins ON codelet_origin = origin_id
  86. WHERE codelet_id = ?"""
  87. with self._conn.cursor(oursql.DictCursor) as dict_cursor:
  88. dict_cursor.executemany(query, [(id,) for id in ids])
  89. for row in dict_cursor.fetchone():
  90. codelet_id = row["codelet_id"]
  91. if row["origin_url_base"]:
  92. url = row["codelet_url"]
  93. else:
  94. url = row["origin_url_base"] + row["codelet_url"]
  95. origin = (row["origin_name"], row["origin_url"],
  96. row["origin_image"])
  97. authors = self._get_authors_for_codelet(cursor, codelet_id)
  98. symbols = self._get_symbols_for_code(cursor, row["code_id"])
  99. yield Codelet(
  100. row["codelet_name"], row["code_code"], None,
  101. row["code_lang"], authors, url,
  102. row["codelet_date_created"], row["codelet_date_modified"],
  103. row["codelet_rank"], symbols, origin)
  104. def _decompose_url(self, cursor, url):
  105. """Break up a URL into an origin (with a URL base) and a suffix."""
  106. query = """SELECT origin_id, SUBSTR(?, LENGTH(origin_url_base))
  107. FROM origins
  108. WHERE origin_url_base IS NOT NULL
  109. AND ? LIKE CONCAT(origin_url_base, "%")"""
  110. cursor.execute(query, (url, url))
  111. result = cursor.fetchone()
  112. return result if result else (1, url)
  113. def _insert_symbols(self, cursor, code_id, sym_type, symbols):
  114. """Insert a list of symbols of a given type into the database."""
  115. query1 = "INSERT INTO symbols VALUES (DEFAULT, ?, ?, ?)"
  116. query2 = """INSERT INTO symbol_locations VALUES
  117. (DEFAULT, ?, ?, ?, ?, ?, ?)"""
  118. for (name, decls, uses) in symbols:
  119. cursor.execute(query1, (code_id, Symbol.TYPES_INV[sym_type], name))
  120. sym_id = cursor.lastrowid
  121. params = ([tuple([sym_id, 0] + list(loc)) for loc in decls] +
  122. [tuple([sym_id, 1] + list(loc)) for loc in uses])
  123. cursor.executemany(query2, params)
  124. def close(self):
  125. """Disconnect from the database."""
  126. self._conn.close()
  127. def search(self, query, page=1):
  128. """
  129. Search the database for a query and return the *n*\ th page of results.
  130. :param query: The query to search for.
  131. :type query: :py:class:`~.query.tree.Tree`
  132. :param page: The result page to display.
  133. :type page: int
  134. :return: The total number of results, and the *n*\ th page of results.
  135. :rtype: 2-tuple of (long, list of :py:class:`.Codelet`\ s)
  136. """
  137. query1 = """SELECT cdata_codelet, cache_count_mnt, cache_count_exp
  138. FROM cache
  139. INNER JOIN cache_data ON cache_id = cdata_cache
  140. WHERE cache_id = ?"""
  141. query2 = "INSERT INTO cache VALUES (?, ?, ?, DEFAULT)"
  142. query3 = "INSERT INTO cache_data VALUES (?, ?)"
  143. cache_id = mmh3.hash64(str(page) + ":" + query.serialize())[0]
  144. with self._conn.cursor() as cursor:
  145. cursor.execute(query1, (cache_id,))
  146. results = cursor.fetchall()
  147. if results: # Cache hit
  148. num_results = results[0][1] * (10 ** results[0][2])
  149. ids = [res[0] for res in results]
  150. else: # Cache miss
  151. ids, num_results = self._search_with_query(cursor, query, page)
  152. num_exp = max(len(str(num_results)) - 3, 0)
  153. num_results = int(round(num_results, -num_exp))
  154. num_mnt = num_results / (10 ** num_exp)
  155. cursor.execute(query2, (cache_id, num_mnt, num_exp))
  156. cursor.executemany(query3, [(cache_id, c_id) for c_id in ids])
  157. codelet_gen = self._get_codelets_from_ids(cursor, ids)
  158. return (num_results, list(codelet_gen))
  159. def insert(self, codelet):
  160. """
  161. Insert a codelet into the database.
  162. :param codelet: The codelet to insert.
  163. :type codelet: :py:class:`.Codelet`
  164. """
  165. query1 = """INSERT INTO code VALUES (?, ?, ?)
  166. ON DUPLICATE KEY UPDATE code_id=code_id"""
  167. query2 = """INSERT INTO codelets VALUES
  168. (DEFAULT, ?, ?, ?, ?, ?, ?, ?)"""
  169. query3 = "INSERT INTO authors VALUES (DEFAULT, ?, ?, ?)"
  170. hash_key = str(codelet.language) + ":" + codelet.code.encode("utf8")
  171. code_id = mmh3.hash64(hash_key)[0]
  172. with self._conn.cursor() as cursor:
  173. cursor.execute(query1, (code_id, codelet.language, codelet.code))
  174. if cursor.rowcount == 1:
  175. for sym_type, symbols in codelet.symbols.iteritems():
  176. self._insert_symbols(cursor, code_id, sym_type, symbols)
  177. origin, url = self._decompose_url(cursor, codelet.url)
  178. cursor.execute(query2, (codelet.name, code_id, origin, url,
  179. codelet.rank, codelet.date_created,
  180. codelet.date_modified))
  181. codelet_id = cursor.lastrowid
  182. authors = [(codelet_id, a[0], a[1]) for a in codelet.authors]
  183. cursor.executemany(query3, authors)