0 Replies Latest reply: May 31, 2014 2:54 AM by 2644621 RSS

    Get data from Berkeley DB

    2644621

      I want to get installed rpm packages from Berkeley DB.

      I found this code into rpm command source code:

       

      /**
       * Find file matches in database.
       * @param db rpm database
       * @param dbi index database handle (always RPMDBI_BASENAMES)
       * @param filespec
       * @param usestate take file state into account?
       * @retval matches
       * @return 0 on success, 1 on not found, -2 on error
       */
      static int rpmdbFindByFile(rpmdb db, dbiIndex dbi, const char *filespec,
         int usestate, dbiIndexSet * matches)
      {
         char * dirName = NULL;
         const char * baseName;
        fingerPrintCache fpc = NULL;
        fingerPrint * fp1 = NULL;
        dbiIndexSet allMatches = NULL;
         unsigned int i;
         int rc = -2; /* assume error */
      
         *matches = NULL;
         if (filespec == NULL) return rc; /* nothing alloced yet */
      
         if ((baseName = strrchr(filespec, '/')) != NULL) {
         size_t len = baseName - filespec + 1;
        dirName = strncpy(xmalloc(len + 1), filespec, len);
        dirName[len] = '\0';
        baseName++;
         } else {
        dirName = xstrdup("");
        baseName = filespec;
         }
         if (baseName == NULL)
         goto exit;
      
        rc = dbiGetToSet(dbi, baseName, 0, &allMatches);
      
         if (rc || allMatches == NULL) goto exit;
      
         *matches = xcalloc(1, sizeof(**matches));
        fpc = fpCacheCreate(allMatches->count, NULL);
        fpLookup(fpc, dirName, baseName, &fp1);
      
        i = 0;
         while (i < allMatches->count) {
         struct rpmtd_s bn, dn, di, fs;
         const char ** baseNames, ** dirNames;
         uint32_t * dirIndexes;
         unsigned int offset = dbiIndexRecordOffset(allMatches, i);
         unsigned int prevoff;
         Header h = rpmdbGetHeaderAt(db, offset);
      
         if (h == NULL) {
        i++;
         continue;
         }
      
        headerGet(h, RPMTAG_BASENAMES, &bn, HEADERGET_MINMEM);
        headerGet(h, RPMTAG_DIRNAMES, &dn, HEADERGET_MINMEM);
        headerGet(h, RPMTAG_DIRINDEXES, &di, HEADERGET_MINMEM);
        baseNames = bn.data;
        dirNames = dn.data;
        dirIndexes = di.data;
         if (usestate)
        headerGet(h, RPMTAG_FILESTATES, &fs, HEADERGET_MINMEM);
      
         do {
         int num = dbiIndexRecordFileNumber(allMatches, i);
         int skip = 0;
      
         if (usestate) {
        rpmtdSetIndex(&fs, num);
         if (!RPMFILE_IS_INSTALLED(rpmtdGetNumber(&fs))) {
        skip = 1;
         }
         }
      
         if (!skip) {
         const char *dirName = dirNames[dirIndexes[num]];
         if (fpLookupEquals(fpc, fp1, dirName, baseNames[num])) {
         struct dbiIndexItem rec = { 
         .hdrNum = dbiIndexRecordOffset(allMatches, i),
         .tagNum = dbiIndexRecordFileNumber(allMatches, i),
         };
        dbiAppendSet(*matches, &rec, 1, sizeof(rec), 0);
         }
         }
      
        prevoff = offset;
        i++;
         if (i < allMatches->count)
        offset = dbiIndexRecordOffset(allMatches, i);
         } while (i < allMatches->count && offset == prevoff);
      
        rpmtdFreeData(&bn);
        rpmtdFreeData(&dn);
        rpmtdFreeData(&di);
         if (usestate)
        rpmtdFreeData(&fs);
        headerFree(h);
         }
      
        free(fp1);
        fpCacheFree(fpc);
      
         if ((*matches)->count == 0) {
         *matches = dbiIndexSetFree(*matches);
        rc = 1;
         } else {
        rc = 0;
         }
      
      exit:
        dbiIndexSetFree(allMatches);
        free(dirName);
         return rc;
      }
      
      int rpmdbCountPackages(rpmdb db, const char * name)
      {
         int rc = -1;
        dbiIndex dbi = rpmdbOpenIndex(db, RPMDBI_NAME, 0);
      
         if (dbi != NULL && name != NULL) {
        dbiIndexSet matches = NULL;
      
        rc = dbiGetToSet(dbi, name, strlen(name), &matches);
      
         if (rc == 0) {
        rc = dbiIndexSetCount(matches);
         } else {
        rc = (rc == DB_NOTFOUND) ? 0 : -1;
         }
        dbiIndex
      
      
      
      
      
      
      
      
      SetFree(matches);
         }
      
         return rc;
      }
      

      The question is how I can use this code to make quires with Java? Is there any way to make the same queries like C in Java?