Return TypeFunction nameArguments
hzEcodehdbIsamfile::Insert(const hzString&,const hzString&,)

Declared in file: hzDatabase.h
Defined in file : hdbIsamfile.cpp

Function Logic:

0:START 1:m_nInitState<1 2:Return E_NOINIT 3:m_nInitState<2 4:Return E_NOTOPEN 5:!newKey 6:Return E_NODATA 7:hzChain::Clear hzString::Length nSpace k nC 8:nC 9:k[nC]==0||k[nC]==(char)10 10:hzChain::Printf 11:Return E_BADVALUE 12:!m_Index.Count() 13:hzString::Length nC memset sprintf ofstream::write ofstream::fail 14:m_WrD.fail() 15:hzChain::Printf 16:Return E_WRITEFAIL 17:ofstream::flush items ofstream::fail 18:m_WrI.fail() 19:hzChain::Printf 20:Return E_WRITEFAIL 21:hzMapM::Insert m_nBlocks ofstream::flush 22:Return E_OK 23:hzMapM::Target nLo hzMapM::GetKey strA 24:nLo>=(int32_t)m_Index.Count() 25:hzMapM::Count nLo hzChain::Printf 26:strA>newKey 27:items hzChain::Printf 28:hzChain::Printf 29:strA hzMapM::GetObj addr hzChain::Printf ifstream::seekg ifstream::fail 30:m_RdD.fail() 31:hzChain::Printf 32:Return E_READFAIL 33:ifstream::read ifstream::fail 34:m_RdD.fail() 35:hzChain::Printf 36:Return E_READFAIL 37:nC 38:m_Buf[nC]==(char)10 39:* 40:!strA 41:strA j 42:*j 43:strB 44:j 45:!strA 46:hzMapM::Insert strB strA 47:hzMapM::Insert ofstream::seekp ofstream::fail 48:m_WrD.fail() 49:hzChain::Printf 50:Return E_WRITEFAIL 51:ofstream::tellp hzChain::Printf hzChain::Clear nC 52:nC 53:hzMapM::GetKey strA hzMapM::GetObj strB hzChain::Size hzString::Length hzString::Length nSpace 54:nSpace>m_nBlkSize 55:memset 56:!zi.eof(); 57:* 58:ofstream::write ofstream::fail 59:m_WrD.fail() 60:hzChain::Printf 61:Return E_WRITEFAIL 62:hzChain::Clear ofstream::flush hzChain::Printf items ofstream::flush hzChain::Clear hzMapM::Insert items ofstream::seekp ofstream::fail 63:m_WrD.fail() 64:hzChain::Printf 65:Return E_WRITEFAIL 66:items hzChain::AddByte 67:strB 68:items 69:hzChain::AddByte hzChain::Size 70:Zout.Size() 71:memset 72:!zi.eof(); 73:* 74:ofstream::write ofstream::fail 75:m_WrD.fail() 76:ofstream::tellp hzChain::Printf 77:Return E_WRITEFAIL 78:ofstream::flush 79:Return rc

Function body:

