|
|
@@ -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,49 +20,45 @@ var Value = require("./Value");
|
|
|
**/
|
|
|
klass.ID_PROPERTY_NAME = "_id";
|
|
|
|
|
|
-/**
|
|
|
- * Return JSON representation of this Document
|
|
|
- *
|
|
|
- * @method toJson
|
|
|
- * @returns JSON representation of this Document
|
|
|
- **/
|
|
|
- klass.toJson = function toJson(document){
|
|
|
- return JSON.stringify(document);
|
|
|
- };
|
|
|
+//SKIPPED: DocumentStorage
|
|
|
|
|
|
/**
|
|
|
* Return JSON representation of this Document
|
|
|
- *
|
|
|
- * @method toJsonWithMetaData
|
|
|
- * @returns JSON representation of this Document, with MetaData
|
|
|
+ * @method toJson
|
|
|
+ * @returns {Object} JSON representation of this Document
|
|
|
**/
|
|
|
-klass.toJsonWithMetaData = function toJsonWithMetaData(document){
|
|
|
- //NOTE: DEVIATION FROM MONGO: Not sure how we are handling metadata
|
|
|
- return JSON.stringify(document);
|
|
|
+klass.toJson = function toJson(doc) {
|
|
|
+ return JSON.parse(JSON.stringify(doc));
|
|
|
};
|
|
|
|
|
|
-/**
|
|
|
- * Return Document with MetaData from its JSON representation
|
|
|
- *
|
|
|
- * @method fromJsonWithMetaData
|
|
|
- * @returns Document with MetaData
|
|
|
- **/
|
|
|
-klass.fromJsonWithMetaData = function fromJsonWithMetaData(json){
|
|
|
- return JSON.parse(json);
|
|
|
-};
|
|
|
+//SKIPPED: metaFieldTextScore
|
|
|
+//SKIPPED: toBsonWithMetaData
|
|
|
+//SKIPPED: fromBsonWithMetaData
|
|
|
|
|
|
-/**
|
|
|
- * Compare two documents.
|
|
|
+//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,
|
|
|
@@ -79,84 +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
|
|
|
|
|
|
- /* NOTREACHED */
|
|
|
- throw new Error("This should never happen"); //verify(false)
|
|
|
-// return 0;
|
|
|
+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);
|
|
|
+};
|
|
|
+
|
|
|
+//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;
|
|
|
};
|
|
|
|
|
|
-//NOTE: not implementing the sorter functions until we understand sorter needs better
|
|
|
-var _waitingOnSorter = function() {
|
|
|
- //TODO: When working on sorter, decide if this is needed
|
|
|
- throw new Error("Not implemented.");
|
|
|
-};
|
|
|
-
|
|
|
-klass.serializeForSorter = function(document) {
|
|
|
- var buffer, size = 0, key;
|
|
|
-
|
|
|
- //determine size for sorter??
|
|
|
- //for (key in document) {
|
|
|
- // if (document.hasOwnProperty(key)) size++;
|
|
|
- //};
|
|
|
- //add size to buffer (don't count size field)
|
|
|
- //buffer = (size-1).toString();
|
|
|
-
|
|
|
- //add each field
|
|
|
- //for (key in document) {
|
|
|
- // buffer += document[key]);
|
|
|
- //};
|
|
|
- //append text score?
|
|
|
- //return buffer;
|
|
|
-
|
|
|
- return JSON.stringify(document);
|
|
|
-};
|
|
|
+//SKIPPED: hasTextScore
|
|
|
+//SKIPPED: getTextScore
|
|
|
|
|
|
-klass.deserializeForSorter = function(stringifiedDocument) {
|
|
|
- //_waitingOnSorter();
|
|
|
+//SKIPPED: memUsageForSorter -- not implementing mem usage right now
|
|
|
+//SKIPPED: getOwned -- not implementing mem usage right now
|
|
|
|
|
|
- //var document, size;
|
|
|
- //size = int.parse(stringifiedDocument.split(",")[0]);
|
|
|
- //document["size"] = size;
|
|
|
- //document = JSON.parse(stringifiedDocument);
|
|
|
-
|
|
|
- return JSON.parse(stringifiedDocument);
|
|
|
-};
|
|
|
-klass.memUsageForSorter = function(value) {
|
|
|
- _waitingOnSorter();
|
|
|
-};
|
|
|
-klass.getOwned = function() {
|
|
|
- return proto;
|
|
|
-};
|
|
|
-// 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: getPtr
|