Revision 4819d34f pithos/backends/lib/sqlite/node.py

b/pithos/backends/lib/sqlite/node.py
157 157
        
158 158
        execute(""" create table if not exists attributes
159 159
                          ( serial integer,
160
                            domain text,
160 161
                            key    text,
161 162
                            value  text,
162
                            primary key (serial, key)
163
                            primary key (serial, domain, key)
163 164
                            foreign key (serial)
164 165
                            references versions(serial)
165 166
                            on update cascade
......
546 547
        self.execute(q, (serial,))
547 548
        return hash
548 549
    
549
    def attribute_get(self, serial, keys=()):
550
    def attribute_get(self, serial, domain, keys=()):
550 551
        """Return a list of (key, value) pairs of the version specified by serial.
551 552
           If keys is empty, return all attributes.
552 553
           Othwerise, return only those specified.
......
556 557
        if keys:
557 558
            marks = ','.join('?' for k in keys)
558 559
            q = ("select key, value from attributes "
559
                 "where key in (%s) and serial = ?" % (marks,))
560
            execute(q, keys + (serial,))
560
                 "where key in (%s) and serial = ? and domain = ?" % (marks,))
561
            execute(q, keys + (serial, domain))
561 562
        else:
562 563
            q = "select key, value from attributes where serial = ?"
563 564
            execute(q, (serial,))
564 565
        return self.fetchall()
565 566
    
566
    def attribute_set(self, serial, items):
567
    def attribute_set(self, serial, domain, items):
567 568
        """Set the attributes of the version specified by serial.
568 569
           Receive attributes as an iterable of (key, value) pairs.
569 570
        """
570 571
        
571
        q = ("insert or replace into attributes (serial, key, value) "
572
             "values (?, ?, ?)")
573
        self.executemany(q, ((serial, k, v) for k, v in items))
572
        q = ("insert or replace into attributes (serial, domain, key, value) "
573
             "values (?, ?, ?, ?)")
574
        self.executemany(q, ((serial, domain, k, v) for k, v in items))
574 575
    
575
    def attribute_del(self, serial, keys=()):
576
    def attribute_del(self, serial, domain, keys=()):
576 577
        """Delete attributes of the version specified by serial.
577 578
           If keys is empty, delete all attributes.
578 579
           Otherwise delete those specified.
579 580
        """
580 581
        
581 582
        if keys:
582
            q = "delete from attributes where serial = ? and key = ?"
583
            self.executemany(q, ((serial, key) for key in keys))
583
            q = "delete from attributes where serial = ? and domain = ? and key = ?"
584
            self.executemany(q, ((serial, domain, key) for key in keys))
584 585
        else:
585
            q = "delete from attributes where serial = ?"
586
            self.execute(q, (serial,))
586
            q = "delete from attributes where serial = ? and domain = ?"
587
            self.execute(q, (serial, domain))
587 588
    
588 589
    def attribute_copy(self, source, dest):
589 590
        q = ("insert or replace into attributes "
590
             "select ?, key, value from attributes "
591
             "select ?, domain, key, value from attributes "
591 592
             "where serial = ?")
592 593
        self.execute(q, (dest, source))
593 594
    
594
    def _construct_filters(self, filterq):
595
        if not filterq:
595
    def _construct_filters(self, domain, filterq):
596
        print '***', domain, filterq
597
        if not domain or not filterq:
596 598
            return None, None
597 599
        
598 600
        subqlist = []
599 601
        append = subqlist.append
600
        included, excluded, opers = parse_filters(filterq.split(','))
602
        included, excluded, opers = parse_filters(filterq)
601 603
        args = []
602 604
        
603 605
        if included:
......
616 618
            t = (("(a.key = ? and a.value %s ?)" % (o,)) for k, o, v in opers)
617 619
            subq = "(" + ' or '.join(t) + ")"
618 620
            for k, o, v in opers:
619
                args += (k, v)
621
                args += [k, v]
620 622
            append(subq)
621 623
        
622 624
        if not subqlist:
623 625
            return None, None
624 626
        
625
        subq = ' ' + ' and '.join(subqlist)
627
        subq = ' and a.domain = ? and ' + ' and '.join(subqlist)
628
        args = [domain] + args
626 629
        
627 630
        return subq, args
628 631
    
......
637 640
        
638 641
        return subq, args
639 642
    
640
    def latest_attribute_keys(self, parent, before=inf, except_cluster=0, pathq=[]):
643
    def latest_attribute_keys(self, parent, domain, before=inf, except_cluster=0, pathq=[]):
641 644
        """Return a list with all keys pairs defined
642 645
           for all latest versions under parent that
643 646
           do not belong to the cluster.
......
654 657
                           "from nodes "
655 658
                           "where parent = ?) "
656 659
             "and a.serial = v.serial "
660
             "and a.domain = ? "
657 661
             "and n.node = v.node")
658
        args = (before, except_cluster, parent)
662
        args = (before, except_cluster, parent, domain)
659 663
        subq, subargs = self._construct_paths(pathq)
660 664
        if subq is not None:
661 665
            q += subq
......
665 669
    
666 670
    def latest_version_list(self, parent, prefix='', delimiter=None,
667 671
                            start='', limit=10000, before=inf,
668
                            except_cluster=0, pathq=[], filterq=None):
672
                            except_cluster=0, pathq=[], domain=None, filterq=[]):
669 673
        """Return a (list of (path, serial) tuples, list of common prefixes)
670 674
           for the current versions of the paths with the given parent,
671 675
           matching the following criteria.
......
736 740
        if subq is not None:
737 741
            q += subq
738 742
            args += subargs
739
        subq, subargs = self._construct_filters(filterq)
743
        subq, subargs = self._construct_filters(domain, filterq)
740 744
        if subq is not None:
741 745
            q += subq
742 746
            args += subargs

Also available in: Unified diff