Run pgindent on code

This commit is contained in:
Matvey Arye 2017-06-21 14:00:00 -04:00 committed by Erik Nordström
parent 9489d06595
commit ce3d630b6d
22 changed files with 232 additions and 190 deletions

View File

@ -133,7 +133,7 @@ catalog_get(void)
for (i = 0; i < _MAX_CACHE_TYPES; i++) for (i = 0; i < _MAX_CACHE_TYPES; i++)
catalog.caches[i].inval_proxy_id = get_relname_relid(cache_proxy_table_names[i], catalog.caches[i].inval_proxy_id = get_relname_relid(cache_proxy_table_names[i],
catalog.cache_schema_id); catalog.cache_schema_id);
return &catalog; return &catalog;
} }

View File

@ -57,12 +57,12 @@ enum Anum_hypertable
typedef struct FormData_hypertable typedef struct FormData_hypertable
{ {
int32 id; int32 id;
NameData schema_name; NameData schema_name;
NameData table_name; NameData table_name;
NameData associated_schema_name; NameData associated_schema_name;
NameData associated_table_prefix; NameData associated_table_prefix;
int16 num_dimensions; int16 num_dimensions;
} FormData_hypertable; } FormData_hypertable;
typedef FormData_hypertable *Form_hypertable; typedef FormData_hypertable *Form_hypertable;
@ -122,17 +122,17 @@ enum Anum_dimension
typedef struct FormData_dimension typedef struct FormData_dimension
{ {
int32 id; int32 id;
int32 hypertable_id; int32 hypertable_id;
NameData column_name; NameData column_name;
Oid column_type; Oid column_type;
bool aligned; bool aligned;
/* closed (space) columns */ /* closed (space) columns */
int16 num_slices; int16 num_slices;
NameData partitioning_func_schema; NameData partitioning_func_schema;
NameData partitioning_func; NameData partitioning_func;
/* open (time) columns */ /* open (time) columns */
int64 interval_length; int64 interval_length;
} FormData_dimension; } FormData_dimension;
typedef FormData_dimension *Form_dimension; typedef FormData_dimension *Form_dimension;
@ -175,10 +175,10 @@ enum Anum_dimension_slice
typedef struct FormData_dimension_slice typedef struct FormData_dimension_slice
{ {
int32 id; int32 id;
int32 dimension_id; int32 dimension_id;
int64 range_start; int64 range_start;
int64 range_end; int64 range_end;
} FormData_dimension_slice; } FormData_dimension_slice;
typedef FormData_dimension_slice *Form_dimension_slice; typedef FormData_dimension_slice *Form_dimension_slice;
@ -200,7 +200,7 @@ enum Anum_dimension_slice_dimension_id_idx
#define Natts_dimension_slice_dimension_id_range_start_range_end_idx \ #define Natts_dimension_slice_dimension_id_range_start_range_end_idx \
(_Anum_dimension_slice_dimension_id_range_start_range_end_idx_max - 1) (_Anum_dimension_slice_dimension_id_range_start_range_end_idx_max - 1)
enum enum
{ {
DIMENSION_SLICE_ID_IDX = 0, DIMENSION_SLICE_ID_IDX = 0,
@ -230,10 +230,10 @@ enum Anum_chunk
typedef struct FormData_chunk typedef struct FormData_chunk
{ {
int32 id; int32 id;
int32 hypertable_id; int32 hypertable_id;
NameData schema_name; NameData schema_name;
NameData table_name; NameData table_name;
} FormData_chunk; } FormData_chunk;
typedef FormData_chunk *Form_chunk; typedef FormData_chunk *Form_chunk;
@ -265,8 +265,8 @@ enum Anum_chunk_constraint
typedef struct FormData_chunk_constraint typedef struct FormData_chunk_constraint
{ {
int32 chunk_id; int32 chunk_id;
int32 dimension_slice_id; int32 dimension_slice_id;
} FormData_chunk_constraint; } FormData_chunk_constraint;
typedef FormData_chunk_constraint *Form_chunk_constraint; typedef FormData_chunk_constraint *Form_chunk_constraint;

View File

@ -17,11 +17,11 @@
static void static void
chunk_fill(Chunk *chunk, HeapTuple tuple) chunk_fill(Chunk *chunk, HeapTuple tuple)
{ {
memcpy(&chunk->fd, GETSTRUCT(tuple), sizeof(FormData_chunk)); memcpy(&chunk->fd, GETSTRUCT(tuple), sizeof(FormData_chunk));
chunk->table_id = get_relname_relid(chunk->fd.table_name.data, chunk->table_id = get_relname_relid(chunk->fd.table_name.data,
get_namespace_oid(chunk->fd.schema_name.data, false)); get_namespace_oid(chunk->fd.schema_name.data, false));
} }
Chunk * Chunk *
@ -44,7 +44,7 @@ chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints)
Chunk * Chunk *
chunk_create_new(Hyperspace *hs, Point *p) chunk_create_new(Hyperspace *hs, Point *p)
{ {
Chunk *chunk; Chunk *chunk;
chunk = spi_chunk_create(hs, p); chunk = spi_chunk_create(hs, p);
Assert(chunk != NULL); Assert(chunk != NULL);
@ -53,13 +53,14 @@ chunk_create_new(Hyperspace *hs, Point *p)
} }
Chunk * Chunk *
chunk_create_stub(int32 id, int16 num_constraints) { chunk_create_stub(int32 id, int16 num_constraints)
Chunk *chunk; {
Chunk *chunk;
chunk = palloc0(CHUNK_SIZE(num_constraints)); chunk = palloc0(CHUNK_SIZE(num_constraints));
chunk->capacity = num_constraints; chunk->capacity = num_constraints;
chunk->num_constraints = 0; chunk->num_constraints = 0;
chunk->fd.id = id; chunk->fd.id = id;
return chunk; return chunk;
} }
@ -67,7 +68,8 @@ chunk_create_stub(int32 id, int16 num_constraints) {
static bool static bool
chunk_tuple_found(TupleInfo *ti, void *arg) chunk_tuple_found(TupleInfo *ti, void *arg)
{ {
Chunk *chunk = arg; Chunk *chunk = arg;
chunk_fill(chunk, ti->tuple); chunk_fill(chunk, ti->tuple);
return false; return false;
} }
@ -79,7 +81,7 @@ chunk_fill_stub(Chunk *chunk_stub, bool tuplock)
{ {
ScanKeyData scankey[1]; ScanKeyData scankey[1];
Catalog *catalog = catalog_get(); Catalog *catalog = catalog_get();
int num_found; int num_found;
ScannerCtx ctx = { ScannerCtx ctx = {
.table = catalog->tables[CHUNK].id, .table = catalog->tables[CHUNK].id,
.index = catalog->tables[CHUNK].index_ids[CHUNK_ID_INDEX], .index = catalog->tables[CHUNK].index_ids[CHUNK_ID_INDEX],
@ -99,7 +101,7 @@ chunk_fill_stub(Chunk *chunk_stub, bool tuplock)
/* /*
* Perform an index scan on chunk ID. * Perform an index scan on chunk ID.
*/ */
ScanKeyInit(&scankey[0], Anum_chunk_id, BTEqualStrategyNumber, ScanKeyInit(&scankey[0], Anum_chunk_id, BTEqualStrategyNumber,
F_INT4EQ, Int32GetDatum(chunk_stub->fd.id)); F_INT4EQ, Int32GetDatum(chunk_stub->fd.id));
num_found = scanner_scan(&ctx); num_found = scanner_scan(&ctx);
@ -164,7 +166,7 @@ chunk_scan_ctx_find_chunk(ChunkScanCtx *ctx)
entry != NULL; entry != NULL;
entry = hash_seq_search(&status)) entry = hash_seq_search(&status))
{ {
Chunk *chunk = entry->chunk; Chunk *chunk = entry->chunk;
if (chunk->num_constraints == ctx->num_dimensions) if (chunk->num_constraints == ctx->num_dimensions)
{ {
@ -182,14 +184,14 @@ chunk_scan_ctx_find_chunk(ChunkScanCtx *ctx)
* This involves: * This involves:
* *
* 1) For each dimension: * 1) For each dimension:
* - Find all dimension slices that match the dimension * - Find all dimension slices that match the dimension
* 2) For each dimension slice: * 2) For each dimension slice:
* - Find all chunk constraints matching the dimension slice * - Find all chunk constraints matching the dimension slice
* 3) For each matching chunk constraint * 3) For each matching chunk constraint
* - Insert a (stub) chunk in a hash table and add the constraint to the chunk * - Insert a (stub) chunk in a hash table and add the constraint to the chunk
* - If chunk already exists in hash table, add the constraint to the chunk * - If chunk already exists in hash table, add the constraint to the chunk
* 4) At the end of the scan, only one chunk in the hash table should have * 4) At the end of the scan, only one chunk in the hash table should have
* N number of constraints. This is the matching chunk. * N number of constraints. This is the matching chunk.
* *
* NOTE: this function allocates transient data, e.g., dimension slice, * NOTE: this function allocates transient data, e.g., dimension slice,
* constraints and chunks, that in the end are not part of the returned * constraints and chunks, that in the end are not part of the returned
@ -200,10 +202,11 @@ chunk_scan_ctx_find_chunk(ChunkScanCtx *ctx)
Chunk * Chunk *
chunk_find(Hyperspace *hs, Point *p) chunk_find(Hyperspace *hs, Point *p)
{ {
Chunk *chunk; Chunk *chunk;
ChunkScanCtx ctx; ChunkScanCtx ctx;
int16 num_dimensions = HYPERSPACE_NUM_DIMENSIONS(hs); int16 num_dimensions = HYPERSPACE_NUM_DIMENSIONS(hs);
int i, j; int i,
j;
/* The scan context will keep the state accumulated during the scan */ /* The scan context will keep the state accumulated during the scan */
chunk_scan_ctx_init(&ctx, num_dimensions); chunk_scan_ctx_init(&ctx, num_dimensions);
@ -216,8 +219,11 @@ chunk_find(Hyperspace *hs, Point *p)
vec = dimension_slice_scan(hs->dimensions[i].fd.id, p->coordinates[i]); vec = dimension_slice_scan(hs->dimensions[i].fd.id, p->coordinates[i]);
for (j = 0; j < vec->num_slices; j++) for (j = 0; j < vec->num_slices; j++)
/* For each dimension slice, find matching constraints. These will
* be saved in the scan context */ /*
* For each dimension slice, find matching constraints. These will
* be saved in the scan context
*/
chunk_constraint_scan_by_dimension_slice_id(vec->slices[j], &ctx); chunk_constraint_scan_by_dimension_slice_id(vec->slices[j], &ctx);
} }
@ -237,8 +243,8 @@ chunk_find(Hyperspace *hs, Point *p)
Chunk * Chunk *
chunk_copy(Chunk *chunk) chunk_copy(Chunk *chunk)
{ {
Chunk *copy; Chunk *copy;
size_t nbytes = CHUNK_SIZE(chunk->capacity); size_t nbytes = CHUNK_SIZE(chunk->capacity);
copy = palloc(nbytes); copy = palloc(nbytes);
memcpy(copy, chunk, nbytes); memcpy(copy, chunk, nbytes);

View File

@ -27,12 +27,12 @@ typedef struct Chunk
/* /*
* The hypercube defines the chunks position in the N-dimensional space. * The hypercube defines the chunks position in the N-dimensional space.
* Each of the N slices in the cube corresponds to a constraint on the chunk * Each of the N slices in the cube corresponds to a constraint on the
* table. * chunk table.
*/ */
Hypercube *cube; Hypercube *cube;
int16 capacity; int16 capacity;
int16 num_constraints; int16 num_constraints;
ChunkConstraint constraints[0]; ChunkConstraint constraints[0];
} Chunk; } Chunk;
@ -50,15 +50,15 @@ typedef struct Chunk
*/ */
typedef struct ChunkScanCtx typedef struct ChunkScanCtx
{ {
HTAB *htab; HTAB *htab;
int16 num_dimensions; int16 num_dimensions;
} ChunkScanCtx; } ChunkScanCtx;
/* The hash table entry for the ChunkScanCtx */ /* The hash table entry for the ChunkScanCtx */
typedef struct ChunkScanEntry typedef struct ChunkScanEntry
{ {
int32 chunk_id; int32 chunk_id;
Chunk *chunk; Chunk *chunk;
} ChunkScanEntry; } ChunkScanEntry;
extern Chunk *chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints); extern Chunk *chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints);

