@comunica/mediator-join-coefficients-fixed 5.4.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2017–now Ruben Taelman, Joachim Van Herwegen
4
+ Comunica Association and Ghent University – imec, Belgium
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -36,10 +36,13 @@ class MediatorJoinCoefficientsFixed extends core_1.Mediator {
36
36
  // eslint-disable-next-line array-callback-return
37
37
  .map((result) => {
38
38
  if (result) {
39
- return result.value.iterations * this.cpuWeight +
39
+ const cost = result.value.iterations * this.cpuWeight +
40
40
  result.value.persistedItems * this.memoryWeight +
41
41
  result.value.blockingItems * this.timeWeight +
42
42
  result.value.requestTime * this.ioWeight;
43
+ // Costs become NaN when multiplying unknown (infinite) cardinalities with zero coefficients.
44
+ // Consider these the worst possible cost, so they never win over actors with a known cost.
45
+ return Number.isNaN(cost) ? Number.POSITIVE_INFINITY : cost;
43
46
  }
44
47
  });
45
48
  const maxCost = Math.max(...costs.filter(cost => cost !== undefined));
@@ -1 +1 @@
1
- {"version":3,"file":"MediatorJoinCoefficientsFixed.js","sourceRoot":"","sources":["MediatorJoinCoefficientsFixed.ts"],"names":[],"mappings":";;;AACA,+DAA+D;AAE/D,yCAAiF;AAIjF;;;GAGG;AACH,MAAa,6BAA8B,SAAQ,eAMlD;IACiB,SAAS,CAAS;IAClB,YAAY,CAAS;IACrB,UAAU,CAAS;IACnB,QAAQ,CAAS;IAEjC,YAAmB,IAAwC;QACzD,KAAK,CAAC,IAAI,CAAC,CAAC;QACZ,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAChC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;QACtC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;IAChC,CAAC;IAES,KAAK,CAAC,WAAW,CACzB,MAAsB,EACtB,WAMD;QAEC,sBAAsB;QACtB,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;QACvD,MAAM,OAAO,GAAG,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE;YAC/D,IAAI,UAAU,CAAC,QAAQ,EAAE,EAAE,CAAC;gBAC1B,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,cAAc,EAAE,CAAC,CAAC;gBACzC,iDAAiD;gBACjD,OAAO;YACT,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,UAAU,CAAC,GAAG,EAAE,EA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AAC,EAAE,EAAE,CAAC;oBAChD,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE;oBAC1E,KAAK;iBACN,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;gBAC3C,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;oBAC1D,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE;oBAC1E,MAAM,EAAE,KAAK;iBACd,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;aAC5C,CAAC,CAAC;QACL,CAAC;QAED,OAAO,IAAA,2BAAoB,EAAC,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAE,CAAC,QAAQ,CAAC,CAAC;IACtE,CAAC;CACF;AA3GD,sEA2GC","sourcesContent":["import type { ActorRdfJoin, IActionRdfJoin, IActorRdfJoinTestSideData } from '@comunica/bus-rdf-join';\nimport { KeysQueryOperation } from '@comunica/context-entries';\nimport type { IActorReply, IMediatorArgs, TestResult } from '@comunica/core';\nimport { passTestWithSideData, failTest, Actor, Mediator } from '@comunica/core';\nimport type { IMediatorTypeJoinCoefficients } from '@comunica/mediatortype-join-coefficients';\nimport type { IQueryOperationResult } from '@comunica/types';\n\n/**\n * A mediator that mediates over actors implementing the Join Coefficients mediator type and assigns fixed weights\n * to calculate an overall score and pick the actor with the lowest score.\n */\nexport class MediatorJoinCoefficientsFixed extends Mediator<\n ActorRdfJoin,\nIActionRdfJoin,\nIMediatorTypeJoinCoefficients,\nIQueryOperationResult,\nIActorRdfJoinTestSideData\n> {\n public readonly cpuWeight: number;\n public readonly memoryWeight: number;\n public readonly timeWeight: number;\n public readonly ioWeight: number;\n\n public constructor(args: IMediatorJoinCoefficientsFixedArgs) {\n super(args);\n this.cpuWeight = args.cpuWeight;\n this.memoryWeight = args.memoryWeight;\n this.timeWeight = args.timeWeight;\n this.ioWeight = args.ioWeight;\n }\n\n protected async mediateWith(\n action: IActionRdfJoin,\n testResults: IActorReply<\n ActorRdfJoin,\nIActionRdfJoin,\nIMediatorTypeJoinCoefficients,\nIQueryOperationResult,\nIActorRdfJoinTestSideData\n>[],\n ): Promise<TestResult<ActorRdfJoin, IActorRdfJoinTestSideData>> {\n // Obtain test results\n const errors: string[] = [];\n const promises = testResults.map(({ reply }) => reply);\n const results = (await Promise.all(promises)).map((testResult) => {\n if (testResult.isFailed()) {\n errors.push(testResult.getFailMessage());\n // eslint-disable-next-line array-callback-return\n return;\n }\n return { value: testResult.get(), sideData: testResult.getSideData() };\n });\n\n // Calculate costs\n let costs: (number | undefined)[] = results\n // eslint-disable-next-line array-callback-return\n .map((result) => {\n if (result) {\n return result.value.iterations * this.cpuWeight +\n result.value.persistedItems * this.memoryWeight +\n result.value.blockingItems * this.timeWeight +\n result.value.requestTime * this.ioWeight;\n }\n });\n const maxCost = Math.max(...costs.filter(cost => cost !== undefined));\n\n // If we have a limit indicator in the context,\n // increase cost of entries that have a number of iterations that is higher than the limit AND block items.\n // In these cases, join operators that produce results early on will be preferred.\n const limitIndicator: number | undefined = action.context.get(KeysQueryOperation.limitIndicator);\n if (limitIndicator) {\n costs = costs.map((cost, i) => {\n if (cost !== undefined && (<any> results[i]?.value).blockingItems > 0 &&\n\n (<any> results[i]?.value).iterations > limitIndicator) {\n return cost + maxCost;\n }\n return cost;\n });\n }\n\n // Determine index with lowest cost\n let minIndex = -1;\n let minValue = Number.POSITIVE_INFINITY;\n for (const [ i, cost ] of costs.entries()) {\n if (cost !== undefined && (minIndex === -1 || cost < minValue)) {\n minIndex = i;\n minValue = cost;\n }\n }\n\n // Reject if all actors rejected\n if (minIndex < 0) {\n return failTest(this.constructFailureMessage(action, errors));\n }\n\n // Return actor with lowest cost\n const bestActor = testResults[minIndex].actor;\n\n // Emit calculations in logger\n if (bestActor.includeInLogs) {\n Actor.getContextLogger(action.context)?.debug(`Determined physical join operator '${bestActor.logicalType}-${bestActor.physicalName}'`, {\n entries: action.entries.length,\n variables: await Promise.all(action.entries\n .map(async entry => (await entry.output.metadata()).variables.map(variable => variable.variable.value))),\n costs: Object.fromEntries(costs.map((coeff, i) => [\n `${testResults[i].actor.logicalType}-${testResults[i].actor.physicalName}`,\n coeff,\n ]).filter(entry => entry[1] !== undefined)),\n coefficients: Object.fromEntries(results.map((result, i) => [\n `${testResults[i].actor.logicalType}-${testResults[i].actor.physicalName}`,\n result?.value,\n ]).filter(entry => entry[1] !== undefined)),\n });\n }\n\n return passTestWithSideData(bestActor, results[minIndex]!.sideData);\n }\n}\n\nexport interface IMediatorJoinCoefficientsFixedArgs extends IMediatorArgs<\n ActorRdfJoin,\nIActionRdfJoin,\nIMediatorTypeJoinCoefficients,\nIQueryOperationResult,\nIActorRdfJoinTestSideData\n> {\n /**\n * Weight for the CPU cost\n */\n cpuWeight: number;\n /**\n * Weight for the memory cost\n */\n memoryWeight: number;\n /**\n * Weight for the execution time cost\n */\n timeWeight: number;\n /**\n * Weight for the I/O cost\n */\n ioWeight: number;\n}\n"]}
1
+ {"version":3,"file":"MediatorJoinCoefficientsFixed.js","sourceRoot":"","sources":["MediatorJoinCoefficientsFixed.ts"],"names":[],"mappings":";;;AACA,+DAA+D;AAE/D,yCAAiF;AAIjF;;;GAGG;AACH,MAAa,6BAA8B,SAAQ,eAMlD;IACiB,SAAS,CAAS;IAClB,YAAY,CAAS;IACrB,UAAU,CAAS;IACnB,QAAQ,CAAS;IAEjC,YAAmB,IAAwC;QACzD,KAAK,CAAC,IAAI,CAAC,CAAC;QACZ,IAAI,CAAC,SAAS,GAAG,IAA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AC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;oBAC1D,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE;oBAC1E,MAAM,EAAE,KAAK;iBACd,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;aAC5C,CAAC,CAAC;QACL,CAAC;QAED,OAAO,IAAA,2BAAoB,EAAC,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAE,CAAC,QAAQ,CAAC,CAAC;IACtE,CAAC;CACF;AA9GD,sEA8GC","sourcesContent":["import type { ActorRdfJoin, IActionRdfJoin, IActorRdfJoinTestSideData } from '@comunica/bus-rdf-join';\nimport { KeysQueryOperation } from '@comunica/context-entries';\nimport type { IActorReply, IMediatorArgs, TestResult } from '@comunica/core';\nimport { passTestWithSideData, failTest, Actor, Mediator } from '@comunica/core';\nimport type { IMediatorTypeJoinCoefficients } from '@comunica/mediatortype-join-coefficients';\nimport type { IQueryOperationResult } from '@comunica/types';\n\n/**\n * A mediator that mediates over actors implementing the Join Coefficients mediator type and assigns fixed weights\n * to calculate an overall score and pick the actor with the lowest score.\n */\nexport class MediatorJoinCoefficientsFixed extends Mediator<\n ActorRdfJoin,\nIActionRdfJoin,\nIMediatorTypeJoinCoefficients,\nIQueryOperationResult,\nIActorRdfJoinTestSideData\n> {\n public readonly cpuWeight: number;\n public readonly memoryWeight: number;\n public readonly timeWeight: number;\n public readonly ioWeight: number;\n\n public constructor(args: IMediatorJoinCoefficientsFixedArgs) {\n super(args);\n this.cpuWeight = args.cpuWeight;\n this.memoryWeight = args.memoryWeight;\n this.timeWeight = args.timeWeight;\n this.ioWeight = args.ioWeight;\n }\n\n protected async mediateWith(\n action: IActionRdfJoin,\n testResults: IActorReply<\n ActorRdfJoin,\nIActionRdfJoin,\nIMediatorTypeJoinCoefficients,\nIQueryOperationResult,\nIActorRdfJoinTestSideData\n>[],\n ): Promise<TestResult<ActorRdfJoin, IActorRdfJoinTestSideData>> {\n // Obtain test results\n const errors: string[] = [];\n const promises = testResults.map(({ reply }) => reply);\n const results = (await Promise.all(promises)).map((testResult) => {\n if (testResult.isFailed()) {\n errors.push(testResult.getFailMessage());\n // eslint-disable-next-line array-callback-return\n return;\n }\n return { value: testResult.get(), sideData: testResult.getSideData() };\n });\n\n // Calculate costs\n let costs: (number | undefined)[] = results\n // eslint-disable-next-line array-callback-return\n .map((result) => {\n if (result) {\n const cost = result.value.iterations * this.cpuWeight +\n result.value.persistedItems * this.memoryWeight +\n result.value.blockingItems * this.timeWeight +\n result.value.requestTime * this.ioWeight;\n // Costs become NaN when multiplying unknown (infinite) cardinalities with zero coefficients.\n // Consider these the worst possible cost, so they never win over actors with a known cost.\n return Number.isNaN(cost) ? Number.POSITIVE_INFINITY : cost;\n }\n });\n const maxCost = Math.max(...costs.filter(cost => cost !== undefined));\n\n // If we have a limit indicator in the context,\n // increase cost of entries that have a number of iterations that is higher than the limit AND block items.\n // In these cases, join operators that produce results early on will be preferred.\n const limitIndicator: number | undefined = action.context.get(KeysQueryOperation.limitIndicator);\n if (limitIndicator) {\n costs = costs.map((cost, i) => {\n if (cost !== undefined && (<any> results[i]?.value).blockingItems > 0 &&\n\n (<any> results[i]?.value).iterations > limitIndicator) {\n return cost + maxCost;\n }\n return cost;\n });\n }\n\n // Determine index with lowest cost\n let minIndex = -1;\n let minValue = Number.POSITIVE_INFINITY;\n for (const [ i, cost ] of costs.entries()) {\n if (cost !== undefined && (minIndex === -1 || cost < minValue)) {\n minIndex = i;\n minValue = cost;\n }\n }\n\n // Reject if all actors rejected\n if (minIndex < 0) {\n return failTest(this.constructFailureMessage(action, errors));\n }\n\n // Return actor with lowest cost\n const bestActor = testResults[minIndex].actor;\n\n // Emit calculations in logger\n if (bestActor.includeInLogs) {\n Actor.getContextLogger(action.context)?.debug(`Determined physical join operator '${bestActor.logicalType}-${bestActor.physicalName}'`, {\n entries: action.entries.length,\n variables: await Promise.all(action.entries\n .map(async entry => (await entry.output.metadata()).variables.map(variable => variable.variable.value))),\n costs: Object.fromEntries(costs.map((coeff, i) => [\n `${testResults[i].actor.logicalType}-${testResults[i].actor.physicalName}`,\n coeff,\n ]).filter(entry => entry[1] !== undefined)),\n coefficients: Object.fromEntries(results.map((result, i) => [\n `${testResults[i].actor.logicalType}-${testResults[i].actor.physicalName}`,\n result?.value,\n ]).filter(entry => entry[1] !== undefined)),\n });\n }\n\n return passTestWithSideData(bestActor, results[minIndex]!.sideData);\n }\n}\n\nexport interface IMediatorJoinCoefficientsFixedArgs extends IMediatorArgs<\n ActorRdfJoin,\nIActionRdfJoin,\nIMediatorTypeJoinCoefficients,\nIQueryOperationResult,\nIActorRdfJoinTestSideData\n> {\n /**\n * Weight for the CPU cost\n */\n cpuWeight: number;\n /**\n * Weight for the memory cost\n */\n memoryWeight: number;\n /**\n * Weight for the execution time cost\n */\n timeWeight: number;\n /**\n * Weight for the I/O cost\n */\n ioWeight: number;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comunica/mediator-join-coefficients-fixed",
3
- "version": "5.4.0",
3
+ "version": "5.5.0",
4
4
  "description": "A join-coefficients-fixed mediator",
5
5
  "lsd:module": true,
6
6
  "license": "MIT",
@@ -39,11 +39,11 @@
39
39
  "build:components": "componentsjs-generator"
40
40
  },
41
41
  "dependencies": {
42
- "@comunica/bus-rdf-join": "^5.4.0",
43
- "@comunica/context-entries": "^5.4.0",
44
- "@comunica/core": "^5.4.0",
45
- "@comunica/mediatortype-join-coefficients": "^5.4.0",
46
- "@comunica/types": "^5.4.0"
42
+ "@comunica/bus-rdf-join": "^5.5.0",
43
+ "@comunica/context-entries": "^5.5.0",
44
+ "@comunica/core": "^5.5.0",
45
+ "@comunica/mediatortype-join-coefficients": "^5.5.0",
46
+ "@comunica/types": "^5.5.0"
47
47
  },
48
- "gitHead": "8a0a10913cb705adda45997210ebbe5502274a60"
48
+ "gitHead": "3a258e018a9c5f0b03f531b30ee1cdc659b5b7c9"
49
49
  }