30 int bulk_onAllMatches(
MonkState* state,
const File* file,
const GArray* matches);
42 gchar* sql = g_strdup_printf(
"SELECT lft, rgt FROM %s WHERE uploadtree_pk = $1", tableName);
43 gchar* stmt = g_strdup_printf(
"setLeftAndRight.%s", tableName);
45 if ((!sql) || (!stmt))
49 fo_dbManager_PrepareStamement(
55 bulkArguments->uploadTreeId
63 if (leftAndRightResult) {
64 if (PQntuples(leftAndRightResult)==1) {
66 bulkArguments->uploadTreeLeft = atol(PQgetvalue(leftAndRightResult, 0, i++));
67 bulkArguments->uploadTreeRight = atol(PQgetvalue(leftAndRightResult, 0, i));
71 PQclear(leftAndRightResult);
76 void bulkArguments_contents_free(
BulkArguments* bulkArguments);
80 int queryBulkArguments(
MonkState* state,
long bulkId) {
84 fo_dbManager_PrepareStamement(
87 "SELECT ut.upload_fk, ut.uploadtree_pk, lrb.user_fk, lrb.group_fk, lrb.rf_text " 88 "FROM license_ref_bulk lrb INNER JOIN uploadtree ut " 89 "ON ut.uploadtree_pk = lrb.uploadtree_fk " 96 if (bulkArgumentsResult) {
97 if (PQntuples(bulkArgumentsResult)==1) {
101 bulkArguments->uploadId = atoi(PQgetvalue(bulkArgumentsResult, 0, column++));
102 bulkArguments->uploadTreeId = atol(PQgetvalue(bulkArgumentsResult, 0, column++));
103 bulkArguments->userId = atoi(PQgetvalue(bulkArgumentsResult, 0, column++));
104 bulkArguments->groupId = atoi(PQgetvalue(bulkArgumentsResult, 0, column++));
105 bulkArguments->refText = g_strdup(PQgetvalue(bulkArgumentsResult, 0, column++));
106 bulkArguments->bulkId = bulkId;
107 bulkArguments->actions = queryBulkActions(state, bulkId);
110 state->ptr = bulkArguments;
112 if (!setLeftAndRight(state)) {
113 printf(
"FATAL: could not retrieve left and right for bulk id=%ld\n", bulkId);
114 bulkArguments_contents_free(state->ptr);
119 printf(
"FATAL: could not retrieve arguments for bulk scan with id=%ld\n", bulkId);
121 PQclear(bulkArgumentsResult);
129 fo_dbManager_PrepareStamement(
132 "SELECT rf_fk, removing, comment, reportinfo, acknowledgement FROM license_set_bulk WHERE lrb_fk = $1",
138 int numberOfRows = bulkActionsResult ? PQntuples(bulkActionsResult) : 0;
142 for (row = 0; row < numberOfRows; row++) {
145 action->licenseId = atoi(PQgetvalue(bulkActionsResult, row, column++));
146 action->removing = (strcmp(PQgetvalue(bulkActionsResult, row, column++),
"t") == 0);
147 action->comment = g_strdup(PQgetvalue(bulkActionsResult, row, column++));
148 action->reportinfo = g_strdup(PQgetvalue(bulkActionsResult, row, column++));
149 action->acknowledgement = g_strdup(PQgetvalue(bulkActionsResult, row, column++));
150 bulkActions[row] = action;
152 bulkActions[row] = NULL;
154 if (bulkActionsResult) {
155 PQclear(bulkActionsResult);
161 void bulkArguments_contents_free(
BulkArguments* bulkArguments) {
163 BulkAction **bulkActions = bulkArguments->actions;
164 for (
int i=0; bulkActions[i] != NULL; i++) {
165 free(bulkActions[i]);
169 g_free(bulkArguments->refText);
174 int bulk_identification(
MonkState* state) {
178 .refId = bulkArguments->licenseId,
180 license.tokens = tokenize(bulkArguments->refText, DELIMITERS);
182 GArray* licenseArray = g_array_new(FALSE, FALSE,
sizeof (
License));
183 g_array_append_val(licenseArray, license);
185 Licenses* licenses = buildLicenseIndexes(licenseArray, MIN_ADJACENT_MATCHES, 0);
187 PGresult* filesResult = queryFileIdsForUploadAndLimits(
189 bulkArguments->uploadId,
190 bulkArguments->uploadTreeLeft,
191 bulkArguments->uploadTreeRight,
192 bulkArguments->groupId
196 if (filesResult != NULL) {
197 int resultsCount = PQntuples(filesResult);
199 #ifdef MONK_MULTI_THREAD 203 MonkState threadLocalStateStore = *state;
204 MonkState* threadLocalState = &threadLocalStateStore;
206 threadLocalState->dbManager = fo_dbManager_fork(state->dbManager);
207 if (threadLocalState->dbManager) {
208 #ifdef MONK_MULTI_THREAD 209 #pragma omp for schedule(dynamic) 211 for (
int i = 0; i<resultsCount; i++) {
215 long fileId = atol(PQgetvalue(filesResult, i, 0));
224 fo_dbManager_finish(threadLocalState->dbManager);
229 PQclear(filesResult);
232 licenses_free(licenses);
237 int main(
int argc,
char** argv) {
245 state->scanMode = MODE_BULK;
250 long bulkId = atol(schedulerCurrent);
252 if (bulkId == 0)
continue;
254 if (!queryBulkArguments(state, bulkId)) {
260 int arsId =
fo_WriteARS(fo_dbManager_getWrappedConnection(state->dbManager),
261 0, bulkArguments->uploadId, state->agentId, AGENT_BULK_ARS, NULL, 0);
266 if (!bulk_identification(state))
269 fo_WriteARS(fo_dbManager_getWrappedConnection(state->dbManager),
270 arsId, bulkArguments->uploadId, state->agentId, AGENT_BULK_ARS, NULL, 1);
272 bulkArguments_contents_free(bulkArguments);
276 scheduler_disconnect(state, 0);
280 int bulk_onAllMatches(
MonkState* state,
const File* file,
const GArray* matches) {
281 int haveAFullMatch = 0;
282 for (guint j=0; j<matches->len; j++) {
285 if (match->type == MATCH_TYPE_FULL) {
296 if (!fo_dbManager_begin(state->dbManager))
299 BulkAction **actions = bulkArguments->actions;
300 for (
int i = 0; actions[i] != NULL; i++) {
304 fo_dbManager_PrepareStamement(
306 "saveBulkResult:decision",
307 "INSERT INTO clearing_event(uploadtree_fk, user_fk, group_fk, job_fk, type_fk, rf_fk, removed, comment, reportinfo, acknowledgement)" 308 " SELECT uploadtree_pk, $2, $3, $4, $5, $6, $7, $8, $9, $10" 310 " WHERE upload_fk = $11 AND pfile_fk = $1 AND lft BETWEEN $12 AND $13" 311 "RETURNING clearing_event_pk",
312 long,
int,
int,
int,
int,
long,
int,
char*,
char*,
char*,
317 bulkArguments->userId,
318 bulkArguments->groupId,
319 bulkArguments->jobId,
322 action->removing ? 1 : 0,
325 action->acknowledgement,
327 bulkArguments->uploadId,
328 bulkArguments->uploadTreeLeft,
329 bulkArguments->uploadTreeRight
332 if (licenseDecisionIds) {
333 for (
int i=0; i<PQntuples(licenseDecisionIds);i++) {
334 long licenseDecisionEventId = atol(PQgetvalue(licenseDecisionIds,i,0));
336 for (guint j=0; j<matches->len; j++) {
337 Match* match = match_array_index(matches, j);
339 if (match->type != MATCH_TYPE_FULL)
342 DiffPoint* highlightTokens = match->ptr.full;
343 DiffPoint highlight = getFullHighlightFor(file->tokens, highlightTokens->start, highlightTokens->length);
346 fo_dbManager_PrepareStamement(
348 "saveBulkResult:highlight",
349 "INSERT INTO highlight_bulk(clearing_event_fk, lrb_fk, start, len) VALUES($1,$2,$3,$4)",
350 long,
long,
size_t,
size_t 352 licenseDecisionEventId,
353 bulkArguments->bulkId,
358 if (highlightResult) {
359 PQclear(highlightResult);
361 fo_dbManager_rollback(state->dbManager);
366 PQclear(licenseDecisionIds);
368 fo_dbManager_rollback(state->dbManager);
374 return fo_dbManager_commit(state->dbManager);
void bail(int exitval)
Disconnect with scheduler returning an error code and exit.
Store the results of a regex match.
char * getUploadTreeTableName(fo_dbManager *dbManager, int uploadId)
Get the upload tree table name for a given upload.
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
char * fo_scheduler_current()
Get the last read string from the scheduler.
The main FOSSology C library.
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well. ...
int queryAgentId(PGconn *dbConn)
Get agent id, exit if agent id is incorrect.
int fo_scheduler_jobId()
Gets the id of the job that the agent is running.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
void matchPFileWithLicenses(CopyrightState const &state, int agentId, unsigned long pFileId, CopyrightDatabaseHandler &databaseHandler)
Get the file contents, scan for statements and save findings to database.