0 Replies Latest reply: May 9, 2012 1:16 AM by 792937 RSS

    Why there is a berkeleydb auto inserted record using secondary db?

    792937
      Hi all,

      I using secondary db to do multi-key query, but I find berkeyleydb insert a dummy record automatically and this dummy record can be queried out using cursor, which is quite annoying.

      Below is my code example:

      Init(const char *pszPath)
      {
           try
           {
                char szFileName[256] = "";
                if (strlen(m_szPath) > 0)
                {
                     sprintf(szFileName, "%s%s", m_szPath, "qmdb_Positions.db");
                }
                else
                {
                     strcpy(szFileName, "qmdb_Positions.db");
                }
                char *pszFileName = NULL;
                if (strcmp("file", "none") == 0)
                {
                     pszFileName = szFileName;
                }
                
                m_pDBPositions = new Db(NULL, 0);
                m_pDBPositions->open(NULL, pszFileName, szFileName, DB_BTREE, DB_CREATE, 0);
                
                m_pSeqPositions = new DbSequence(m_pDBPositions, 0);
                Dbt key((void *)m_SeqNamePositions, (u_int32_t)strlen(m_SeqNamePositions));
                m_pSeqPositions->open(NULL, &key, DB_CREATE);
                
                m_pIdxInstrument = new Db(NULL, 0);
                m_pIdxInstrument->set_flags(DB_DUPSORT);
                if (strlen(m_szPath) > 0)
                {
                     sprintf(szFileName, "%s%s", m_szPath, "qmdbs_Positions_Instrument.db");
                }
                else
                {
                     strcpy(szFileName, "qmdbs_Positions_Instrument.db");
                }
                m_pIdxInstrument->open(NULL, pszFileName, szFileName, DB_BTREE, DB_CREATE, 0);
                m_pDBPositions->associate(NULL, m_pIdxInstrument, &qmPositionsTable::ExtractInstrument, 0);
                m_pIdxDirection = new Db(NULL, 0);
                m_pIdxDirection->set_flags(DB_DUPSORT);
                if (strlen(m_szPath) > 0)
                {
                     sprintf(szFileName, "%s%s", m_szPath, "qmdbs_Positions_Direction.db");
                }
                else
                {
                     strcpy(szFileName, "qmdbs_Positions_Direction.db");
                }
                m_pIdxDirection->open(NULL, pszFileName, szFileName, DB_BTREE, DB_CREATE, 0);
                m_pDBPositions->associate(NULL, m_pIdxDirection, &qmPositionsTable::ExtractDirection, 0);

                Dbc pCursor = NULL;
                m_pDBPositions->cursor(NULL, &pCursor, 0);
                Dbt k, data;
                if (DB_NOTFOUND != pCursor->get(&k, &data, DB_NEXT))
                *{*
                     qmPositionsRecord record;
                     memcpy(&record, data.get_data(), sizeof(qmPositionsRecord));
                     printf("%s", record.Instrument);
                *}*
                
           }
           catch (DbException &e)
           {
                string error = e.what();
           }
           catch (std::exception &e)
           {
           }
           return 0;
      }

      int qmPositionsTable::
      ExtractInstrument(Db dbp, const Dbt pkey, const Dbt pdata, Dbt skey)
      {
           qmPositionsRecord *pRecord;
           pRecord = (qmPositionsRecord *)pdata->get_data();

           skey->set_data((void *)GetAddressOfInstrument(pRecord->Instrument));
           skey->set_size(GetLengthOfInstrument(pRecord->Instrument));

           return 0;
      }

      int qmPositionsTable::
      ExtractDirection(Db dbp, const Dbt pkey, const Dbt pdata, Dbt skey)
      {
           qmPositionsRecord *pRecord;
           pRecord = (qmPositionsRecord *)pdata->get_data();

           skey->set_data((void *)GetAddressOfDirection(pRecord->Direction));
           skey->set_size(GetLengthOfDirection(pRecord->Direction));

           return 0;
      }

      Edited by: Bruce on 2012-5-8 下午11:14