Skip to content

Add Indexes to Schema API #4240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions spec/ParseQuery.FullTextSearch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,21 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
return databaseAdapter.getIndexes('TestObject');
}).then((indexes) => {
expect(indexes.length).toEqual(2);
done();
rp.get({
url: 'http://localhost:8378/1/schemas/TestObject',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
json: true,
}, (error, response, body) => {
expect(body.indexes._id_).toBeDefined();
expect(body.indexes._id_._id).toEqual(1);
expect(body.indexes.subject_text_comment_text).toBeDefined();
expect(body.indexes.subject_text_comment_text.subject).toEqual('text');
expect(body.indexes.subject_text_comment_text.comment).toEqual('text');
done();
});
}).catch(done.fail);
});

Expand Down Expand Up @@ -366,7 +380,21 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
return databaseAdapter.getIndexes('TestObject');
}).then((indexes) => {
expect(indexes.length).toEqual(2);
done();
rp.get({
url: 'http://localhost:8378/1/schemas/TestObject',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
json: true,
}, (error, response, body) => {
expect(body.indexes._id_).toBeDefined();
expect(body.indexes._id_._id).toEqual(1);
expect(body.indexes.text_test).toBeDefined();
expect(body.indexes.text_test.subject).toEqual('text');
expect(body.indexes.text_test.comment).toEqual('text');
done();
});
}).catch(done.fail);
});

