Skip to content

Instantly share code, notes, and snippets.

@maxpert
Created September 25, 2012 13:14
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save maxpert/3781749 to your computer and use it in GitHub Desktop.
Save maxpert/3781749 to your computer and use it in GitHub Desktop.
Quick and dirty Whoosh Redis storage
"""
Copyright (c) 2012 Zohaib Sibte Hassan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
from whoosh.fields import *
from whoosh.qparser import QueryParser
from redisstore import RedisStore
import redis
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
storage = RedisStore(redis.Redis(connection_pool=pool), "piratebay")
schema = Schema(Title=TEXT(stored=True), Id=ID(stored=True), size=STORED, link=STORED, seeders=STORED, leechers=STORED)
ix = storage.create_index(schema, "IDX")
writer = ix.writer()
fl = open('complete', 'r')
total = 0
for i in fl:
total = total + 1
info = i.split('|')
if info[0] and info[1]:
writer.add_document(
Title=unicode(info[1], errors='ignore'),
Id=unicode(info[0], errors='ignore'),
size=unicode(info[2], errors='ignore'),
link=unicode(info[5], errors='ignore'),
seeders=unicode(info[3], errors='ignore'),
leechers=unicode(info[4], errors='ignore')
)
if total % 10000 == 0:
writer.commit()
writer = ix.writer()
print info[1], total
else:
print "Error!!!"
"""
Copyright (c) 2012 Zohaib Sibte Hassan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from whoosh.fields import *
from whoosh.qparser import QueryParser
from redisstore import RedisStore
import redis
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
storage = RedisStore(redis.Redis(connection_pool=pool), "piratebay")
schema = Schema(Title=TEXT(stored=True), Id=ID(stored=True), size=STORED, link=STORED, seeders=STORED, leechers=STORED)
ix = storage.open_index("IDX", schema)
with ix.searcher() as searcher:
query = QueryParser("Title", ix.schema).parse(u"windows")
results = searcher.search(query)
print results
for i in results:
print i
"""
Redis storage adapter for Whoosh (the absolute dirty way).
Copyright (c) 2012 Zohaib Sibte Hassan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
from cStringIO import StringIO
from threading import Lock
from whoosh.index import _DEF_INDEX_NAME
from whoosh.filedb.structfile import StructFile
from whoosh.filedb.filestore import Storage, create_index, open_index
class RedisStore(Storage):
"""Storage object that keeps the index in redis.
"""
supports_mmap = False
def __file(self, name):
return self.redis.hget("RedisStore:%s" % self.folder, name)
def __init__(self, redis, namespace='redis_store'):
self.folder = namespace
self.redis = redis
self.locks = {}
def create_index(self, schema, indexname=_DEF_INDEX_NAME):
return create_index(self, schema, indexname)
def file_modified(self, name):
return -1
def open_index(self, indexname=_DEF_INDEX_NAME, schema=None):
return open_index(self, schema, indexname)
def list(self):
return self.redis.hkeys("RedisStore:%s" % self.folder)
def clean(self):
self.redis.delete("RedisStore:%s" % self.folder)
def total_size(self):
return sum(self.file_length(f) for f in self.list())
def file_exists(self, name):
return self.redis.hexists("RedisStore:%s" % self.folder, name)
def file_length(self, name):
if not self.file_exists(name):
raise NameError
return len(self.__file(name))
def delete_file(self, name):
if not self.file_exists(name):
raise NameError
self.redis.hdel("RedisStore:%s" % self.folder, name)
def rename_file(self, name, newname, safe=False):
if not self.file_exists(name):
raise NameError("File %r does not exist" % name)
if safe and self.file_exists(newname):
raise NameError("File %r exists" % newname)
content = self.__file(name)
pl = self.redis.pipeline()
pl.hdel("RedisStore:%s" % self.folder, name)
pl.hset("RedisStore:%s" % self.folder, newname, content)
pl.execute()
def create_file(self, name, **kwargs):
def onclose_fn(sfile):
self.redis.hset("RedisStore:%s" % self.folder, name, sfile.file.getvalue())
f = StructFile(StringIO(), name=name, onclose=onclose_fn)
return f
def open_file(self, name, *args, **kwargs):
if not self.file_exists(name):
raise NameError("No such file %r" % name)
def onclose_fn(sfile):
self.redis.hset("RedisStore:%s" % self.folder, name, sfile.file.getvalue())
#print "Opened file %s %s " % (name, self.__file(name))
return StructFile(StringIO(self.__file(name)), name=name, onclose=onclose_fn, *args, **kwargs)
def lock(self, name):
if name not in self.locks:
self.locks[name] = Lock()
return self.locks[name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment