Exemple #1
0
    def execQuery(self, qry):
        '''
Executes a SQL statement, returning the results
      '''
        self.connect()
        logger.debug('Executing query: ' + qry)
        qry = qry.strip()
        if not len(qry):
            return []
        # any error handling we should do?
        #print qry
        #x = raw_input()
        #if x == '':
        #  x = qry
        #self.dbconn = None
        #self.dbconn.reset()
        #self.connect()

        #Check to see if the connection is valid
        if (self.dbconn.status != 1):
            logger.info("DB Connection bad, attempting reset")
            self.dbconn.reset()

        response = self.dbconn.query(qry)
        if response: return response.getresult()
Exemple #2
0
    def loadharvest(self, dir=config.datadir):
        '''Uses psql to stage harvested data in the quarry database'''

        qlog.info("Loading harvest from %s" % (dir, ))
        t = time.time()

        sql = "%s;\n %s;\n %s;\n %s;" % (
            "begin transaction", queries.load_resources(dir),
            queries.load_descriptors(dir), "commit")
        sqlfile = config.tmpdir + "/temp.sql"

        f = file(sqlfile, "w")
        f.write(sql)
        f.close()

        self.closeFiles()
        cmd = '''%s -h %s -f "%s" %s''' % (config.psqlpath, self.qdb.hostname,
                                           sqlfile, self.qdb.dbname)

        flusher = popen2.Popen3(cmd)
        output = flusher.fromchild
        # don't quit till this process finishes
        qlog.debug('psql response:\n' + output.read())
        flusher.wait()

        qlog.info("...bulk loaded in %s seconds." % (time.time() - t, ))
Exemple #3
0
    def loadharvest(self, dir=config.datadir):

      '''Uses psql to stage harvested data in the quarry database'''

      qlog.info("Loading harvest from %s" % (dir,))
      t = time.time()

      sql = "%s;\n %s;\n %s;\n %s;" % ("begin transaction",
                                       queries.load_resources(dir), 
                                       queries.load_descriptors(dir),
                                       "commit")
      sqlfile = config.tmpdir + "/temp.sql"
    
      f = file(sqlfile, "w")
      f.write(sql)
      f.close()
      
      self.closeFiles()
      cmd = '''%s -h %s -f "%s" %s''' % (config.psqlpath, 
                                         self.qdb.hostname, 
                                         sqlfile, 
                                         self.qdb.dbname)

      flusher = popen2.Popen3(cmd)
      output = flusher.fromchild
      # don't quit till this process finishes
      qlog.debug('psql response:\n' + output.read())
      flusher.wait()
      
      qlog.info("...bulk loaded in %s seconds." % (time.time() - t,))
Exemple #4
0
    def execQuery(self, qry):
      '''
Executes a SQL statement, returning the results
      '''
      self.connect()
      logger.debug('Executing query: ' + qry)
      qry = qry.strip()
      if not len(qry):
        return []
      # any error handling we should do?
      #print qry
      #x = raw_input()
      #if x == '':
      #  x = qry
      #self.dbconn = None
      #self.dbconn.reset()
      #self.connect()

      #Check to see if the connection is valid
      if (self.dbconn.status != 1):
        logger.info("DB Connection bad, attempting reset")
        self.dbconn.reset()

      response = self.dbconn.query(qry)
      if response: return response.getresult()
Exemple #5
0
    def DispatchNewResources(self):
        qlog.debug("Dispatch New Resources")
        self._RefreshUniqueSignatures()
        for s in self.UniqueSignatures():
            qlog.info("Computing extent for signature %s" % (s, ))
            s.ComputeExtent()

        # resource table is deprecated
        self._InsertNewResources()
Exemple #6
0
  def DispatchNewResources(self):
    qlog.debug("Dispatch New Resources")
    self._RefreshUniqueSignatures()
    for s in self.UniqueSignatures():
      qlog.info("Computing extent for signature %s"%(s,))
      s.ComputeExtent()

    # resource table is deprecated
    self._InsertNewResources()
Exemple #7
0
    def DeleteExistingResources(self):
        qlog.debug("Deleting Existing Resources")
        # need to find all the resources previously dispatched
        # that are staged to be re-dispatched, and delete them

        #self._DeleteFromSignatureExtents()

        # resource table is deprecated
        #self._DeleteResources()
        pass
Exemple #8
0
 def DeleteExistingResources(self):
   qlog.debug("Deleting Existing Resources")
   # need to find all the resources previously dispatched
   # that are staged to be re-dispatched, and delete them
    
   #self._DeleteFromSignatureExtents()
   
   # resource table is deprecated
   #self._DeleteResources()
   pass
Exemple #9
0
  def _makeTable(self):
    '''
Create a table for the signature.  This method is idempotent and non-destructive
    '''
    qlog.debug("Creating table " + self.strid)
    x = self.qdb.checkTable(self.strid)
    if not x:
      fields = self.columns()
      types = ['text']*len(fields)
      self.qdb.createTable( self.strid, fields, types )
Exemple #10
0
    def execCommand(self, qry):
      '''Executes a SQL statement, ignoring the results'''
      #self.connect()
       # any error handling we should do?
      logger.debug('Executing command: ' + qry)

      #Check to see if the connection is valid
      if (self.dbconn.status != 1):
        logger.info("DB Connection bad, attempting reset")
        self.dbconn.reset()

      result = self.dbconn.query(qry)
Exemple #11
0
    def execCommand(self, qry):
        '''Executes a SQL statement, ignoring the results'''
        #self.connect()
        # any error handling we should do?
        logger.debug('Executing command: ' + qry)

        #Check to see if the connection is valid
        if (self.dbconn.status != 1):
            logger.info("DB Connection bad, attempting reset")
            self.dbconn.reset()

        result = self.dbconn.query(qry)
Exemple #12
0
def Describe(key): 
  qlog.info("Describe(%s)" % (key,))
  for s in sm.UniqueSignatures():
    q = s.ConjunctiveQuery(conditions=[('userkey', key)], wildcard=False)
    rs = db.execQuery(q);
    if len(rs) != 0:
      break
  
  if len(rs) == 0:    
    raise ValueError("no resource '%s' was found" % (key,))
  else:
    qlog.debug("columns: %s" % (s.rawcolumns(),))
    return zip(s.rawcolumns(), [str(x) for x in rs[0]])    
Exemple #13
0
def Describe(key):
    qlog.info("Describe(%s)" % (key, ))
    for s in sm.UniqueSignatures():
        q = s.ConjunctiveQuery(conditions=[('userkey', key)], wildcard=False)
        rs = db.execQuery(q)
        if len(rs) != 0:
            break

    if len(rs) == 0:
        raise ValueError("no resource '%s' was found" % (key, ))
    else:
        qlog.debug("columns: %s" % (s.rawcolumns(), ))
        return zip(s.rawcolumns(), [str(x) for x in rs[0]])
Exemple #14
0
    def activate(self, target):
        '''test this rule against a target, executing the script 
           on a match.'''
        qlog.debug("Testing Rule '%s' against '%s'" % (self, target))
        if self.regexp.match(target):
          qlog.debug('Match')
          try:
	    return reduce(operator.concat, [s.execute(target) for s in self.scripts])
            #return self.script.execute(target)
          except:
            (e, v, t) = sys.exc_info()
            qlog.error(str(e) + ": " + str(v))
            raise e, v,t
            return '' 
        else:
            return ''
Exemple #15
0
 def activate(self, target):
     '''test this rule against a target, executing the script 
        on a match.'''
     qlog.debug("Testing Rule '%s' against '%s'" % (self, target))
     if self.regexp.match(target):
         qlog.debug('Match')
         try:
             return reduce(operator.concat,
                           [s.execute(target) for s in self.scripts])
         #return self.script.execute(target)
         except:
             (e, v, t) = sys.exc_info()
             qlog.error(str(e) + ": " + str(v))
             raise e, v, t
             return ''
     else:
         return ''
Exemple #16
0
    def LoadTriples(self, fname):
        '''Load triples from an ASCII file.  Use config.py to configure delimiters'''

        # TODO: support RDF and N-triples format, at least
        t = time.time()

        qlog.info("Loading triples from %s" % (fname, ))
        cmd = '''%s -h %s -c "%s" %s''' % (config.psqlpath, self.qdb.hostname,
                                           queries.copy_triples(
                                               fname, csv=""), self.qdb.dbname)
        flusher = popen2.Popen3(cmd)
        output = flusher.fromchild
        # don't quit till this process finishes
        qlog.debug('psql response:\n' + output.read())
        flusher.wait()

        qlog.info("...triples loaded in %s seconds." % (time.time() - t, ))
Exemple #17
0
  def _populateTable(self):
    '''
Load a profile table from the staging data.
    '''

    sigid = self.strid
    fields = ','.join(self.properties)
    qlog.debug("Populating Table " + sigid)

    crossqry = self.crosstab(fields)

    # insert into profile table
    scheme = self.columns()
    scheme_s = ','.join(scheme)
    insertqry = "INSERT INTO %s (%s) (%s)" % (sigid, scheme_s, crossqry)
   
    self.qdb.execCommand(insertqry)
Exemple #18
0
def PathValues(path,
               conditions,
               property,
               offset=0,
               limit='all',
               sorted=False):
    '''Retrieve values of property for resources that 1) satisfy conditions and 2)
are avilable in the path context provided.'''

    raw = "PathValues(%s, %s, %s)[%s,%s]%s" % (path, conditions, property,
                                               offset, limit, sorted)
    qlog.info(raw)
    try:
        callstr = pattre.sub('_', raw)

        start_time = time.time()
        if iscached(callstr):
            tuples = getCachedResults(callstr)
            updateCache(callstr)
            results = [t[0] for t in tuples]
        else:

            # ----------------
            if not path:
                results = ValidValues(conditions, property, offset, limit,
                                      sorted)
            else:
                newpath = path + [(conditions, property)]
                q = TraverseQuery(newpath)
                q = PagedQuery(q, offset, limit, sorted)
                tuples = db.execQuery(q)
                tuples = asList(tuples)
                results = [t[0] for t in tuples]
            # ----------------
            t = time.time() - start_time
            if t > INTERACTIVE_SPEED and not iscached(callstr):
                cacheQuery(callstr, results)
            qlog.debug(str(results))
            qlog.info("----- finished in: %f secs" % (t, ))

        return results

    except Exception, e:
        (et, v, t) = sys.exc_info()
        qlog.ExceptionMessage(et, v, t)
        raise
Exemple #19
0
    def LoadTriples(self, fname):
      '''Load triples from an ASCII file.  Use config.py to configure delimiters'''

      # TODO: support RDF and N-triples format, at least
      t = time.time()

      qlog.info("Loading triples from %s" % (fname,))
      cmd = '''%s -h %s -c "%s" %s''' % (config.psqlpath, 
                                         self.qdb.hostname, 
                                         queries.copy_triples(fname, csv=""), 
                                         self.qdb.dbname)
      flusher = popen2.Popen3(cmd)
      output = flusher.fromchild
      # don't quit till this process finishes
      qlog.debug('psql response:\n' + output.read())
      flusher.wait()
      
      qlog.info("...triples loaded in %s seconds." % (time.time() - t,))
Exemple #20
0
def PathValues(path, conditions, property, offset=0, limit='all', sorted=False):
  '''Retrieve values of property for resources that 1) satisfy conditions and 2)
are avilable in the path context provided.'''

  raw = "PathValues(%s, %s, %s)[%s,%s]%s" % (path, conditions,property,offset,limit,sorted)
  qlog.info(raw)
  try:
    callstr = pattre.sub('_', raw)

    start_time = time.time()
    if iscached(callstr):
      tuples = getCachedResults(callstr)
      updateCache(callstr)
      results = [t[0] for t in tuples]
    else:

      # ----------------
      if not path:
        results = ValidValues(conditions, property, offset, limit, sorted)
      else:
        newpath = path + [(conditions, property)]
        q = TraverseQuery(newpath)
        q = PagedQuery(q, offset, limit, sorted) 
        tuples = db.execQuery(q)
        tuples = asList(tuples)
        results = [t[0] for t in tuples]
       # ----------------
      t = time.time() - start_time
      if t > INTERACTIVE_SPEED and not iscached(callstr):
         cacheQuery(callstr, results)
      qlog.debug(str(results))
      qlog.info("----- finished in: %f secs" % (t,))

    return results

  except Exception, e:
    (et,v,t) = sys.exc_info()
    qlog.ExceptionMessage(et,v,t)
    raise
Exemple #21
0
def PathProperties(path, conds):
    '''Retrieve unique properties for resources that 1) satisfy conditions and 2)
are avilable in the path context provided.'''

    raw = "PathProperties(%s, %s)" % (path, conds)
    qlog.info(raw)
    try:
        callstr = pattre.sub('_', raw)

        start_time = time.time()
        if iscached(callstr):
            tuples = getCachedResults(callstr)
            updateCache(callstr)
            results = ('', [t[0] for t in tuples])
        else:

            # ----------------
            if not path:
                props = ValidProps(conds)
                results = '', props
            elif not conds:
                results = PropertiesOf(TraverseQuery(path), True)
            else:
                newpath = path + [(conds, 'userkey')]
                results = Traverse(newpath)
            # ----------------
            t = time.time() - start_time
            if t > INTERACTIVE_SPEED and not iscached(callstr):
                cacheQuery(callstr, results[1])
            qlog.debug(str(results))
            qlog.info("----- finished in: %f secs" % (t, ))

        return results

    except Exception, e:
        (et, v, t) = sys.exc_info()
        qlog.ExceptionMessage(et, v, t)
        raise
Exemple #22
0
def PathProperties(path, conds):
  '''Retrieve unique properties for resources that 1) satisfy conditions and 2)
are avilable in the path context provided.'''

  raw = "PathProperties(%s, %s)" % (path, conds)
  qlog.info(raw) 
  try:
    callstr = pattre.sub('_', raw)
 
    start_time = time.time()
    if iscached(callstr):
      tuples = getCachedResults(callstr)
      updateCache(callstr)
      results = ('', [t[0] for t in tuples])
    else:
 
      # ----------------
      if not path:
        props = ValidProps(conds)
        results = '', props
      elif not conds:
        results = PropertiesOf(TraverseQuery(path), True)
      else: 
        newpath = path + [(conds, 'userkey')]
        results = Traverse(newpath)
       # ---------------- 
      t = time.time() - start_time 
      if t > INTERACTIVE_SPEED and not iscached(callstr): 
         cacheQuery(callstr, results[1]) 
      qlog.debug(str(results)) 
      qlog.info("----- finished in: %f secs" % (t,)) 
    
    return results
  
  except Exception, e:
    (et,v,t) = sys.exc_info()
    qlog.ExceptionMessage(et,v,t)
    raise
Exemple #23
0
 def ClearSignatures(self):
   qlog.debug("Clearing all Signatures")
   for s in self.UniqueSignatures():
     s.DropExtent()
     s.DeleteSignature()
Exemple #24
0
 def export_PathProperties(self, path, conditions):
     qlog.debug("Hello?")
     return quarry.PathProperties(path, conditions)
Exemple #25
0
 def ClearStagingArea(self):
     qlog.debug("Clearing Staging Area")
     self.qdb.execCommand(queries.delete_staging_area)
Exemple #26
0
 def ClearSignatures(self):
     qlog.debug("Clearing all Signatures")
     for s in self.UniqueSignatures():
         s.DropExtent()
         s.DeleteSignature()
Exemple #27
0
 def export_PathProperties(self, path, conditions):
   qlog.debug("Hello?")
   return quarry.PathProperties(path, conditions)
Exemple #28
0
 def ComputeExtent(self):
   qlog.debug("Compute Extent for signature %s" % (self.properties,))
   self._makeTable()
   self._populateTable()
Exemple #29
0
 def ClearStagingArea(self):
   qlog.debug("Clearing Staging Area")
   self.qdb.execCommand(queries.delete_staging_area)