Navigation Menu

Skip to content

Commit

Permalink
Implement IndexField#upgradeToMultipleValuesSync()
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 13, 2012
1 parent cde9b4f commit 3d28feb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/database/index-field.js
Expand Up @@ -168,7 +168,12 @@ IndexField.prototype = {
return 'Active';
},

createSync: function() {
get multipleValues() {
return !!this.column &&
this.column.flags.indexOf(nroonga.COLUMN_VECTOR) > -1;
},

createSync: function(multipleValues) {
var indexTableName = this.domain.termsTableName;

var type = this.type;
Expand All @@ -183,10 +188,12 @@ IndexField.prototype = {
indexTableName = this.indexTableName;
}

var columnFlags = multipleValues ?
nroonga.COLUMN_VECTOR : nroonga.COLUMN_SCALAR;
this.context.commandSync('column_create', {
table: this.domain.tableName,
name: this.columnName,
flags: nroonga.COLUMN_SCALAR,
flags: columnFlags,
type: columnType
});
this.context.commandSync('column_create', {
Expand Down Expand Up @@ -249,6 +256,14 @@ IndexField.prototype = {

exists: function() {
return !!this.column;
},

upgradeToMultipleValuesSync: function() {
if (this.multipleValues) return;
var values = this.domain.dump();
this.deleteSync();
this.createSync(true);
this.domain.load(values);
}
};

Expand Down
67 changes: 67 additions & 0 deletions test/database-index-field.test.js
Expand Up @@ -226,6 +226,7 @@ suite('database', function() {

field.createSync();
assert.isTrue(field.exists());
assert.isFalse(field.multipleValues);

var dump = context.commandSync('dump', {
tables: domain.tableName
Expand Down Expand Up @@ -268,6 +269,7 @@ suite('database', function() {

field.createSync();
assert.isTrue(field.exists());
assert.isFalse(field.multipleValues);

var dump = context.commandSync('dump', {
tables: domain.tableName
Expand Down Expand Up @@ -312,6 +314,7 @@ suite('database', function() {

field.createSync();
assert.isTrue(field.exists());
assert.isFalse(field.multipleValues);

var dump = context.commandSync('dump', {
tables: 'companies'
Expand Down Expand Up @@ -348,5 +351,69 @@ suite('database', function() {
assert.equal(dump, expected);
});
});

suite('multiple values column', function() {
var temporaryDatabase;
var context;
var domain;

setup(function() {
temporaryDatabase = utils.createTemporaryDatabase();
context = temporaryDatabase.get();
domain = new Domain('companies', context);
domain.createSync();
});

teardown(function() {
temporaryDatabase.teardown();
temporaryDatabase = undefined;
});

test('createSync (multiple values)', function() {
var field = new IndexField('name', domain).setType('text');
assert.isFalse(field.exists());
field.createSync(true);
assert.isTrue(field.exists());
assert.isTrue(field.multipleValues);

var dump = context.commandSync('dump', {
tables: domain.tableName
});
var expected = 'table_create ' + domain.tableName + ' ' +
'TABLE_HASH_KEY ShortText\n' +
'column_create ' + domain.tableName + ' ' +
field.columnName + ' COLUMN_VECTOR ShortText\n' +
'table_create ' + domain.termsTableName + ' ' +
'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
'--default_tokenizer TokenBigram\n' +
'column_create ' + domain.termsTableName + ' ' +
field.indexColumnName + ' ' +
'COLUMN_INDEX|WITH_POSITION ' + domain.tableName +
' ' + field.columnName;
assert.equal(dump, expected);
});

test('upgradeToMultipleValuesSync', function() {
var field = new IndexField('product', domain).setType('literal');
field.createSync();
assert.isFalse(field.multipleValues);

field.domain.load([
{ id: 'id1', product: 'groonga' },
{ id: 'id2', product: 'nroonga' }
]);

field.upgradeToMultipleValuesSync();
assert.isTrue(field.exists());
assert.isTrue(field.multipleValues);

var actualDump = field.domain.dump();
var expectedDump = [
{ id: 'id1', product: ['groonga'] },
{ id: 'id2', product: ['nroonga'] }
];
assert.deepEqual(actualDump, expectedDump);
});
});
});
});

0 comments on commit 3d28feb

Please sign in to comment.