Skip to content

Commit

Permalink
New TRoE support for troePostBatchDelete
Browse files Browse the repository at this point in the history
  • Loading branch information
kzangeli committed May 3, 2021
1 parent 2bc11aa commit bcb794e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 69 deletions.
2 changes: 2 additions & 0 deletions src/lib/orionld/troe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ SET (SOURCES
pgSubAttributeBuild.cpp
pgQuotedString.cpp
pgCommands.cpp
pgEntityAppend.cpp
)

SET (HEADERS
Expand Down Expand Up @@ -159,6 +160,7 @@ SET (HEADERS
pgSubAttributeBuild.h
pgQuotedString.h
pgCommands.h
pgEntityAppend.h
)


Expand Down
53 changes: 53 additions & 0 deletions src/lib/orionld/troe/pgEntityAppend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* Copyright 2021 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include <stdio.h> // snprintf

#include "orionld/common/orionldState.h" // orionldState
#include "orionld/troe/PgAppendBuffer.h" // PgAppendBuffer
#include "orionld/troe/pgAppend.h" // pgAppend
#include "orionld/troe/pgEntityAppend.h" // pgEntityAppend



// ----------------------------------------------------------------------------
//
// pgEntityAppend -
//
// INSERT INTO entities(instanceId,
// ts,
// opMode,
// id,
// type) VALUES (), (), ...
//
void pgEntityAppend(PgAppendBuffer* entitiesBufferP, const char* opMode, const char* entityId, const char* entityType, const char* instanceId)
{
char buf[1024];
const char* comma = (entitiesBufferP->values != 0)? "," : "";

snprintf(buf, sizeof(buf), "%s('%s', '%s', '%s', '%s', '%s')", comma, instanceId, orionldState.requestTimeString, opMode, entityId, entityType);

pgAppend(entitiesBufferP, buf, 0);
entitiesBufferP->values += 1;
}
44 changes: 44 additions & 0 deletions src/lib/orionld/troe/pgEntityAppend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef SRC_LIB_ORIONLD_TROE_PGENTITYAPPEND_H_
#define SRC_LIB_ORIONLD_TROE_PGENTITYAPPEND_H_

/*
*
* Copyright 2021 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include "orionld/troe/PgAppendBuffer.h" // PgAppendBuffer



// ----------------------------------------------------------------------------
//
// pgEntityAppend -
//
// INSERT INTO entities(instanceId,
// ts,
// opMode,
// id,
// type) VALUES (), (), ...
//
extern void pgEntityAppend(PgAppendBuffer* entitiesBufferP, const char* opMode, const char* entityId, const char* entityType, const char* instanceId);

#endif // SRC_LIB_ORIONLD_TROE_PGENTITYAPPEND_H_
32 changes: 2 additions & 30 deletions src/lib/orionld/troe/pgEntityBuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,14 @@ extern "C"
#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/uuidGenerate.h" // uuidGenerate
#include "orionld/troe/troe.h" // TroeMode, troeMode
#include "orionld/troe/pgAppend.h" // pgAppend
#include "orionld/troe/PgAppendBuffer.h" // PgAppendBuffer
#include "orionld/troe/pgAttributeBuild.h" // pgAttributeBuild
#include "orionld/troe/pgSubAttributeBuild.h" // pgSubAttributeBuild
#include "orionld/troe/pgEntityAppend.h" // pgEntityAppend
#include "orionld/troe/pgEntityBuild.h" // Own interface



// ----------------------------------------------------------------------------
//
// entityAppend -
//
// INSERT INTO entities(instanceId,
// ts,
// opMode,
// id,
// type) VALUES (), (), ...
//
static void entityAppend(PgAppendBuffer* entitiesBufferP, const char* opMode, const char* entityId, const char* entityType, const char* instanceId)
{
char buf[1024];
const char* comma = (entitiesBufferP->values != 0)? "," : "";

snprintf(buf, sizeof(buf), "%s('%s', '%s', '%s', '%s', '%s')", comma, instanceId, orionldState.requestTimeString, opMode, entityId, entityType);

LM_TMP(("Calling pgAppend"));
pgAppend(entitiesBufferP, buf, 0);
LM_TMP(("Back from pgAppend"));
entitiesBufferP->values += 1;
LM_TMP(("Here"));
}



// ----------------------------------------------------------------------------
//
// pgEntityBuild -
Expand Down Expand Up @@ -117,9 +91,7 @@ bool pgEntityBuild
LM_RE(false, ("Missing Entity id/type"));

// We have all the entity info - time to add the entity
LM_TMP(("Entity SQL buffer BEFORE: '%s'", entitiesBufferP->buf));
entityAppend(entitiesBufferP, opMode, entityId, entityType, instanceId);
LM_TMP(("Entity SQL buffer AFTER: '%s'", entitiesBufferP->buf));
pgEntityAppend(entitiesBufferP, opMode, entityId, entityType, instanceId);

// All that remains in 'entityNodeP' are attributes!
for (KjNode* attrP = entityNodeP->value.firstChildP; attrP != NULL; attrP = attrP->next)
Expand Down
54 changes: 15 additions & 39 deletions src/lib/orionld/troe/troePostBatchDelete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@

#include "rest/ConnectionInfo.h" // ConnectionInfo
#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/orionldErrorResponse.h" // orionldErrorResponseCreate
#include "orionld/common/uuidGenerate.h" // uuidGenerate
#include "orionld/troe/pgConnectionGet.h" // pgConnectionGet
#include "orionld/troe/pgConnectionRelease.h" // pgConnectionRelease
#include "orionld/troe/pgTransactionBegin.h" // pgTransactionBegin
#include "orionld/troe/pgTransactionRollback.h" // pgTransactionRollback
#include "orionld/troe/pgTransactionCommit.h" // pgTransactionCommit
#include "orionld/troe/pgEntityDelete.h" // pgEntityDelete
#include "orionld/troe/PgTableDefinitions.h" // PG_ATTRIBUTE_INSERT_START, PG_SUB_ATTRIBUTE_INSERT_START
#include "orionld/troe/PgAppendBuffer.h" // PgAppendBuffer
#include "orionld/troe/pgAppendInit.h" // pgAppendInit
#include "orionld/troe/pgAppend.h" // pgAppend
#include "orionld/troe/pgEntityAppend.h" // pgEntityAppend
#include "orionld/troe/pgCommands.h" // pgCommands
#include "orionld/troe/troePostBatchDelete.h" // Own interface


Expand All @@ -47,48 +46,25 @@
//
bool troePostBatchDelete(ConnectionInfo* ciP)
{
PGconn* connectionP = pgConnectionGet(orionldState.troeDbName);
if (connectionP == NULL)
LM_RE(false, ("no connection to postgres"));
PgAppendBuffer entitiesBuffer;

if (pgTransactionBegin(connectionP) != true)
{
pgConnectionRelease(connectionP);
LM_RE(false, ("pgTransactionBegin failed"));
}
pgAppendInit(&entitiesBuffer, 2*1024); // 4k - will be reallocated if necessary
pgAppend(&entitiesBuffer, PG_ENTITY_INSERT_START, 0);

bool allGood = true;
for (KjNode* entityIdP = orionldState.requestTree->value.firstChildP; entityIdP != NULL; entityIdP = entityIdP->next)
{
char* entityId = entityIdP->value.s;
char instanceId[80];
uuidGenerate(instanceId, sizeof(instanceId), true);

if (pgEntityDelete(connectionP, instanceId, entityIdP->value.s) == false)
{
LM_E(("Database Error (batch delete entities TRoE layer failed)"));
allGood = false;
break;
}
}
uuidGenerate(instanceId, sizeof(instanceId), true);

if (allGood == true)
{
if (pgTransactionCommit(connectionP) != true)
{
pgConnectionRelease(connectionP);
LM_RE(false, ("pgTransactionCommit failed"));
}
pgEntityAppend(&entitiesBuffer, "Delete", entityId, "NULL", instanceId);
}
else
{
if (pgTransactionRollback(connectionP) == false)
LM_E(("pgTransactionRollback failed"));

pgConnectionRelease(connectionP);
return false;
}
const char* sqlV[1] = { entitiesBuffer.buf };
int commands = 1;

pgConnectionRelease(connectionP);
pgCommands(sqlV, commands);

return true;
}

0 comments on commit bcb794e

Please sign in to comment.