hzEcode hdbIsamfile::Insert (const hzString& newKey, const hzString& newObj)
{
   _hzfunc("hdbIsamfile::Insert") ;
   hzMapM<hzString,hzString>   tmp ;
   hzChain     Zout ;
   hzChain     Xind ;
   chIter      zi ;
   char*       i ;
   char*       j ;
   const char* k ;
   hzString    strA ;
   hzString    strB ;
   uint32_t    addr = 0;
   uint32_t    nSpace = 0;
   uint32_t    nC ;
   int32_t     nLo ;
   hzEcode     rc = E_OK ;
   if (m_nInitState < 1)   return E_NOINIT ;
   if (m_nInitState < 2)   return E_NOTOPEN ;
   if (!newKey)
       return E_NODATA ;
   m_Error.Clear() ;
   nSpace = newKey.Length() ;
   k = *newKey ;
   for (nC = 0; nC < nSpace ; nC++)
   {
       if (k[nC] == 0|| k[nC] == CHAR_NL)
           { m_Error.Printf("Illegal value %c (%d) found in key [%s]\n", k[nC], k[nC], *newKey) ; return E_BADVALUE ; }
   }
   if (!m_Index.Count())
   {
       nC = newKey.Length() ;
       memset(m_Buf, 0,HZ_BLOCKSIZE) ;
       sprintf(m_Buf, "%s\n%s\n", *newKey, *newObj) ;
       m_WrD.write(m_Buf, HZ_BLOCKSIZE) ;
       if (m_WrD.fail())
           { m_Error.Printf("Could not write to virgin data file\n") ; return E_WRITEFAIL ; }
       m_WrD.flush() ;
       m_WrI << "00000000," << "\n" ;
       if (m_WrI.fail())
           { m_Error.Printf("Could not write to virgin index file\n") ; return E_WRITEFAIL ; }
       m_Index.Insert(_hzGlobal_nullString, 0);
       m_nBlocks = 1;
       m_WrI.flush() ;
       return E_OK ;
   }
   nLo = m_Index.Target(newKey) ;
   strA = m_Index.GetKey(nLo) ;
   if (nLo >&eq; (int32_t) m_Index.Count())
       { nLo = m_Index.Count() -1; m_Error.Printf("%s: Case 0: identified index posn %d\n", *_fn, nLo) ; }
   else if (strA > newKey)
       { nLo-- ; m_Error.Printf("%s: Case 1: identified index posn %d\n", *_fn, nLo) ; }
   else
       m_Error.Printf("%s: Case 2: identified index posn %d\n", *_fn, nLo) ;
   strA = (char*) 0;
   addr = m_Index.GetObj(nLo) ;
       m_Error.Printf("%s: Case 2: identified block %d\n", *_fn, nLo) ;
   m_RdD.seekg(addr * m_nBlkSize) ;
   if (m_RdD.fail())
       { m_Error.Printf("%s: Failed to seek to block %d (position %d) in data block\n", *_fn, addr, addr * m_nBlkSize) ; return E_READFAIL ; }
   m_RdD.read(m_Buf, HZ_BLOCKSIZE) ;
   if (m_RdD.fail())
       { m_Error.Printf("%s: Failed to read block %d (position %d) in data block\n", *_fn, addr, addr * m_nBlkSize) ; return E_READFAIL ; }
   for (i = j = m_Buf, nC = 0; nC < HZ_BLOCKSIZE ; i++, nC++)
   {
       if (m_Buf[nC] == CHAR_NL)
       {
           *i = 0;
           if (!strA)
               { strA = j ; j = i + 1; continue ; }
           if (*j)
               strB = j ;
           j = i + 1;
           if (!strA)
               break ;
           tmp.Insert(strA, strB) ;
           strA = strB = (char*) 0;
       }
   }
   tmp.Insert(newKey, newObj) ;
   m_WrD.seekp(addr * m_nBlkSize) ;
   if (m_WrD.fail())
       { m_Error.Printf("Could not seek to posn %u in data file\n", addr * m_nBlkSize) ; return E_WRITEFAIL ; }
   m_Error.Printf("Seeked to posn %u in data file\n", m_WrD.tellp()) ;
   Zout.Clear() ;
   for (nC = 0; nC < tmp.Count() ; nC++)
   {
       strA = tmp.GetKey(nC) ;
       strB = tmp.GetObj(nC) ;
       nSpace = Zout.Size() + strA.Length() + strB.Length() + 2;
       if (nSpace > m_nBlkSize)
       {
           memset(m_Buf, 0,HZ_BLOCKSIZE) ;
           for (i = m_Buf, zi = Zout ; !zi.eof() ; i++, zi++)
               *i = *zi ;
           m_WrD.write(m_Buf, HZ_BLOCKSIZE) ;
           if (m_WrD.fail())
               { m_Error.Printf("Could not write to data file\n") ; return E_WRITEFAIL ; }
           Zout.Clear() ;
           m_WrD.flush() ;
           Xind.Printf("%08x,%s\n", m_nBlocks, *strA) ; m_WrI << Xind ; m_WrI.flush() ; Xind.Clear() ;
           m_Index.Insert(strA, m_nBlocks) ;
           m_nBlocks++ ;
           m_WrD.seekp(0,ios_base::end) ;
           if (m_WrD.fail())
               { m_Error.Printf("Alloc - Could not seek to end in data file\n") ; return E_WRITEFAIL ; }
       }
       Zout << strA ;
       Zout.AddByte(CHAR_NL) ;
       if (strB)
           Zout << strB ;
       Zout.AddByte(CHAR_NL) ;
   }
   if (Zout.Size())
   {
       memset(m_Buf, 0,HZ_BLOCKSIZE) ;
       for (i = m_Buf, zi = Zout ; !zi.eof() ; i++, zi++)
           *i = *zi ;
       m_WrD.write(m_Buf, HZ_BLOCKSIZE) ;
       if (m_WrD.fail())
           { m_Error.Printf("Could not write to posn %u in data file\n", m_WrD.tellp()) ; return E_WRITEFAIL ; }
       m_WrD.flush() ;
   }
   return rc ;
}