Navigation Menu

Skip to content

Commit

Permalink
Support removing unused tables on VACUUM
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jan 25, 2015
1 parent 80a8508 commit 752a3c3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
56 changes: 56 additions & 0 deletions pgroonga.c
Expand Up @@ -17,6 +17,8 @@

#include <groonga.h>

#include <stdlib.h>

PG_MODULE_MAGIC;

typedef struct GrnBuildStateData
Expand Down Expand Up @@ -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
*/
Expand All @@ -1159,6 +1213,8 @@ pgroonga_vacuumcleanup(PG_FUNCTION_ARGS)
stats = GrnBulkDeleteResult(info,
GrnLookupIDsTable(info->index, WARNING));

GrnRemoveUnusedTables();

PG_RETURN_POINTER(stats);
}

Expand Down
3 changes: 2 additions & 1 deletion pgroonga.h
Expand Up @@ -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"

Expand Down

0 comments on commit 752a3c3

Please sign in to comment.