How to store uScopy data in MondoDB

From KitwarePublic
Revision as of 20:07, 18 January 2011 by Dhanannjay.deo (talk | contribs)
Jump to navigationJump to search

Server side code

Python code <source lang="python">

login as: dj dj@amber11's password: Last login: Fri Jan 14 21:44:16 2011 from 10.9.0.3 [dj@amber11 ~]$ ls Connectome mod_wsgi-3.3 pymongo www Connectome-build mod_wsgi-3.3.tar.gz python downloads mongo-cxx-driver-nightly VTK libmongoclient.a mongodb-linux-x86_64-static-legacy-1.7.4 VTK-build [dj@amber11 ~]$ cd /var/www-dj/ [dj@amber11 www-dj]$ ls index.py test1.py test4.py test6.pyc tiles3.py mongo.py test2.py test5.py test7.py tiles.py mongo.pyc test3.py test6.py tiles2.py www.tgz [dj@amber11 www-dj]$ ls -la total 72 drwxrwxr-x 2 root dj 4096 Jan 3 13:16 . drwxr-xr-x 25 root root 4096 Dec 22 17:01 .. -rw-r--r-- 1 dj dj 2844 Dec 28 16:10 index.py -rw-r--r-- 1 dj dj 200 Dec 28 16:07 mongo.py -rw-r--r-- 1 dj dj 586 Jan 3 12:32 mongo.pyc -rw-r--r-- 1 dj dj 559 Dec 16 13:11 test1.py -rw-r--r-- 1 dj dj 2844 Dec 9 19:53 test2.py -rw-r--r-- 1 dj dj 1752 Dec 9 20:33 test3.py -rw-r--r-- 1 dj dj 1752 Dec 16 14:40 test4.py -rw-r--r-- 1 dj dj 1752 Dec 16 14:40 test5.py -rw-r--r-- 1 dj dj 70 Dec 16 16:58 test6.py -rw-r--r-- 1 dj dj 202 Dec 28 15:54 test6.pyc -rw-r--r-- 1 dj dj 1295 Dec 28 15:48 test7.py -rw-r--r-- 1 dj dj 1717 Dec 16 19:34 tiles2.py -rw-r--r-- 1 dj dj 1819 Jan 3 12:37 tiles3.py -rw-r--r-- 1 dj dj 1694 Dec 26 21:40 tiles.py -rw-rw-r-- 1 dj dj 3505 Dec 28 16:09 www.tgz [dj@amber11 www-dj]$ ls index.py test1.py test4.py test6.pyc tiles3.py mongo.py test2.py test5.py test7.py tiles.py mongo.pyc test3.py test6.py tiles2.py www.tgz [dj@amber11 www-dj]$ vim tiles3.py """

Decodes the path info and fetches the corresponding tile from pymongo

""" import pymongo import sys

sys.path.append('/var/www-dj/')

from mongo import m

def application(environ, start_response):

       """ The WSGI test application """
       def error(error_msg):
               status = "200 OK"
               response_headers = [('Content-type', 'text/plain'),
                           ('Content-Length', str(len(error_msg)))]
               start_response(status, response_headers)
               return [error_msg]
       # emit status / headers
       db = m.conn['clay1']
       #print 'starting request ..'
       try:
               str2 = environ['PATH_INFO']
       except:
               str2 = '/tiles3.py/n1/t.jpg'
       path = str2.split('/')
       if len(path) != 3:
               #incorrect syntax
               return error(str(path))
       col = db[path[1]]
       res = col.find_one({"name" : path[2]})
       m.conn.end_request()
       if res == None:
               return error('Item not found in database ..')
       # Create the response headers
       status = "200 OK"
       response_headers = [('Content-type', 'image/jpeg'),
                           ('Content-Length', str(len(res['file'])))]
       start_response(status, response_headers)
       #print 'Started response'
       return [str(res['file'])]
       #except:

if __name__ == '__main__':

   # this runs when script is started directly from commandline
   try:
       # create a simple WSGI server and run the application
       from wsgiref import simple_server
       print "Running test application - point your browser at http://localhost:8000/ ..."
       httpd = simple_server.WSGIServer((, 8000), simple_server.WSGIRequestHandler)
       httpd.set_app(application)
       httpd.serve_forever()
   except ImportError:
       # wsgiref not installed, just output html to stdout
                                                                                                                                                                                            1,1           Top

</source>

Mongo module manages connection pool internally. Another simple script is used to recreate indexes after data has been added.



Demo