예제 #1
0
    def get_links(self, session, relation, *args, **kw):
        """Get linked rows in the named relation.

        NOT API
        """
        condvals = []
        conds = []
        for obj in args:
            oid = obj.id
            if (self.idNormalizer):
                oid = self.idNormalizer.process_string(session, oid)
            # Allows get_links for objects other than Records
            condvals.append(oid)
            conds.append("{0} = %s".format(self.table))

        for (name, value) in kw.iteritems():
            condvals.append(value)
            conds.append("{0} = %s".format(name))
        query = ("SELECT * FROM {0}_{1} WHERE {2};"
                 "".format(self.table, relation, ', '.join(conds)))
        res = self._query(query, tuple(condvals))
        links = []
        reln = self.relations[relation]
        for row in res:
            link = []
            linkHash = {}
            for i in range(len(row)):
                name = reln[i - 1][0]
                if (reln[i - 1][2]):
                    link.append(SimpleResultSetItem(session, row[i], name))
                else:
                    linkHash[name] = row[i]

            links.append((link, linkHash))
        return links
예제 #2
0
    def search(self, session, query, db):
        # take CQL query and translate to Lucene
        pm = db.get_path(session, 'protocolMap')
        if not pm:
            db._cacheProtocolMaps(session)
            pm = db.protocolMaps.get('http://www.loc.gov/zing/srw/')
        query.config = pm
        lq = cqlToLucene(session, query, pm)
        q = self.parser.parse(lq)
        results = self.searcher.search(q, lucene.Sort.RELEVANCE)

        # now map to a ResultSet
        items = []
        for x in range(len(results)):
            hit = results[x]
            w = results.score(x)
            rsid = hit.getField('id').stringValue()
            (recStore, id) = rsid.split('/')
            if id.isdigit():
                id = int(id)
            rsi = SimpleResultSetItem(session, id, recStore, weight=w)
            items.append(rsi)

        rs = SimpleResultSet(session, items)
        return rs
예제 #3
0
    def get_links(self, session, relation, *args, **kw):
        conds = []
        for obj in args:
            oid = obj.id
            if (self.idNormalizer):
                oid = self.idNormalizer.process_string(session, oid)
            #cond += ("%s = %r, " % (obj.recordStore, oid))
            conds.append(
                "%s = %r" %
                (self.table,
                 oid))  # allows get_links for objects other than Records
        for (name, value) in kw.iteritems():
            conds.append("%s = %r" % (name, value))
        query = "SELECT * FROM %s_%s WHERE %s;" % (self.table, relation,
                                                   ', '.join(conds))
        res = self._query(query)

        links = []
        reln = self.relations[relation]
        for row in res.getresult():
            link = []
            linkHash = {}
            for i in range(len(row)):
                name = reln[i - 1][0]
                if (reln[i - 1][2]):
                    link.append(SimpleResultSetItem(session, row[i], name))
                else:
                    linkHash[name] = row[i]

            links.append((link, linkHash))
        return links
예제 #4
0
    def setUp(self):
        """Setup some ResultsetItems and put them into ResultSets to evaluate.

        N.B. a == b, other pairs should not evaluate as equal
        """
        self.session = session = Session()
        # Set up same 4 ResultSetItems as for SimpleResultSetItemTestCase
        self.rsi1 = SimpleResultSetItem(session,
                                        id=0,
                                        recStore="recordStore",
                                        occs=5,
                                        database="",
                                        diagnostic=None,
                                        weight=0.5,
                                        resultSet=None,
                                        numeric=None)
        self.rsi2 = SimpleResultSetItem(session,
                                        id=0,
                                        recStore="recordStore",
                                        occs=3,
                                        database="",
                                        diagnostic=None,
                                        weight=0.5,
                                        resultSet=None,
                                        numeric=None)
        self.rsi3 = SimpleResultSetItem(session,
                                        id=1,
                                        recStore="recordStore",
                                        occs=1,
                                        database="",
                                        diagnostic=None,
                                        weight=0.5,
                                        resultSet=None,
                                        numeric=None)
        self.rsi4 = SimpleResultSetItem(session,
                                        id=0,
                                        recStore="recordStore2",
                                        occs=2,
                                        database="",
                                        diagnostic=None,
                                        weight=0.5,
                                        resultSet=None,
                                        numeric=None)
        # Put identical (rsi1 and rsi2) into separate ResultSets
        self.a = SimpleResultSet(session, [self.rsi1, self.rsi3], id="a")
        self.b = SimpleResultSet(session, [self.rsi2, self.rsi4], id="b")
예제 #5
0
 def construct_resultSetItem(self, session, recId,
                             recStoreId, nOccs, rsiType=None):
     numericId = recId
     recStore = ["recordStore"][recStoreId]
     return SimpleResultSetItem(session, recId, recStore,
                                nOccs, session.database,
                                numeric=numericId
                                )
