Re: svn commit: r290569 - /httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

2005-09-22 Thread Gregory (Grisha) Trubetskoy


Can we have a little discussion on pros/cons of this? Does this make 
mod_python dependent on sqlite?


Thanks,

Grisha


On Tue, 20 Sep 2005 [EMAIL PROTECTED] wrote:


Author: nlehuen
Date: Tue Sep 20 14:28:32 2005
New Revision: 290569

URL: http://svn.apache.org/viewcvs?rev=290569view=rev
Log:
A first try at implementing a session storage relying on SQLite. It is slower 
than FileSession but could scale better ?

Added:
   httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

Added: httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py
URL: 
http://svn.apache.org/viewcvs/httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py?rev=290569view=auto
==
--- httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py (added)
+++ httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py Tue Sep 20 
14:28:32 2005
@@ -0,0 +1,150 @@
+ #
+ # Copyright 2004 Apache Software Foundation
+ #
+ # Licensed under the Apache License, Version 2.0 (the License); you
+ # may not use this file except in compliance with the License.  You
+ # may obtain a copy of the License at
+ #
+ #  http://www.apache.org/licenses/LICENSE-2.0
+ #
+ # Unless required by applicable law or agreed to in writing, software
+ # distributed under the License is distributed on an AS IS BASIS,
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ # implied.  See the License for the specific language governing
+ # permissions and limitations under the License.
+ #
+ # Originally developed by Gregory Trubetskoy.
+ #
+ # $Id: Session.py 231126 2005-08-09 22:26:38Z jgallacher $
+from Session import *
+from time import time
+
+try:
+# If this code is included into Session.py,
+# we don't want to add a dependency to SQLite
+from pysqlite2 import dbapi2 as sqlite
+except:
+pass
+else:
+class SQLiteSession(BaseSession):
+ A session implementation using SQLite to store session data.
+pysqlite2 is required, see http://pysqlite.org/
+
+
+def __init__(self, req, filename=None, sid=0, secret=None, timeout=0, 
lock=1):
+# if no filename is given, get it from PythonOption
+if not filename:
+opts = req.get_options()
+if opts.has_key(session_filename):
+filename = opts[session_filename]
+else:
+# or build a session file in a temporary directory
+filename = os.path.join(
+opts.get('session_directory', tempdir),
+'mp_sess.sqlite'
+)
+
+self.filename = filename
+
+# check whether the sessions table exists, and create it if not
+db = sqlite.connect(self.filename)
+try:
+try:
+cur = db.cursor()
+cur.execute(
+select name from sqlite_master
+where name='sessions' and type='table'
+)
+if cur.fetchone() is None:
+cur.execute(
+create table sessions
+(id text,data blob,timeout real)
+)
+cur.execute(
+create unique index idx_session on sessions (id)
+)
+db.commit()
+finally:
+cur.close()
+finally:
+db.close()
+
+BaseSession.__init__(self, req, sid=sid, secret=secret,
+ timeout=timeout, lock=lock)
+
+def count(self):
+db = sqlite.connect(self.filename)
+try:
+try:
+cur = db.cursor()
+cur.execute(select count(*) from sessions)
+return cur.fetchone()[0]
+finally:
+cur.close()
+finally:
+db.close()
+
+def do_cleanup(self):
+db = sqlite.connect(self.filename)
+try:
+try:
+cur = db.cursor()
+cur.execute(
+delete from sessions where timeout?,
+(time(),)
+)
+db.commit()
+finally:
+cur.close()
+finally:
+db.close()
+
+def do_load(self):
+db = sqlite.connect(self.filename)
+try:
+try:
+cur = db.cursor()
+cur.execute(
+select data from sessions where id=?,
+(self._sid,)
+)
+row = cur.fetchone()
+if row is None:
+

Re: svn commit: r290569 - /httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

2005-09-22 Thread Robert Sanderson

Can we have a little discussion on pros/cons of this? Does this make
mod_python dependent on sqlite?

