Added bucket functionality

This commit is contained in:
Granyaa
2026-01-14 16:06:07 +02:00
parent 4999d05903
commit dc85e0e789
4 changed files with 75 additions and 15 deletions

View File

@@ -278,31 +278,28 @@ isNanoQueueEmpty(NanoAttachment *attachment)
return false;
}
// TODO: Implement
// [hash on session id] -> [verdict+modification]
// Updating the table, and then return the session ID to the caller
SessionID
PopFromNanoQueue(NanoAttachment *attachment)
{
// Pop from queue
// Put into hash table.
AttachmentVerdictResponse response;
SessionID session_id = 0;
NanoCommunicationResult res;
// Pop from queue
res = NanoAsyncAddResponse(attachment, session_id, &response);
if (res != NANO_OK) {
return 0;
}
return session_id;
}
// TODO: Implement
// given a sessionID return a verdict from the table.
AttachmentVerdictResponse
getAttachmentVerdictResponse(NanoAttachment *attachment, SessionID session_id)
{
(void)attachment;
(void)session_id;
return (AttachmentVerdictResponse) {
.verdict = ATTACHMENT_VERDICT_INSPECT,
.session_id = session_id,
.modifications = NULL
};
AttachmentVerdictResponse response = NanoAsyncFindResponse(attachment, session_id);
NanoAsyncRemoveResponse(attachment, session_id);
return response;
}

View File

@@ -1 +1,57 @@
#include "nano_attachment_bucket.h"
#define CP_ASYNC_CTX_BUCKETS 2048 ///< Hash table buckets for better distribution
///
/// @brief Hash function for session IDs using Knuth's multiplicative method
/// @param[in] session_id Session ID to hash
/// @return Hash bucket index
///
static uint
nano_attachment_async_ctx_hash(SessionID session_id)
{
// Knuth's multiplicative hash with golden ratio constant
static const uint32_t KNUTH_CONSTANT = 2654435761U; // (sqrt(5) - 1) / 2 * 2^32
return (session_id * KNUTH_CONSTANT) % CP_ASYNC_CTX_BUCKETS;
}
AttachmentVerdictResponse
NanoAsyncFindResponse(NanoAttachment *attachment, SessionID session_id)
{
AttachmentVerdictResponse response;
uint bucket = nano_attachment_async_ctx_hash(session_id);
response.verdict = attachment->async_buckets[bucket]->verdict;
response.session_id = attachment->async_buckets[bucket]->session_id;
response.modifications = attachment->async_buckets[bucket]->modifications;
response.web_response_data = attachment->async_buckets[bucket]->web_response_data;
return response;
}
NanoCommunicationResult
NanoAsyncAddResponse(NanoAttachment *attachment, SessionID session_id, AttachmentVerdictResponse *response)
{
uint bucket;
if (response == NULL) {
return NANO_ERROR;
}
bucket = nano_attachment_async_ctx_hash(session_id);
attachment->async_buckets[bucket]->session_id = response->session_id;
attachment->async_buckets[bucket]->verdict = response->verdict;
attachment->async_buckets[bucket]->modifications = response->modifications;
attachment->async_buckets[bucket]->web_response_data = response->web_response_data;
return NANO_OK;
}
void
NanoAsyncRemoveResponse(NanoAttachment *attachment, SessionID session_id)
{
uint bucket = nano_attachment_async_ctx_hash(ctx->session_id);
attachment->async_buckets[bucket]->session_id = 0;
attachment->async_buckets[bucket]->verdict = ATTACHMENT_VERDICT_INSPECT;
attachment->async_buckets[bucket]->modifications = NULL;
attachment->async_buckets[bucket]->web_response_data = NULL;
}

View File

@@ -5,4 +5,9 @@
#include "nano_attachment_common.h"
#include "nano_initializer.h"
AttachmentVerdictResponse NanoAsyncFindResponse(NanoAttachment *attachment, SessionID session_id);
NanoCommunicationResult NanoAsyncAddResponse(NanoAttachment *attachment, SessionID session_id, AttachmentVerdictResponse *response);
void NanoAsyncRemoveResponse(NanoAttachment *attachment, SessionID session_id);
#endif // __NANO_ATTACHMENT_BUCKET_H__

View File

@@ -86,6 +86,8 @@ typedef struct NanoAttachment {
uint64_t metric_data[METRIC_TYPES_COUNT];
uint64_t metric_average_data_divisor[METRIC_TYPES_COUNT];
#endif
AttachmentVerdictResponse async_buckets[CP_ASYNC_CTX_BUCKETS]; ///< Buckets for storing verdict responses.
} NanoAttachment;
///