Skip to content
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

Adding a disconnectAsync method #141

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1,622 changes: 1,032 additions & 590 deletions dist/browser-connector.js

Large diffs are not rendered by default.

1,609 changes: 1,025 additions & 584 deletions dist/node-connector.js

Large diffs are not rendered by default.

138 changes: 135 additions & 3 deletions src/mapd-con-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ class MapdCon {
headers: { Connection: "close" },
https: protocol === "https:"
})
connection.on("error", console.error) // eslint-disable-line no-console
connection.on("error", (err) => {
throw new Error(
`Thrift connection error - ${err.message}\n${JSON.stringify(err, null, 2)}`
)
})
client = thriftWrapper.createClient(MapDThrift, connection)
resetThriftClientOnArgumentErrorForMethods(this, client, [
"connect",
Expand Down Expand Up @@ -323,6 +327,17 @@ class MapdCon {
return this
}

disconnectAsync = () =>
new Promise((resolve, reject) => {
this.disconnect((error, con) => {
if (error) {
reject(error)
} else {
resolve(con)
}
})
})

removeConnection(conId) {
if (conId < 0 || conId >= this.numConnections) {
const err = {
Expand Down Expand Up @@ -1564,7 +1579,7 @@ class MapdCon {
const curNonce = (this._nonce++).toString()

if (!callback) {
return this.processPixelResults(
return this.processHitTestResults(
undefined, // eslint-disable-line no-undefined
this._client[this._lastRenderCon].get_result_row_for_pixel(
this._sessionId[this._lastRenderCon],
Expand All @@ -1586,7 +1601,7 @@ class MapdCon {
columnFormat,
pixelRadius,
curNonce,
this.processPixelResults.bind(this, callback)
this.processHitTestResults.bind(this, callback)
)

return curNonce
Expand Down Expand Up @@ -1655,6 +1670,69 @@ class MapdCon {
}
}

/**
* Formats results from getResultRowForPixel calls.
*
* @param {Function} callback A callback function with the signature `(err, result) => result`.
* @param {Object} error An error if thrown; otherwise null.
* @param {Array|Object} results Unformatted results of getResultRowForPixel call.
*
* @returns {Object} An object with formatted hit-test results.
*/
processHitTestResults(callback, error, results) {
if (error) {
if (callback) {
return callback(error, results)
} else {
throw new Error(
`Unable to process getResultRowForPixel() results: ${error}`
)
}
}

const processResultsOptions = {
isImage: false,
eliminateNullRows: false,
query: "getResultRowForPixel request",
queryId: -2
}
results.row_set = this.processResults(processResultsOptions, results)

if (typeof results.pixel.x.valueOf === "function") {
// TPixels x/y values are I64, which gets converted to a special thrift Int64 representation,
// so need to convert to real javascript numbers via the 'valueOf' member function.
results.pixel.x = results.pixel.x.valueOf()
results.pixel.y = results.pixel.y.valueOf()
}

// eslint-disable-next-line no-console
console.assert(results.table_id.length === results.row_id.length)

if (
results.table_id.length &&
typeof results.table_id[0].valueOf === "function"
) {
// eslint-disable-next-line no-console
console.assert(
typeof results.table_id[0].valueOf === typeof results.row_id[0].valueOf
)
for (let i = 0; i < results.table_id.length; ++i) {
results.table_id[i] = results.table_id[i].valueOf()
results.row_id[i] = results.row_id[i].valueOf()
}
}

// For backwards compatibility, we need to make the returned results an array.
// Previously getResultRowForPixel results were passed thru the processPixelResults()
// function which converts the rsults into for a long time
// an array, so clients expect the results to be an array.
if (callback) {
return callback(error, [results])
} else {
return [results]
}
}

// ** Configuration methods **

/**
Expand Down Expand Up @@ -1934,6 +2012,60 @@ class MapdCon {
})
}

/**
* Clears cpu memory server-side.
* @param {Function} callback A callback that takes (`err, results`). When successful,
* err is null and results is undefined as the method returns nothing.
* @returns {undefined} This method returns nothing and instead relies on the callback
*/
clearCpuMemory(callback) {
this._client[0].clear_cpu_memory(this._sessionId[0], callback)
}

/**
* Clears cpu memory server side.
* @returns {Promise.<undefined>} Undefined (when successful) or Error.
*/
clearCpuMemoryAsync = this.handleErrors(
() =>
new Promise((resolve, reject) => {
this.clearCpuMemory((err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
)

/**
* Clears gpu memory server-side.
* @param {Function} callback A callback that takes (`err, results`). When successful,
* err is null and results is undefined as the method returns nothing.
* @returns {undefined} This method returns nothing and instead relies on the callback
*/
clearGpuMemory(callback) {
this._client[0].clear_gpu_memory(this._sessionId[0], callback)
}

/**
* Clears gpu memory server side.
* @returns {Promise.<undefined>} Undefined (when successful) or Error.
*/
clearGpuMemoryAsync = this.handleErrors(
() =>
new Promise((resolve, reject) => {
this.clearGpuMemory((err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
)

isTimeoutError(result) {
return (
result instanceof TOmniSciException &&
Expand Down
Loading