From ce3d630b6d5138790d2b31e28c2ec983d275e8e7 Mon Sep 17 00:00:00 2001 From: Matvey Arye Date: Wed, 21 Jun 2017 14:00:00 -0400 Subject: [PATCH] Run pgindent on code --- src/catalog.c | 2 +- src/catalog.h | 56 +++++++++++++++++------------------ src/chunk.c | 50 +++++++++++++++++-------------- src/chunk.h | 18 ++++++------ src/chunk_constraint.c | 25 +++++++++------- src/chunk_constraint.h | 8 ++--- src/dimension.c | 31 +++++++++++--------- src/dimension.h | 24 +++++++-------- src/dimension_slice.c | 57 ++++++++++++++++++++++-------------- src/dimension_slice.h | 21 +++++++------ src/hypertable.c | 17 ++++++----- src/hypertable.h | 4 +-- src/hypertable_cache.c | 3 +- src/insert.c | 14 ++++----- src/insert_chunk_state.c | 2 ++ src/insert_statement_state.c | 8 +++-- src/metadata_queries.c | 10 ++++--- src/partitioning.c | 4 +-- src/partitioning.h | 14 ++++----- src/planner.c | 21 +++++++------ src/subspace_store.c | 29 ++++++++++-------- src/subspace_store.h | 4 +-- 22 files changed, 232 insertions(+), 190 deletions(-) diff --git a/src/catalog.c b/src/catalog.c index 98f2e0d06..59fd7cb27 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -133,7 +133,7 @@ catalog_get(void) for (i = 0; i < _MAX_CACHE_TYPES; 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; } diff --git a/src/catalog.h b/src/catalog.h index 1a32f7f28..6be692a49 100644 --- a/src/catalog.h +++ b/src/catalog.h @@ -57,12 +57,12 @@ enum Anum_hypertable typedef struct FormData_hypertable { - int32 id; - NameData schema_name; - NameData table_name; - NameData associated_schema_name; - NameData associated_table_prefix; - int16 num_dimensions; + int32 id; + NameData schema_name; + NameData table_name; + NameData associated_schema_name; + NameData associated_table_prefix; + int16 num_dimensions; } FormData_hypertable; typedef FormData_hypertable *Form_hypertable; @@ -122,17 +122,17 @@ enum Anum_dimension typedef struct FormData_dimension { - int32 id; - int32 hypertable_id; - NameData column_name; - Oid column_type; - bool aligned; - /* closed (space) columns */ - int16 num_slices; - NameData partitioning_func_schema; - NameData partitioning_func; - /* open (time) columns */ - int64 interval_length; + int32 id; + int32 hypertable_id; + NameData column_name; + Oid column_type; + bool aligned; + /* closed (space) columns */ + int16 num_slices; + NameData partitioning_func_schema; + NameData partitioning_func; + /* open (time) columns */ + int64 interval_length; } FormData_dimension; typedef FormData_dimension *Form_dimension; @@ -175,10 +175,10 @@ enum Anum_dimension_slice typedef struct FormData_dimension_slice { - int32 id; - int32 dimension_id; - int64 range_start; - int64 range_end; + int32 id; + int32 dimension_id; + int64 range_start; + int64 range_end; } FormData_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 \ (_Anum_dimension_slice_dimension_id_range_start_range_end_idx_max - 1) - + enum { DIMENSION_SLICE_ID_IDX = 0, @@ -230,10 +230,10 @@ enum Anum_chunk typedef struct FormData_chunk { - int32 id; - int32 hypertable_id; - NameData schema_name; - NameData table_name; + int32 id; + int32 hypertable_id; + NameData schema_name; + NameData table_name; } FormData_chunk; typedef FormData_chunk *Form_chunk; @@ -265,8 +265,8 @@ enum Anum_chunk_constraint typedef struct FormData_chunk_constraint { - int32 chunk_id; - int32 dimension_slice_id; + int32 chunk_id; + int32 dimension_slice_id; } FormData_chunk_constraint; typedef FormData_chunk_constraint *Form_chunk_constraint; diff --git a/src/chunk.c b/src/chunk.c index 11e42aa25..5aad16da6 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -17,11 +17,11 @@ static void -chunk_fill(Chunk *chunk, HeapTuple tuple) +chunk_fill(Chunk *chunk, HeapTuple tuple) { memcpy(&chunk->fd, GETSTRUCT(tuple), sizeof(FormData_chunk)); 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 * @@ -44,7 +44,7 @@ chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints) Chunk * chunk_create_new(Hyperspace *hs, Point *p) { - Chunk *chunk; + Chunk *chunk; chunk = spi_chunk_create(hs, p); Assert(chunk != NULL); @@ -53,13 +53,14 @@ chunk_create_new(Hyperspace *hs, Point *p) } Chunk * -chunk_create_stub(int32 id, int16 num_constraints) { - Chunk *chunk; +chunk_create_stub(int32 id, int16 num_constraints) +{ + Chunk *chunk; chunk = palloc0(CHUNK_SIZE(num_constraints)); chunk->capacity = num_constraints; chunk->num_constraints = 0; - + chunk->fd.id = id; return chunk; } @@ -67,7 +68,8 @@ chunk_create_stub(int32 id, int16 num_constraints) { static bool chunk_tuple_found(TupleInfo *ti, void *arg) { - Chunk *chunk = arg; + Chunk *chunk = arg; + chunk_fill(chunk, ti->tuple); return false; } @@ -79,7 +81,7 @@ chunk_fill_stub(Chunk *chunk_stub, bool tuplock) { ScanKeyData scankey[1]; Catalog *catalog = catalog_get(); - int num_found; + int num_found; ScannerCtx ctx = { .table = catalog->tables[CHUNK].id, .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. */ - ScanKeyInit(&scankey[0], Anum_chunk_id, BTEqualStrategyNumber, + ScanKeyInit(&scankey[0], Anum_chunk_id, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(chunk_stub->fd.id)); num_found = scanner_scan(&ctx); @@ -164,7 +166,7 @@ chunk_scan_ctx_find_chunk(ChunkScanCtx *ctx) entry != NULL; entry = hash_seq_search(&status)) { - Chunk *chunk = entry->chunk; + Chunk *chunk = entry->chunk; if (chunk->num_constraints == ctx->num_dimensions) { @@ -182,14 +184,14 @@ chunk_scan_ctx_find_chunk(ChunkScanCtx *ctx) * This involves: * * 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: - * - Find all chunk constraints matching the dimension slice + * - Find all chunk constraints matching the dimension slice * 3) For each matching chunk constraint - * - 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 + * - 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 * 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, * 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_find(Hyperspace *hs, Point *p) { - Chunk *chunk; + Chunk *chunk; ChunkScanCtx ctx; - int16 num_dimensions = HYPERSPACE_NUM_DIMENSIONS(hs); - int i, j; + int16 num_dimensions = HYPERSPACE_NUM_DIMENSIONS(hs); + int i, + j; /* The scan context will keep the state accumulated during the scan */ 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]); 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); } @@ -237,8 +243,8 @@ chunk_find(Hyperspace *hs, Point *p) Chunk * chunk_copy(Chunk *chunk) { - Chunk *copy; - size_t nbytes = CHUNK_SIZE(chunk->capacity); + Chunk *copy; + size_t nbytes = CHUNK_SIZE(chunk->capacity); copy = palloc(nbytes); memcpy(copy, chunk, nbytes); diff --git a/src/chunk.h b/src/chunk.h index 6d3b9e561..0f1f488cf 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -27,12 +27,12 @@ typedef struct Chunk /* * 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 - * table. + * Each of the N slices in the cube corresponds to a constraint on the + * chunk table. */ - Hypercube *cube; - int16 capacity; - int16 num_constraints; + Hypercube *cube; + int16 capacity; + int16 num_constraints; ChunkConstraint constraints[0]; } Chunk; @@ -50,15 +50,15 @@ typedef struct Chunk */ typedef struct ChunkScanCtx { - HTAB *htab; - int16 num_dimensions; + HTAB *htab; + int16 num_dimensions; } ChunkScanCtx; /* The hash table entry for the ChunkScanCtx */ typedef struct ChunkScanEntry { - int32 chunk_id; - Chunk *chunk; + int32 chunk_id; + Chunk *chunk; } ChunkScanEntry; extern Chunk *chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints); diff --git a/src/chunk_constraint.c b/src/chunk_constraint.c index 9b372db34..4e4ee0dc7 100644 --- a/src/chunk_constraint.c +++ b/src/chunk_constraint.c @@ -10,6 +10,7 @@ static inline ChunkConstraint * chunk_constraint_from_form_data(Form_chunk_constraint fd) { ChunkConstraint *cc; + cc = palloc0(sizeof(ChunkConstraint)); memcpy(&cc->fd, fd, sizeof(FormData_chunk_constraint)); return cc; @@ -25,12 +26,12 @@ chunk_constraint_fill(ChunkConstraint *cc, HeapTuple tuple) static inline ChunkConstraint * 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 { - Chunk *chunk; + Chunk *chunk; } ChunkConstraintCtx; static bool @@ -60,7 +61,7 @@ chunk_constraint_scan_by_chunk_id(Chunk *chunk) ChunkConstraintCtx data = { .chunk = chunk, }; - int num_found; + int num_found; ScannerCtx scanCtx = { .table = catalog->tables[CHUNK_CONSTRAINT].id, .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; ChunkConstraint constraint; - Chunk *chunk; + Chunk *chunk; ChunkScanEntry *entry; - bool found; + bool found; 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); entry->chunk = chunk; - } else { + } + else + { chunk = entry->chunk; } 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) return false; - + return true; } @@ -122,7 +127,7 @@ chunk_constraint_scan_by_dimension_slice_id(DimensionSlice *slice, ChunkScanCtx { Catalog *catalog = catalog_get(); ScanKeyData scankey[1]; - int num_found; + int num_found; ScannerCtx scanCtx = { .table = catalog->tables[CHUNK_CONSTRAINT].id, .index = catalog->tables[CHUNK_CONSTRAINT].index_ids[CHUNK_CONSTRAINT_CHUNK_ID_DIMENSION_SLICE_ID_IDX], diff --git a/src/chunk_constraint.h b/src/chunk_constraint.h index fa9b48f40..2cb52cb8f 100644 --- a/src/chunk_constraint.h +++ b/src/chunk_constraint.h @@ -15,15 +15,15 @@ typedef struct ChunkConstraint typedef struct ChunkConstraintVec { - int16 num_constraints; + int16 num_constraints; ChunkConstraint constraints[0]; -} ChunkConstraintVec; +} ChunkConstraintVec; typedef struct Chunk Chunk; typedef struct DimensionSlice DimensionSlice; typedef struct ChunkScanCtx ChunkScanCtx; 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 */ diff --git a/src/dimension.c b/src/dimension.c index 78d4c3015..b03eb7b71 100644 --- a/src/dimension.c +++ b/src/dimension.c @@ -27,8 +27,8 @@ dimension_fill_in_from_tuple(Dimension *d, HeapTuple tuple, Oid main_table_relid if (d->type == DIMENSION_TYPE_CLOSED) d->partitioning = partitioning_info_create(d->fd.num_slices, - NameStr(d->fd.partitioning_func_schema), - NameStr(d->fd.partitioning_func), + NameStr(d->fd.partitioning_func_schema), + NameStr(d->fd.partitioning_func), NameStr(d->fd.column_name), main_table_relid); 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 *hs = palloc0(HYPERSPACE_SIZE(num_dimensions)); + hs->hypertable_id = hypertable_id; hs->main_table_relid = main_table_relid; hs->capacity = num_dimensions; @@ -50,7 +51,7 @@ dimension_tuple_found(TupleInfo *ti, void *data) { Hyperspace *hs = data; DimensionType type = dimension_type(ti->tuple); - Dimension *d; + Dimension *d; if (type == DIMENSION_TYPE_OPEN) 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(); Hyperspace *space = hyperspace_create(hypertable_id, main_table_relid, num_dimensions); ScanKeyData scankey[1]; - ScannerCtx scanCtx = { + ScannerCtx scanCtx = { .table = catalog->tables[DIMENSION].id, .index = catalog->tables[DIMENSION].index_ids[DIMENSION_HYPERTABLE_ID_IDX], .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. */ 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); @@ -92,7 +93,8 @@ dimension_scan(int32 hypertable_id, Oid main_table_relid, int16 num_dimensions) static Point * point_create(int16 num_dimensions) { - Point *p = palloc0(POINT_SIZE(num_dimensions)); + Point *p = palloc0(POINT_SIZE(num_dimensions)); + p->cardinality = num_dimensions; p->num_closed = p->num_open = 0; return p; @@ -101,15 +103,16 @@ point_create(int16 num_dimensions) const char * point_to_string(Point *p) { - char *buf = palloc(100); - int i, j = 1; + char *buf = palloc(100); + int i, + j = 1; buf[0] = '('; for (i = 0; i < p->cardinality; i++) j += snprintf(buf + j, 100, "" INT64_FORMAT ",", p->coordinates[i]); - buf[j-1] = ')'; + buf[j - 1] = ')'; return buf; } @@ -117,17 +120,17 @@ point_to_string(Point *p) Point * hyperspace_calculate_point(Hyperspace *hs, HeapTuple tuple, TupleDesc tupdesc) { - Point *p = point_create(HYPERSPACE_NUM_DIMENSIONS(hs)); - int i; + Point *p = point_create(HYPERSPACE_NUM_DIMENSIONS(hs)); + int 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)) { - Datum datum; - bool isnull; + Datum datum; + bool isnull; datum = heap_getattr(tuple, d->column_attno, tupdesc, &isnull); diff --git a/src/dimension.h b/src/dimension.h index 8d507135f..d5ac2e275 100644 --- a/src/dimension.h +++ b/src/dimension.h @@ -20,7 +20,7 @@ typedef struct Dimension FormData_dimension fd; DimensionType type; AttrNumber column_attno; - Oid main_table_relid; + Oid main_table_relid; PartitioningInfo *partitioning; } Dimension; @@ -36,13 +36,13 @@ typedef struct Dimension */ typedef struct Hyperspace { - int32 hypertable_id; - Oid main_table_relid; - uint16 capacity; - int16 num_open_dimensions; - int16 num_closed_dimensions; + int32 hypertable_id; + Oid main_table_relid; + uint16 capacity; + int16 num_open_dimensions; + int16 num_closed_dimensions; /* Open dimensions should be stored before closed dimensions */ - Dimension dimensions[0]; + Dimension dimensions[0]; } Hyperspace; #define HYPERSPACE_NUM_DIMENSIONS(hs) \ @@ -60,11 +60,11 @@ typedef struct Hyperspace */ typedef struct Point { - int16 cardinality; - uint8 num_open; - uint8 num_closed; + int16 cardinality; + uint8 num_open; + uint8 num_closed; /* Open dimension coordinates are stored before the closed coordinates */ - int64 coordinates[0]; + int64 coordinates[0]; } Point; #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); -#endif /* TIMESCALEDB_DIMENSION_H */ +#endif /* TIMESCALEDB_DIMENSION_H */ diff --git a/src/dimension_slice.c b/src/dimension_slice.c index 52907f2ad..6ec604300 100644 --- a/src/dimension_slice.c +++ b/src/dimension_slice.c @@ -15,6 +15,7 @@ static inline DimensionSlice * dimension_slice_from_form_data(Form_dimension_slice fd) { DimensionSlice *ds; + ds = palloc0(sizeof(DimensionSlice)); memcpy(&ds->fd, fd, sizeof(FormData_dimension_slice)); ds->storage_free = NULL; @@ -25,13 +26,14 @@ dimension_slice_from_form_data(Form_dimension_slice fd) static inline DimensionSlice * 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 * hypercube_alloc(int16 num_dimensions) { - Hypercube *hc = palloc0(HYPERCUBE_SIZE(num_dimensions)); + Hypercube *hc = palloc0(HYPERCUBE_SIZE(num_dimensions)); + hc->capacity = num_dimensions; return hc; } @@ -39,7 +41,7 @@ hypercube_alloc(int16 num_dimensions) static inline void hypercube_free(Hypercube *hc) { - int i; + int i; for (i = 0; i < hc->num_slices; i++) pfree(hc->slices[i]); @@ -50,14 +52,14 @@ hypercube_free(Hypercube *hc) Hypercube * hypercube_copy(Hypercube *hc) { - Hypercube *copy; - size_t nbytes = HYPERCUBE_SIZE(hc->capacity); - int i; + Hypercube *copy; + size_t nbytes = HYPERCUBE_SIZE(hc->capacity); + int i; copy = palloc(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]); return copy; @@ -68,6 +70,7 @@ dimension_vec_tuple_found(TupleInfo *ti, void *data) { DimensionVec **vecptr = data; DimensionSlice *slice = dimension_slice_from_tuple(ti->tuple); + dimension_vec_add_slice(vecptr, slice); return true; } @@ -90,12 +93,14 @@ dimension_slice_scan(int32 dimension_id, int64 coordinate) .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, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_id)); 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, BTGreaterStrategyNumber, F_INT8GT, Int64GetDatum(coordinate)); @@ -108,6 +113,7 @@ static bool dimension_slice_tuple_found(TupleInfo *ti, void *data) { DimensionSlice **slice = data; + *slice = dimension_slice_from_tuple(ti->tuple); 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, - BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_slice_id)); + BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(dimension_slice_id)); scanner_scan(&scanCtx); return slice; @@ -141,6 +147,7 @@ DimensionSlice * dimension_slice_copy(const DimensionSlice *original) { DimensionSlice *new = palloc(sizeof(DimensionSlice)); + memcpy(new, original, sizeof(DimensionSlice)); return new; } @@ -168,12 +175,13 @@ hypercube_slice_sort(Hypercube *hc) Hypercube * hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints) { - Hypercube *hc = hypercube_alloc(num_constraints); - int i; + Hypercube *hc = hypercube_alloc(num_constraints); + int i; for (i = 0; i < num_constraints; i++) { DimensionSlice *slice = dimension_slice_scan_by_id(constraints[i].fd.dimension_slice_id); + Assert(slice != NULL); hc->slices[hc->num_slices++] = slice; } @@ -182,7 +190,8 @@ hypercube_from_constraints(ChunkConstraint constraints[], int16 num_constraints) return hc; } -void dimension_slice_free(DimensionSlice *slice) +void +dimension_slice_free(DimensionSlice *slice) { if (slice->storage_free != NULL) slice->storage_free(slice->storage); @@ -215,7 +224,7 @@ cmp_slices(const void *left, const void *right) static int cmp_coordinate_and_slice(const void *left, const void *right) { - int64 coord = *((int64 *) left); + int64 coord = *((int64 *) left); const DimensionSlice *slice = *((DimensionSlice **) right); if (coord < slice->fd.range_start) @@ -247,6 +256,7 @@ DimensionVec * dimension_vec_create(int32 initial_num_slices) { DimensionVec *vec = dimension_vec_expand(NULL, initial_num_slices); + vec->capacity = initial_num_slices; vec->num_slices = 0; return vec; @@ -269,6 +279,7 @@ DimensionVec * dimension_vec_add_slice_sort(DimensionVec **vecptr, DimensionSlice *slice) { DimensionVec *vec = *vecptr; + *vecptr = vec = dimension_vec_add_slice(vecptr, slice); qsort(vec->slices, vec->num_slices, sizeof(DimensionSlice *), cmp_slices); return vec; @@ -277,17 +288,19 @@ dimension_vec_add_slice_sort(DimensionVec **vecptr, DimensionSlice *slice) DimensionSlice * dimension_vec_find_slice(DimensionVec *vec, int64 coordinate) { - DimensionSlice **res = bsearch(&coordinate, vec->slices, vec->num_slices, - sizeof(DimensionSlice *), cmp_coordinate_and_slice); - if (res == NULL) - return NULL; + DimensionSlice **res = bsearch(&coordinate, vec->slices, vec->num_slices, + sizeof(DimensionSlice *), cmp_coordinate_and_slice); - 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++) dimension_slice_free(vec->slices[i]); diff --git a/src/dimension_slice.h b/src/dimension_slice.h index 94a687c63..088eaf96a 100644 --- a/src/dimension_slice.h +++ b/src/dimension_slice.h @@ -12,8 +12,9 @@ typedef struct DimensionSlice { FormData_dimension_slice fd; DimensionType type; - void (*storage_free)(void *); - void *storage; //used in the cache + void (*storage_free) (void *); + void *storage; + //used in the cache } DimensionSlice; /* @@ -22,8 +23,9 @@ typedef struct DimensionSlice */ typedef struct Hypercube { - int16 capacity; /* capacity of slices[] */ - int16 num_slices; /* actual number of slices (should equal num_dimensions after create) */ + int16 capacity; /* capacity of slices[] */ + int16 num_slices; /* actual number of slices (should equal + * num_dimensions after create) */ /* Open slices are stored before closed slices */ DimensionSlice *slices[0]; } Hypercube; @@ -32,13 +34,14 @@ typedef struct Hypercube (sizeof(Hypercube) + sizeof(DimensionSlice *) * (num_dimensions)) /* - * DimensionVec is a collection of slices (ranges) along one dimension for a - * time range. + * DimensionVec is a collection of slices (ranges) along one dimension for a + * time range. */ typedef struct DimensionVec { - int32 capacity; /* The capacity of the slices array */ - int32 num_slices; /* The current number of slices in slices array */ + int32 capacity; /* The capacity of the slices array */ + int32 num_slices; /* The current number of slices in slices + * array */ DimensionSlice *slices[0]; } 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_copy(Hypercube *hc); -#endif /* TIMESCALEDB_DIMENSION_SLICE_H */ +#endif /* TIMESCALEDB_DIMENSION_SLICE_H */ diff --git a/src/hypertable.c b/src/hypertable.c index 9c3728bfe..7f33c0497 100644 --- a/src/hypertable.c +++ b/src/hypertable.c @@ -14,7 +14,7 @@ Hypertable * hypertable_from_tuple(HeapTuple tuple) { Hypertable *h; - Oid namespace_oid; + Oid namespace_oid; h = palloc0(sizeof(Hypertable)); memcpy(&h->fd, GETSTRUCT(tuple), sizeof(FormData_hypertable)); @@ -26,28 +26,29 @@ hypertable_from_tuple(HeapTuple tuple) 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) { MemoryContext old; /* - chunk_find() must execute on the transaction memory context since it - allocates a lot of transient data. + * chunk_find() must execute on the transaction memory context since + * it allocates a lot of transient data. */ chunk = chunk_find(h->space, point); - + old = MemoryContextSwitchTo(subspace_store_mcxt(h->chunk_cache)); if (NULL == chunk) - chunk = chunk_create_new(h->space, point); + chunk = chunk_create_new(h->space, point); else /* Make a copy which lives in the chunk cache's memory context */ chunk = chunk_copy(chunk); - + Assert(NULL != chunk); subspace_store_add(h->chunk_cache, chunk->cube, chunk, pfree); MemoryContextSwitchTo(old); diff --git a/src/hypertable.h b/src/hypertable.h index 3f7f9fbca..7a9ead49b 100644 --- a/src/hypertable.h +++ b/src/hypertable.h @@ -14,7 +14,7 @@ typedef struct HeapTupleData *HeapTuple; typedef struct Hypertable { FormData_hypertable fd; - Oid main_table_relid; + Oid main_table_relid; Hyperspace *space; SubspaceStore *chunk_cache; } Hypertable; @@ -22,4 +22,4 @@ typedef struct Hypertable extern Hypertable *hypertable_from_tuple(HeapTuple tuple); extern Chunk *hypertable_get_chunk(Hypertable *h, Point *point); -#endif /* TIMESCALEDB_HYPERTABLE_H */ +#endif /* TIMESCALEDB_HYPERTABLE_H */ diff --git a/src/hypertable_cache.c b/src/hypertable_cache.c index dab86b3ab..614fcdcdc 100644 --- a/src/hypertable_cache.c +++ b/src/hypertable_cache.c @@ -61,7 +61,7 @@ hypertable_cache_create() .get_key = hypertable_cache_get_key, .create_entry = hypertable_cache_create_entry, }; - + *cache = template; cache_init(cache); @@ -78,6 +78,7 @@ static bool hypertable_tuple_found(TupleInfo *ti, void *data) { HypertableNameCacheEntry *entry = data; + entry->hypertable = hypertable_from_tuple(ti->tuple); return false; } diff --git a/src/insert.c b/src/insert.c index 2fcf62039..3701c70dc 100644 --- a/src/insert.c +++ b/src/insert.c @@ -31,8 +31,8 @@ insert_statement_state_cleanup(InsertStatementState **state_p) } -Datum insert_main_table_trigger(PG_FUNCTION_ARGS); -Datum insert_main_table_trigger_after(PG_FUNCTION_ARGS); +Datum insert_main_table_trigger(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_after); @@ -47,12 +47,12 @@ Datum insert_main_table_trigger(PG_FUNCTION_ARGS) { TriggerData *trigdata = (TriggerData *) fcinfo->context; - HeapTuple tuple; + HeapTuple tuple; Hypertable *ht; - Point *point; + Point *point; InsertChunkState *cstate; - Oid relid = trigdata->tg_relation->rd_id; - TupleDesc tupdesc = trigdata->tg_relation->rd_att; + Oid relid = trigdata->tg_relation->rd_id; + TupleDesc tupdesc = trigdata->tg_relation->rd_att; PG_TRY(); { @@ -79,7 +79,7 @@ insert_main_table_trigger(PG_FUNCTION_ARGS) /* Find or create the insert state matching the point */ cstate = insert_statement_state_get_insert_chunk_state(insert_statement_state, - ht->space, point); + ht->space, point); insert_chunk_state_insert_tuple(cstate, tuple); } PG_CATCH(); diff --git a/src/insert_chunk_state.c b/src/insert_chunk_state.c index 11ae21be1..5633d3baf 100644 --- a/src/insert_chunk_state.c +++ b/src/insert_chunk_state.c @@ -155,6 +155,7 @@ insert_chunk_state_destroy(InsertChunkState *state) foreach(lc, state->replica_states) { InsertChunkStateRel *rel_state = lfirst(lc); + 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) { InsertChunkStateRel *rel_state = lfirst(lc); + insert_chunk_state_rel_insert_tuple(rel_state, tup); } } diff --git a/src/insert_statement_state.c b/src/insert_statement_state.c index 358542c8d..06339e58a 100644 --- a/src/insert_statement_state.c +++ b/src/insert_statement_state.c @@ -20,7 +20,7 @@ insert_statement_state_new(Oid relid) ALLOCSET_DEFAULT_SIZES); InsertStatementState *state; Hypertable *ht; - Cache *hypertable_cache; + Cache *hypertable_cache; oldctx = MemoryContextSwitchTo(mctx); @@ -50,9 +50,11 @@ insert_statement_state_destroy(InsertStatementState *state) 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; + insert_chunk_state_destroy(ics); } @@ -69,7 +71,7 @@ insert_statement_state_get_insert_chunk_state(InsertStatementState *state, Hyper if (NULL == ics) { - Chunk *new_chunk; + Chunk *new_chunk; MemoryContext old; new_chunk = hypertable_get_chunk(state->hypertable, point); diff --git a/src/metadata_queries.c b/src/metadata_queries.c index aa163d481..db8e3dbf3 100644 --- a/src/metadata_queries.c +++ b/src/metadata_queries.c @@ -66,13 +66,14 @@ DEFINE_PLAN(create_chunk_plan, CHUNK_CREATE, 2, CHUNK_CREATE_ARGS) static HeapTuple chunk_tuple_create_spi_connected(Hyperspace *hs, Point *p, SPIPlanPtr plan) { - int i, ret; + int i, + ret; HeapTuple tuple; Datum dimension_ids[HYPERSPACE_NUM_DIMENSIONS(hs)]; Datum dimension_values[HYPERSPACE_NUM_DIMENSIONS(hs)]; 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_values[i] = Int64GetDatum(p->coordinates[i]); @@ -99,7 +100,8 @@ spi_chunk_create(Hyperspace *hs, Point *p) { HeapTuple tuple; Chunk *chunk; - MemoryContext old, top = CurrentMemoryContext; + MemoryContext old, + top = CurrentMemoryContext; SPIPlanPtr plan = create_chunk_plan(); if (SPI_connect() < 0) @@ -108,7 +110,7 @@ spi_chunk_create(Hyperspace *hs, Point *p) tuple = chunk_tuple_create_spi_connected(hs, p, plan); 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); SPI_finish(); diff --git a/src/partitioning.c b/src/partitioning.c index 5358157f8..598ac5870 100644 --- a/src/partitioning.c +++ b/src/partitioning.c @@ -80,7 +80,7 @@ partitioning_func_apply(PartitioningInfo *pinfo, Datum value) Datum text = FunctionCall1(&pinfo->partfunc.textfunc_fmgr, value); char *partition_val = DatumGetCString(text); Datum keyspace_datum = FunctionCall1(&pinfo->partfunc.func_fmgr, - CStringGetTextDatum(partition_val)); + CStringGetTextDatum(partition_val)); return DatumGetInt32(keyspace_datum); } @@ -117,7 +117,7 @@ get_partition_for_key(PG_FUNCTION_ARGS) hash_u = hash_any((unsigned char *) VARDATA_ANY(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_RETURN_INT32(res); diff --git a/src/partitioning.h b/src/partitioning.h index 7d6b0f199..1656f10fa 100644 --- a/src/partitioning.h +++ b/src/partitioning.h @@ -39,12 +39,12 @@ typedef struct PartitioningInfo extern PartitioningInfo *partitioning_info_create(int num_partitions, - const char *schema, - const char *partfunc, - const char *partcol, - Oid relid); - -extern int32 partitioning_func_apply(PartitioningInfo *pinfo, Datum value); -extern int32 partitioning_func_apply_tuple(PartitioningInfo *pinfo, HeapTuple tuple, TupleDesc desc); + const char *schema, + const char *partfunc, + const char *partcol, + Oid relid); + +extern int32 partitioning_func_apply(PartitioningInfo *pinfo, Datum value); +extern int32 partitioning_func_apply_tuple(PartitioningInfo *pinfo, HeapTuple tuple, TupleDesc desc); #endif /* TIMESCALEDB_PARTITIONING_H */ diff --git a/src/planner.c b/src/planner.c index e2e29eec9..75949c30c 100644 --- a/src/planner.c +++ b/src/planner.c @@ -121,10 +121,10 @@ get_partitioning_info_for_partition_column_var(Var *var_expr, Query *parse, Cach if (rte->relid == hentry->main_table_relid) { - Dimension *closed_dim = hyperspace_get_closed_dimension(hentry->space, 0); - - if (closed_dim != NULL && - strncmp(closed_dim->fd.column_name.data, varname, NAMEDATALEN) == 0) + Dimension *closed_dim = hyperspace_get_closed_dimension(hentry->space, 0); + + if (closed_dim != NULL && + strncmp(closed_dim->fd.column_name.data, varname, NAMEDATALEN) == 0) return closed_dim->partitioning; } return NULL; @@ -135,7 +135,7 @@ get_partitioning_info_for_partition_column_var(Var *var_expr, Query *parse, Cach * all nodes given in input. */ static Expr * create_partition_func_equals_const(Var *var_expr, Const *const_expr, char *partitioning_func_schema, - char *partitioning_func) + char *partitioning_func) { Expr *op_expr; 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, * replace with partitioning_column = const AND - * partitioning_func(partition_column) = - * partitioning_func(const) + * partitioning_func(partition_column) = partitioning_func(const) */ if (IsA(node, OpExpr)) { @@ -247,15 +246,15 @@ add_partitioning_func_qual_mutator(Node *node, AddPartFuncQualCtx *context) */ PartitioningInfo *pi = get_partitioning_info_for_partition_column_var(var_expr, - context->parse, - context->hcache, - context->hentry); + context->parse, + context->hcache, + context->hentry); if (pi != NULL) { /* The var is a partitioning column */ 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)); diff --git a/src/subspace_store.c b/src/subspace_store.c index b320f23be..f3c4bfa24 100644 --- a/src/subspace_store.c +++ b/src/subspace_store.c @@ -4,10 +4,11 @@ #include "dimension_slice.h" #include "subspace_store.h" -typedef struct SubspaceStore { +typedef struct SubspaceStore +{ MemoryContext mcxt; - int16 num_dimensions; - DimensionVec *origin; /* origin of the tree */ + int16 num_dimensions; + DimensionVec *origin; /* origin of the tree */ } SubspaceStore; static inline DimensionVec * @@ -21,6 +22,7 @@ subspace_store_init(int16 num_dimensions, MemoryContext mcxt) { MemoryContext old = MemoryContextSwitchTo(mcxt); SubspaceStore *sst = palloc(sizeof(SubspaceStore)); + sst->origin = subspace_store_dimension_create(); sst->num_dimensions = num_dimensions; sst->mcxt = mcxt; @@ -29,18 +31,19 @@ subspace_store_init(int16 num_dimensions, MemoryContext mcxt) } 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 *end_store, void (*end_store_free)(void *)) +void +subspace_store_add(SubspaceStore *cache, const Hypercube *hc, + void *end_store, void (*end_store_free) (void *)) { DimensionVec **vecptr = &cache->origin; DimensionSlice *last = NULL; MemoryContext old = MemoryContextSwitchTo(cache->mcxt); - int i; + int i; Assert(hc->num_slices == cache->num_dimensions); @@ -69,17 +72,18 @@ void subspace_store_add(SubspaceStore *cache, const Hypercube *hc, if (match == NULL) { DimensionSlice *copy = dimension_slice_copy(target); + dimension_vec_add_slice_sort(vecptr, copy); match = copy; } last = match; /* internal nodes point to the next dimension's vector */ - vecptr = (DimensionVec **)&last->storage; + vecptr = (DimensionVec **) &last->storage; } 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; MemoryContextSwitchTo(old); } @@ -87,7 +91,7 @@ void subspace_store_add(SubspaceStore *cache, const Hypercube *hc, void * subspace_store_get(SubspaceStore *cache, Point *target) { - int i; + int i; DimensionVec *vec = cache->origin; DimensionSlice *match = NULL; @@ -112,7 +116,8 @@ subspace_store_free(SubspaceStore *cache) pfree(cache); } -MemoryContext subspace_store_mcxt(SubspaceStore *cache) +MemoryContext +subspace_store_mcxt(SubspaceStore *cache) { return cache->mcxt; } diff --git a/src/subspace_store.h b/src/subspace_store.h index de59998eb..95f4e57db 100644 --- a/src/subspace_store.h +++ b/src/subspace_store.h @@ -10,7 +10,7 @@ typedef struct SubspaceStore SubspaceStore; extern SubspaceStore *subspace_store_init(int16 num_dimensions, MemoryContext mcxt); 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); @@ -18,4 +18,4 @@ extern void subspace_store_free(SubspaceStore *cache); extern MemoryContext subspace_store_mcxt(SubspaceStore *cache); -#endif /* TIMESCALEDB_SUBSPACE_STORE_H */ +#endif /* TIMESCALEDB_SUBSPACE_STORE_H */