Skip to content

Commit

Permalink
Merging changes back in
Browse files Browse the repository at this point in the history
  • Loading branch information
dlaxar committed Jun 16, 2013
2 parents 02c2264 + 558377a commit 3817658
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
62 changes: 47 additions & 15 deletions lib/shared/base.js
Expand Up @@ -5,14 +5,44 @@

"use strict";

var UUID = 0;
var UUID_INCR = 0;

function binarySearchBitshift(list, val) {
var min = 0;
var max = list.length - 1;

for(;;) {
// fall back to linear search, 11 seems to be a good threshold, but this
// depends on the uses comparator!! (here: ===)
if (min + 11 > max) {
for (var i = min; i <= max; ++i) {
if (val === list[i]) {
return i;
}
}

return -1;
}

var mid = (min + max) >> 1;
var dat = list[mid];

if (val === dat)
return mid;

if (val > dat)
min = mid + 1;
else
max = mid - 1;
}
};

var Base = module.exports = Object.freeze(Object.create(Object.prototype, {
UUID: {
value: UUID
value: UUID_INCR
},
UUIDS: {
value: [UUID]
value: [UUID_INCR]
},
/**
* Creates an object that inherits from `this` object (Analog of
Expand Down Expand Up @@ -74,28 +104,30 @@ var Base = module.exports = Object.freeze(Object.create(Object.prototype, {
merge: {
value: function merge() {
var descriptor = {};
var uuids = this.UUIDS || [this.UUID || 0];
var base = 0;
var uuids = Array.isArray(this.UUIDS) ? [].concat(this.UUIDS) : [this.UUID];
Array.prototype.forEach.call(arguments, function(properties) {
// Make sure the merged-in object has a UUID
if (!properties.hasOwnProperty("UUID")) {
properties.UUID = base | 1 << ++UUID;
properties.UUID = ++UUID_INCR;
if (!properties.UUIDS)
properties.UUIDS = [];
properties.UUIDS.push(properties.UUID);
}
// Copy the object properties over
Object.getOwnPropertyNames(properties).forEach(function(name) {
if (name == "UUIDS")
uuids = uuids.concat(properties[name]);
if (name == "UUIDS") {
properties[name].forEach(function(uuid) {
if (binarySearchBitshift(uuids, uuid) == -1)
uuids.push(uuid);
});
// Keep the list sorted at all times (binary search)
uuids.sort(function (a, b) { return a - b; });
}
else
descriptor[name] = Object.getOwnPropertyDescriptor(properties, name);
});
});
uuids.forEach(function(uuid) {
if (base & uuid)
return;
base = base | uuid;
});
base = base || (base | 1 << ++UUID);
var base = ++UUID_INCR;
descriptor.UUID = {
value: base
};
Expand Down Expand Up @@ -221,7 +253,7 @@ var Base = module.exports = Object.freeze(Object.create(Object.prototype, {
*/
hasFeature: {
value: function hasFeature(base) {
return typeof base.UUID != "undefined" && this.UUIDS.indexOf(base.UUID) !== -1;
return typeof base.UUID != "undefined" && binarySearchBitshift(this.UUIDS, base.UUID) != -1;
}
}
}));
9 changes: 6 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "jsDAV",
"description": "jsDAV allows you to easily add WebDAV support to a NodeJS application. jsDAV is meant to cover the entire standard, and attempts to allow integration using an easy to understand API.",
"version": "0.3.1",
"version": "0.3.2",
"homepage" : "http://github.com/mikedeboer/jsDAV",
"engines": {"node": ">= 0.4.0"},
"author": "Mike de Boer <info@mikedeboer.nl>",
Expand All @@ -20,14 +20,17 @@
"jsftp": "~0.5.4",
"node-sftp": "0.1.1",
"xmldom": "~0.1.13",
"xpath": "~0.0.5",
"gnu-tools": "0.0.x"
"xpath": "~0.0.5"
},
"optionalDependencies": {
"jsftp": "~0.5.4",
"node-sftp": "0.1.1",
"dbox": "~0.5.6",
"redis": "~0.8.2",
"mongodb": "~1.2.12",
"pg": "~1.3.0"
"mongodb": "~1.2.12",
"gnu-tools": "0.0.x"
},
"devDependencies": {
"chai": "latest"
Expand Down
8 changes: 4 additions & 4 deletions test/test_base.js
Expand Up @@ -5,7 +5,7 @@ var jsDAV_FS_Directory = require("./../lib/DAV/backends/fs/directory");
var jsDAV_FS_File = require("./../lib/DAV/backends/fs/file");

// interfaces for directories:
var jsDAV_Directory = require("./../lib/DAV/directory");
var jsDAV_Collection = require("./../lib/DAV/collection");
var jsDAV_iQuota = require("./../lib/DAV/interfaces/iQuota");
var jsDAV_iCollection = require("./../lib/DAV/interfaces/iCollection");

Expand All @@ -18,7 +18,7 @@ var jsDAV_iHref = require("./../lib/DAV/interfaces/iHref");

// test dir properties
var dir = jsDAV_FS_Directory.new("somepath/to/a/dir");
Assert.ok(dir.hasFeature(jsDAV_Directory));
Assert.ok(dir.hasFeature(jsDAV_Collection));
Assert.ok(dir.hasFeature(jsDAV_iQuota));
Assert.ok(dir.hasFeature(jsDAV_iCollection));

Expand All @@ -31,13 +31,13 @@ var file = jsDAV_FS_File.new("somepath/to/a/file");
Assert.ok(file.hasFeature(jsDAV_File));
Assert.ok(file.hasFeature(jsDAV_iFile));

Assert.ok(!file.hasFeature(jsDAV_Directory));
Assert.ok(!file.hasFeature(jsDAV_Collection));
Assert.ok(!file.hasFeature(jsDAV_iQuota));
Assert.ok(!file.hasFeature(jsDAV_iCollection));
Assert.ok(!file.hasFeature(jsDAV_iHref));

// re-test dir properties
Assert.ok(dir.hasFeature(jsDAV_Directory));
Assert.ok(dir.hasFeature(jsDAV_Collection));
Assert.ok(dir.hasFeature(jsDAV_iQuota));
Assert.ok(dir.hasFeature(jsDAV_iCollection));

Expand Down

0 comments on commit 3817658

Please sign in to comment.