Skip to content

Commit

Permalink
Added EditEventStore transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucio Anderlini authored and Lucio Anderlini committed Aug 20, 2023
1 parent a4a93e4 commit 3e7b915
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ project(SQLamarr)
src/GenerativePlugin.cpp
src/TemporaryTable.cpp
src/CleanEventStore.cpp
src/EditEventStore.cpp
src/version.cpp
src/blib/LbParticleId.cpp
src/main.cpp
Expand All @@ -63,6 +64,7 @@ project(SQLamarr)
src/GenerativePlugin.cpp
src/TemporaryTable.cpp
src/CleanEventStore.cpp
src/EditEventStore.cpp
src/python_bindings.cpp
)

Expand Down
52 changes: 52 additions & 0 deletions include/SQLamarr/EditEventStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// (c) Copyright 2022 CERN for the benefit of the LHCb Collaboration.
//
// This software is distributed under the terms of the GNU General Public
// Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE".
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#pragma once

// STL
#include <string>
#include <vector>

// SQLamarr
#include "SQLamarr/BaseSqlInterface.h"
#include "SQLamarr/Transformer.h"

namespace SQLamarr
{
/** Execute a query non returning anything, but modifying the event store.
*
* Sometimes it may be necessary to modify the event model during the
* execution of a pipeline, for example to drop tables or to add columns
* to existing ones, or even to update some table.
*
* EditEventStore provides a generic building block which enables
* executing arbitrary queries to the SQLite3 database, without storing the
* output, if any.
*
*/
class EditEventStore: public BaseSqlInterface, public Transformer
{
public:
/// Constructor
EditEventStore (
SQLite3DB& db,
///< Reference to the database
const std::vector<std::string>& queries
///< Vector of queries to be executed in a single transaction
);


/// Execute the algorithm, cleaning the database
void execute () override;

private:
const std::vector<std::string> m_queries;
};
}

57 changes: 57 additions & 0 deletions python/SQLamarr/EditEventStore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# (c) Copyright 2022 CERN for the benefit of the LHCb Collaboration.
#
# This software is distributed under the terms of the GNU General Public
# Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE".
#
# In applying this licence, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.
import ctypes
from ctypes import POINTER
from SQLamarr import clib, c_TransformerPtr

from SQLamarr.db_functions import SQLite3DB

from typing import List, Union

clib.new_EditEventStore.argtypes = (
ctypes.c_void_p, # void *db,
ctypes.c_char_p, # const char* semicolon_separated_queries,
)

clib.new_EditEventStore.restype = c_TransformerPtr

class EditEventStore:
"""
Execute a transaction with multiple custom queries without
returning any output.
Refer to SQLamarr::EditEventStore for implementation details.
"""
def __init__ (self, db: SQLite3DB, queries: Union[str,List[str]]):
"""
Configure a Transformer to edit the schema of the EventStore
@param db: An open database connection.
@param queries: One or more queries to be processed in single transaction
"""
if isinstance(queries, str):
queries = [queries]

self._self = clib.new_EditEventStore(
db.get(),
";".join(queries).encode('ascii')
)

def __del__(self):
"""@private: Release the bound class instance"""
clib.del_Transformer(self._self)

@property
def raw_pointer(self):
"""@private: Return the raw pointer to the algorithm."""
return self._self




1 change: 1 addition & 0 deletions python/SQLamarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class c_TransformerPtr (ctypes.Structure):
from SQLamarr.GenerativePlugin import GenerativePlugin
from SQLamarr.TemporaryTable import TemporaryTable
from SQLamarr.CleanEventStore import CleanEventStore
from SQLamarr.EditEventStore import EditEventStore

## Python Transfomer
from SQLamarr.PyTransformer import PyTransformer
Expand Down
51 changes: 51 additions & 0 deletions src/EditEventStore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// (c) Copyright 2022 CERN for the benefit of the LHCb Collaboration.
//
// This software is distributed under the terms of the GNU General Public
// Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE".
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.


// STL
#include <iostream>
#include <algorithm>

// SQLite3
#include "sqlite3.h"

// SQLamarr
#include "SQLamarr/EditEventStore.h"

namespace SQLamarr
{
//============================================================================
// Constructor
//============================================================================
EditEventStore::EditEventStore(
SQLite3DB& db,
const std::vector<std::string>& queries
)
: BaseSqlInterface(db)
, m_queries (queries)
{}

//============================================================================
// execute
//============================================================================
void EditEventStore::execute()
{
int counter = 0;
char buffer[1024];
exec_stmt(get_statement("BEGIN", "BEGIN EXCLUSIVE TRANSACTION"));
for (auto& query: m_queries)
{
sprintf(buffer, "EditEventStore%02d", counter++);
exec_stmt(get_statement(buffer, query.c_str()));
}

exec_stmt(get_statement("COMMIT", "COMMIT TRANSACTION"));
}
}

22 changes: 22 additions & 0 deletions src/python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "SQLamarr/GlobalPRNG.h"
#include "SQLamarr/TemporaryTable.h"
#include "SQLamarr/CleanEventStore.h"
#include "SQLamarr/EditEventStore.h"
#include "SQLamarr/SQLiteError.h"

constexpr int SQL_ERRORSHIFT = 10000;
Expand All @@ -37,6 +38,7 @@ typedef enum {
, GenerativePlugin
, TemporaryTable
, CleanEventStore
, EditEventStore
} TransformerType;

struct TransformerPtr {
Expand Down Expand Up @@ -237,6 +239,21 @@ TransformerPtr new_CleanEventStore (void *db)
return {CleanEventStore, new SQLamarr::CleanEventStore(*udb)};
}

//==============================================================================
// EditEventStore
//==============================================================================
extern "C"
TransformerPtr new_EditEventStore (
void *db,
const char* semicolon_separated_queries
)
{
SQLite3DB *udb = reinterpret_cast<SQLite3DB *>(db);
return {EditEventStore, new SQLamarr::EditEventStore(*udb,
tokenize(semicolon_separated_queries)
)};
}

//==============================================================================
// Delete Transformer
//==============================================================================
Expand Down Expand Up @@ -266,6 +283,9 @@ void del_Transformer (TransformerPtr self)
case CleanEventStore:
delete reinterpret_cast<SQLamarr::CleanEventStore*> (self.p);
break;
case EditEventStore:
delete reinterpret_cast<SQLamarr::EditEventStore*> (self.p);
break;
default:
throw std::bad_cast();
}
Expand Down Expand Up @@ -293,6 +313,8 @@ SQLamarr::Transformer* resolve_polymorphic_transformer(TransformerPtr self)
return reinterpret_cast<SQLamarr::TemporaryTable*> (self.p);
case CleanEventStore:
return reinterpret_cast<SQLamarr::CleanEventStore*> (self.p);
case EditEventStore:
return reinterpret_cast<SQLamarr::EditEventStore*> (self.p);
}

throw std::bad_cast();
Expand Down

0 comments on commit 3e7b915

Please sign in to comment.