[mirrorbrain-commits] r7908 - in /trunk/mirrordoctor: mb/dbmaint.py mirrordoctor.py

From: <poeml_at_mirrorbrain.org>
Date: Sat, 05 Dec 2009 15:00:40 -0000
Author: poeml
Date: Sat Dec  5 16:00:40 2009
New Revision: 7908

URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain?rev=7908&view=rev
Log:
mb vacuum:
- command renamed to "mb db", with more features:
  mb vacuum - cleans up dead references
  mb sizes - shows sizes of all relations
  mb shell - conveniently open a shell for the database 

Modified:
    trunk/mirrordoctor/mb/dbmaint.py
    trunk/mirrordoctor/mirrordoctor.py

Modified: trunk/mirrordoctor/mb/dbmaint.py
URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/mirrordoctor/mb/dbmaint.py?rev=7908&r1=7907&r2=7908&view=diff
==============================================================================
--- trunk/mirrordoctor/mb/dbmaint.py (original)
+++ trunk/mirrordoctor/mb/dbmaint.py Sat Dec  5 16:00:40 2009
_at_@ -22,7 +22,7 @@
     print 'Done.'
 
 
-def dbstats(conn):
+def stats(conn):
     """show statistics about stale files in the database"""
 
     query = """SELECT relname, relkind, relfilenode, reltuples, relpages, 
_at_@ -42,3 +42,16 @@
         print '%5.1f    %s' % (sizeMB, name)
 
     print 'Total: %.1f' % total
+
+
+def shell(c):
+    """spawn a database shell (psql).
+    
+    The argument is the configuration structure for that MirrorBrain instance."""
+    import os
+    os.environ['PGHOST'] = c.get('dbhost')
+    os.environ['PGPORT'] = c.get('dbport', '5432')
+    os.environ['PGUSER'] = c.get('dbuser')
+    os.environ['PGPASSWORD'] = c.get('dbpass')
+    os.environ['PGDATABASE'] = c.get('dbname')
+    os.system('psql')

Modified: trunk/mirrordoctor/mirrordoctor.py
URL: http://svn.mirrorbrain.org/viewvc/mirrorbrain/trunk/mirrordoctor/mirrordoctor.py?rev=7908&r1=7907&r2=7908&view=diff
==============================================================================
--- trunk/mirrordoctor/mirrordoctor.py (original)
+++ trunk/mirrordoctor/mirrordoctor.py Sat Dec  5 16:00:40 2009
_at_@ -820,38 +820,71 @@
             mirror.score = int(score)
         
 
-    _at_cmdln.option('--size', action='store_true',
-                  help='don\'t delete, but show how much size the database uses.')
+    # the previous command name
+    _at_cmdln.alias('vacuum')
+
     _at_cmdln.option('-n', '--dry-run', action='store_true',
                   help='don\'t delete, but only show statistics.')
-    def do_vacuum(self, subcmd, opts, *args):
+    def do_db(self, subcmd, opts, *args):
         """${cmd_name}: perform database maintenance
         
-        If called without further argumets, the command cleans up unreferenced
-        files from the mirror database.
-        This should be done once a week for a busy file tree.  Otherwise it
-        should be rarely needed, but can possibly improve performance if it is
-        able to shrink the database.
-
-        When called with the -n option, only the number of files to be cleaned
-        up is printed. This is purely for information.
-
-        When called with the --size option, the size of each database relation
-        will be preinted, which can provide insight for the appropriate
-        database tuning.
-
-        ${cmd_usage}
-        ${cmd_option_list}
-        """
-
-        import mb.vacuum
-
-        if opts.size:
-            mb.vacuum.dbstats(self.conn)
-        else:
-            mb.vacuum.stale(self.conn)
+        This command needs to be called with one of the following actions:
+        
+        vacuum
+          Clean up unreferenced
+          files from the mirror database.
+          This should be done once a week for a busy file tree.  Otherwise it
+          should be rarely needed, but can possibly improve performance if it
+          is able to shrink the database.
+
+          When called with the -n option, only the number of files to be
+          cleaned up is printed. This is purely for information.
+
+        sizes
+          Print the size of each database relation. This can provide insight
+          for the most appropriate database tuning.
+
+        shell
+          Conveniently open a database shell.
+
+
+        usage:
+            mb vacuum [-n]
+            mb sizes
+            mb shell
+        ${cmd_option_list}
+        """
+
+        import mb.dbmaint
+
+        # this subcommand was renamed from "mb vacuum" to "mb db <action>"
+        # let's keep the old way working
+        if subcmd == 'vacuum':
+                mb.dbmaint.stale(self.conn)
+                mb.dbmaint.vacuum(self.conn)
+                sys.exit(0)
+
+        if len(args) < 1:
+            sys.exit('Too few arguments.')
+        action = args[0]
+
+        if action == 'sizes':
+            mb.dbmaint.stats(self.conn)
+        elif action == 'vacuum':
             if not opts.dry_run:
-                mb.vacuum.vacuum(self.conn)
+                mb.dbmaint.stale(self.conn)
+                mb.dbmaint.vacuum(self.conn)
+            else:
+                mb.dbmaint.stale(self.conn)
+        elif action == 'shell':
+            mb.dbmaint.shell(self.config.dbconfig)
+
+        else:
+            sys.exit('unknown action %r' % action)
+
+
+
+
 
 
     _at_cmdln.option('-u', '--url', action='store_true',




_______________________________________________
mirrorbrain-commits mailing list
Archive: http://mirrorbrain.org/archive/mirrorbrain-commits/

Note: To remove yourself from this list, send a mail with the content
 	unsubscribe
to the address mirrorbrain-commits-request_at_mirrorbrain.org
Received on Sat Dec 05 2009 - 15:00:41 GMT

This archive was generated by hypermail 2.3.0 : Mon Feb 20 2012 - 23:47:04 GMT