Exemplo n.º 1
0
 def __init__(self, session, data=0, recordStore=None):
     if isinstance(data, SimpleBitfield):
         self.bitfield = data
     else:
         self.bitfield = SimpleBitfield(data)
     self.currItems = None
     self.recordStore = recordStore
     self.relevancy = 0
Exemplo n.º 2
0
 def test_difference(self):
     "Test difference (i.e. AND NOT) of two SimpleBitfields."
     field = SimpleBitfield('110011')
     field.difference(SimpleBitfield('001100'))
     self.assertEqual(field._d, int('110011', 2))
     field = SimpleBitfield('110011')
     field.difference(SimpleBitfield('001110'))
     self.assertEqual(field._d, int('110001', 2))
     field = SimpleBitfield('110011')
     field.difference(SimpleBitfield('011100'))
     self.assertEqual(field._d, int('100011', 2))
Exemplo n.º 3
0
 def test_intersection(self):
     "Test intersection of two SimpleBitfields."
     field = SimpleBitfield('110011')
     field.intersection(SimpleBitfield('001100'))
     self.assertEqual(field._d, int('000000', 2))
     field = SimpleBitfield('110011')
     field.intersection(SimpleBitfield('001110'))
     self.assertEqual(field._d, int('000010', 2))
     field = SimpleBitfield('110011')
     field.intersection(SimpleBitfield('011100'))
     self.assertEqual(field._d, int('010000', 2))
Exemplo n.º 4
0
 def __init__(self, session, data=0, recordStore=None):
     if isinstance(data, SimpleBitfield):
         self.bitfield = data
     else:
         self.bitfield = SimpleBitfield(data)
     self.currItems = None
     self.recordStore = recordStore
     self.relevancy = 0
Exemplo n.º 5
0
 def test_lenTrueItems(self):
     "Test lenTrueItems method."
     field = SimpleBitfield('110011')
     self.assertEqual(field.lenTrueItems(), 4)
     field = SimpleBitfield('100011')
     self.assertEqual(field.lenTrueItems(), 3)
     field = SimpleBitfield('0011')
     self.assertEqual(field.lenTrueItems(), 2)
     field = SimpleBitfield('1')
     self.assertEqual(field.lenTrueItems(), 1)
Exemplo n.º 6
0
 def test_trueItems(self):
     "Test lenTrueItems method."
     field = SimpleBitfield('110011')
     self.assertListEqual(field.trueItems(),
                          [0, 1, 4, 5])
     field = SimpleBitfield('110001')
     self.assertListEqual(field.trueItems(),
                          [0, 1, 5])
     field = SimpleBitfield('1011')
     self.assertListEqual(field.trueItems(),
                          [0, 2, 3])
     field = SimpleBitfield('1')
     self.assertListEqual(field.trueItems(),
                          [0])
Exemplo n.º 7
0
 def deserialize(self, data):
     self.bitfield = SimpleBitfield(data)
