|
|
@@ -11,10 +11,8 @@ var Document = module.exports = function Document(){
|
|
|
if(this.constructor == Document) throw new Error("Never create instances! Use static helpers only.");
|
|
|
}, klass = Document, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
|
|
|
|
|
|
-// DEPENDENCIES
|
|
|
var Value = require("./Value");
|
|
|
|
|
|
-// STATIC MEMBERS
|
|
|
/**
|
|
|
* Shared "_id"
|
|
|
* @static
|
|
|
@@ -22,18 +20,45 @@ var Value = require("./Value");
|
|
|
**/
|
|
|
klass.ID_PROPERTY_NAME = "_id";
|
|
|
|
|
|
+//SKIPPED: DocumentStorage
|
|
|
+
|
|
|
/**
|
|
|
- * Compare two documents.
|
|
|
+ * Return JSON representation of this Document
|
|
|
+ * @method toJson
|
|
|
+ * @returns {Object} JSON representation of this Document
|
|
|
+ **/
|
|
|
+klass.toJson = function toJson(doc) {
|
|
|
+ return JSON.parse(JSON.stringify(doc));
|
|
|
+};
|
|
|
+
|
|
|
+//SKIPPED: metaFieldTextScore
|
|
|
+//SKIPPED: toBsonWithMetaData
|
|
|
+//SKIPPED: fromBsonWithMetaData
|
|
|
+
|
|
|
+//SKIPPED: MutableDocument
|
|
|
+
|
|
|
+//SKIPPED: getNestedFieldHelper
|
|
|
+//SKIPPED: getNestedField -- same as getNestedFieldHelper in our code
|
|
|
+//SKIPPED: getApproximateSize -- not implementing mem usage right now
|
|
|
+//SKIPPED: hash_combine
|
|
|
+
|
|
|
+/** Compare two documents.
|
|
|
*
|
|
|
- * BSON document field order is significant, so this just goes through the fields in order.
|
|
|
- * The comparison is done in roughly the same way as strings are compared, but comparing one field at a time instead of one character at a time.
|
|
|
+ * BSON document field order is significant, so this just goes through
|
|
|
+ * the fields in order. The comparison is done in roughly the same way
|
|
|
+ * as strings are compared, but comparing one field at a time instead
|
|
|
+ * of one character at a time.
|
|
|
+ *
|
|
|
+ * Note: This does not consider metadata when comparing documents.
|
|
|
*
|
|
|
- * @static
|
|
|
* @method compare
|
|
|
- * @param rL left document
|
|
|
- * @param rR right document
|
|
|
- * @returns an integer less than zero, zero, or an integer greater than zero, depending on whether rL < rR, rL == rR, or rL > rR
|
|
|
- **/
|
|
|
+ * @static
|
|
|
+ * @param l {Object} left document
|
|
|
+ * @param r {Object} right document
|
|
|
+ * @returns an integer less than zero, zero, or an integer greater than
|
|
|
+ * zero, depending on whether lhs < rhs, lhs == rhs, or lhs > rhs
|
|
|
+ * Warning: may return values other than -1, 0, or 1
|
|
|
+ */
|
|
|
klass.compare = function compare(l, r){ //TODO: might be able to replace this with a straight compare of docs using JSON.stringify()
|
|
|
var lPropNames = Object.getOwnPropertyNames(l),
|
|
|
lPropNamesLength = lPropNames.length,
|
|
|
@@ -48,42 +73,65 @@ klass.compare = function compare(l, r){ //TODO: might be able to replace this wi
|
|
|
|
|
|
if (i >= rPropNamesLength) return 1; // right document is shorter
|
|
|
|
|
|
- var nameCmp = Value.compare(lPropNames[i], rPropNames[i]);
|
|
|
+ var rField = rPropNames[i],
|
|
|
+ lField = lPropNames[i];
|
|
|
+ var nameCmp = Value.compare(lField, rField);
|
|
|
if (nameCmp !== 0) return nameCmp; // field names are unequal
|
|
|
|
|
|
- var valueCmp = Value.compare(l[lPropNames[i]], r[rPropNames[i]]);
|
|
|
+ var valueCmp = Value.compare(l[lPropNames[i]], r[rField]);
|
|
|
if (valueCmp) return valueCmp; // fields are unequal
|
|
|
}
|
|
|
+};
|
|
|
+
|
|
|
+//SKIPPED: toString
|
|
|
+
|
|
|
+klass.serializeForSorter = function serializeForSorter(doc) {
|
|
|
+ //NOTE: DEVIATION FROM MONGO: they take a buffer to output the current instance into, ours is static and takes a doc and returns the serialized output
|
|
|
+ return JSON.stringify(doc);
|
|
|
+};
|
|
|
+
|
|
|
+klass.deserializeForSorter = function deserializeForSorter(docStr, sorterDeserializeSettings) {
|
|
|
+ JSON.parse(docStr);
|
|
|
+};
|
|
|
|
|
|
- /* NOTREACHED */
|
|
|
- throw new Error("This should never happen"); //verify(false)
|
|
|
-// return 0;
|
|
|
+//SKIPPED: swap
|
|
|
+//SKIPPED: []
|
|
|
+//SKIPPED: getField -- inline as: obj[key]
|
|
|
+//SKIPPED: getNestedField -- use fieldPath? might need to implement this...
|
|
|
+//SKIPPED: size -- need this? Number of fields in this document. O(n) -- recursive
|
|
|
+klass.empty = function(obj) {
|
|
|
+ return Object.keys(obj).length === 0;
|
|
|
};
|
|
|
+//SKIPPED: operator <<
|
|
|
+//SKIPPED: positionOf
|
|
|
|
|
|
/**
|
|
|
* Clone a document
|
|
|
* @static
|
|
|
* @method clone
|
|
|
- * @param document
|
|
|
- **/
|
|
|
-klass.clone = function(document){
|
|
|
+ * @param doc
|
|
|
+ */
|
|
|
+klass.clone = function clone(doc) {
|
|
|
var obj = {};
|
|
|
- for(var key in document){
|
|
|
- if(document.hasOwnProperty(key)){
|
|
|
- var withObjVal = document[key];
|
|
|
- if(withObjVal === null) { // necessary to handle null values without failing
|
|
|
- obj[key] = withObjVal;
|
|
|
- }
|
|
|
- else if(withObjVal.constructor === Object){
|
|
|
- obj[key] = Document.clone(withObjVal);
|
|
|
- }else{
|
|
|
- obj[key] = withObjVal;
|
|
|
+ for (var key in doc) {
|
|
|
+ if (doc.hasOwnProperty(key)) {
|
|
|
+ var val = doc[key];
|
|
|
+ if (val === undefined || val === null) { // necessary to handle null values without failing
|
|
|
+ obj[key] = val;
|
|
|
+ } else if (val instanceof Object && val.constructor === Object) {
|
|
|
+ obj[key] = Document.clone(val);
|
|
|
+ } else {
|
|
|
+ obj[key] = val;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return obj;
|
|
|
};
|
|
|
|
|
|
-// proto.addField = function addField(){ throw new Error("Instead of `Document#addField(key,val)` you should just use `obj[key] = val`"); }
|
|
|
-// proto.setField = function addField(){ throw new Error("Instead of `Document#setField(key,val)` you should just use `obj[key] = val`"); }
|
|
|
-// proto.getField = function getField(){ throw new Error("Instead of `Document#getField(key)` you should just use `var val = obj[key];`"); }
|
|
|
+//SKIPPED: hasTextScore
|
|
|
+//SKIPPED: getTextScore
|
|
|
+
|
|
|
+//SKIPPED: memUsageForSorter -- not implementing mem usage right now
|
|
|
+//SKIPPED: getOwned -- not implementing mem usage right now
|
|
|
+
|
|
|
+//SKIPPED: getPtr
|