View File

@ -10,6 +10,7 @@ static inline ChunkConstraint *
chunk_constraint_from_form_data(Form_chunk_constraint fd) chunk_constraint_from_form_data(Form_chunk_constraint fd)
{ {
ChunkConstraint *cc; ChunkConstraint *cc;
cc = palloc0(sizeof(ChunkConstraint)); cc = palloc0(sizeof(ChunkConstraint));
memcpy(&cc->fd, fd, sizeof(FormData_chunk_constraint)); memcpy(&cc->fd, fd, sizeof(FormData_chunk_constraint));
return cc; return cc;
@ -25,12 +26,12 @@ chunk_constraint_fill(ChunkConstraint *cc, HeapTuple tuple)
static inline ChunkConstraint * static inline ChunkConstraint *
chunk_constraint_from_tuple(HeapTuple tuple) chunk_constraint_from_tuple(HeapTuple tuple)
{ {
return chunk_constraint_from_form_data((Form_chunk_constraint ) GETSTRUCT(tuple)); return chunk_constraint_from_form_data((Form_chunk_constraint) GETSTRUCT(tuple));
} }
typedef struct ChunkConstraintCtx typedef struct ChunkConstraintCtx
{ {
Chunk *chunk; Chunk *chunk;
} ChunkConstraintCtx; } ChunkConstraintCtx;
static bool static bool
@ -60,7 +61,7 @@ chunk_constraint_scan_by_chunk_id(Chunk *chunk)
ChunkConstraintCtx data = { ChunkConstraintCtx data = {
.chunk = chunk, .chunk = chunk,
}; };
int num_found; int num_found;
ScannerCtx scanCtx = { ScannerCtx scanCtx = {
.table = catalog->tables[CHUNK_CONSTRAINT].id, .table = catalog->tables[CHUNK_CONSTRAINT].id,
.index = catalog->tables[CHUNK_CONSTRAINT].index_ids[CHUNK_CONSTRAINT_CHUNK_ID_DIMENSION_SLICE_ID_IDX], .index = catalog->tables[CHUNK_CONSTRAINT].index_ids[CHUNK_CONSTRAINT_CHUNK_ID_DIMENSION_SLICE_ID_IDX],
@ -91,9 +92,9 @@ chunk_constraint_dimension_id_tuple_found(TupleInfo *ti, void *data)
{ {
ChunkScanCtx *ctx = data; ChunkScanCtx *ctx = data;
ChunkConstraint constraint; ChunkConstraint constraint;
Chunk *chunk; Chunk *chunk;
ChunkScanEntry *entry; ChunkScanEntry *entry;
bool found; bool found;
chunk_constraint_fill(&constraint, ti->tuple); chunk_constraint_fill(&constraint, ti->tuple);
@ -103,17 +104,21 @@ chunk_constraint_dimension_id_tuple_found(TupleInfo *ti, void *data)
{ {
chunk = chunk_create_stub(constraint.fd.chunk_id, ctx->num_dimensions); chunk = chunk_create_stub(constraint.fd.chunk_id, ctx->num_dimensions);
entry->chunk = chunk; entry->chunk = chunk;
} else { }
else
{
chunk = entry->chunk; chunk = entry->chunk;
} }
chunk_add_constraint(chunk, &constraint); chunk_add_constraint(chunk, &constraint);
/* If the chunk has N constraints, it is the chunk we are looking for and /*
* can abort the scan */ * If the chunk has N constraints, it is the chunk we are looking for and
* can abort the scan
*/
if (chunk->num_constraints == ctx->num_dimensions) if (chunk->num_constraints == ctx->num_dimensions)
return false; return false;
return true; return true;
} }
@ -122,7 +127,7 @@ chunk_constraint_scan_by_dimension_slice_id(DimensionSlice *slice, ChunkScanCtx
{ {
Catalog *catalog = catalog_get(); Catalog *catalog = catalog_get();
ScanKeyData scankey[1]; ScanKeyData scankey[1];
int num_found; int num_found;
ScannerCtx scanCtx = { ScannerCtx scanCtx = {
.table = catalog->tables[CHUNK_CONSTRAINT].id, .table = catalog->tables[CHUNK_CONSTRAINT].id,
.index = catalog->tables[CHUNK_CONSTRAINT].index_ids[CHUNK_CONSTRAINT_CHUNK_ID_DIMENSION_SLICE_ID_IDX], .index = catalog->tables[CHUNK_CONSTRAINT].index_ids[CHUNK_CONSTRAINT_CHUNK_ID_DIMENSION_SLICE_ID_IDX],

View File

@ -15,15 +15,15 @@ typedef struct ChunkConstraint
typedef struct ChunkConstraintVec typedef struct ChunkConstraintVec
{ {
int16 num_constraints; int16 num_constraints;
ChunkConstraint constraints[0]; ChunkConstraint constraints[0];
} ChunkConstraintVec; } ChunkConstraintVec;
typedef struct Chunk Chunk; typedef struct Chunk Chunk;
typedef struct DimensionSlice DimensionSlice; typedef struct DimensionSlice DimensionSlice;
typedef struct ChunkScanCtx ChunkScanCtx; typedef struct ChunkScanCtx ChunkScanCtx;
extern Chunk *chunk_constraint_scan_by_chunk_id(Chunk *chunk); extern Chunk *chunk_constraint_scan_by_chunk_id(Chunk *chunk);
extern int chunk_constraint_scan_by_dimension_slice_id(DimensionSlice *slice, ChunkScanCtx *ctx); extern int chunk_constraint_scan_by_dimension_slice_id(DimensionSlice *slice, ChunkScanCtx *ctx);
#endif /* TIMESCALEDB_CHUNK_CONSTRAINT_H */ #endif /* TIMESCALEDB_CHUNK_CONSTRAINT_H */

View File

@ -27,8 +27,8 @@ dimension_fill_in_from_tuple(Dimension *d, HeapTuple tuple, Oid main_table_relid
if (d->type == DIMENSION_TYPE_CLOSED) if (d->type == DIMENSION_TYPE_CLOSED)
d->partitioning = partitioning_info_create(d->fd.num_slices, d->partitioning = partitioning_info_create(d->fd.num_slices,
NameStr(d->fd.partitioning_func_schema), NameStr(d->fd.partitioning_func_schema),
NameStr(d->fd.partitioning_func), NameStr(d->fd.partitioning_func),
NameStr(d->fd.column_name), NameStr(d->fd.column_name),
main_table_relid); main_table_relid);
d->column_attno = get_attnum(main_table_relid, NameStr(d->fd.column_name)); d->column_attno = get_attnum(main_table_relid, NameStr(d->fd.column_name));
@ -38,6 +38,7 @@ static Hyperspace *
hyperspace_create(int32 hypertable_id, Oid main_table_relid, uint16 num_dimensions) hyperspace_create(int32 hypertable_id, Oid main_table_relid, uint16 num_dimensions)
{ {
Hyperspace *hs = palloc0(HYPERSPACE_SIZE(num_dimensions)); Hyperspace *hs = palloc0(HYPERSPACE_SIZE(num_dimensions));
hs->hypertable_id = hypertable_id; hs->hypertable_id = hypertable_id;
hs->main_table_relid = main_table_relid; hs->main_table_relid = main_table_relid;
hs->capacity = num_dimensions; hs->capacity = num_dimensions;
@ -50,7 +51,7 @@ dimension_tuple_found(TupleInfo *ti, void *data)
{ {
Hyperspace *hs = data; Hyperspace *hs = data;
DimensionType type = dimension_type(ti->tuple); DimensionType type = dimension_type(ti->tuple);
Dimension *d; Dimension *d;
if (type == DIMENSION_TYPE_OPEN) if (type == DIMENSION_TYPE_OPEN)
d = &hs->dimensions[hs->num_open_dimensions++]; d = &hs->dimensions[hs->num_open_dimensions++];
@ -68,7 +69,7 @@ dimension_scan(int32 hypertable_id, Oid main_table_relid, int16 num_dimensions)
Catalog *catalog = catalog_get(); Catalog *catalog = catalog_get();
Hyperspace *space = hyperspace_create(hypertable_id, main_table_relid, num_dimensions); Hyperspace *space = hyperspace_create(hypertable_id, main_table_relid, num_dimensions);
ScanKeyData scankey[1]; ScanKeyData scankey[1];
ScannerCtx scanCtx = { ScannerCtx scanCtx = {
.table = catalog->tables[DIMENSION].id, .table = catalog->tables[DIMENSION].id,
.index = catalog->tables[DIMENSION].index_ids[DIMENSION_HYPERTABLE_ID_IDX], .index = catalog->tables[DIMENSION].index_ids[DIMENSION_HYPERTABLE_ID_IDX],
.scantype = ScannerTypeIndex, .scantype = ScannerTypeIndex,
@ -82,7 +83,7 @@ dimension_scan(int32 hypertable_id, Oid main_table_relid, int16 num_dimensions)
/* Perform an index scan on hypertable_id. */ /* Perform an index scan on hypertable_id. */
ScanKeyInit(&scankey[0], Anum_dimension_hypertable_id_idx_hypertable_id, ScanKeyInit(&scankey[0], Anum_dimension_hypertable_id_idx_hypertable_id,
BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(hypertable_id)); BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(hypertable_id));
scanner_scan(&scanCtx); scanner_scan(&scanCtx);
@ -92,7 +93,8 @@ dimension_scan(int32 hypertable_id, Oid main_table_relid, int16 num_dimensions)
static Point * static Point *
point_create(int16 num_dimensions) point_create(int16 num_dimensions)
{ {
Point *p = palloc0(POINT_SIZE(num_dimensions)); Point *p = palloc0(POINT_SIZE(num_dimensions));
p->cardinality = num_dimensions; p->cardinality = num_dimensions;
p->num_closed = p->num_open = 0; p->num_closed = p->num_open = 0;
return p; return p;
@ -101,15 +103,16 @@ point_create(int16 num_dimensions)
const char * const char *
point_to_string(Point *p) point_to_string(Point *p)
{ {
char *buf = palloc(100); char *buf = palloc(100);
int i, j = 1; int i,
j = 1;
buf[0] = '('; buf[0] = '(';
for (i = 0; i < p->cardinality; i++) for (i = 0; i < p->cardinality; i++)
j += snprintf(buf + j, 100, "" INT64_FORMAT ",", p->coordinates[i]); j += snprintf(buf + j, 100, "" INT64_FORMAT ",", p->coordinates[i]);
buf[j-1] = ')'; buf[j - 1] = ')';
return buf; return buf;
} }
@ -117,17 +120,17 @@ point_to_string(Point *p)
Point * Point *
hyperspace_calculate_point(Hyperspace *hs, HeapTuple tuple, TupleDesc tupdesc) hyperspace_calculate_point(Hyperspace *hs, HeapTuple tuple, TupleDesc tupdesc)
{ {
Point *p = point_create(HYPERSPACE_NUM_DIMENSIONS(hs)); Point *p = point_create(HYPERSPACE_NUM_DIMENSIONS(hs));
int i; int i;
for (i = 0; i < HYPERSPACE_NUM_DIMENSIONS(hs); i++) for (i = 0; i < HYPERSPACE_NUM_DIMENSIONS(hs); i++)
{ {
Dimension *d = &hs->dimensions[i]; Dimension *d = &hs->dimensions[i];
if (IS_OPEN_DIMENSION(d)) if (IS_OPEN_DIMENSION(d))
{ {
Datum datum; Datum datum;
bool isnull; bool isnull;
datum = heap_getattr(tuple, d->column_attno, tupdesc, &isnull); datum = heap_getattr(tuple, d->column_attno, tupdesc, &isnull);

View File

@ -20,7 +20,7 @@ typedef struct Dimension
FormData_dimension fd; FormData_dimension fd;
DimensionType type; DimensionType type;
AttrNumber column_attno; AttrNumber column_attno;
Oid main_table_relid; Oid main_table_relid;
PartitioningInfo *partitioning; PartitioningInfo *partitioning;
} Dimension; } Dimension;
@ -36,13 +36,13 @@ typedef struct Dimension
*/ */
typedef struct Hyperspace typedef struct Hyperspace
{ {
int32 hypertable_id; int32 hypertable_id;
Oid main_table_relid; Oid main_table_relid;
uint16 capacity; uint16 capacity;
int16 num_open_dimensions; int16 num_open_dimensions;
int16 num_closed_dimensions; int16 num_closed_dimensions;
/* Open dimensions should be stored before closed dimensions */ /* Open dimensions should be stored before closed dimensions */
Dimension dimensions[0]; Dimension dimensions[0];
} Hyperspace; } Hyperspace;
#define HYPERSPACE_NUM_DIMENSIONS(hs) \ #define HYPERSPACE_NUM_DIMENSIONS(hs) \
@ -60,11 +60,11 @@ typedef struct Hyperspace
*/ */
typedef struct Point typedef struct Point
{ {
int16 cardinality; int16 cardinality;
uint8 num_open; uint8 num_open;
uint8 num_closed; uint8 num_closed;
/* Open dimension coordinates are stored before the closed coordinates */ /* Open dimension coordinates are stored before the closed coordinates */
int64 coordinates[0]; int64 coordinates[0];
} Point; } Point;
#define POINT_SIZE(cardinality) \ #define POINT_SIZE(cardinality) \
@ -84,4 +84,4 @@ extern Point *hyperspace_calculate_point(Hyperspace *h, HeapTuple tuple, TupleDe
extern const char *point_to_string(Point *p); extern const char *point_to_string(Point *p);
#endif /* TIMESCALEDB_DIMENSION_H */ #endif /* TIMESCALEDB_DIMENSION_H */

View File

@ -15,6 +15,7 @@ static inline DimensionSlice *
dimension_slice_from_form_data(Form_dimension_slice fd) dimension_slice_from_form_data(Form_dimension_slice fd)
{ {
DimensionSlice *ds; DimensionSlice *ds;
ds = palloc0(sizeof(DimensionSlice)); ds = palloc0(sizeof(DimensionSlice));
memcpy(&ds->fd, fd, sizeof(FormData_dimension_slice)); memcpy(&ds->fd, fd, sizeof(FormData_dimension_slice));
ds->storage_free = NULL; ds->storage_free = NULL;
@ -25,13 +26,14 @@ dimension_slice_from_form_data(Form_dimension_slice fd)
static inline DimensionSlice * static inline DimensionSlice *
dimension_slice_from_tuple(HeapTuple tuple) dimension_slice_from_tuple(HeapTuple tuple)
{ {
return dimension_slice_from_form_data((Form_dimension_slice ) GETSTRUCT(tuple)); return dimension_slice_from_form_data((Form_dimension_slice) GETSTRUCT(tuple));
} }
static inline Hypercube * static inline Hypercube *
hypercube_alloc(int16 num_dimensions) hypercube_alloc(int16 num_dimensions)
{ {
Hypercube *hc = palloc0(HYPERCUBE_SIZE(num_dimensions)); Hypercube *hc = palloc0(HYPERCUBE_SIZE(num_dimensions));
hc->capacity = num_dimensions; hc->capacity = num_dimensions;
return hc; return hc;
} }
@ -39,7 +41,7 @@ hypercube_alloc(int16 num_dimensions)
static inline void static inline void
hypercube_free(Hypercube *hc) hypercube_free(Hypercube *hc)
{ {
int i; int i;
for (i = 0; i < hc->num_slices; i++) for (i = 0; i < hc->num_slices; i++)
pfree(hc->slices[i]); pfree(hc->slices[i]);
@ -50,14 +52,14 @@ hypercube_free(Hypercube *hc)
Hypercube * Hypercube *
hypercube_copy(Hypercube *hc) hypercube_copy(Hypercube *hc)
{ {
Hypercube *copy; Hypercube *copy;
size_t nbytes = HYPERCUBE_SIZE(hc->capacity); size_t nbytes = HYPERCUBE_SIZE(hc->capacity);
int i; int i;
copy = palloc(nbytes); copy = palloc(nbytes);
memcpy(copy, hc, nbytes); memcpy(copy, hc, nbytes);
for(i = 0; i < hc->num_slices; i++) for (i = 0; i < hc->num_slices; i++)
copy->slices[i] = dimension_slice_copy(hc->slices[i]); copy->slices[i] = dimension_slice_copy(hc->slices[i]);
return copy; return copy;
@ -68,6 +70,7 @@ dimension_vec_tuple_found(TupleInfo *ti, void *data)
{ {
DimensionVec **vecptr = data; DimensionVec **vecptr = data;
DimensionSlice *slice = dimension_slice_from_tuple(ti->tuple); DimensionSlice *slice = dimension_slice_from_tuple(ti->tuple);
dimension_vec_add_slice(vecptr, slice); dimension_vec_add_slice(vecptr, slice);
return true; return true;
} }
@ -90,12 +93,14 @@ dimension_slice_scan(int32 dimension_id, int64 coordinate)
.scandirection = ForwardScanDirection, .scandirection = ForwardScanDirection,
}; };
/* Perform an index scan for slice matching the dimension's ID and which /*
* encloses the coordinate */ * Perform an index scan for slice matching the dimension's ID and which
* encloses the coordinate
*/
ScanKeyInit(&scankey[0], Anum_dimension_slice_dimension_id_range_start_range_end_idx_dimension_id, ScanKeyInit(&scankey[0], Anum_dimension_slice_dimension_id_range_start_range_end_idx_dimension_id,
BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_id)); BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_id));
ScanKeyInit(&scankey[1], Anum_dimension_slice_dimension_id_range_start_range_end_idx_range_start, ScanKeyInit(&scankey[1], Anum_dimension_slice_dimension_id_range_start_range_end_idx_range_start,
BTLessEqualStrategyNumber, F_INT8LE, Int64GetDatum(coordinate)); BTLessEqualStrategyNumber, F_INT8LE, Int64GetDatum(coordinate));
ScanKeyInit(&scankey[2], Anum_dimension_slice_dimension_id_range_start_range_end_idx_range_end, ScanKeyInit(&scankey[2], Anum_dimension_slice_dimension_id_range_start_range_end_idx_range_end,
BTGreaterStrategyNumber, F_INT8GT, Int64GetDatum(coordinate)); BTGreaterStrategyNumber, F_INT8GT, Int64GetDatum(coordinate));
@ -108,6 +113,7 @@ static bool
dimension_slice_tuple_found(TupleInfo *ti, void *data) dimension_slice_tuple_found(TupleInfo *ti, void *data)
{ {
DimensionSlice **slice = data; DimensionSlice **slice = data;
*slice = dimension_slice_from_tuple(ti->tuple); *slice = dimension_slice_from_tuple(ti->tuple);
return false; return false;
} }
@ -131,7 +137,7 @@ dimension_slice_scan_by_id(int32 dimension_slice_id)
}; };
ScanKeyInit(&scankey[0], Anum_dimension_slice_dimension_id_idx_dimension_id, ScanKeyInit(&scankey[0], Anum_dimension_slice_dimension_id_idx_dimension_id,
BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_slice_id)); BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_slice_id));
scanner_scan(&scanCtx); scanner_scan(&scanCtx);
return slice; return slice;
@ -141,6 +147,7 @@ DimensionSlice *
dimension_slice_copy(const DimensionSlice *original) dimension_slice_copy(const DimensionSlice *original)
{ {
DimensionSlice *new = palloc(sizeof(DimensionSlice)); DimensionSlice *new = palloc(sizeof(DimensionSlice));
memcpy(new, original, sizeof(DimensionSlice)); memcpy(new, original, sizeof(DimensionSlice));
return new; return new;
} }
@ -168,12 +175,13 @@ hypercube_slice_sort(Hypercube *hc)
Hypercube * Hypercube *
hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints) hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints)
{ {
Hypercube *hc = hypercube_alloc(num_constraints); Hypercube *hc = hypercube_alloc(num_constraints);
int i; int i;
for (i = 0; i < num_constraints; i++) for (i = 0; i < num_constraints; i++)
{ {
DimensionSlice *slice = dimension_slice_scan_by_id(constraints[i].fd.dimension_slice_id); DimensionSlice *slice = dimension_slice_scan_by_id(constraints[i].fd.dimension_slice_id);
Assert(slice != NULL); Assert(slice != NULL);
hc->slices[hc->num_slices++] = slice; hc->slices[hc->num_slices++] = slice;
} }
@ -182,7 +190,8 @@ hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints)
return hc; return hc;
} }
void dimension_slice_free(DimensionSlice *slice) void
dimension_slice_free(DimensionSlice *slice)
{ {
if (slice->storage_free != NULL) if (slice->storage_free != NULL)
slice->storage_free(slice->storage); slice->storage_free(slice->storage);
@ -215,7 +224,7 @@ cmp_slices(const void *left, const void *right)
static int static int
cmp_coordinate_and_slice(const void *left, const void *right) cmp_coordinate_and_slice(const void *left, const void *right)
{ {
int64 coord = *((int64 *) left); int64 coord = *((int64 *) left);
const DimensionSlice *slice = *((DimensionSlice **) right); const DimensionSlice *slice = *((DimensionSlice **) right);
if (coord < slice->fd.range_start) if (coord < slice->fd.range_start)
@ -247,6 +256,7 @@ DimensionVec *
dimension_vec_create(int32 initial_num_slices) dimension_vec_create(int32 initial_num_slices)
{ {
DimensionVec *vec = dimension_vec_expand(NULL, initial_num_slices); DimensionVec *vec = dimension_vec_expand(NULL, initial_num_slices);
vec->capacity = initial_num_slices; vec->capacity = initial_num_slices;
vec->num_slices = 0; vec->num_slices = 0;
return vec; return vec;
@ -269,6 +279,7 @@ DimensionVec *
dimension_vec_add_slice_sort(DimensionVec **vecptr, DimensionSlice *slice) dimension_vec_add_slice_sort(DimensionVec **vecptr, DimensionSlice *slice)
{ {
DimensionVec *vec = *vecptr; DimensionVec *vec = *vecptr;
*vecptr = vec = dimension_vec_add_slice(vecptr, slice); *vecptr = vec = dimension_vec_add_slice(vecptr, slice);
qsort(vec->slices, vec->num_slices, sizeof(DimensionSlice *), cmp_slices); qsort(vec->slices, vec->num_slices, sizeof(DimensionSlice *), cmp_slices);
return vec; return vec;
@ -277,17 +288,19 @@ dimension_vec_add_slice_sort(DimensionVec **vecptr, DimensionSlice *slice)
DimensionSlice * DimensionSlice *
dimension_vec_find_slice(DimensionVec *vec, int64 coordinate) dimension_vec_find_slice(DimensionVec *vec, int64 coordinate)
{ {
DimensionSlice **res = bsearch(&coordinate, vec->slices, vec->num_slices, DimensionSlice **res = bsearch(&coordinate, vec->slices, vec->num_slices,
sizeof(DimensionSlice *), cmp_coordinate_and_slice); sizeof(DimensionSlice *), cmp_coordinate_and_slice);
if (res == NULL)
return NULL;
return *res; if (res == NULL)
return NULL;
return *res;
} }
void dimension_vec_free(DimensionVec *vec) void
dimension_vec_free(DimensionVec *vec)
{ {
int i; int i;
for (i = 0; i < vec->num_slices; i++) for (i = 0; i < vec->num_slices; i++)
dimension_slice_free(vec->slices[i]); dimension_slice_free(vec->slices[i]);

View File

@ -12,8 +12,9 @@ typedef struct DimensionSlice
{ {
FormData_dimension_slice fd; FormData_dimension_slice fd;
DimensionType type; DimensionType type;
void (*storage_free)(void *); void (*storage_free) (void *);
void *storage; //used in the cache void *storage;
//used in the cache
} DimensionSlice; } DimensionSlice;
/* /*
@ -22,8 +23,9 @@ typedef struct DimensionSlice
*/ */
typedef struct Hypercube typedef struct Hypercube
{ {
int16 capacity; /* capacity of slices[] */ int16 capacity; /* capacity of slices[] */
int16 num_slices; /* actual number of slices (should equal num_dimensions after create) */ int16 num_slices; /* actual number of slices (should equal
* num_dimensions after create) */
/* Open slices are stored before closed slices */ /* Open slices are stored before closed slices */
DimensionSlice *slices[0]; DimensionSlice *slices[0];
} Hypercube; } Hypercube;
@ -32,13 +34,14 @@ typedef struct Hypercube
(sizeof(Hypercube) + sizeof(DimensionSlice *) * (num_dimensions)) (sizeof(Hypercube) + sizeof(DimensionSlice *) * (num_dimensions))
/* /*
* DimensionVec is a collection of slices (ranges) along one dimension for a * DimensionVec is a collection of slices (ranges) along one dimension for a
* time range. * time range.
*/ */
typedef struct DimensionVec typedef struct DimensionVec
{ {
int32 capacity; /* The capacity of the slices array */ int32 capacity; /* The capacity of the slices array */
int32 num_slices; /* The current number of slices in slices array */ int32 num_slices; /* The current number of slices in slices
* array */
DimensionSlice *slices[0]; DimensionSlice *slices[0];
} DimensionVec; } DimensionVec;
@ -59,4 +62,4 @@ extern void dimension_vec_free(DimensionVec *vec);
extern Hypercube *hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints); extern Hypercube *hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints);
extern Hypercube *hypercube_copy(Hypercube *hc); extern Hypercube *hypercube_copy(Hypercube *hc);
#endif /* TIMESCALEDB_DIMENSION_SLICE_H */ #endif /* TIMESCALEDB_DIMENSION_SLICE_H */

View File

@ -14,7 +14,7 @@ Hypertable *
hypertable_from_tuple(HeapTuple tuple) hypertable_from_tuple(HeapTuple tuple)
{ {
Hypertable *h; Hypertable *h;
Oid namespace_oid; Oid namespace_oid;
h = palloc0(sizeof(Hypertable)); h = palloc0(sizeof(Hypertable));
memcpy(&h->fd, GETSTRUCT(tuple), sizeof(FormData_hypertable)); memcpy(&h->fd, GETSTRUCT(tuple), sizeof(FormData_hypertable));
@ -26,28 +26,29 @@ hypertable_from_tuple(HeapTuple tuple)
return h; return h;
} }
Chunk *hypertable_get_chunk(Hypertable *h, Point *point) Chunk *
hypertable_get_chunk(Hypertable *h, Point *point)
{ {
Chunk *chunk = subspace_store_get(h->chunk_cache, point); Chunk *chunk = subspace_store_get(h->chunk_cache, point);
if (NULL == chunk) if (NULL == chunk)
{ {
MemoryContext old; MemoryContext old;
/* /*
chunk_find() must execute on the transaction memory context since it * chunk_find() must execute on the transaction memory context since
allocates a lot of transient data. * it allocates a lot of transient data.
*/ */
chunk = chunk_find(h->space, point); chunk = chunk_find(h->space, point);
old = MemoryContextSwitchTo(subspace_store_mcxt(h->chunk_cache)); old = MemoryContextSwitchTo(subspace_store_mcxt(h->chunk_cache));
if (NULL == chunk) if (NULL == chunk)
chunk = chunk_create_new(h->space, point); chunk = chunk_create_new(h->space, point);
else else
/* Make a copy which lives in the chunk cache's memory context */ /* Make a copy which lives in the chunk cache's memory context */
chunk = chunk_copy(chunk); chunk = chunk_copy(chunk);
Assert(NULL != chunk); Assert(NULL != chunk);
subspace_store_add(h->chunk_cache, chunk->cube, chunk, pfree); subspace_store_add(h->chunk_cache, chunk->cube, chunk, pfree);
MemoryContextSwitchTo(old); MemoryContextSwitchTo(old);

View File

@ -14,7 +14,7 @@ typedef struct HeapTupleData *HeapTuple;
typedef struct Hypertable typedef struct Hypertable
{ {
FormData_hypertable fd; FormData_hypertable fd;
Oid main_table_relid; Oid main_table_relid;
Hyperspace *space; Hyperspace *space;
SubspaceStore *chunk_cache; SubspaceStore *chunk_cache;
} Hypertable; } Hypertable;
@ -22,4 +22,4 @@ typedef struct Hypertable
extern Hypertable *hypertable_from_tuple(HeapTuple tuple); extern Hypertable *hypertable_from_tuple(HeapTuple tuple);
extern Chunk *hypertable_get_chunk(Hypertable *h, Point *point); extern Chunk *hypertable_get_chunk(Hypertable *h, Point *point);
#endif /* TIMESCALEDB_HYPERTABLE_H */ #endif /* TIMESCALEDB_HYPERTABLE_H */

View File

@ -61,7 +61,7 @@ hypertable_cache_create()
.get_key = hypertable_cache_get_key, .get_key = hypertable_cache_get_key,
.create_entry = hypertable_cache_create_entry, .create_entry = hypertable_cache_create_entry,
}; };
*cache = template; *cache = template;
cache_init(cache); cache_init(cache);
@ -78,6 +78,7 @@ static bool
hypertable_tuple_found(TupleInfo *ti, void *data) hypertable_tuple_found(TupleInfo *ti, void *data)
{ {
HypertableNameCacheEntry *entry = data; HypertableNameCacheEntry *entry = data;
entry->hypertable = hypertable_from_tuple(ti->tuple); entry->hypertable = hypertable_from_tuple(ti->tuple);
return false; return false;
} }

View File

@ -31,8 +31,8 @@ insert_statement_state_cleanup(InsertStatementState **state_p)
} }
Datum insert_main_table_trigger(PG_FUNCTION_ARGS); Datum insert_main_table_trigger(PG_FUNCTION_ARGS);
Datum insert_main_table_trigger_after(PG_FUNCTION_ARGS); Datum insert_main_table_trigger_after(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(insert_main_table_trigger); PG_FUNCTION_INFO_V1(insert_main_table_trigger);
PG_FUNCTION_INFO_V1(insert_main_table_trigger_after); PG_FUNCTION_INFO_V1(insert_main_table_trigger_after);
@ -47,12 +47,12 @@ Datum
insert_main_table_trigger(PG_FUNCTION_ARGS) insert_main_table_trigger(PG_FUNCTION_ARGS)
{ {
TriggerData *trigdata = (TriggerData *) fcinfo->context; TriggerData *trigdata = (TriggerData *) fcinfo->context;
HeapTuple tuple; HeapTuple tuple;
Hypertable *ht; Hypertable *ht;
Point *point; Point *point;
InsertChunkState *cstate; InsertChunkState *cstate;
Oid relid = trigdata->tg_relation->rd_id; Oid relid = trigdata->tg_relation->rd_id;
TupleDesc tupdesc = trigdata->tg_relation->rd_att; TupleDesc tupdesc = trigdata->tg_relation->rd_att;
PG_TRY(); PG_TRY();
{ {
@ -79,7 +79,7 @@ insert_main_table_trigger(PG_FUNCTION_ARGS)
/* Find or create the insert state matching the point */ /* Find or create the insert state matching the point */
cstate = insert_statement_state_get_insert_chunk_state(insert_statement_state, cstate = insert_statement_state_get_insert_chunk_state(insert_statement_state,
ht->space, point); ht->space, point);
insert_chunk_state_insert_tuple(cstate, tuple); insert_chunk_state_insert_tuple(cstate, tuple);
} }
PG_CATCH(); PG_CATCH();

View File

@ -155,6 +155,7 @@ insert_chunk_state_destroy(InsertChunkState *state)
foreach(lc, state->replica_states) foreach(lc, state->replica_states)
{ {
InsertChunkStateRel *rel_state = lfirst(lc); InsertChunkStateRel *rel_state = lfirst(lc);
insert_chunk_state_rel_destroy(rel_state); insert_chunk_state_rel_destroy(rel_state);
} }
} }
@ -167,6 +168,7 @@ insert_chunk_state_insert_tuple(InsertChunkState *state, HeapTuple tup)
foreach(lc, state->replica_states) foreach(lc, state->replica_states)
{ {
InsertChunkStateRel *rel_state = lfirst(lc); InsertChunkStateRel *rel_state = lfirst(lc);
insert_chunk_state_rel_insert_tuple(rel_state, tup); insert_chunk_state_rel_insert_tuple(rel_state, tup);
} }
} }

View File

@ -20,7 +20,7 @@ insert_statement_state_new(Oid relid)
ALLOCSET_DEFAULT_SIZES); ALLOCSET_DEFAULT_SIZES);
InsertStatementState *state; InsertStatementState *state;
Hypertable *ht; Hypertable *ht;
Cache *hypertable_cache; Cache *hypertable_cache;
oldctx = MemoryContextSwitchTo(mctx); oldctx = MemoryContextSwitchTo(mctx);
@ -50,9 +50,11 @@ insert_statement_state_destroy(InsertStatementState *state)
MemoryContextDelete(state->mctx); MemoryContextDelete(state->mctx);
} }
static void destroy_insert_chunk_state(void *ics_ptr) static void
destroy_insert_chunk_state(void *ics_ptr)
{ {
InsertChunkState *ics = ics_ptr; InsertChunkState *ics = ics_ptr;
insert_chunk_state_destroy(ics); insert_chunk_state_destroy(ics);
} }
@ -69,7 +71,7 @@ insert_statement_state_get_insert_chunk_state(InsertStatementState *state, Hyper
if (NULL == ics) if (NULL == ics)
{ {
Chunk *new_chunk; Chunk *new_chunk;
MemoryContext old; MemoryContext old;
new_chunk = hypertable_get_chunk(state->hypertable, point); new_chunk = hypertable_get_chunk(state->hypertable, point);

View File

@ -66,13 +66,14 @@ DEFINE_PLAN(create_chunk_plan, CHUNK_CREATE, 2, CHUNK_CREATE_ARGS)
static HeapTuple static HeapTuple
chunk_tuple_create_spi_connected(Hyperspace *hs, Point *p, SPIPlanPtr plan) chunk_tuple_create_spi_connected(Hyperspace *hs, Point *p, SPIPlanPtr plan)
{ {
int i, ret; int i,
ret;
HeapTuple tuple; HeapTuple tuple;
Datum dimension_ids[HYPERSPACE_NUM_DIMENSIONS(hs)]; Datum dimension_ids[HYPERSPACE_NUM_DIMENSIONS(hs)];
Datum dimension_values[HYPERSPACE_NUM_DIMENSIONS(hs)]; Datum dimension_values[HYPERSPACE_NUM_DIMENSIONS(hs)];
Datum args[2]; Datum args[2];
for (i = 0; i < HYPERSPACE_NUM_DIMENSIONS(hs);i++) for (i = 0; i < HYPERSPACE_NUM_DIMENSIONS(hs); i++)
{ {
dimension_ids[i] = Int32GetDatum(hs->dimensions[i].fd.id); dimension_ids[i] = Int32GetDatum(hs->dimensions[i].fd.id);
dimension_values[i] = Int64GetDatum(p->coordinates[i]); dimension_values[i] = Int64GetDatum(p->coordinates[i]);
@ -99,7 +100,8 @@ spi_chunk_create(Hyperspace *hs, Point *p)
{ {
HeapTuple tuple; HeapTuple tuple;
Chunk *chunk; Chunk *chunk;
MemoryContext old, top = CurrentMemoryContext; MemoryContext old,
top = CurrentMemoryContext;
SPIPlanPtr plan = create_chunk_plan(); SPIPlanPtr plan = create_chunk_plan();
if (SPI_connect() < 0) if (SPI_connect() < 0)
@ -108,7 +110,7 @@ spi_chunk_create(Hyperspace *hs, Point *p)
tuple = chunk_tuple_create_spi_connected(hs, p, plan); tuple = chunk_tuple_create_spi_connected(hs, p, plan);
old = MemoryContextSwitchTo(top); old = MemoryContextSwitchTo(top);
chunk = chunk_create_from_tuple(tuple, HYPERSPACE_NUM_DIMENSIONS(hs)); chunk = chunk_create_from_tuple(tuple, HYPERSPACE_NUM_DIMENSIONS(hs));
MemoryContextSwitchTo(old); MemoryContextSwitchTo(old);
SPI_finish(); SPI_finish();

View File

@ -80,7 +80,7 @@ partitioning_func_apply(PartitioningInfo *pinfo, Datum value)
Datum text = FunctionCall1(&pinfo->partfunc.textfunc_fmgr, value); Datum text = FunctionCall1(&pinfo->partfunc.textfunc_fmgr, value);
char *partition_val = DatumGetCString(text); char *partition_val = DatumGetCString(text);
Datum keyspace_datum = FunctionCall1(&pinfo->partfunc.func_fmgr, Datum keyspace_datum = FunctionCall1(&pinfo->partfunc.func_fmgr,
CStringGetTextDatum(partition_val)); CStringGetTextDatum(partition_val));
return DatumGetInt32(keyspace_datum); return DatumGetInt32(keyspace_datum);
} }
@ -117,7 +117,7 @@ get_partition_for_key(PG_FUNCTION_ARGS)
hash_u = hash_any((unsigned char *) VARDATA_ANY(data), hash_u = hash_any((unsigned char *) VARDATA_ANY(data),
VARSIZE_ANY_EXHDR(data)); VARSIZE_ANY_EXHDR(data));
res = (int32) (hash_u & 0x7fffffff); /* Only positive numbers */ res = (int32) (hash_u & 0x7fffffff); /* Only positive numbers */
PG_FREE_IF_COPY(data, 0); PG_FREE_IF_COPY(data, 0);
PG_RETURN_INT32(res); PG_RETURN_INT32(res);

View File

@ -39,12 +39,12 @@ typedef struct PartitioningInfo
extern PartitioningInfo *partitioning_info_create(int num_partitions, extern PartitioningInfo *partitioning_info_create(int num_partitions,
const char *schema, const char *schema,
const char *partfunc, const char *partfunc,
const char *partcol, const char *partcol,
Oid relid); Oid relid);
extern int32 partitioning_func_apply(PartitioningInfo *pinfo, Datum value); extern int32 partitioning_func_apply(PartitioningInfo *pinfo, Datum value);
extern int32 partitioning_func_apply_tuple(PartitioningInfo *pinfo, HeapTuple tuple, TupleDesc desc); extern int32 partitioning_func_apply_tuple(PartitioningInfo *pinfo, HeapTuple tuple, TupleDesc desc);
#endif /* TIMESCALEDB_PARTITIONING_H */ #endif /* TIMESCALEDB_PARTITIONING_H */

View File

@ -121,10 +121,10 @@ get_partitioning_info_for_partition_column_var(Var *var_expr, Query *parse, Cach
if (rte->relid == hentry->main_table_relid) if (rte->relid == hentry->main_table_relid)
{ {
Dimension *closed_dim = hyperspace_get_closed_dimension(hentry->space, 0); Dimension *closed_dim = hyperspace_get_closed_dimension(hentry->space, 0);
if (closed_dim != NULL && if (closed_dim != NULL &&
strncmp(closed_dim->fd.column_name.data, varname, NAMEDATALEN) == 0) strncmp(closed_dim->fd.column_name.data, varname, NAMEDATALEN) == 0)
return closed_dim->partitioning; return closed_dim->partitioning;
} }
return NULL; return NULL;
@ -135,7 +135,7 @@ get_partitioning_info_for_partition_column_var(Var *var_expr, Query *parse, Cach
* all nodes given in input. */ * all nodes given in input. */
static Expr * static Expr *
create_partition_func_equals_const(Var *var_expr, Const *const_expr, char *partitioning_func_schema, create_partition_func_equals_const(Var *var_expr, Const *const_expr, char *partitioning_func_schema,
char *partitioning_func) char *partitioning_func)
{ {
Expr *op_expr; Expr *op_expr;
List *func_name = list_make2(makeString(partitioning_func_schema), makeString(partitioning_func)); List *func_name = list_make2(makeString(partitioning_func_schema), makeString(partitioning_func));
@ -200,8 +200,7 @@ add_partitioning_func_qual_mutator(Node *node, AddPartFuncQualCtx *context)
/* /*
* Detect partitioning_column = const. If not fall-thru. If detected, * Detect partitioning_column = const. If not fall-thru. If detected,
* replace with partitioning_column = const AND * replace with partitioning_column = const AND
* partitioning_func(partition_column) = * partitioning_func(partition_column) = partitioning_func(const)
* partitioning_func(const)
*/ */
if (IsA(node, OpExpr)) if (IsA(node, OpExpr))
{ {
@ -247,15 +246,15 @@ add_partitioning_func_qual_mutator(Node *node, AddPartFuncQualCtx *context)
*/ */
PartitioningInfo *pi = PartitioningInfo *pi =
get_partitioning_info_for_partition_column_var(var_expr, get_partitioning_info_for_partition_column_var(var_expr,
context->parse, context->parse,
context->hcache, context->hcache,
context->hentry); context->hentry);
if (pi != NULL) if (pi != NULL)
{ {
/* The var is a partitioning column */ /* The var is a partitioning column */
Expr *partitioning_clause = create_partition_func_equals_const(var_expr, const_expr, Expr *partitioning_clause = create_partition_func_equals_const(var_expr, const_expr,
pi->partfunc.schema, pi->partfunc.name); pi->partfunc.schema, pi->partfunc.name);
return (Node *) make_andclause(list_make2(node, partitioning_clause)); return (Node *) make_andclause(list_make2(node, partitioning_clause));

View File

@ -4,10 +4,11 @@
#include "dimension_slice.h" #include "dimension_slice.h"
#include "subspace_store.h" #include "subspace_store.h"
typedef struct SubspaceStore { typedef struct SubspaceStore
{
MemoryContext mcxt; MemoryContext mcxt;
int16 num_dimensions; int16 num_dimensions;
DimensionVec *origin; /* origin of the tree */ DimensionVec *origin; /* origin of the tree */
} SubspaceStore; } SubspaceStore;
static inline DimensionVec * static inline DimensionVec *
@ -21,6 +22,7 @@ subspace_store_init(int16 num_dimensions, MemoryContext mcxt)
{ {
MemoryContext old = MemoryContextSwitchTo(mcxt); MemoryContext old = MemoryContextSwitchTo(mcxt);
SubspaceStore *sst = palloc(sizeof(SubspaceStore)); SubspaceStore *sst = palloc(sizeof(SubspaceStore));
sst->origin = subspace_store_dimension_create(); sst->origin = subspace_store_dimension_create();
sst->num_dimensions = num_dimensions; sst->num_dimensions = num_dimensions;
sst->mcxt = mcxt; sst->mcxt = mcxt;
@ -29,18 +31,19 @@ subspace_store_init(int16 num_dimensions, MemoryContext mcxt)
} }
static inline void static inline void
subspace_store_free_internal_node(void * node) subspace_store_free_internal_node(void *node)
{ {
dimension_vec_free((DimensionVec *)node); dimension_vec_free((DimensionVec *) node);
} }
void subspace_store_add(SubspaceStore *cache, const Hypercube *hc, void
void *end_store, void (*end_store_free)(void *)) subspace_store_add(SubspaceStore *cache, const Hypercube *hc,
void *end_store, void (*end_store_free) (void *))
{ {
DimensionVec **vecptr = &cache->origin; DimensionVec **vecptr = &cache->origin;
DimensionSlice *last = NULL; DimensionSlice *last = NULL;
MemoryContext old = MemoryContextSwitchTo(cache->mcxt); MemoryContext old = MemoryContextSwitchTo(cache->mcxt);
int i; int i;
Assert(hc->num_slices == cache->num_dimensions); Assert(hc->num_slices == cache->num_dimensions);
@ -69,17 +72,18 @@ void subspace_store_add(SubspaceStore *cache, const Hypercube *hc,
if (match == NULL) if (match == NULL)
{ {
DimensionSlice *copy = dimension_slice_copy(target); DimensionSlice *copy = dimension_slice_copy(target);
dimension_vec_add_slice_sort(vecptr, copy); dimension_vec_add_slice_sort(vecptr, copy);
match = copy; match = copy;
} }
last = match; last = match;
/* internal nodes point to the next dimension's vector */ /* internal nodes point to the next dimension's vector */
vecptr = (DimensionVec **)&last->storage; vecptr = (DimensionVec **) &last->storage;
} }
Assert(last->storage == NULL); Assert(last->storage == NULL);
last->storage = end_store; /* at the end we store the object */ last->storage = end_store; /* at the end we store the object */
last->storage_free = end_store_free; last->storage_free = end_store_free;
MemoryContextSwitchTo(old); MemoryContextSwitchTo(old);
} }
@ -87,7 +91,7 @@ void subspace_store_add(SubspaceStore *cache, const Hypercube *hc,
void * void *
subspace_store_get(SubspaceStore *cache, Point *target) subspace_store_get(SubspaceStore *cache, Point *target)
{ {
int i; int i;
DimensionVec *vec = cache->origin; DimensionVec *vec = cache->origin;
DimensionSlice *match = NULL; DimensionSlice *match = NULL;
@ -112,7 +116,8 @@ subspace_store_free(SubspaceStore *cache)
pfree(cache); pfree(cache);
} }
MemoryContext subspace_store_mcxt(SubspaceStore *cache) MemoryContext
subspace_store_mcxt(SubspaceStore *cache)
{ {
return cache->mcxt; return cache->mcxt;
} }

View File

@ -10,7 +10,7 @@ typedef struct SubspaceStore SubspaceStore;
extern SubspaceStore *subspace_store_init(int16 num_dimensions, MemoryContext mcxt); extern SubspaceStore *subspace_store_init(int16 num_dimensions, MemoryContext mcxt);
extern void subspace_store_add(SubspaceStore *cache, const Hypercube *hc, extern void subspace_store_add(SubspaceStore *cache, const Hypercube *hc,
void *end_store, void (*end_store_free)(void *)); void *end_store, void (*end_store_free) (void *));
extern void *subspace_store_get(SubspaceStore *cache, Point *target); extern void *subspace_store_get(SubspaceStore *cache, Point *target);
@ -18,4 +18,4 @@ extern void subspace_store_free(SubspaceStore *cache);
extern MemoryContext subspace_store_mcxt(SubspaceStore *cache); extern MemoryContext subspace_store_mcxt(SubspaceStore *cache);
#endif /* TIMESCALEDB_SUBSPACE_STORE_H */ #endif /* TIMESCALEDB_SUBSPACE_STORE_H */