|
| 1 | +// Copyright (c) 2024, Oracle and/or its affiliates. |
| 2 | + |
| 3 | +//----------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// This software is dual-licensed to you under the Universal Permissive License |
| 6 | +// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 7 | +// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 8 | +// either license. |
| 9 | +// |
| 10 | +// If you elect to accept the software under the Apache License, Version 2.0, |
| 11 | +// the following applies: |
| 12 | +// |
| 13 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | +// you may not use this file except in compliance with the License. |
| 15 | +// You may obtain a copy of the License at |
| 16 | +// |
| 17 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | +// |
| 19 | +// Unless required by applicable law or agreed to in writing, software |
| 20 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | +// See the License for the specific language governing permissions and |
| 23 | +// limitations under the License. |
| 24 | +// |
| 25 | +// Node file defining the StatementCache class used to manage cached statements |
| 26 | +//----------------------------------------------------------------------------- |
| 27 | + |
| 28 | +const errors = require("../errors.js"); |
| 29 | +const { Statement } = require("./statement"); |
| 30 | + |
| 31 | +class StatementCache { |
| 32 | + constructor(maxSize) { |
| 33 | + this._cachedStatements = new Map(); |
| 34 | + this._maxSize = maxSize; |
| 35 | + this._cursorsToClose = new Set(); |
| 36 | + this._openCursors = new Set(); |
| 37 | + } |
| 38 | + |
| 39 | + //--------------------------------------------------------------------------- |
| 40 | + // _addCursorToClose() |
| 41 | + // |
| 42 | + // Add the statement's cursor to the list of cursors that need to be closed. |
| 43 | + //--------------------------------------------------------------------------- |
| 44 | + _addCursorToClose(statement) { |
| 45 | + if (this._cursorsToClose.has(statement.cursorId)) { |
| 46 | + const reason = `attempt to close cursor ${statement.cursorId} twice`; |
| 47 | + errors.throwErr(errors.ERR_INTERNAL, reason); |
| 48 | + } |
| 49 | + if (statement.cursorId != 0) { |
| 50 | + this._cursorsToClose.add(statement.cursorId); |
| 51 | + } |
| 52 | + this._openCursors.delete(statement); |
| 53 | + } |
| 54 | + |
| 55 | + //--------------------------------------------------------------------------- |
| 56 | + // _adjustCache() |
| 57 | + // Adjust the cache so that no more than the maximum number of statements |
| 58 | + // are cached by removing least recently used statements |
| 59 | + //--------------------------------------------------------------------------- |
| 60 | + _adjustCache() { |
| 61 | + while (this._cachedStatements.size > this._maxSize) { |
| 62 | + const sql = this._cachedStatements.keys().next().value; |
| 63 | + const stmt = this._cachedStatements.get(sql); |
| 64 | + this._cachedStatements.delete(sql); |
| 65 | + if (stmt.inUse) { |
| 66 | + stmt.returnToCache = false; |
| 67 | + } else if (stmt.cursorId !== 0) { |
| 68 | + this._addCursorToClose(stmt); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + //--------------------------------------------------------------------------- |
| 74 | + //clearOpenCursors() { |
| 75 | + // Clears the list of open cursors and removes the list of cursors that |
| 76 | + // need to be closed. This is required when a DRCP session change has |
| 77 | + // taken place as the cursor ID values are invalidated. |
| 78 | + //--------------------------------------------------------------------------- |
| 79 | + clearOpenCursors() { |
| 80 | + const newOpenCursors = new Set(); |
| 81 | + for (const stmt of this._openCursors) { |
| 82 | + stmt.cursorId = 0; |
| 83 | + if (stmt.inUse) { |
| 84 | + newOpenCursors.add(stmt); |
| 85 | + } |
| 86 | + } |
| 87 | + this._openCursors = newOpenCursors; |
| 88 | + } |
| 89 | + |
| 90 | + //--------------------------------------------------------------------------- |
| 91 | + // get_statement() |
| 92 | + // Get a statement from the statement cache, or prepare a new statement |
| 93 | + // for use. If a statement is already in use or the statement is not |
| 94 | + // supposed to be cached, a copy will be made (and not returned to the |
| 95 | + // cache). |
| 96 | + //--------------------------------------------------------------------------- |
| 97 | + getStatement(sql, cacheStatement = false) { |
| 98 | + let stmt = null; |
| 99 | + if (sql) { |
| 100 | + stmt = this._cachedStatements.get(sql); |
| 101 | + if (!cacheStatement) { |
| 102 | + this._openCursors.add(stmt); |
| 103 | + this._cachedStatements.delete(sql); |
| 104 | + } |
| 105 | + } |
| 106 | + if (!stmt) { |
| 107 | + stmt = new Statement(); |
| 108 | + if (sql) { |
| 109 | + stmt._prepare(sql); |
| 110 | + } |
| 111 | + if (cacheStatement && !stmt.isDdl && this._maxSize > 0) { |
| 112 | + stmt.returnToCache = true; |
| 113 | + this._cachedStatements.set(sql, stmt); |
| 114 | + this._adjustCache(); |
| 115 | + } |
| 116 | + this._openCursors.add(stmt); |
| 117 | + } else if (stmt.inUse || !cacheStatement) { |
| 118 | + stmt = stmt._copy(); |
| 119 | + this._openCursors.add(stmt); |
| 120 | + } else { |
| 121 | + this._cachedStatements.delete(sql); |
| 122 | + this._cachedStatements.set(sql, stmt); |
| 123 | + } |
| 124 | + stmt.inUse = true; |
| 125 | + return stmt; |
| 126 | + } |
| 127 | + |
| 128 | + //--------------------------------------------------------------------------- |
| 129 | + // returnStatement() |
| 130 | + // Return the statement to the statement cache, if applicable. If the |
| 131 | + // statement must not be returned to the statement cache, add the cursor |
| 132 | + // id to the list of cursor ids to close on the next round trip to the |
| 133 | + // database. Clear all bind variables and fetch variables in order to |
| 134 | + // ensure that unnecessary references are not retained. |
| 135 | + //--------------------------------------------------------------------------- |
| 136 | + returnStatement(statement) { |
| 137 | + if (statement.bindInfoList) { |
| 138 | + statement.bindInfoList.forEach(bindInfo => { |
| 139 | + bindInfo.bindVar = null; |
| 140 | + }); |
| 141 | + } |
| 142 | + if (statement.queryVars) { |
| 143 | + statement.queryVars.forEach(queryVar => { |
| 144 | + queryVar.values.fill(null); |
| 145 | + }); |
| 146 | + } |
| 147 | + if (statement.returnToCache) { |
| 148 | + statement.inUse = false; |
| 149 | + } else if (statement.cursorId !== 0) { |
| 150 | + this._addCursorToClose(statement); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + //--------------------------------------------------------------------------- |
| 155 | + // writeCursorsToClose() |
| 156 | + // Write the list of cursors to close to the buffer. |
| 157 | + //--------------------------------------------------------------------------- |
| 158 | + writeCursorsToClose(buf) { |
| 159 | + buf.writeUB4(this._cursorsToClose.size); |
| 160 | + for (const cursorNum of this._cursorsToClose.keys()) { |
| 161 | + buf.writeUB4(cursorNum); |
| 162 | + } |
| 163 | + this._cursorsToClose.clear(); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +module.exports = StatementCache; |
0 commit comments