Skip to content

Commit

Permalink
Merge pull request #221 from hknustwmf/master
Browse files Browse the repository at this point in the history
Removed  eslint-mocha and eslint-config-node-services.
  • Loading branch information
Pchelolo committed Jan 24, 2019
2 parents 10bd8a9 + 428e6fb commit ece11c8
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 257 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
@@ -0,0 +1,3 @@
test/
coverage/
scripts/
29 changes: 28 additions & 1 deletion .eslintrc.yml
@@ -1 +1,28 @@
extends: 'eslint-config-node-services'
extends:
- 'wikimedia/server'

plugins:
- json
- jsdoc

rules:
array-bracket-spacing:
- off
camelcase:
- error
- properties: never
computed-property-spacing:
- error
- never
indent:
- error
- 4
- SwitchCase: 1
MemberExpression: 'off'
no-multi-spaces:
- off
no-underscore-dangle:
- off
space-in-parens:
- error
- never
4 changes: 0 additions & 4 deletions .jshintignore

This file was deleted.

28 changes: 13 additions & 15 deletions index.js
@@ -1,4 +1,4 @@
"use strict";
'use strict';

/*
* Cassandra-backed table storage service
Expand Down Expand Up @@ -80,11 +80,11 @@ class RBCassandra {
}
const domain = req.params.domain;
return this.store.get(domain, req.body)
.then(res => ({
.then((res) => ({
status: res.items.length ? 200 : 404,
body: res
}))
.catch(e => ({
.catch((e) => ({
status: 500,

body: {
Expand All @@ -95,7 +95,7 @@ class RBCassandra {
req: {
uri: req.uri,
headers: req.headers,
body: req.body && JSON.stringify(req.body).slice(0,200)
body: req.body && JSON.stringify(req.body).slice(0, 200)
}
}
}));
Expand All @@ -110,7 +110,7 @@ class RBCassandra {
// created
status: 201
})
.catch(e => ({
.catch((e) => ({
status: 500,

body: {
Expand All @@ -121,7 +121,7 @@ class RBCassandra {
req: {
uri: req.uri,
headers: req.headers,
body: req.body && JSON.stringify(req.body).slice(0,200)
body: req.body && JSON.stringify(req.body).slice(0, 200)
}
}
}));
Expand All @@ -135,7 +135,7 @@ class RBCassandra {
// deleted
status: 204
})
.catch(e => ({
.catch((e) => ({
status: 500,

body: {
Expand All @@ -146,7 +146,7 @@ class RBCassandra {
req: {
uri: req.uri,
headers: req.headers,
body: req.body && JSON.stringify(req.body).slice(0,200)
body: req.body && JSON.stringify(req.body).slice(0, 200)
}
}
}));
Expand All @@ -159,7 +159,7 @@ class RBCassandra {
// done
status: 204
})
.catch(e => ({
.catch((e) => ({
status: 500,

body: {
Expand All @@ -170,7 +170,7 @@ class RBCassandra {
req: {
uri: req.uri,
headers: req.headers,
body: req.body && JSON.stringify(req.body).slice(0,200)
body: req.body && JSON.stringify(req.body).slice(0, 200)
}
}
}));
Expand All @@ -179,11 +179,11 @@ class RBCassandra {
getTableSchema(rb, req) {
const domain = req.params.domain;
return this.store.getTableSchema(domain, req.params.table)
.then(res => ({
.then((res) => ({
status: 200,
body: res.schema
}))
.catch(e => ({
.catch((e) => ({
status: 500,
body: {
type: 'schema_query_error',
Expand All @@ -193,7 +193,7 @@ class RBCassandra {
req: {
uri: req.uri,
headers: req.headers,
body: req.body && JSON.stringify(req.body).slice(0,200)
body: req.body && JSON.stringify(req.body).slice(0, 200)
}
}
}));
Expand All @@ -215,7 +215,6 @@ class RBCassandra {
}
}


/**
* Factory
* @param {Object} options
Expand All @@ -228,4 +227,3 @@ function makeRBCassandra(options) {
}

module.exports = makeRBCassandra;

60 changes: 33 additions & 27 deletions lib/db.js
@@ -1,4 +1,4 @@
"use strict";
'use strict';

const P = require('bluebird');
const cass = require('cassandra-driver');
Expand Down Expand Up @@ -28,6 +28,8 @@ class InternalRequest {
/**
* Construct a new InternalRequest based on an existing one, optionally
* overriding existing properties.
* @param {Object} opts overriding settings
* @return {Object} new request
*/
extend(opts) {
const req = new InternalRequest(this);
Expand All @@ -43,8 +45,8 @@ class DB {
this.conf = options.conf;
this.log = options.log;

this.defaultConsistency = cass.types.consistencies[this.conf.defaultConsistency]
|| cass.types.consistencies.localOne;
this.defaultConsistency = cass.types.consistencies[this.conf.defaultConsistency] ||
cass.types.consistencies.localOne;

// cassandra client
this.client = client;
Expand All @@ -66,7 +68,7 @@ class DB {
table: 'meta',
attributes: {
key: 'string',
value: 'json',
value: 'json'
},
index: [
{ attribute: 'key', type: 'hash' }
Expand All @@ -85,6 +87,11 @@ class DB {
/**
* Set up internal request-related information and wrap it into an
* InternalRequest instance.
* @param {string} domain in dot notation
* @param {string} table logical table name
* @param {Object} query query object
* @param {Object} consistency consistency level
* @return {Object}
*/
_makeInternalRequest(domain, table, query, consistency) {
consistency = consistency || this.defaultConsistency;
Expand Down Expand Up @@ -145,8 +152,8 @@ class DB {
return req;
} else {
// Check if the meta column family exists
return this.client.execute('SELECT table_name FROM system_schema.tables '
+ 'WHERE keyspace_name=? and table_name=?', [req.keyspace, 'meta'])
return this.client.execute('SELECT table_name FROM system_schema.tables ' +
'WHERE keyspace_name=? and table_name=?', [req.keyspace, 'meta'])
.then((res) => {
if (res && res.rows.length === 0) {
// meta column family doesn't exist yet
Expand All @@ -165,12 +172,12 @@ class DB {
* chars from the requested name as far as possible, but fall back to a sha1
* if not possible. Also respect Cassandra's limit of 48 or fewer alphanum
* chars & first char being an alpha char.
* @param {string} domain in dot notation
* @param {string} table the logical table name
* @return {string} Valid Cassandra keyspace key
* @param {string} domain in dot notation
* @param {string} table the logical table name
* @return {string} Valid Cassandra keyspace key
*/
keyspaceName(domain, table) {
const cacheKey = JSON.stringify([domain,table]);
const cacheKey = JSON.stringify([domain, table]);
const cachedName = this.keyspaceNameCache[cacheKey];
if (cachedName) {
return cachedName;
Expand All @@ -187,8 +194,8 @@ class DB {

/**
* Finds the storage group for a given domain.
* @param {string} domain the domain's name
* @return {Object} the group object matching the domain
* @param {string} domain the domain's name
* @return {Object} the group object matching the domain
*/
_resolveStorageGroup(domain) {
let group = this.storageGroupsCache[domain];
Expand Down Expand Up @@ -242,8 +249,8 @@ class DB {
options = options || {};

if (!req.schema) {
throw new Error(`restbase-mod-table-cassandra: `
+ `No schema for ${req.keyspace}, table: ${req.columnfamily}`);
throw new Error('restbase-mod-table-cassandra: ' +
`No schema for ${req.keyspace}, table: ${req.columnfamily}`);
}

if (!req.schema.iKeyMap) {
Expand Down Expand Up @@ -397,11 +404,11 @@ class DB {
status: 400,
body: {
type: 'bad_request',
title: `The table already exists, and it cannot `
+ `be upgraded to the requested schema (${error}).`,
title: 'The table already exists, and it cannot ' +
`be upgraded to the requested schema (${error}).`,
keyspace: req.keyspace,
schema: newSchema,
stack: error.stack,
stack: error.stack
}
});
this.log('error/cassandra/table_update', newErr);
Expand Down Expand Up @@ -537,7 +544,6 @@ class DB {
cql += 'primary key (';
cql += `${[`(${hashBits.join(',')})`].concat(rangeBits).join(',')}))`;


let clusteringCQL;
if (orderBits.length) {
clusteringCQL = `clustering order by (${orderBits.join(',')})`;
Expand Down Expand Up @@ -566,21 +572,21 @@ class DB {

// Drop the native secondary indexes we used to create on the "_domain" column.
_dropDomainIndex(req) {
const cql = "select index_name from system.schema_columns where keyspace_name = ? "
+ " and columnfamily_name = ? and column_name = '_domain';";
const cql = 'select index_name from system.schema_columns where keyspace_name = ? ' +
" and columnfamily_name = ? and column_name = '_domain';";
return this.client.execute(cql, [req.keyspace, req.columnfamily], { prepare: true })
.then((res) => {
if (res.rows.length && res.rows[0].index_name) {
// drop the index
return this.client.execute(`drop index if exists `
+ `${cassID(req.keyspace)}.${cassID(res.rows[0].index_name)}`);
return this.client.execute('drop index if exists ' +
`${cassID(req.keyspace)}.${cassID(res.rows[0].index_name)}`);
}
});
}

_createKeyspace(req) {
const cql = `create keyspace if not exists ${cassID(req.keyspace)} `
+ `WITH REPLICATION = ${this._createReplicationOptionsCQL(req.query.options)}`;
const cql = `create keyspace if not exists ${cassID(req.keyspace)} ` +
`WITH REPLICATION = ${this._createReplicationOptionsCQL(req.query.options)}`;
return this.client.execute(cql, [],
{ consistency: req.consistency || this.defaultConsistency });
}
Expand Down Expand Up @@ -669,8 +675,8 @@ class DB {
*/
_setReplication(domain, table, options) {
const keyspace = this.keyspaceName(domain, table);
const cql = `ALTER KEYSPACE ${dbu.cassID(keyspace)} WITH `
+ `replication = ${this._createReplicationOptionsCQL(options)}`;
const cql = `ALTER KEYSPACE ${dbu.cassID(keyspace)} WITH ` +
`replication = ${this._createReplicationOptionsCQL(options)}`;
this.log('warn/cassandra/replication', {
message: `Updating replication for ${keyspace}`,
replicas: this._replicationPolicy(options),
Expand Down Expand Up @@ -701,7 +707,7 @@ class DB {
if (Object.keys(current).length !== Object.keys(expected).length) {
return false;
}
return Object.keys(current).every(a => current[a] === expected[a]);
return Object.keys(current).every((a) => current[a] === expected[a]);
};

return this._getReplication(domain, table)
Expand Down

0 comments on commit ece11c8

Please sign in to comment.