Skip to content

Commit

Permalink
even faster
Browse files Browse the repository at this point in the history
  • Loading branch information
lroal committed Jul 11, 2024
1 parent 442d1f9 commit d48fdc9
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 137 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orange-orm",
"version": "4.4.0-beta.0",
"version": "4.4.0-beta.1",
"main": "./src/index.js",
"browser": "./src/client/index.mjs",
"bin": {
Expand Down
220 changes: 131 additions & 89 deletions src/getManyDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const negotiateRawSqlFilter = require('./table/column/negotiateRawSqlFilter');
const strategyToSpan = require('./table/strategyToSpan');
const executeQueries = require('./table/executeQueries');

async function getManyDto(table, filter, strategy, spanFromParent) {
async function getManyDto(table, filter, strategy, spanFromParent, updateParent) {
filter = negotiateRawSqlFilter(filter, table);
if (strategy && strategy.where) {
let arg = typeof strategy.where === 'function' ? strategy.where(table) : strategy.where;
Expand All @@ -16,95 +16,122 @@ async function getManyDto(table, filter, strategy, spanFromParent) {

const query = newQuery(table, filter, span, alias);
const res = await executeQueries([query]);
return decode(strategy, span, await res[0]);
return decode(strategy, span, await res[0], undefined, updateParent);
}

// function newCreateRow(span) {
// let columnsMap = span.columns;
// const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
// const protoRow = createProto(columns, span);
// const manyNames = [];

// const c = {};
// c.visitJoin = () => { };
// c.visitOne = () => { };
// c.visitMany = function(leg) {
// manyNames.push(leg.name);
// };

// span.legs.forEach(onEachLeg);
// return createRow;

// function onEachLeg(leg) {
// leg.accept(c);
// }

// function createRow() {
// const obj = Object.create(protoRow);
// for (let i = 0; i < manyNames.length; i++) {
// obj[manyNames[i]] = [];
// }
// return obj;
// }
// }


function newCreateRow(span) {
const columnsMap = span.columns;
let columnsMap = span.columns;
const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
const ProtoRow = createProto(columns, span);
const protoRow = createProto(columns, span);
const manyNames = [];

const c = {
visitJoin: () => { },
visitOne: () => { },
visitMany: function(leg) {
manyNames.push(leg.name);
}
const c = {};
c.visitJoin = () => { };
c.visitOne = () => { };
c.visitMany = function(leg) {
manyNames.push(leg.name);
};

span.legs.forEach(leg => leg.accept(c));

span.legs.forEach(onEachLeg);
return createRow;

function onEachLeg(leg) {
leg.accept(c);
}

function createRow() {
const obj = new ProtoRow();
manyNames.forEach(name => {
obj[name] = [];
});
const obj = Object.create(protoRow);
for (let i = 0; i < manyNames.length; i++) {
obj[manyNames[i]] = [];
}
return obj;
}
}

function createProto(columns, span) {
function ProtoRow() {
columns.forEach(column => {
this[column.alias] = null;
});
let obj = {};
for (let i = 0; i < columns.length; i++) {
obj[columns[i].alias] = null;
}
for (let key in span.aggregates) {
obj[key] = null;
}
const c = {};

for (const key in span.aggregates) {
this[key] = null;
}
c.visitJoin = function(leg) {
obj[leg.name] = null;
};
c.visitOne = c.visitJoin;
c.visitMany = function(leg) {
obj[leg.name] = null;
};

const c = {
visitJoin: (leg) => {
this[leg.name] = null;
},
visitOne: (leg) => {
this[leg.name] = null;
},
visitMany: (leg) => {
this[leg.name] = null;
}
};
span.legs.forEach(onEachLeg);

span.legs.forEach(leg => leg.accept(c));
function onEachLeg(leg) {
leg.accept(c);
}

return ProtoRow;
return obj;
}


// function newCreateRow(span) {
// const columnsMap = span.columns;
// const columns = span.table._columns.filter(column => !columnsMap || columnsMap.get(column));
// const ProtoRow = createProto(columns, span);
// const manyNames = [];

// const c = {
// visitJoin: () => { },
// visitOne: () => { },
// visitMany: function(leg) {
// manyNames.push(leg.name);
// }
// };

// span.legs.forEach(leg => leg.accept(c));

// return createRow;

// function createRow() {
// const obj = new ProtoRow();
// manyNames.forEach(name => {
// obj[name] = [];
// });
// return obj;
// }
// }

// function createProto(columns, span) {
// function ProtoRow() {
// columns.forEach(column => {
// this[column.alias] = null;
// });

// for (const key in span.aggregates) {
// this[key] = null;
// }

// const c = {
// visitJoin: (leg) => {
// this[leg.name] = null;
// },
// visitOne: (leg) => {
// this[leg.name] = null;
// },
// visitMany: (leg) => {
// this[leg.name] = null;
// }
// };

// span.legs.forEach(leg => leg.accept(c));
// }

// return ProtoRow;
// }


function hasManyRelations(span) {
let result;
const c = {};
Expand All @@ -122,7 +149,7 @@ function hasManyRelations(span) {
}
}

async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : [], parentRows, parentProp) {
async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys(rows[0]) : [], updateParent) {
const table = span.table;
let columnsMap = span.columns;
const columns = table._columns.filter(column => !columnsMap || columnsMap.get(column));
Expand Down Expand Up @@ -162,8 +189,10 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys
}

outRows[i] = outRow;
if (parentRows)
parentRows[i][parentProp] = outRow;
// if (parentRows)
// parentRows[i][parentProp] = outRow;
if (updateParent)
updateParent(outRow, i);
if (shouldCreateMap) {
fkIds[i] = getIds(outRow);
addToMap(rowsMap, fkIds[i], outRow);
Expand All @@ -178,8 +207,12 @@ async function decode(strategy, span, rows, keys = rows.length > 0 ? Object.keys

const all = [];

if (shouldCreateMap)
all.push(decodeManyRelations(strategy, span).then(() => decodeRelations2(strategy, span, rows, outRows, keys)));
if (shouldCreateMap) {
all.push(decodeManyRelations(strategy, span));
all.push(decodeRelations2(strategy, span, rows, outRows, keys));
}
// decodeRelations2(strategy, span, rows, outRows, keys);
// }
else
all.push(decodeRelations2(strategy, span, rows, outRows, keys));

Expand Down Expand Up @@ -222,13 +255,22 @@ async function decodeManyRelations(strategy, span) {
const filter = createOneFilter(relation, span._ids);
const extractKey = createExtractKey(leg);
const extractFromMap = createExtractFromMap(rowsMap, table._primaryColumns);
const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
for (let i = 0; i < subRows.length; i++) {
const key = extractKey(subRows[i]);
const parentRow = extractFromMap(key);
parentRow[name].push(subRows[i]);
}
});

const p = getManyDto(relation.childTable, filter, strategy[name], leg.span, updateParent);
// .then(subRows => {
// for (let i = 0; i < subRows.length; i++) {
// const key = extractKey(subRows[i]);
// const parentRow = extractFromMap(key);
// parentRow[name].push(subRows[i]);
// }
// });

function updateParent(subRow) {
const key = extractKey(subRow);
const parentRow = extractFromMap(key);
parentRow[name].push(subRow);
}

promises.push(p);
};

Expand Down Expand Up @@ -257,28 +299,28 @@ async function decodeManyRelations(strategy, span) {

await Promise.all(promises);
}

async function decodeRelations2(strategy, span, rawRows, resultRows, keys) {
const promises = [];
const c = {};
c.visitJoin = function(leg) {
const name = leg.name;
const p = decode(strategy[name], leg.span, rawRows, keys, resultRows, name);
promises.push(p);
return decode(strategy[name], leg.span, rawRows, keys, updateParent);

function updateParent(subRow, i) {
resultRows[i][name] = subRow;

}
};

c.visitOne = c.visitJoin;

c.visitMany = () => { };


span.legs.forEach(onEachLeg);

function onEachLeg(leg) {
leg.accept(c);
async function processLegsSequentially(legs) {
for (const leg of legs.toArray()) {
await leg.accept(c);
}
}

await Promise.all(promises);
await processLegsSequentially(span.legs);
}

function createOneFilter(relation, ids) {
Expand Down
3 changes: 0 additions & 3 deletions src/table/column/binary/newEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ function _new(_column) {
};

encode.direct = function(value) {
value = purify(value);
if (value === null)
return null;
return Buffer.from(value, 'base64');
};

Expand Down
6 changes: 0 additions & 6 deletions src/table/column/boolean/newEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ function _new(column) {
};

encode.direct = function(value) {
value = purify(value);
if (value === null) {
if (column.dbNull === null)
return null;
return column.dbNull ;
}
var encodeCore = getSessionSingleton('encodeBoolean');

return encodeCore(value);
Expand Down
6 changes: 0 additions & 6 deletions src/table/column/date/newEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ function _new(column) {
};

encode.direct = function(value) {
value = purify(value);
if (value == null) {
if (column.dbNull === null)
null;
return column.dbNull;
}
var encodeCore = getSessionSingleton('encodeDate') || encodeDate;
return encodeCore(value);
};
Expand Down
6 changes: 0 additions & 6 deletions src/table/column/dateWithTimeZone/newEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ function _new(column) {
};

encode.direct = function(value) {
value = purify(value);
if (value == null) {
if (column.dbNull === null)
return null;
return column.dbNull;
}
return encodeDate(value);
};

Expand Down
8 changes: 1 addition & 7 deletions src/table/column/guid/newEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ function _new(column) {
return '\'' + value + '\'';
};

encode.direct = function(candidate) {
var value = purify(candidate);
if (value == null) {
if (column.dbNull === null)
return null;
return column.dbNull;
}
encode.direct = function(value) {
return value ;
};

Expand Down
8 changes: 1 addition & 7 deletions src/table/column/json/newEncode.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ function _new(column) {
return value;
};

encode.direct = function(candidate) {
var value = purify(candidate);
if (value == null) {
if(column.dbNull === null)
return null;
return column.dbNull;
}
encode.direct = function(value) {
var encodeCore = getSessionSingleton('encodeJSON');

if (encodeCore) {
Expand Down
Loading

0 comments on commit d48fdc9

Please sign in to comment.