##// END OF EJS Templates
Ability to cache commits before writing to disk, to save power.
Thomas Kluyver -
Show More
@@ -20,7 +20,6 b' import sqlite3'
20 import IPython.utils.io
20 import IPython.utils.io
21
21
22 from IPython.testing import decorators as testdec
22 from IPython.testing import decorators as testdec
23 from IPython.utils.pickleshare import PickleShareDB
24 from IPython.utils.io import ask_yes_no
23 from IPython.utils.io import ask_yes_no
25 from IPython.utils.warn import warn
24 from IPython.utils.warn import warn
26
25
@@ -49,6 +48,11 b' class HistoryManager(object):'
49 db = None
48 db = None
50 # The number of the current session in the history database
49 # The number of the current session in the history database
51 session_number = None
50 session_number = None
51 # Number of lines to cache before writing to the database (to save power)
52 # - if 0, lines will be written instantly.
53 db_cache_size = 0
54 # The line cache
55 db_cache = None
52
56
53 # Private interface
57 # Private interface
54 # Variables used to store the three last inputs from the user. On each new
58 # Variables used to store the three last inputs from the user. On each new
@@ -126,6 +130,7 b' class HistoryManager(object):'
126 self.db.execute("""UPDATE singletons SET value=? WHERE
130 self.db.execute("""UPDATE singletons SET value=? WHERE
127 name='session_number'""", (self.session_number+1,))
131 name='session_number'""", (self.session_number+1,))
128 self.db.commit()
132 self.db.commit()
133 self.db_cache = []
129
134
130 def get_db_history(self, session, start=1, stop=None, raw=True):
135 def get_db_history(self, session, start=1, stop=None, raw=True):
131 """Retrieve input history from the database by session.
136 """Retrieve input history from the database by session.
@@ -249,6 +254,12 b' class HistoryManager(object):'
249
254
250 self.input_hist_parsed.append(source.rstrip())
255 self.input_hist_parsed.append(source.rstrip())
251 self.input_hist_raw.append(source_raw.rstrip())
256 self.input_hist_raw.append(source_raw.rstrip())
257 if self.db_cache_size:
258 self.db_cache.append((self.session_number,
259 self.shell.execution_count, source, source_raw))
260 if len(self.db_cache) > self.db_cache_size:
261 self.writeout_cache()
262 else: # Instant write
252 with self.db:
263 with self.db:
253 self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
264 self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
254 (self.session_number, self.shell.execution_count,
265 (self.session_number, self.shell.execution_count,
@@ -268,6 +279,12 b' class HistoryManager(object):'
268 new_i : self._i00 }
279 new_i : self._i00 }
269 self.shell.user_ns.update(to_main)
280 self.shell.user_ns.update(to_main)
270
281
282 def writeout_cache(self):
283 with self.db:
284 self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)",
285 self.db_cache)
286 self.db_cache = []
287
271 def sync_inputs(self):
288 def sync_inputs(self):
272 """Ensure raw and translated histories have same length."""
289 """Ensure raw and translated histories have same length."""
273 lr = len(self.input_hist_raw)
290 lr = len(self.input_hist_raw)
@@ -2529,6 +2529,9 b' class InteractiveShell(Configurable, Magic):'
2529 except OSError:
2529 except OSError:
2530 pass
2530 pass
2531
2531
2532 # Write anything in the history cache to the database.
2533 self.history_manager.writeout_cache()
2534
2532 # Clear all user namespaces to release all references cleanly.
2535 # Clear all user namespaces to release all references cleanly.
2533 self.reset()
2536 self.reset()
2534
2537
General Comments 0
You need to be logged in to leave comments. Login now