Fetches objects matching the key or falling within a range of two keys.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hdbIsamfile::Fetch | (hzArray<hzPair>&,hzString&,hzString&,) |
Declared in file: hzDatabase.h
Defined in file : hdbIsamfile.cpp
Function Logic:
Function body:
hzEcode hdbIsamfile::Fetch (hzArray<hzPair>& result)hzString& keyLo, hzString& keyHi,
{
// Fetches objects matching the key or falling within a range of two keys.
//
// Arguments: 1) obj The object to be populated
// 2) datumId The object id
//
// Returns: E_NOTOPEN If the reader stream is not open for reading
// E_READFAIL The reader stream is already in a fialed state
// E_CORRUPT Row metadata not as expected. Possible overwrites
// E_FORMAT Data format error
// E_OK The operation was successful
_hzfunc("hdbIsamfile::Fetch") ;
hzMapM<hzString,hzString> tmp ; // Temp map of key/object pairs found in target data area
char* i ; // Buffer iterator
char* j ; // Buffer iterator
hzString keyA ; // Key string
hzString keyB ; // Key string
hzString strA ; // Key string
hzString strB ; // Object string
hzPair pair ; // For output
uint32_t nPos ; // Position of target within m_Index map
uint32_t addr ; // Address of target data block
uint32_t nC ; // Counter
int32_t nLo ; // Position of first target within tmp map
int32_t nHi ; // Position of last target within tmp map
hzEcode rc = E_OK ; // Return code
// Check init state
if (m_nInitState < 1) return E_NOINIT ;
if (m_nInitState < 2) return E_NOTOPEN ;
result.Clear() ;
m_Error.Clear() ;
// Sort the keys
if (!keyLo && !keyHi)
{ m_Error.Printf("No keys supplied\n") ; return E_ARGUMENT ; }
if (!m_Index.Count())
return E_NOTFOUND ;
if (!keyHi)
keyA = keyB = keyLo ;
else if (!keyLo)
keyA = keyB = keyHi ;
else
{
if (keyHi < keyLo)
{ keyA = keyHi ; keyB = keyLo ; }
else
{ keyA = keyLo ; keyB = keyHi ; }
}
// Identify target data block
nPos = m_Index.Target(keyA) ;
if (nPos >&eq; m_Index.Count())
nPos = m_Index.Count() -1;
strA = m_Index.GetKey(nPos) ;
if (strA > keyA)
nPos-- ;
for (; nPos < m_Index.Count() ; nPos++)
{
if (m_Index.GetKey(nPos) > keyB)
break ;
// Read in target data block
addr = m_Index.GetObj(nPos) ;
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;
}
}
}
// If object id is zero return blank object
nLo = tmp.First(keyA) ;
if (nLo < 0)
return E_NOTFOUND ;
nHi = tmp.Last(keyB) ;
for (; nLo <&eq; nHi ; nLo++)
{
pair.name = tmp.GetKey(nLo) ;
pair.value = tmp.GetObj(nLo) ;
result.Add(pair) ;
}
return rc ;
}