Skip to content

Commit

Permalink
Clean up after running pgindent
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-mlodgenski committed Nov 14, 2024
1 parent 70c6c52 commit c18a0bd
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 211 deletions.
45 changes: 23 additions & 22 deletions include/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,45 @@
#define uthash_free(ptr,sz) pfree(ptr)
#define uthash_nonfatal_oom(e) do{elog(ERROR, "Unable to allocate memory");}while(0)

#define COLLECTION_MAGIC 8675309 /* ID for debugging crosschecks */
#define COLLECTION_MAGIC 8675309 /* ID for debugging crosschecks */

typedef struct collection {
char *key;
Datum value;
UT_hash_handle hh;
} collection;
typedef struct collection
{
char *key;
Datum value;
UT_hash_handle hh;
} collection;

typedef struct CollectionHeader
{
/* Standard header for expanded objects */
ExpandedObjectHeader hdr;

/* Magic value identifying an expanded array (for debugging only) */
int collection_magic;
int collection_magic;

Oid value_type; /* value type OID */
int16 value_type_len;
bool value_byval;
Oid value_type; /* value type OID */
int16 value_type_len;
bool value_byval;

/*
* flat_size is the current space requirement for the flat equivalent of
* the expanded array, if known; otherwise it's 0. We store this to make
* consecutive calls of get_flat_size cheap.
*/
size_t flat_size;
size_t flat_size;

collection *current;
collection *head;
} CollectionHeader;
collection *current;
collection *head;
} CollectionHeader;

typedef struct FlatCollectionType
{
int32 vl_len_; /* varlena header (do not touch directly!) */
int32 num_entries;
Oid value_type;
char values[];
} FlatCollectionType;
} FlatCollectionType;

typedef struct StatsCounters
{
Expand All @@ -83,19 +84,19 @@ typedef struct StatsCounters
int64 delete;
int64 find;
int64 sort;
} StatsCounters;
} StatsCounters;

extern StatsCounters stats;

extern CollectionHeader *parse_collection(char *json);
extern CollectionHeader *fetch_collection(FunctionCallInfo fcinfo, int argno);
extern CollectionHeader *construct_empty_collection(MemoryContext parentcontext);
extern CollectionHeader * parse_collection(char *json);
extern CollectionHeader * fetch_collection(FunctionCallInfo fcinfo, int argno);
extern CollectionHeader * construct_empty_collection(MemoryContext parentcontext);
CollectionHeader *DatumGetExpandedCollection(Datum d);

/* "Methods" required for an expanded object */
Size collection_get_flat_size(ExpandedObjectHeader *eohptr);
void collection_flatten_into(ExpandedObjectHeader *eohptr,
void *result, Size allocated_size);
Size collection_get_flat_size(ExpandedObjectHeader *eohptr);
void collection_flatten_into(ExpandedObjectHeader *eohptr,
void *result, Size allocated_size);

/* custom wait event values, retrieved from shared memory */
extern uint32 collection_we_flatsize;
Expand Down
66 changes: 34 additions & 32 deletions src/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "postgres.h"
#include "postgres.h"

#include <ctype.h>
#include <limits.h>
Expand Down Expand Up @@ -74,17 +74,17 @@ _PG_init(void)
collection_we_to_table = PG_WAIT_EXTENSION;
collection_we_fetch = PG_WAIT_EXTENSION;
collection_we_assign = PG_WAIT_EXTENSION;
collection_we_input =PG_WAIT_EXTENSION;
collection_we_output =PG_WAIT_EXTENSION;
collection_we_input = PG_WAIT_EXTENSION;
collection_we_output = PG_WAIT_EXTENSION;
#endif
}

