-
Notifications
You must be signed in to change notification settings - Fork 1
Add kv_list and refactor session_settings with it #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+223
−39
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #ifndef PG_CLICKHOUSE_KV_LIST_H | ||
| #define PG_CLICKHOUSE_KV_LIST_H | ||
|
|
||
| #include <stdbool.h> | ||
| #include "postgres.h" | ||
| #include "nodes/pathnodes.h" | ||
|
|
||
| /* A key/value pair. */ | ||
| typedef struct kv_pair | ||
| { | ||
| char *name; | ||
| char *value; | ||
| } kv_pair; | ||
|
|
||
| /* | ||
| * A simple data structure with a list of key/value string pairs. Use | ||
| * new_kv_list_from_list() to create. | ||
| * | ||
| * kv_list * pairs = new_kv_list_from_pg_list(list); | ||
| * if (!kv_list) | ||
| * { | ||
| * printf("out of memory\n"); | ||
| * exit(1); | ||
| * } | ||
| * | ||
| * for (int i = 0; i < kv_list->length; i++) { | ||
| * kv_pair * pair = kv_list->items[i]; | ||
| * printf("%s => %s\n", pair->name, pair->value); | ||
| * } | ||
| * | ||
| * kv_list_free(pairs); | ||
| */ | ||
| typedef struct kv_list | ||
| { | ||
| int length; | ||
| kv_pair **items; | ||
| } kv_list; | ||
|
|
||
| /* | ||
| * Create an empty kv_list, with length set to zero and items uninitialized. | ||
| */ | ||
| kv_list * new_empty_kv_list(void); | ||
|
|
||
| /* | ||
| * Create a new kv_list from a PostgreSQL List of DefElem. Logs an error | ||
| * message and returns NULL on memory errors. | ||
| */ | ||
| kv_list * new_kv_list_from_pg_list(List * list); | ||
|
|
||
| /* Frees the memory owned by the kv_list. */ | ||
| void kv_list_free(kv_list * pairs); | ||
|
|
||
| #endif /* PG_CLICKHOUSE_KV_LIST_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include <stddef.h> | ||
| #include "kv_list.h" | ||
| #include "utils/elog.h" | ||
| #include "nodes/pathnodes.h" | ||
| #include "utils/guc.h" | ||
|
|
||
| extern kv_list * new_empty_kv_list(void) | ||
| { | ||
| kv_list *pairs = (kv_list *) malloc(sizeof(kv_list)); | ||
|
|
||
| if (pairs == NULL) | ||
| { | ||
| ereport(LOG, errcode(ERRCODE_FDW_OUT_OF_MEMORY), errmsg("out of memory")); | ||
| return NULL; | ||
| } | ||
| pairs->length = 0; | ||
| return pairs; | ||
| } | ||
|
|
||
| extern kv_list * new_kv_list_from_pg_list(List * list) | ||
| { | ||
| ListCell *lc; | ||
| DefElem *elem; | ||
| kv_list *pairs; | ||
| int i = 0; | ||
|
|
||
| /* Allocate the memory for the pairs object. */ | ||
| pairs = (kv_list *) malloc(sizeof(kv_list)); | ||
| if (pairs == NULL) | ||
| { | ||
| ereport(LOG, errcode(ERRCODE_FDW_OUT_OF_MEMORY), errmsg("out of memory")); | ||
| return NULL; | ||
| } | ||
|
|
||
| /* Allocate the memory for kv_pair pointers to all the items in list. */ | ||
| pairs->items = (kv_pair * *) malloc(list_length(list) * sizeof(kv_pair *)); | ||
| if (pairs->items == NULL) | ||
| { | ||
| free(pairs); | ||
| ereport(LOG, errcode(ERRCODE_FDW_OUT_OF_MEMORY), errmsg("out of memory")); | ||
| return NULL; | ||
| } | ||
| pairs->length = list_length(list); | ||
|
|
||
| /* Copy the values from list into pairs. */ | ||
| foreach(lc, list) | ||
| { | ||
| kv_pair *pair = malloc(sizeof(kv_pair)); | ||
|
|
||
| if (!pair) | ||
| { | ||
| kv_list_free(pairs); | ||
| ereport(LOG, errcode(ERRCODE_FDW_OUT_OF_MEMORY), errmsg("out of memory")); | ||
| return NULL; | ||
| } | ||
| elem = (DefElem *) lfirst(lc); | ||
| pair->name = strdup(elem->defname); | ||
| pair->value = strdup(strVal(elem->arg)); | ||
| if (!pair->name || !pair->value) | ||
| { | ||
| kv_list_free(pairs); | ||
| ereport(LOG, errcode(ERRCODE_FDW_OUT_OF_MEMORY), errmsg("out of memory")); | ||
| return NULL; | ||
| } | ||
| pairs->items[i] = pair; | ||
| i++; | ||
| } | ||
|
|
||
| return pairs; | ||
| } | ||
|
|
||
| extern void | ||
| kv_list_free(kv_list * pairs) | ||
| { | ||
| if (pairs == NULL) | ||
| return; | ||
| for (int i = 0; i < pairs->length; i++) | ||
| if (pairs->items[i] != NULL) | ||
| { | ||
| if (pairs->items[i]->name) | ||
| free(pairs->items[i]->name); | ||
| if (pairs->items[i]->value) | ||
| free(pairs->items[i]->value); | ||
| free(pairs->items[i]); | ||
| } | ||
| free(pairs->items); | ||
| free(pairs); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.