How to store uScopy data in MondoDB: Difference between revisions
From KitwarePublic
Jump to navigationJump to search
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Server side code | |||
Python code | |||
<source lang="python"> | |||
""" | |||
Decodes the path info and fetches the corresponding tile from pymongo | |||
""" | |||
import pymongo | |||
import sys | |||
# add module path | |||
sys.path.append('/var/www-dj/') | |||
# import module with singleton instance | |||
from mongo import m | |||
def application(environ, start_response): | |||
""" The WSGI application to retrieve images from database """ | |||
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'] | |||
try: | |||
str2 = environ['PATH_INFO'] | |||
except: | |||
# Give some default request if | |||
# no parameters passed in URL | |||
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) | |||
return [str(res['file'])] | |||
</source> | |||
Mongo module manages connection pool internally. | |||
Another simple script is used to recreate indexes after data has been added. | |||
==Demo== | ==Demo== | ||
* [http://dash1old/connectome/index2.php?image=z3459 | * [http://dash1old/connectome/index2.php?image=z3459 From Kitware network] | ||
* [http://tripoint.kitware.com/connectome/index2.php?image=z3459 | * [http://tripoint.kitware.com/connectome/index2.php?image=z3459 From outside] |
Latest revision as of 20:13, 18 January 2011
Server side code
Python code <source lang="python"> """
Decodes the path info and fetches the corresponding tile from pymongo
""" import pymongo import sys
- add module path
sys.path.append('/var/www-dj/')
- import module with singleton instance
from mongo import m
def application(environ, start_response):
""" The WSGI application to retrieve images from database """ 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']
try: str2 = environ['PATH_INFO'] except: # Give some default request if # no parameters passed in URL 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) return [str(res['file'])]
</source>
Mongo module manages connection pool internally. Another simple script is used to recreate indexes after data has been added.