예제 #6
0
 def _deserialise(self, session, data, size, id):
     data = data.decode('string_escape')
     fmt = 'l' * size
     ids = struct.unpack(fmt, data)
     # can't use bitfield, as need to preserve order
     rset = SimpleResultSet(session)
     items = [SimpleResultSetItem(session, x, resultSet=rset) for x in ids]
     rset.fromList(items)
     rset.id = id
     return rset
예제 #7
0
 def _get_test_resultSets(self):
     for x in range(5):
         rs = SimpleResultSet(self.session)
         for y in range(5):
             occs = 5 - x
             rs.append(
                 SimpleResultSetItem(self.session,
                                     id=y,
                                     recStore="recordStore",
                                     occs=occs,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None))
         yield rs
예제 #8
0
    def setUp(self):
        """Setup some ResultsetItems to evaluate.

        N.B. a == b, other pairs should not evaluate as equal
        """
        self.session = session = Session()
        self.a = SimpleResultSetItem(session,
                                     id=0,
                                     recStore="recordStore",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
        self.b = SimpleResultSetItem(session,
                                     id=0,
                                     recStore="recordStore",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
        self.c = SimpleResultSetItem(session,
                                     id=1,
                                     recStore="recordStore",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
        self.d = SimpleResultSetItem(session,
                                     id=0,
                                     recStore="recordStore2",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
예제 #9
0
    def construct_resultSet(self, session, terms, queryHash={}):
        # in: res.dictresult()

        s = SimpleResultSet(session, [])
        rsilist = []
        for t in terms['records']:
            (store, id) = t['recordid'].split('/', 1)
            occs = t['occurences']
            item = SimpleResultSetItem(session, id, store, occs, resultSet=s)
            rsilist.append(item)
        s.fromList(rsilist)
        s.index = self
        if queryHash:
            s.queryTerm = queryHash['text']
            s.queryFreq = queryHash['occurences']
        s.totalRecs = terms['totalRecs']
        s.totalOccs = terms['totalOccs']
        return s
예제 #10
0
 def search(self, session, query, db):
     # Kludgey optimisation
     pm = db.get_path(session, 'protocolMap')
     if not pm:
         db._cacheProtocolMaps(session)
         pm = db.protocolMaps.get('http://www.loc.gov/zing/srw/')
         db.paths['protocolMap'] = pm
     query = self._cql_to_sql(session, query, pm)
     res = self._query(query)
     dr = res.dictresult()
     rset = SimpleResultSet([])
     rsilist = []
     for t in dr:
         (store, id) = t['recordid'].split('/', 1)
         item = SimpleResultSetItem(session, id, store, 1, resultSet=rset)
         rsilist.append(item)
     rset.fromList(rsilist)
     return rset
예제 #11
0
class SimpleResultSetItemTestCase(unittest.TestCase):

    def setUp(self):
        """Setup some ResultsetItems to evaluate.

        N.B. a == b, other pairs should not evaluate as equal
        """
        self.session = session = Session()
        self.a = SimpleResultSetItem(session,
                                     id=0,
                                     recStore="recordStore",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
        self.b = SimpleResultSetItem(session,
                                     id=0,
                                     recStore="recordStore",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
        self.c = SimpleResultSetItem(session,
                                     id=1,
                                     recStore="recordStore",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)
        self.d = SimpleResultSetItem(session,
                                     id=0,
                                     recStore="recordStore2",
                                     occs=0,
                                     database="",
                                     diagnostic=None,
                                     weight=0.5,
                                     resultSet=None,
                                     numeric=None)

    def testEquality(self):
        """Test ResultSetItem equality"""
        self.assertEqual(self.a, self.b,
                         "ResultSetItems do not evaluate as equal")
        self.assertNotEqual(self.a, self.c,
                            "ResultSetItems do not evaluate as equal")
        self.assertNotEqual(self.a, self.d,
                            "ResultSetItems do not evaluate as equal")
        self.assertNotEqual(self.b, self.c,
                            "ResultSetItems do not evaluate as equal")
        self.assertNotEqual(self.b, self.d,
                            "ResultSetItems do not evaluate as equal")

    def testCmp(self):
        """Test ResultSetItem comparisons"""
        self.assertLess(self.a, self.c)
        self.assertLess(self.a, self.d)
        self.assertLess(self.b, self.c)
        self.assertLess(self.b, self.d)
        self.assertLess(self.d, self.c)

    def testSerialize(self):
        """Test ResultsetItem serialization."""
        # Check that a and b serialize to the same string
        strA = self.a.serialize(self.session, pickleOk=1)
        strB = self.b.serialize(self.session, pickleOk=1)
        strC = self.c.serialize(self.session, pickleOk=1)
        strD = self.d.serialize(self.session, pickleOk=1)
        self.assertEqual(strA, strB)
        # Check serialization expected to be different
        self.assertNotEqual(strA, strC)
        self.assertNotEqual(strA, strD)
        self.assertNotEqual(strB, strC)
        self.assertNotEqual(strB, strD)
        self.assertNotEqual(strC, strD)
        # Check serialization is well-formed XML
        for s in [strA, strB, strC, strD]:
            etree.fromstring(s)