Size
collection_get_flat_size(ExpandedObjectHeader *eohptr)
{
CollectionHeader *colhdr = (CollectionHeader *) eohptr;
collection *cur;
size_t sz = 0;
CollectionHeader *colhdr = (CollectionHeader *) eohptr;
collection *cur;
size_t sz = 0;

Assert(colhdr->collection_magic == COLLECTION_MAGIC);

Expand All @@ -93,14 +93,16 @@ collection_get_flat_size(ExpandedObjectHeader *eohptr)

pgstat_report_wait_start(collection_we_flatsize);

for (cur = colhdr->head; cur != NULL; cur = cur->hh.next) {
for (cur = colhdr->head; cur != NULL; cur = cur->hh.next)
{
sz += strlen(cur->key);

if (colhdr->value_type_len != -1)
sz += colhdr->value_type_len;
else
{
struct varlena *s = (struct varlena *) DatumGetPointer(cur->value);

sz += (Size) VARSIZE_ANY(s);
}
sz += sizeof(int16);
Expand All @@ -117,12 +119,12 @@ collection_get_flat_size(ExpandedObjectHeader *eohptr)

void
collection_flatten_into(ExpandedObjectHeader *eohptr,
void *result, Size allocated_size)
void *result, Size allocated_size)
{
CollectionHeader *colhdr = (CollectionHeader *) eohptr;
CollectionHeader *colhdr = (CollectionHeader *) eohptr;
FlatCollectionType *cresult = (FlatCollectionType *) result;
collection *cur;
int location = 0;
collection *cur;
int location = 0;

Assert(allocated_size == colhdr->flat_size);

Expand Down Expand Up @@ -150,23 +152,24 @@ collection_flatten_into(ExpandedObjectHeader *eohptr,
else
{
struct varlena *s = (struct varlena *) DatumGetPointer(cur->value);

value_len = (size_t) VARSIZE_ANY(s);
is_varlena = true;
}

memcpy(cresult->values + location, (char*)&key_len, sizeof(key_len));
memcpy(cresult->values + location, (char *) &key_len, sizeof(key_len));
location += sizeof(key_len);

memcpy(cresult->values + location, (char*)&value_len, sizeof(value_len));
memcpy(cresult->values + location, (char *) &value_len, sizeof(value_len));
location += sizeof(value_len);

memcpy(cresult->values + location, cur->key, key_len);
location += key_len;

if (is_varlena)
memcpy((char *)cresult->values + location, (char *)cur->value, value_len);
memcpy((char *) cresult->values + location, (char *) cur->value, value_len);
else
memcpy((char *)cresult->values + location, (char *)&cur->value, value_len);
memcpy((char *) cresult->values + location, (char *) &cur->value, value_len);

location += value_len;
}
Expand Down Expand Up @@ -210,24 +213,24 @@ construct_empty_collection(MemoryContext parentcontext)
colhdr->current = NULL;
colhdr->head = NULL;

return colhdr;
return colhdr;
}

CollectionHeader *
DatumGetExpandedCollection(Datum d)
{
CollectionHeader *colhdr;
CollectionHeader *colhdr;
FlatCollectionType *fc;
MemoryContext oldcxt;
int location = 0;
int i = 0;
MemoryContext oldcxt;
int location = 0;
int i = 0;

if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(d)))
{
colhdr = (CollectionHeader *) DatumGetEOHP(d);

Assert(colhdr->collection_magic == COLLECTION_MAGIC);

return colhdr;
}

Expand All @@ -254,30 +257,30 @@ DatumGetExpandedCollection(Datum d)
Datum *value;
collection *item;

memcpy((unsigned char *)&key_len, fc->values + location, sizeof(int16));
memcpy((unsigned char *) &key_len, fc->values + location, sizeof(int16));
location += sizeof(int16);

memcpy((unsigned char *)&value_len, fc->values + location, sizeof(size_t));
memcpy((unsigned char *) &value_len, fc->values + location, sizeof(size_t));
location += sizeof(size_t);

item = (collection *)palloc(sizeof(collection));
item = (collection *) palloc(sizeof(collection));


key = (char *)palloc(key_len + 1);
key = (char *) palloc(key_len + 1);
memcpy(key, fc->values + location, key_len);
key[key_len] = '\0';
location += key_len;

value = (Datum *)palloc(value_len);
memcpy((unsigned char *)value, fc->values + location, value_len);
value = (Datum *) palloc(value_len);
memcpy((unsigned char *) value, fc->values + location, value_len);
location += value_len;

item->key = key;

if (colhdr->value_type_len != -1)
item->value = datumCopy((Datum)*value, colhdr->value_byval, value_len);
item->value = datumCopy((Datum) *value, colhdr->value_byval, value_len);
else
item->value = datumCopy((Datum)value, colhdr->value_byval, value_len);
item->value = datumCopy((Datum) value, colhdr->value_byval, value_len);


HASH_ADD(hh, colhdr->current, key[0], strlen(key), item);
Expand All @@ -294,4 +297,3 @@ DatumGetExpandedCollection(Datum d)

return colhdr;
}

Loading

0 comments on commit c18a0bd

Please sign in to comment.