Exemplo n.º 8
0
class BitmapResultSet(ResultSet):
    bitfield = None
    currItems = None
    recordStore = None

    relevancy = 0
    termid = -1
    totalOccs = 0
    totalRecs = 0
    id = ""
    index = None
    queryTerm = ""
    queryFreq = 0
    queryPositions = []
    relevancy = 0
    maxWeight = 0
    minWeight = 0

    def __init__(self, session, data=0, recordStore=None):
        if isinstance(data, SimpleBitfield):
            self.bitfield = data
        else:
            self.bitfield = SimpleBitfield(data)
        self.currItems = None
        self.recordStore = recordStore
        self.relevancy = 0

    def __getitem__(self, k):
        if self.currItems == None:
            self.currItems = self.bitfield.trueItems()            
        return SimpleResultSetItem(None, self.currItems[k], self.recordStore, 1)

    def __len__(self):
        return self.bitfield.lenTrueItems()

    def serialise(self, session):
        return self.serialize(session)
    
    def serialize(self, session):
        return str(self.bitfield)

    def deserialise(self, data):
        return self.deserialize(data)
        
    def deserialize(self, data):
        self.bitfield = SimpleBitfield(data)

    def get_item(self, item):
        try:
            if self.bitfield[item.id]:
                return item
        except IndexError:
            pass
        return None

    def combine(self, session, others, clause, db=None):
        if (isinstance(clause, cqlParser.Triple)):
            cql = clause.boolean
        else:
            cql = clause.relation
        v = cql.value

        # Check if all are bitmaps
        if v in ['=', 'exact', 'prox']:
            if len(others) == 1:
                return others[0]
            else:
                raise NotImplementedError()

        allbits = 1
        for o in others:
            if not hasattr(o, 'bitfield'):
                allbits = 0
                break

        if allbits:
            if (v in ['all', 'and']):            
                s = others[0].bitfield
                for o in others[1:]:
                    s.intersection(o.bitfield)
            elif (v in ['any', 'or', '>', '>=', '<', '<=']):
                s = others[0].bitfield
                for o in others[1:]:
                    s.union(o.bitfield)
            elif (v == 'not'):
                s = others[0].bitfield
                for o in others[1:]:
                    s.difference(o.bitfield)
            else:
                raise NotImplementedError()
            self.bitfield = s
        else:
            # XXX Merging Bitmap with non bitmap
            pass
        return self

    
    def order(self, spec):
        # Reorder a bitmap?!
        raise NotImplementedError()

    def retrieve(self, numReq, start, cache=0):
        end = min(start+numrecs+1, len(self))
        recs = []
        # XXX This should cache server, db and resultSet
        for r in range(start, end):
            recs.append(self[r].fetch_record(session))
        return recs
Exemplo n.º 9
0
 def deserialize(self, data):
     self.bitfield = SimpleBitfield(data)
Exemplo n.º 10
0
class BitmapResultSet(ResultSet):
    bitfield = None
    currItems = None
    recordStore = None
    fromStore = 0
    relevancy = 0
    termid = -1
    totalOccs = 0
    totalRecs = 0
    id = ""
    index = None
    queryTerm = ""
    queryFreq = 0
    queryPositions = []
    relevancy = 0
    maxWeight = 0
    minWeight = 0

    def __init__(self, session, data=0, recordStore=None):
        if isinstance(data, SimpleBitfield):
            self.bitfield = data
        else:
            self.bitfield = SimpleBitfield(data)
        self.currItems = None
        self.recordStore = recordStore
        self.relevancy = 0

    def __getitem__(self, k):
        if self.currItems is None:
            self.currItems = self.bitfield.trueItems()            
        return SimpleResultSetItem(None, self.currItems[k], self.recordStore, 1)

    def __len__(self):
        return self.bitfield.lenTrueItems()

    def serialise(self, session):
        return self.serialize(session)
    
    def serialize(self, session):
        return str(self.bitfield)

    def deserialise(self, data):
        return self.deserialize(data)
        
    def deserialize(self, data):
        self.bitfield = SimpleBitfield(data)

    def get_item(self, item):
        try:
            if self.bitfield[item.id]:
                return item
        except IndexError:
            pass
        return None

    def combine(self, session, others, clause, db=None):
        if (isinstance(clause, cqlParser.Triple)):
            cql = clause.boolean
        else:
            cql = clause.relation
        v = cql.value

        # Check if all are bitmaps
        if v in ['=', 'exact', 'prox']:
            if len(others) == 1:
                return others[0]
            else:
                raise NotImplementedError()

        allbits = 1
        for o in others:
            if not hasattr(o, 'bitfield'):
                allbits = 0
                break

        if allbits:
            if (v in ['all', 'and']):            
                s = others[0].bitfield
                for o in others[1:]:
                    s.intersection(o.bitfield)
            elif (v in ['any', 'or', '>', '>=', '<', '<=']):
                s = others[0].bitfield
                for o in others[1:]:
                    s.union(o.bitfield)
            elif (v == 'not'):
                s = others[0].bitfield
                for o in others[1:]:
                    s.difference(o.bitfield)
            else:
                raise NotImplementedError()
            self.bitfield = s
        else:
            # XXX Merging Bitmap with non bitmap
            pass
        return self

    
    def order(self, spec):
        # Reorder a bitmap?!
        raise NotImplementedError()

    def retrieve(self, numReq, start, cache=0):
        end = min(start+numrecs+1, len(self))
        recs = []
        # XXX This should cache server, db and resultSet
        for r in range(start, end):
            recs.append(self[r].fetch_record(session))
        return recs