Skip to content

Commit

Permalink
Added Support for LinkMap #332
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Sep 14, 2018
1 parent 8149b68 commit f1005c1
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 79 deletions.
35 changes: 31 additions & 4 deletions lib/client/network/protocol37/deserializer-binary.js
Expand Up @@ -164,6 +164,9 @@ function parseValue(input, type, offset, fieldName, reader) {
case 15:
parsed = parseLinkCollection(input, offset);
break;
case 16:
parsed = parseLinkMap(input, offset);
break;
case 17:
parsed = parseByte(input, offset);
break;
Expand Down Expand Up @@ -227,10 +230,16 @@ function parseLink(input, offset) {
offset += clusterId.read;
let clusterPosition = parseNumber(input, offset);
offset += clusterPosition.read;
let rid = new RecordID({
cluster: clusterId.value,
position: clusterPosition.value
});

let rid;
if (clusterId.value === -2 && clusterPosition.value === -1) {
rid = null;
} else {
rid = new RecordID({
cluster: clusterId.value,
position: clusterPosition.value
});
}
return { read: offset - baseOffset, value: rid };
}

Expand Down Expand Up @@ -286,6 +295,24 @@ function parseEmbedded(input, offset, reader) {
}
return parsed;
}

function parseLinkMap(input, offset) {
let parsed = parseNumber(input, offset);
let baseOffset = offset;
offset += parsed.read;
let map = {};
for (let i = 0; i < parsed.value; i++) {
// only string as key
let type = input.readInt8(offset);
offset += 1;
let key = parseString(input, offset);
offset += key.read;
let ridParsed = parseLink(input, offset);
offset += ridParsed.read;
map[key.value] = ridParsed.value;
}
return { read: offset - baseOffset, value: map };
}
function parseEmbeddedMap(input, offset) {
let parsed = parseNumber(input, offset);
let baseOffset = offset;
Expand Down
187 changes: 112 additions & 75 deletions test/database/database-complex-records-test.js
@@ -1,156 +1,193 @@
var should = require('should');
var should = require("should");

var ORidBag = require('../../lib/client/database/bag').ORidBag;
var RID = require('../../lib').RID;
var ORidBag = require("../../lib/client/database/bag").ORidBag;
var RID = require("../../lib").RID;

describe("ODatabase API - Open / Simple Query", function () {
before(CAN_RUN(37, function () {
return CREATE_DB("test_complex_records");

}));
after(function () {
describe("ODatabase API - Open / Simple Query", function() {
before(
CAN_RUN(37, function() {
return CREATE_DB("test_complex_records");
})
);
after(function() {
return DROP_DB("test_complex_records");
});


describe('Database::Links & Embedded', function () {

before(function () {
return TEST_CLIENT.session({name: "test_complex_records"})
.then((db) => {
describe("Database::Links & Embedded", function() {
before(function() {
return TEST_CLIENT.session({ name: "test_complex_records" })
.then(db => {
this.db = db;
return db.command('create class Foo').all();
}).then(() => {
return this.db.query('SELECT * FROM OUser').all()
}).then((results) => {
this.links = results.map((r) => {
return db.command("create class Foo").all();
})
.then(() => {
return this.db.query("SELECT * FROM OUser").all();
})
.then(results => {
this.links = results.map(r => {
return r["@rid"];
})
});
});

});
after(function () {
after(function() {
return this.db.close();
});

beforeEach(function () {
beforeEach(function() {
return this.db.command("delete from Foo").all();
})
it('should create a simple record with Links', function () {
});
it("should create a simple record with Links", function() {
var record = {
"@class": "Foo",
"links": this.links
}
return this.db.record.create(record)
links: this.links
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
})
.then(response => {
response[0].links.should.be.eql(this.links);
});
});


it('should create a simple record with embedded map', function () {
it("should create a simple record with embedded map", function() {
var record = {
"@class": "Foo",
"embedded": {"name": "Foo"}
}
return this.db.record.create(record)
embedded: { name: "Foo" }
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
response[0].embedded.should.be.eql({"name": "Foo"});
})
.then(response => {
response[0].embedded.should.be.eql({ name: "Foo" });
});
});

it('should create a simple record with embedded document with class', function () {
it("should create a simple record with embedded document with class", function() {
var record = {
"@class": "Foo",
"embedded": {"@class": "Foo", "name": "Foo"}
}
return this.db.record.create(record)
embedded: { "@class": "Foo", name: "Foo" }
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
response[0].embedded.should.be.eql({"@class": "Foo", "name": "Foo"});
})
.then(response => {
response[0].embedded.should.be.eql({ "@class": "Foo", name: "Foo" });
});
});

it('should create a simple record with embedded strings', function () {
it("should create a simple record with embedded strings", function() {
var record = {
"@class": "Foo",
"strings": ["foo", "bar"]
}
return this.db.record.create(record)
strings: ["foo", "bar"]
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
})
.then(response => {
response[0].strings.should.be.eql(["foo", "bar"]);
});
});

it('should create a simple record with embedded numbers', function () {
it("should create a simple record with embedded numbers", function() {
var record = {
"@class": "Foo",
"numbers": [2, 10]
}
return this.db.record.create(record)
numbers: [2, 10]
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
})
.then(response => {
response[0].numbers.should.be.eql([2, 10]);
});
});

it('should create a simple record with embedded dates', function () {
it("should create a simple record with embedded dates", function() {
var dates = [new Date(), new Date()];
var record = {
"@class": "Foo",
"dates": dates
}
return this.db.record.create(record)
dates: dates
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
})
.then(response => {
response[0].dates.should.be.eql(dates);
});
});

it('should create a simple record with embedded mix numbers/strings/date', function () {
it("should create a simple record with embedded mix numbers/strings/date", function() {
var mix = [2, "Now", new Date()];
var record = {
"@class": "Foo",
"mixes": mix
}
return this.db.record.create(record)
mixes: mix
};
return this.db.record
.create(record)
.then(() => {
return this.db.query("select from Foo").all();
}).then((response) => {
})
.then(response => {
response[0].mixes.should.be.eql(mix);
});
});
});

describe('Database::RidBags', function () {

before(function () {
return TEST_CLIENT.session({name: "test_complex_records"})
.then((db) => {
this.db = db;
it("should create a simple record with link map", function() {
var record = {
"@class": "Foo",
name: "Test"
};
return this.db.record
.create(record)
.then(response => {
return this.db
.command(
`insert into Foo set linkMap = { "foo" : ${
response["@rid"]
}, "bar" : null }`
)
.one();
})
.then(response => {
return this.db.query("select from " + response["@rid"]).one();
})
.then(response => {
response.linkMap.foo.should.be.an.instanceOf(RID);
should(response.linkMap["bar"]).be.null;
});
});
});

describe("Database::RidBags", function() {
before(function() {
return TEST_CLIENT.session({ name: "test_complex_records" }).then(db => {
this.db = db;
});
});
after(function () {
after(function() {
return this.db.close();
});

it('should create two vertices and edge', function () {
it("should create two vertices and edge", function() {
var query = `let v1 = create vertex V set name = 'Foo';
let v2 = create vertex V set name = 'Foo1';
create edge from $v1 to $v2;
return $v1`;
return this.db.batch(query).all()
.then((response) => {
return this.db
.batch(query)
.all()
.then(response => {
response[0]["@class"].should.be.eql("V");
response[0]["out_"].should.be.an.instanceOf(ORidBag);
response[0]["out_"].size().should.be.eql(1);
Expand All @@ -162,5 +199,5 @@ describe("ODatabase API - Open / Simple Query", function () {
rids[0].should.be.an.instanceOf(RID);
});
});
})
});
});

0 comments on commit f1005c1

Please sign in to comment.