I think Option 1 if faster on GAE Datastore. Option 3 is the best on
SQL because you can do

db.define_table('data', Field('value'))
db.define_table('tag', Field('record_id', db.data), Field('name'))

def search_or(data=db.data, tag=db.tag, tags=[]):
    rows = db(data.id==tag.record_id)\
             (tag.name.belongs(tags)).select(
                   data.ALL,
                   orderby=data.id,
                   groupby=data.id,
                   distinct=True)
    return rows

def search_and(data=db.data,tag=db.tag,tags=[]):
    n = len(tags)
    rows = db(data.id==tag.record_id)\
             (tag.name.belongs(tags)).select(
                   data.ALL,
                   orderby=data.id,
                   groupby=data.id,
                   having=data.id.count()==n)
    return rows

@Bruno. It is in the recipes book. ;-)


On Oct 10, 8:53 pm, Bruno Rocha <rochacbr...@gmail.com> wrote:
> Clarifying:
>
> Option 1:
>
> > db.define_table('item',Field('tags','list:string'))
>
> *Pro*:
> Easy search:
> tag = request.args(0)
> results = db(db.item.tags.contains(tag)).select()
>
> *Con*:
> build a tag cloud
> records = db(db.item.id>0).select(db.item.tags)
> alltags = [] # loop thought records to populate the tags list (here you can
> use reduce, list comprehension)
>
>
>
> > Option 2:
> > db.define_table('tag')
> > db.define_table('item',Field('tags','list:reference tag'))
>
> *Pro*:
> build a tag clous
> alltags = [ r.tag for r in db(db.tag.id>0).select('tag')]
>
> *Con:*
> Search is expensive
> tag = request.args(0) # it is a string like: "book"
> tag_id = db.tag(tag=tag)[0].id # or a taglist
> records = db(db.item.tag.contains(tag_id)).select()
>
> Options 3:
>
> > db.define_table('item')
> > db.define_table('tag')
> > db.define_table('item_tag',Field('item'),Field('tag'))
>
> *Con:*
> In this case I see Tag searching and tagcloud as a expensive process.
>
> But, certainly Massimo has a DAL trick to solve this use case in one line of
> code ;)
>
> --
> Bruno Rocha
> [ About me:http://zerp.ly/rochacbruno]
> [ Aprenda a programar:http://CursoDePython.com.br]

Reply via email to