The INSERT operation adds a new data object to a repository and creates a new object id in respect of it. If any indexes apply, these are updated accordingly. Note that in INSERT operations, the supplied object is expected to be an entirely new, and whole, object of the repository native data class. All subclass objects will have been added to the native class data object, prior to this function call. Because the supplied data object is entirely new, a whole object delta is generated. In all cases the new data object is committed to a binary datum repository as a whole object delta. If RAM Primacy applies, the new data object is also written as an EDO to repository cache. The whole object delta is produced by calling ExportDelta() on the supplied object. The EDO is produced by calling ExportEDO() on the supplied object. If the object has populated BINARY or TXTDOC members, their values are committed to the applicable binary datum repository before the delta and EDO exports. This is so that the values are assigned datum ids - which are the only representation the values will have in the delta and in the EDO.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hdbObjRepos::Insert | (uint32_t&,hdbObject&,) |
Declared in file: hzDatabase.h
Defined in file : hdbObjRepos.cpp
Function Logic:
Function body:
hzEcode hdbObjRepos::Insert (uint32_t& objId)hdbObject& theObj,
{
// The INSERT operation adds a new data object to a repository and creates a new object id in respect of it. If any indexes apply, these are updated accordingly.
//
// Note that in INSERT operations, the supplied object is expected to be an entirely new, and whole, object of the repository native data class. All subclass objects will have
// been added to the native class data object, prior to this function call. Because the supplied data object is entirely new, a whole object delta is generated.
//
// In all cases the new data object is committed to a binary datum repository as a whole object delta. If RAM Primacy applies, the new data object is also written as an EDO to
// repository cache. The whole object delta is produced by calling ExportDelta() on the supplied object. The EDO is produced by calling ExportEDO() on the supplied object.
//
// If the object has populated BINARY or TXTDOC members, their values are committed to the applicable binary datum repository before the delta and EDO exports. This is so that
// the values are assigned datum ids - which are the only representation the values will have in the delta and in the EDO.
//
// Arguments: 1) objId The object id that will be assigned by this operation
// 2) pObj Pointer to object to be inserted
//
// Returns: E_NOINIT If either the cache or the supplied object is not initialized
// E_ARGUMENT If the object is not supplied
// E_TYPE If the object is not of the same data class as the cache
// E_DUPLICATE If the object cannot be inserted because one or more members violate uniqueness
// E_WRITEFAIL If the object deltas cannot be written
// E_OK If the insert operation was successful
_hzfunc("hdbObjRepos::Insert") ;
const hdbMember* pMbr ; // Member pointer
hzChain Z ; // For building output to data file
hzChain theChain ; // For extracting atom data stored as chains
hzChain edo ; // For EDO export and commital
hzChain delta ; // For delta export
hzAtom atom ; // Atom from member of populating object
_atomval av ; // Atomic value
hdbIndex* pIdx ; // Index pointer
hdbIndexUkey* pIdxU ; // Index pointer
// hdbIndexEnum* pIdxE ; // Index pointer
hzString strVal ; // Temp string
uint32_t mbrNo ; // Member number
hzEcode rc = E_OK ; // Return value
// Check Init state
if (!this)
Fatal("No Instance\n") ;
objId = 0;
_hdb_ck_initstate(m_Name, m_eReposInit, HDB_REPOS_OPEN) ;
if (!theObj.Class())
return hzerr(E_NOINIT, "Supplied object is not initialized") ;
// Check object class is the same as the host class or contains a sub-class that is the same as the sub-class
if (theObj.Class() != m_pClass)
{
if (!m_pADP->IsSubClass(m_pClass, theObj.Class()))
{
hzerr(E_TYPE, "Supplied object class %s not compatible with this cache class %s", theObj.Classname(), m_pClass->txtType()) ;
return E_TYPE ;
}
}
// For an INSERT the object id (in the supplied object), must be 0
if (theObj.GetObjId() != 0)
return hzerr(E_RANGE, "Supplied object has an object id of %u", theObj.GetObjId()) ;
/*
** ** Check members to see if any of them require unique values. Any that do will have an unique key index. If the member value already exists, abort the INSERT.
** */
for (mbrNo = 0; mbrNo < m_pClass->MbrCount() ; mbrNo++)
{
pMbr = m_pClass->GetMember(mbrNo) ;
pIdx = m_mapIndex[pMbr->DeltaId()] ;
if (!pIdx)
continue ;
if (pIdx->Whatami() == HZINDEX_UKEY)
{
pIdxU = (hdbIndexUkey*) pIdx ;
rc = theObj.GetValue(atom, pMbr) ;
if (rc == E_NOTFOUND)
continue ;
rc = pIdxU->Select(objId, atom) ;
if (rc != E_OK)
return hzerr(rc, "%s: Could not select on index for member %s", *m_Name, pMbr->txtName()) ;
if (objId)
return hzerr(E_DUPLICATE, "%s: The value for member %s, already exists in object %u", *m_Name, pMbr->txtName(), objId) ;
}
}
/*
** ** The new object does not conflict with an existing one and so insertation can proceed. The objId is assigned as the number of existing objects + 1
** */
// Issue the object id
objId = ++m_nSeqId ;
theObj.SetObjId(objId) ;
// Commit BINARY/TXTDOC values
if (m_pBR_Datum)
{
rc = theObj.CommitBinaries(m_pBR_Datum) ;
if (rc != E_OK)
return hzerr(rc, "Could not commit binaries") ;
}
// Export whole object delta
rc = theObj.ExportDelta(delta) ;
if (rc != E_OK)
return hzerr(rc, "Could not export whole object delta") ;
// Update the repository delta file
m_osDelta << delta ;
m_osDelta.flush() ;
// Originate delta to DS
m_pADP->DeltaOriginateObj(this, delta) ;
// Commit whole object delta to binary datum repository
if (m_eMode & HDB_REPOS_CACHE)
{
// Commit EDO
rc = theObj.ExportEDO(edo) ;
if (rc != E_OK)
return hzerr(rc, "Could not export EDO") ;
// Commit EDO to cache
rc = CommitEDO(edo, objId, false) ;
if (rc != E_OK)
return hzerr(rc, "Could not commit EDO") ;
}
// Update indexes
rc = _updateIdx(theObj) ;
if (rc != E_OK)
return hzerr(rc, "Could not update indexes") ;
return rc ;
}