Expand Down
18 changes: 17 additions & 1 deletion spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
}
})).toEqual(undefined);
Expand All @@ -1922,6 +1923,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
}
});
Expand Down Expand Up @@ -1973,6 +1975,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
name2: { bString: 1 },
name3: { cString: 1, dString: 1 },
Expand All @@ -1997,6 +2000,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
name2: { bString: 1 },
name3: { cString: 1, dString: 1 },
Expand Down Expand Up @@ -2042,6 +2046,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
}
})).toEqual(undefined);
Expand All @@ -2065,6 +2070,9 @@ describe('schemas', () => {
aString: {type: 'String'},
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
}
});
config.database.adapter.getIndexes('NewClass').then((indexes) => {
expect(indexes.length).toEqual(1);
Expand Down Expand Up @@ -2112,6 +2120,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
name2: { bString: 1 },
name3: { cString: 1 },
Expand Down Expand Up @@ -2141,6 +2150,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name3: { cString: 1 },
}
});
Expand Down Expand Up @@ -2192,6 +2202,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1 },
name2: { bString: 1 },
name3: { cString: 1 },
Expand Down Expand Up @@ -2223,6 +2234,7 @@ describe('schemas', () => {
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name3: { cString: 1 },
name4: { dString: 1 },
}
Expand Down Expand Up @@ -2323,7 +2335,7 @@ describe('schemas', () => {
obj.set('subject', 'subject');
obj.set('comment', 'comment');
obj.save().then(() => {
return config.database.adapter.createIndex('TestObject', {subject: 'text', comment: 'text'});
return config.database.adapter.createIndex('TestObject', {subject: 'text', comment: 1});
}).then(() => {
return reconfigureServer({
appId: 'test',
Expand All @@ -2336,8 +2348,12 @@ describe('schemas', () => {
headers: masterKeyHeaders,
json: true,
}, (error, response, body) => {
console.log(body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log here should be removed

expect(body.indexes._id_).toBeDefined();
expect(body.indexes._id_._id).toEqual(1);
expect(body.indexes.subject_text_comment_text).toBeDefined();
expect(body.indexes.subject_text_comment_text.subject).toEqual('text');
expect(body.indexes.subject_text_comment_text.comment).toEqual('text');
done();
});
});
Expand Down
49 changes: 31 additions & 18 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ export class MongoStorageAdapter {
}));
}

setIndexes(className, submittedIndexes, existingIndexes = {}, fields) {
setIndexesWithSchemaFormat(className, submittedIndexes, existingIndexes = {}, fields) {
if (submittedIndexes === undefined) {
return Promise.resolve();
}
if (Object.keys(existingIndexes).length === 0) {
existingIndexes = { _id_: { _id: 1} };
}
const deletePromises = [];
const insertedIndexes = [];
Object.keys(submittedIndexes).forEach(name => {
Expand Down Expand Up @@ -213,11 +216,34 @@ export class MongoStorageAdapter {
}));
}

setIndexesFromMongo(className) {
return this.getIndexes(className).then((indexes) => {
indexes = indexes.reduce((obj, index) => {
if (index.key._fts) {
delete index.key._fts;
delete index.key._ftsx;
for (const field in index.weights) {
index.key[field] = 'text';
}
}
obj[index.name] = index.key;
return obj;
}, {});
return this._schemaCollection()
.then(schemaCollection => schemaCollection.updateSchema(className, {
$set: { _metadata: { indexes: indexes } }
}));
}).catch(() => {
// Ignore if collection not found
return Promise.resolve();
});
}

createClass(className, schema) {
schema = convertParseSchemaToMongoSchema(schema);
const mongoObject = mongoSchemaFromFieldsAndClassNameAndCLP(schema.fields, className, schema.classLevelPermissions, schema.indexes);
mongoObject._id = className;
return this.setIndexes(className, schema.indexes, {}, schema.fields)
return this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields)
.then(() => this._schemaCollection())
.then(schemaCollection => schemaCollection._collection.insertOne(mongoObject))
.then(result => MongoSchemaCollection._TESTmongoSchemaToParseSchema(result.ops[0]))
Expand Down Expand Up @@ -541,11 +567,10 @@ export class MongoStorageAdapter {
const textIndex = {
[indexName]: { [fieldName]: 'text' }
};
return this.setIndexes(className, textIndex, existingIndexes, schema.fields)
return this.setIndexesWithSchemaFormat(className, textIndex, existingIndexes, schema.fields)
.catch((error) => {
if (error.code === 85) { // Index exist with different options
return this.getIndexes(className)
.then((indexes) => this.setIndexes(className, {}, indexes, schema.fields));
return this.setIndexesFromMongo(className);
}
throw error;
});
Expand All @@ -572,19 +597,7 @@ export class MongoStorageAdapter {
return this.getAllClasses()
.then((classes) => {
const promises = classes.map((schema) => {
return this.getIndexes(schema.className).then((indexes) => {
indexes = indexes.reduce((obj, index) => {
obj[index.name] = index.key;
return obj;
}, {});
return this._schemaCollection()
.then(schemaCollection => schemaCollection.updateSchema(schema.className, {
$set: { _metadata: { indexes: indexes } }
}));
}).catch(() => {
// Ignore if collection not found
return Promise.resolve();
});
return this.setIndexesFromMongo(schema.className);
});
return Promise.all(promises);
});
Expand Down
8 changes: 6 additions & 2 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,14 @@ export class PostgresStorageAdapter {
});
}

setIndexes(className, submittedIndexes, existingIndexes = {}, fields, conn) {
setIndexesWithSchemaFormat(className, submittedIndexes, existingIndexes = {}, fields, conn) {
conn = conn || this._client;
if (submittedIndexes === undefined) {
return Promise.resolve();
}
if (Object.keys(existingIndexes).length === 0) {
existingIndexes = { _id_: { _id: 1} };
}
const deletedIndexes = [];
const insertedIndexes = [];
Object.keys(submittedIndexes).forEach(name => {
Expand Down Expand Up @@ -658,7 +661,7 @@ export class PostgresStorageAdapter {
return this._client.tx(t => {
const q1 = this.createTable(className, schema, t);
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
const q3 = this.setIndexes(className, schema.indexes, {}, schema.fields, t);
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);

return t.batch([q1, q2, q3]);
})
Expand Down Expand Up @@ -1251,6 +1254,7 @@ export class PostgresStorageAdapter {
}

find(className, schema, query, { skip, limit, sort, keys }) {
console.log('find');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

debug('find', className, query, {skip, limit, sort, keys });
const hasLimit = limit !== undefined;
const hasSkip = skip !== undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/SchemaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export default class SchemaController {
return Promise.all(promises);
})
.then(() => this.setPermissions(className, classLevelPermissions, newSchema))
.then(() => this._dbAdapter.setIndexes(className, indexes, schema.indexes, newSchema))
.then(() => this._dbAdapter.setIndexesWithSchemaFormat(className, indexes, schema.indexes, newSchema))
.then(() => this.reloadData({ clearCache: true }))
//TODO: Move this logic into the database adapter
.then(() => {
Expand Down