mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Refactored chunkStore to use linvodb3, broke mapping (ofc)
This commit is contained in:
1 parent
cd2d00d224
commit
1b405b8701
3 files changed
+62
-104
No files matched your search
+33
-82
@@ -3,6 +3,10 @@ var mkdirpSync = require("mkdirp").sync;
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var objectOps = require("./objectOps");
|
var objectOps = require("./objectOps");
|
||||||
|
|
||||||
|
var LinvoDB = require("linvodb3");
|
||||||
|
LinvoDB.dbPath = "./database/linvodb/";
|
||||||
|
|
||||||
|
|
||||||
let allowLogging = true;
|
let allowLogging = true;
|
||||||
let log = string => (allowLogging)? console.log(string):""
|
let log = string => (allowLogging)? console.log(string):""
|
||||||
|
|
||||||
@@ -13,98 +17,51 @@ module.exports = class chunkStore {
|
|||||||
this.path = (path[path.length] == "/") ? path : path+"/";
|
this.path = (path[path.length] == "/") ? path : path+"/";
|
||||||
mkdirpSync(path+name);
|
mkdirpSync(path+name);
|
||||||
|
|
||||||
this.entityWriteQueue = [];
|
this.db = new LinvoDB(name, {}, {});
|
||||||
this.chunkWritesInProgress = {};
|
|
||||||
this.entityWriteInProgress = false;
|
|
||||||
}
|
|
||||||
checkChunkWrite(x, y){
|
|
||||||
if(!this.chunkWritesInProgress[x]) this.chunkWritesInProgress[x] = {};
|
|
||||||
if(!this.chunkWritesInProgress[x][y]){
|
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setChunkWrite(x,y, bool){
|
|
||||||
if(!this.chunkWritesInProgress[x]) this.chunkWritesInProgress[x] = {};
|
|
||||||
if(!this.chunkWritesInProgress[x][y]) this.chunkWritesInProgress[x][y] = bool;
|
|
||||||
}
|
}
|
||||||
setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){
|
setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var localX = x > 0 ? Math.round(x % this.chunkSize) : Math.round(x % this.chunkSize)+64;
|
x = Math.floor(x);
|
||||||
x = Math.floor(Math.round(x)/this.chunkSize);
|
y = Math.floor(y);
|
||||||
var localY = y > 0 ? Math.round(y % this.chunkSize) : Math.round(y % this.chunkSize)+64;
|
this.db.find({x, y}, (err, docs) => {
|
||||||
y = Math.floor(Math.round(y)/this.chunkSize);
|
if(typeof newEntity == "object"){
|
||||||
console.log("localX"+localX)
|
if(err) reject(err);
|
||||||
console.log("localY"+localY)
|
let entityDoc = {x, y, entity: newEntity};
|
||||||
console.log(x+ " "+y)
|
if(docs && docs[0] && docs[0]._id){
|
||||||
this.entityWriteQueue.push({x, y, localX, localY, newEntity});
|
entityDoc._id = docs[0]._id;
|
||||||
if(this.checkChunkWrite(x,y)){
|
}
|
||||||
// we don't need to initiate another request for a chunk because there is one running already which should take care of it
|
this.db.save(entityDoc, (err,docs) => {
|
||||||
} else {
|
if(err) reject(err);
|
||||||
this.setChunkWrite(x, y, true);
|
resolve(docs);
|
||||||
this.getChunk(x,y).then(chunk => {
|
|
||||||
log("Setting "+this.entityWriteQueue.length+" entities in chunk X: "+x+" Y: "+y);
|
|
||||||
|
|
||||||
// add each queued entity to the chunk
|
|
||||||
this.entityWriteQueue.forEach(queuedEntity => {
|
|
||||||
if(queuedEntity.x == x && queuedEntity.y == y){
|
|
||||||
let localX = queuedEntity.localX;
|
|
||||||
let localY = queuedEntity.localY;
|
|
||||||
let newEntity = queuedEntity.newEntity;
|
|
||||||
|
|
||||||
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {};
|
|
||||||
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {};
|
|
||||||
|
|
||||||
if(newEntity && typeof newEntity == "object"){
|
|
||||||
log("Overwriting with new entity at "+localX+", "+localY);
|
|
||||||
chunk.dataObject[localX][localY] = newEntity;
|
|
||||||
} else {
|
|
||||||
log("Deleting entry at "+localX+", "+localY);
|
|
||||||
chunk.dataObject[localX][localY] = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
// clear the now processed queue
|
} else {
|
||||||
this.entityWriteQueue = this.entityWriteQueue.filter(entity => {
|
docs.forEach(doc => {
|
||||||
if(entity.x != x && entity.y != y) return true
|
doc.remove(()=>{
|
||||||
|
// removed :)
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// save the chunk and resolve the promise
|
}
|
||||||
this.setChunk(x, y, chunk).then(()=>{
|
});
|
||||||
log("Successfully saved chunk!");
|
|
||||||
//this.entityWriteInProgress = false;
|
|
||||||
this.setChunkWrite(x,y,false);
|
|
||||||
resolve(chunk);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
setChunk(x, y, chunk){
|
getEntity(x, y){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
mkdirp(this.path+this.name+"/"+x).then(() => {
|
this.db.find({x:Math.floor(x), y:Math.floor(y)}, (err, docs) => {
|
||||||
fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk, null, 4), function(err){
|
if(err) reject(err);
|
||||||
if(err) {
|
resolve(docs);
|
||||||
reject(err);
|
|
||||||
} else resolve();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
getChunk(x, y){
|
getChunk(x, y){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
/*
|
||||||
// make new chunk in case we can't find an existing one
|
// make new chunk in case we can't find an existing one
|
||||||
let chunk = {
|
let chunk = {
|
||||||
dataObject: {},
|
dataObject: {},
|
||||||
position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y}
|
position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y}
|
||||||
chunkSize:this.chunkSize,
|
chunkSize:this.chunkSize
|
||||||
/*get: function(x, y){
|
|
||||||
if(this.dataObject[Math.round(x)] && this.dataObject[Math.round(x)][Math.round(y)]){
|
|
||||||
return this.dataObject[Math.round(x)][Math.round(y)];
|
|
||||||
} else {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
},*/
|
|
||||||
};
|
};
|
||||||
fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => {
|
fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => {
|
||||||
|
|
||||||
@@ -115,13 +72,6 @@ module.exports = class chunkStore {
|
|||||||
if(objectOps.isJSON(chunkFile)) {
|
if(objectOps.isJSON(chunkFile)) {
|
||||||
log("returned from file")
|
log("returned from file")
|
||||||
let chunk = JSON.parse(chunkFile);
|
let chunk = JSON.parse(chunkFile);
|
||||||
/*chunk.get = function(x, y){
|
|
||||||
if(this.dataObject[Math.round(x)] && this.dataObject[Math.round(x)][Math.round(y)]){
|
|
||||||
return this.dataObject[Math.round(x)][Math.round(y)];
|
|
||||||
} else {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
resolve(chunk);
|
resolve(chunk);
|
||||||
} else {
|
} else {
|
||||||
log("file failed")
|
log("file failed")
|
||||||
@@ -129,6 +79,7 @@ module.exports = class chunkStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}); // add x and y values as well and we are done?
|
}); // add x and y values as well and we are done?
|
||||||
|
*/
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+23
-18
@@ -15,28 +15,33 @@ describe("chunkStore.js", () => {
|
|||||||
store = new chunkStore("910239"/* instanceID, use defaults for the rest */);
|
store = new chunkStore("910239"/* instanceID, use defaults for the rest */);
|
||||||
assert.equal(store.name, "910239");
|
assert.equal(store.name, "910239");
|
||||||
});
|
});
|
||||||
it("allows you to store the locations of entities in a chunk divided database", () => {
|
it("allows you to store the locations of entities", done => {
|
||||||
return store.setEntity(inserterX, inserterY, {name:inserterName}).then(() => {
|
store.setEntity(inserterX, inserterY, {name:inserterName, otherData:"can be arbitrary"}).then(() => {
|
||||||
|
done();
|
||||||
// nothing to do here really
|
// nothing to do here really
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("returns items located in a chunk", (done) => {
|
it("returns entities at positions you query for", done => {
|
||||||
// We return the promise to mocha as it supports promises natively, works a lot better than calling done() manually.
|
setTimeout(()=>{
|
||||||
let x = 0;
|
store.getEntity(inserterX, inserterY).then(entities => {
|
||||||
let y = -1
|
entity = entities[0]
|
||||||
store.getChunk(x,y).then(chunkData => {
|
// console.log(entity)
|
||||||
console.log(chunkData);
|
assert.equal(entity.x, 14);
|
||||||
assert.equal(typeof chunkData, "object");
|
assert.equal(entity.y, -18);
|
||||||
assert.equal(chunkData.dataObject[15][47].name, inserterName);
|
assert.equal(entity.entity.name, "inserter");
|
||||||
assert.equal(chunkData.position.x, x);
|
assert.equal(entity.entity.otherData, "can be arbitrary");
|
||||||
assert.equal(chunkData.position.y, y);
|
done();
|
||||||
done();
|
});
|
||||||
});
|
},500)
|
||||||
});
|
});
|
||||||
it("deletes entities when not provided with an object as 3rd arg", done => {
|
it("deletes entities when not provided with an object as 3rd arg", done => {
|
||||||
store.setEntity(inserterX, inserterY).then((chunk)=>{
|
store.setEntity(inserterX, inserterY).then(entity => {
|
||||||
assert(!chunk.dataObject(15, 47));
|
assert(entity === undefined);
|
||||||
done();
|
store.getEntity(inserterX, inserterY).then(entities => {
|
||||||
|
// console.log(entities)
|
||||||
|
assert.equal(entities.length, 0, "Why are we getting results, it should be empty?");
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -36,7 +36,6 @@ class remoteMap {
|
|||||||
|
|
||||||
data = data.split(" ");
|
data = data.split(" ");
|
||||||
if(data && data[0] == undefined){
|
if(data && data[0] == undefined){
|
||||||
// this position is now empty, delete whatever was there from DB
|
|
||||||
console.log("empty: ");
|
console.log("empty: ");
|
||||||
console.log(data);
|
console.log(data);
|
||||||
return
|
return
|
||||||
@@ -46,12 +45,15 @@ class remoteMap {
|
|||||||
let yPos = data[2];
|
let yPos = data[2];
|
||||||
if(xPos && yPos && name){
|
if(xPos && yPos && name){
|
||||||
if(name == "deleted"){
|
if(name == "deleted"){
|
||||||
this.chunkMap.setEntity(xPos, yPos, "delete this entity").then(data => {
|
// this position is now empty, delete whatever was there from DB
|
||||||
this.messageInterface("Deleted data "+data.position.x+", "+data.position.y);
|
this.chunkMap.setEntity(xPos, yPos, "delete this entity").then(() => {
|
||||||
|
this.messageInterface("Deleted data "+xPos+", "+yPos);
|
||||||
}).catch((err)=>this.messageInterface(err));
|
}).catch((err)=>this.messageInterface(err));
|
||||||
} else {
|
} else {
|
||||||
|
// save this entity in the database
|
||||||
this.chunkMap.setEntity(xPos, yPos, {name}).then(data => {
|
this.chunkMap.setEntity(xPos, yPos, {name}).then(data => {
|
||||||
this.messageInterface("Added "+name+" to data "+data.position.x+", "+data.position.y);
|
data = data[0];
|
||||||
|
this.messageInterface("Added "+name+" to data "+data.x+", "+data.y);
|
||||||
}).catch((err)=>this.messageInterface(err));
|
}).catch((err)=>this.messageInterface(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user