##// 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 20 import IPython.utils.io
21 21
22 22 from IPython.testing import decorators as testdec
23 from IPython.utils.pickleshare import PickleShareDB
24 23 from IPython.utils.io import ask_yes_no
25 24 from IPython.utils.warn import warn
26 25
@@ -49,6 +48,11 b' class HistoryManager(object):'
49 48 db = None
50 49 # The number of the current session in the history database
51 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 57 # Private interface
54 58 # Variables used to store the three last inputs from the user. On each new
@@ -126,6 +130,7 b' class HistoryManager(object):'
126 130 self.db.execute("""UPDATE singletons SET value=? WHERE
127 131 name='session_number'""", (self.session_number+1,))
128 132 self.db.commit()
133 self.db_cache = []
129 134
130 135 def get_db_history(self, session, start=1, stop=None, raw=True):
131 136 """Retrieve input history from the database by session.
@@ -249,8 +254,14 b' class HistoryManager(object):'
249 254
250 255 self.input_hist_parsed.append(source.rstrip())
251 256 self.input_hist_raw.append(source_raw.rstrip())
252 with self.db:
253 self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
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
263 with self.db:
264 self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
254 265 (self.session_number, self.shell.execution_count,
255 266 source, source_raw))
256 267
@@ -267,6 +278,12 b' class HistoryManager(object):'
267 278 '_iii': self._iii,
268 279 new_i : self._i00 }
269 280 self.shell.user_ns.update(to_main)
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 = []
270 287
271 288 def sync_inputs(self):
272 289 """Ensure raw and translated histories have same length."""
@@ -2528,6 +2528,9 b' class InteractiveShell(Configurable, Magic):'
2528 2528 os.unlink(tfile)
2529 2529 except OSError:
2530 2530 pass
2531
2532 # Write anything in the history cache to the database.
2533 self.history_manager.writeout_cache()
2531 2534
2532 2535 # Clear all user namespaces to release all references cleanly.
2533 2536 self.reset()
General Comments 0
You need to be logged in to leave comments. Login now