Navigation Menu

Skip to content

Commit

Permalink
Support WAL again
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 9, 2017
1 parent 52b00a7 commit 7ee23fd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/pgrn-create.c
Expand Up @@ -45,7 +45,7 @@ PGrnCreateSourcesTableFinish(PGrnCreateData *data)

snprintf(sourcesTableName, sizeof(sourcesTableName),
PGrnSourcesTableNameFormat, data->relNode);
PGrnRenameTable(data->sourcesTable, sourcesTableName);
PGrnRenameTable(data->index, data->sourcesTable, sourcesTableName);
}

void
Expand Down
30 changes: 21 additions & 9 deletions src/pgrn-groonga.c
Expand Up @@ -391,6 +391,27 @@ PGrnIndexColumnSetSourceIDs(Relation index,
PGrnWALSetSourceIDs(index, indexColumn, sourceIDs);
}

void
PGrnRenameTable(Relation index, grn_obj *table, const char *newName)
{
char name[GRN_TABLE_MAX_KEY_SIZE];
int nameSize;
size_t newNameSize;

nameSize = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE);
newNameSize = strlen(newName);
grn_table_rename(ctx, table, newName, strlen(newName));
PGrnCheck("failed to rename table: <%s> -> <%s>",
PGrnInspectName(table),
newName);

PGrnWALRenameTable(index,
name,
nameSize,
newName,
newNameSize);
}

void
PGrnRemoveObject(const char *name)
{
Expand Down Expand Up @@ -465,12 +486,3 @@ PGrnRemoveColumns(grn_obj *table)

grn_hash_close(ctx, columns);
}

void
PGrnRenameTable(grn_obj *table, const char *newName)
{
grn_table_rename(ctx, table, newName, strlen(newName));
PGrnCheck("failed to rename table: <%s> -> <%s>",
PGrnInspectName(table),
newName);
}
6 changes: 4 additions & 2 deletions src/pgrn-groonga.h
Expand Up @@ -70,11 +70,13 @@ void PGrnIndexColumnSetSourceIDs(Relation index,
grn_obj *indexColumn,
grn_obj *sourceIDs);

void PGrnRenameTable(Relation index,
grn_obj *table,
const char *newName);

void PGrnRemoveObject(const char *name);
void PGrnRemoveObjectWithSize(const char *name, size_t nameSize);

void PGrnRemoveColumns(grn_obj *table);

void PGrnFlushObject(grn_obj *object, bool recursive);

void PGrnRenameTable(grn_obj *table, const char *newName);
69 changes: 68 additions & 1 deletion src/pgrn-wal.c
Expand Up @@ -47,7 +47,8 @@ typedef enum {
PGRN_WAL_ACTION_INSERT,
PGRN_WAL_ACTION_CREATE_TABLE,
PGRN_WAL_ACTION_CREATE_COLUMN,
PGRN_WAL_ACTION_SET_SOURCES
PGRN_WAL_ACTION_SET_SOURCES,
PGRN_WAL_ACTION_RENAME_TABLE
} PGrnWALAction;

#define PGRN_WAL_META_PAGE_SPECIAL_VERSION 1
Expand Down Expand Up @@ -870,6 +871,40 @@ PGrnWALSetSourceIDs(Relation index,
#endif
}

void
PGrnWALRenameTable(Relation index,
const char *name,
size_t nameSize,
const char *newName,
size_t newNameSize)
{
#ifdef PGRN_SUPPORT_WAL
PGrnWALData *data;
msgpack_packer *packer;
size_t nElements = 3;

data = PGrnWALStart(index);
if (!data)
return;

packer = &(data->packer);
msgpack_pack_map(packer, nElements);

msgpack_pack_cstr(packer, "_action");
msgpack_pack_uint32(packer, PGRN_WAL_ACTION_RENAME_TABLE);

msgpack_pack_cstr(packer, "name");
msgpack_pack_str(packer, nameSize);
msgpack_pack_str_body(packer, name, nameSize);

msgpack_pack_cstr(packer, "new_name");
msgpack_pack_str(packer, newNameSize);
msgpack_pack_str_body(packer, newName, newNameSize);

PGrnWALFinish(data);
#endif
}

#ifdef PGRN_SUPPORT_WAL
typedef struct {
Relation index;
Expand Down Expand Up @@ -1460,6 +1495,35 @@ PGrnWALApplySetSources(PGrnWALApplyData *data,
grn_obj_set_info(ctx, column, GRN_INFO_SOURCE, sourceIDs);
}

static void
PGrnWALApplyRenameTable(PGrnWALApplyData *data,
msgpack_object_map *map,
uint32_t currentElement)
{
const char *context = "rename table";
grn_obj *table = NULL;
const char *newName = NULL;
size_t newNameSize = 0;
uint32_t i;

for (i = currentElement; i < map->size; i++)
{
msgpack_object_kv *kv;

kv = &(map->ptr[i]);
if (PGrnWALApplyKeyEqual(context, &(kv->key), "name"))
{
table = PGrnWALApplyValueGetGroongaObject(context, kv);
}
else if (PGrnWALApplyKeyEqual(context, &(kv->key), "new_name"))
{
PGrnWALApplyValueGetString(context, kv, &newName, &newNameSize);
}
}

grn_table_rename(ctx, table, newName, newNameSize);
}

static void
PGrnWALApplyObject(PGrnWALApplyData *data, msgpack_object *object)
{
Expand Down Expand Up @@ -1504,6 +1568,9 @@ PGrnWALApplyObject(PGrnWALApplyData *data, msgpack_object *object)
case PGRN_WAL_ACTION_SET_SOURCES:
PGrnWALApplySetSources(data, map, currentElement);
break;
case PGRN_WAL_ACTION_RENAME_TABLE:
PGrnWALApplyRenameTable(data, map, currentElement);
break;
default:
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
Expand Down
6 changes: 6 additions & 0 deletions src/pgrn-wal.h
Expand Up @@ -52,4 +52,10 @@ void PGrnWALSetSourceIDs(Relation index,
grn_obj *column,
grn_obj *sourceIDs);

void PGrnWALRenameTable(Relation index,
const char *name,
size_t nameSize,
const char *newName,
size_t newNameSize);

void PGrnWALApply(Relation index);

0 comments on commit 7ee23fd

Please sign in to comment.