Kouhei Sutou
null+****@clear*****
Mon Jan 26 00:35:31 JST 2015
Kouhei Sutou 2015-01-26 00:35:31 +0900 (Mon, 26 Jan 2015) New Revision: 752a3c3b3cae93e25836b5c0a57390d864fce962 https://github.com/pgroonga/pgroonga/commit/752a3c3b3cae93e25836b5c0a57390d864fce962 Message: Support removing unused tables on VACUUM Modified files: pgroonga.c pgroonga.h Modified: pgroonga.c (+56 -0) =================================================================== --- pgroonga.c 2015-01-22 23:38:58 +0900 (3a3d4a6) +++ pgroonga.c 2015-01-26 00:35:31 +0900 (79d58fd) @@ -17,6 +17,8 @@ #include <groonga.h> +#include <stdlib.h> + PG_MODULE_MAGIC; typedef struct GrnBuildStateData @@ -1146,6 +1148,58 @@ pgroonga_bulkdelete(PG_FUNCTION_ARGS) PG_RETURN_POINTER(stats); } +static void +GrnRemoveUnusedTables(void) +{ + grn_table_cursor *cursor; + const char *min = GrnIDsTableNamePrefix; + + cursor = grn_table_cursor_open(ctx, grn_ctx_db(ctx), + min, strlen(min), + NULL, 0, + 0, -1, GRN_CURSOR_BY_KEY|GRN_CURSOR_PREFIX); + while (grn_table_cursor_next(ctx, cursor) != GRN_ID_NIL) { + char *name; + char *nameEnd; + int nameSize; + Oid relationID; + Relation relation; + + nameSize = grn_table_cursor_get_key(ctx, cursor, (void **)&name); + nameEnd = name + nameSize; + relationID = strtol(name + strlen(min), &nameEnd, 10); + relation = RelationIdGetRelation(relationID); + if (relation) + { + RelationClose(relation); + continue; + } + + { + char tableName[GRN_TABLE_MAX_KEY_SIZE]; + grn_obj *table; + + snprintf(tableName, sizeof(tableName), + GrnLexiconNameFormat, relationID); + table = grn_ctx_get(ctx, tableName, strlen(tableName)); + if (table) + { + grn_obj_remove(ctx, table); + } + + snprintf(tableName, sizeof(tableName), + GrnIDsTableNameFormat, relationID); + table = grn_ctx_get(ctx, tableName, strlen(tableName)); + if (table) + { + grn_obj_remove(ctx, table); + } + } + } + grn_table_cursor_close(ctx, cursor); +} + + /** * pgroonga.vacuumcleanup() -- amvacuumcleanup */ @@ -1159,6 +1213,8 @@ pgroonga_vacuumcleanup(PG_FUNCTION_ARGS) stats = GrnBulkDeleteResult(info, GrnLookupIDsTable(info->index, WARNING)); + GrnRemoveUnusedTables(); + PG_RETURN_POINTER(stats); } Modified: pgroonga.h (+2 -1) =================================================================== --- pgroonga.h 2015-01-22 23:38:58 +0900 (9d1921d) +++ pgroonga.h 2015-01-26 00:35:31 +0900 (1a25f37) @@ -29,7 +29,8 @@ /* file and table names */ #define GrnDatabaseBasename "grn" -#define GrnIDsTableNameFormat "IDs%u" +#define GrnIDsTableNamePrefix "IDs" +#define GrnIDsTableNameFormat GrnIDsTableNamePrefix "%u" #define GrnLexiconNameFormat "Lexicon%u" #define GrnIndexColumnName "index" -------------- next part -------------- HTML����������������������������... Download