A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

217 lines
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 = len(ids) # 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. for codelet_id in ids:
  89. dict_cursor.execute(query, (codelet_id,))
  90. row = dict_cursor.fetchall()[0]
  91. codelet_id = row["codelet_id"]
  92. if row["origin_url_base"]:
  93. url = row["codelet_url"]
  94. else:
  95. url = row["origin_url_base"] + row["codelet_url"]
  96. origin = (row["origin_name"], row["origin_url"],
  97. row["origin_image"])
  98. authors = self._get_authors_for_codelet(cursor, codelet_id)
  99. symbols = self._get_symbols_for_code(cursor, row["code_id"])
  100. yield Codelet(
  101. row["codelet_name"], row["code_code"], None,
  102. row["code_lang"], authors, url,
  103. row["codelet_date_created"], row["codelet_date_modified"],
  104. row["codelet_rank"], symbols, origin)
  105. def _decompose_url(self, cursor, url):
  106. """Break up a URL into an origin (with a URL base) and a suffix."""
  107. query = """SELECT origin_id, SUBSTR(?, LENGTH(origin_url_base))
  108. FROM origins
  109. WHERE origin_url_base IS NOT NULL
  110. AND ? LIKE CONCAT(origin_url_base, "%")"""
  111. cursor.execute(query, (url, url))
  112. result = cursor.fetchone()
  113. return result if result else (1, url)
  114. def _insert_symbols(self, cursor, code_id, sym_type, symbols):
  115. """Insert a list of symbols of a given type into the database."""
  116. query1 = "INSERT INTO symbols VALUES (DEFAULT, ?, ?, ?)"
  117. query2 = """INSERT INTO symbol_locations VALUES
  118. (DEFAULT, ?, ?, ?, ?, ?, ?)"""
  119. for (name, decls, uses) in symbols:
  120. cursor.execute(query1, (code_id, Symbol.TYPES_INV[sym_type], name))
  121. sym_id = cursor.lastrowid
  122. params = ([tuple([sym_id, 0] + list(loc)) for loc in decls] +
  123. [tuple([sym_id, 1] + list(loc)) for loc in uses])
  124. cursor.executemany(query2, params)
  125. def close(self):
  126. """Disconnect from the database."""
  127. self._conn.close()
  128. def search(self, query, page=1):
  129. """
  130. Search the database for a query and return the *n*\ th page of results.
  131. :param query: The query to search for.
  132. :type query: :py:class:`~.query.tree.Tree`
  133. :param page: The result page to display.
  134. :type page: int
  135. :return: The total number of results, and the *n*\ th page of results.
  136. :rtype: 2-tuple of (long, list of :py:class:`.Codelet`\ s)
  137. """
  138. query1 = """SELECT cdata_codelet, cache_count_mnt, cache_count_exp
  139. FROM cache
  140. INNER JOIN cache_data ON cache_id = cdata_cache
  141. WHERE cache_id = ?"""
  142. query2 = "INSERT INTO cache VALUES (?, ?, ?, DEFAULT)"
  143. query3 = "INSERT INTO cache_data VALUES (?, ?)"
  144. cache_id = mmh3.hash64(str(page) + ":" + query.serialize())[0]
  145. with self._conn.cursor() as cursor:
  146. cursor.execute(query1, (cache_id,))
  147. results = cursor.fetchall()
  148. if results: # Cache hit
  149. num_results = results[0][1] * (10 ** results[0][2])
  150. ids = [res[0] for res in results]
  151. else: # Cache miss
  152. ids, num_results = self._search_with_query(cursor, query, page)
  153. num_exp = max(len(str(num_results)) - 3, 0)
  154. num_results = int(round(num_results, -num_exp))
  155. num_mnt = num_results / (10 ** num_exp)
  156. cursor.execute(query2, (cache_id, num_mnt, num_exp))
  157. cursor.executemany(query3, [(cache_id, c_id) for c_id in ids])
  158. codelet_gen = self._get_codelets_from_ids(cursor, ids)
  159. return (num_results, list(codelet_gen))
  160. def insert(self, codelet):
  161. """
  162. Insert a codelet into the database.
  163. :param codelet: The codelet to insert.
  164. :type codelet: :py:class:`.Codelet`
  165. """
  166. query1 = """INSERT INTO code VALUES (?, ?, ?)
  167. ON DUPLICATE KEY UPDATE code_id=code_id"""
  168. query2 = """INSERT INTO codelets VALUES
  169. (DEFAULT, ?, ?, ?, ?, ?, ?, ?)"""
  170. query3 = "INSERT INTO authors VALUES (DEFAULT, ?, ?, ?)"
  171. hash_key = str(codelet.language) + ":" + codelet.code.encode("utf8")
  172. code_id = mmh3.hash64(hash_key)[0]
  173. with self._conn.cursor() as cursor:
  174. cursor.execute(query1, (code_id, codelet.language, codelet.code))
  175. if cursor.rowcount == 1:
  176. for sym_type, symbols in codelet.symbols.iteritems():
  177. self._insert_symbols(cursor, code_id, sym_type, symbols)
  178. origin, url = self._decompose_url(cursor, codelet.url)
  179. cursor.execute(query2, (codelet.name, code_id, origin, url,
  180. codelet.rank, codelet.date_created,
  181. codelet.date_modified))
  182. codelet_id = cursor.lastrowid
  183. authors = [(codelet_id, a[0], a[1]) for a in codelet.authors]
  184. cursor.executemany(query3, authors)