"""
Prepare some data to index, we take all files in /etc

 $ cp -fr /etc /tmp/
 $ cd /tmp/etc/ 
 $ du -sh 
 24M     .
 $ find ./ -type f | wc -l 
 2485
"""

from time import time

# create_catalog_and_index = """
t = time()
from itools.catalog.Catalog import Catalog
catalog = Catalog(fields= [('name', 'keyword', True, True),
                           ('path', 'path', True, True),
                           ('mdate', 'keyword', True, True),
                           ('body', 'text', True, False)])


from itools.handlers import get_handler, File

for h in get_handler('.').traverse():
  name, path = h.name, h.get_abspath()
  mdate = h.timestamp.strftime('%F_%X')

  body = u''
  if isinstance(h, File.File):
      body = h.to_str()

  doc = {'name': name, 
         'path': path,
         'mdate':  mdate, 
         'body': body}
  id = catalog.index_document(doc)

print '%s seconds' % (time() - t, )
# """

s1 = """
for doc in catalog.search(body='mod_mime_magic'):
    print '[%s] %s' % (doc.mdate, doc.path)
"""

s2 = """
for doc in catalog.search(body='mod_mime_magic', path='mods-available'):
    print '[%s] %s' % (doc.mdate, doc.path)
"""

s3 = """
from itools.catalog import queries
q = queries.Equal('body', 'hello')
for doc in catalog.search(q):
    print '[%s] %s' % (doc.mdate, doc.path)
"""

s4 = """
for b in catalog.search(queries.Phrase('body', 'set the value')):
    print '[%s] %s' % (doc.mdate, doc.path)
"""


