You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const MTree = require('./m-tree/mtree');
|
|
const dimensions = 2;
|
|
|
|
const Generator = require('./data/generator');
|
|
|
|
const app = express();
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
function euclideanDistance(a, b) {
|
|
return Math.sqrt(a.reduce((acc, val, i) => acc + (val - b[i]) ** 2, 0));
|
|
}
|
|
const mtree = new MTree(dimensions, 10, euclideanDistance);
|
|
|
|
const generator = new Generator(dimensions);
|
|
const points = generator.generateMany(1000);
|
|
let i = 0;
|
|
points.forEach(point => { mtree.insert(point); i++; console.log(i); });
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(__dirname + '/index.html');
|
|
});
|
|
|
|
app.get('/tree', (req, res) => {
|
|
res.send(JSON.parse(JSON.stringify(mtree, (key, value) => {
|
|
if (key === 'parent') return value && value.id;
|
|
return value;
|
|
})));
|
|
});
|
|
|
|
app.post('/insert', (req, res) => {
|
|
const point = req.body.point;
|
|
if (!point || !Array.isArray(point)) {
|
|
return res.status(400).send('Invalid point');
|
|
}
|
|
mtree.insert(point);
|
|
res.send('Point inserted');
|
|
});
|
|
|
|
app.get('/rangeQuery', (req, res) => {
|
|
const { queryPoint, radius } = req.query;
|
|
if (!queryPoint || !radius) {
|
|
return res.status(400).send('Invalid query parameters');
|
|
}
|
|
const result = mtree.rangeQuery(JSON.parse(queryPoint), parseFloat(radius));
|
|
res.send(result);
|
|
});
|
|
|
|
app.get('/kNNQuery', (req, res) => {
|
|
const { queryPoint, k } = req.query;
|
|
if (!queryPoint || !k) {
|
|
return res.status(400).send('Invalid query parameters');
|
|
}
|
|
const result = mtree.kNNQuery(JSON.parse(queryPoint), parseInt(k, 10));
|
|
res.send(result);
|
|
});
|
|
|
|
app.listen(3000, () => {
|
|
console.log('MTree API is running on port 3000');
|
|
//console.log(mtree);
|
|
});
|
|
|
|
app.post('/recreate', (req, res) => {
|
|
const { dimensions } = req.body;
|
|
if (!dimensions || typeof dimensions !== 'number') {
|
|
return res.status(400).send('Invalid dimensions');
|
|
}
|
|
mtree = new MTree(dimensions, 10);
|
|
points = generator.generateMany(100);
|
|
points.forEach(point => mtree.insert(point));
|
|
res.send('MTree recreated');
|
|
});
|