Nope.  It'll just silently fail if it can't import dbapi2.

Rob

 +try:
 +# If this code is included into Session.py,
 +# we don't want to add a dependency to SQLite
 +from pysqlite2 import dbapi2 as sqlite
 +except:
 +pass
 +else:
 +class SQLiteSession(BaseSession):


Re: svn commit: r290569 - /httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

2005-09-22 Thread Gregory (Grisha) Trubetskoy


OK, my next question would be - is MySQL, PostgreSQL, Informix, Oracle, 
etc next, and is this the path we want to take, or is there something 
about sqlite that makes it unique?


Grisha


On Thu, 22 Sep 2005, Robert Sanderson wrote:




Can we have a little discussion on pros/cons of this? Does this make
mod_python dependent on sqlite?


Nope.  It'll just silently fail if it can't import dbapi2.

Rob


+try:
+# If this code is included into Session.py,
+# we don't want to add a dependency to SQLite
+from pysqlite2 import dbapi2 as sqlite
+except:
+pass
+else:
+class SQLiteSession(BaseSession):




Re: svn commit: r290569 - /httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

2005-09-22 Thread Jim Gallacher

Gregory (Grisha) Trubetskoy wrote:


OK, my next question would be - is MySQL, PostgreSQL, Informix, Oracle, 
etc next, 


Yes. ;)

and is this the path we want to take, or is there something 
about sqlite that makes it unique?




I don't know if it is that path we *want* to take, but I think there is 
a certain inevitability that it's the path we *will* take. I'm 
personally thinking about a MySQL backend, not because I need it but 
because I'm curious to compare the performance with FileSession.


The best approach might be to refactor Session.py to easily accommodate 
subclasses for other persistent stores. Any new session subclasses get 
their own file eg.


mod_python/sessions/sqlite.py
mod_python/sessions/mysql.py

That way there are no dependency problems in Session.py. If some code 
imports sessions.sqlite and sqlite is not installed then let it raise an 
exception as it should.


The current Session.py could stay as is, or we could split FileSession, 
DbmSession and MemorySession into their own files and just have a stub 
in Session.py for backward compatability. This reorganization may also 
make it easier for users to create their own session subclasses.Related 
to this, the current code for Session.Session only allows one of the 
standard session classes to be specified by PythonOption session. It 
would be nice if it there was a way to make this more dynamic and the 
refactoring might assist in this.


On another note, if we are starting to roll with new features for 3.3 I 
would suggest we need to immediately create a new svn branch for 3.2 
bugfixes.


Regards,
Jim



On Thu, 22 Sep 2005, Robert Sanderson wrote:




Can we have a little discussion on pros/cons of this? Does this make
mod_python dependent on sqlite?



Nope.  It'll just silently fail if it can't import dbapi2.

Rob


+try:
+# If this code is included into Session.py,
+# we don't want to add a dependency to SQLite
+from pysqlite2 import dbapi2 as sqlite
+except:
+pass
+else:
+class SQLiteSession(BaseSession):









Re: svn commit: r290569 - /httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

2005-09-22 Thread Jim Gallacher

Grisha,

Your message implies that there is a mailing list for mod_python svn 
commit messages. How can I subscribe to this?


Thanks,
Jim

Gregory (Grisha) Trubetskoy wrote:


Can we have a little discussion on pros/cons of this? Does this make 
mod_python dependent on sqlite?


Thanks,

Grisha


On Tue, 20 Sep 2005 [EMAIL PROTECTED] wrote:


Author: nlehuen
Date: Tue Sep 20 14:28:32 2005
New Revision: 290569

URL: http://svn.apache.org/viewcvs?rev=290569view=rev
Log:
A first try at implementing a session storage relying on SQLite. It is 
slower than FileSession but could scale better ?


Added:
   httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py

Added: httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py
URL: 
http://svn.apache.org/viewcvs/httpd/mod_python/trunk/lib/python/mod_python/SQLiteSession.py?rev=290569view=auto