Node.js:如何使用循环引用序列化大对象

时间:2015-09-26 12:51:12

标签: javascript node.js asynchronous

我使用Node.js并希望将大型javascript对象序列化为HDD。该对象基本上是一个" hashmap"并且只包含数据,而不是函数。该对象包含具有循环引用的元素。

这是一个在线应用程序,因此该过程不应阻止主循环。在我的用例中,非阻塞比速度更重要(数据是实时内存数据,仅在启动时加载,每隔X分钟和关机/故障时进行定时备份)

最好的方法是什么?指向我想要的图书馆的指针非常受欢迎。

1 个答案:

答案 0 :(得分:0)

我有一个很好的解决方案,我一直在使用。它的缺点是它有一个O(n ^ 2)运行时间让我感到难过。

以下是代码:



// I defined these functions as part of a utility library called "U".
var U = {
  isObj: function(obj, cls) {
    try { return obj.constructor === cls; } catch(e) { return false; };
  },
  straighten: function(item) {
    /*
    Un-circularizes data. Works if `item` is a simple Object, an Array, or any inline value (string, int, null, etc).
    */
    var arr = [];
    U.straighten0(item, arr);
    return arr.map(function(item) { return item.calc; });
  },
  straighten0: function(item, items) {
    /*
    The "meat" of the un-circularization process. Returns the index of `item`
    within the array `items`. If `item` didn't initially exist within
    `items`, it will by the end of this function, therefore this function
    always produces a usable index.
    
    Also, `item` is guaranteed to have no more circular references (it will
    be in a new format) once its index is obtained.
    */
    
    /*
    STEP 1) If `item` is already in `items`, simply return it.
    
    Note that an object's existence can only be confirmed by comparison to
    itself, not an un-circularized version of itself. For this reason an
    `orig` value is kept ahold of to make such comparisons possible. This
    entails that every entry in `items` has both an `orig` value (the
    original object, for comparison) and a `calc` value (the calculated, un
    circularized value).
    */
    for (var i = 0, len = items.length; i < len; i++) // This is O(n^2) :(
      if (items[i].orig === item) return i;
    
    var ind = items.length;
    
    // STEP 2) Depending on the type of `item`, un-circularize it differently
    if (U.isObj(item, Object)) {
      
      /*
      STEP 2.1) `item` is an `Object`. Create an un-circularized version of
      that `Object` - keep all its keys, but replace each value with an index
      that points to that values.
      */
      var obj = {};
      items.push({ orig: item, calc: obj }); // Note both `orig` AND `calc`.
      for (var k in item)
        obj[k] = U.straighten0(item[k], items);
        
    } else if (U.isObj(item, Array)) {
      
      /*
      STEP 2.2) `item` is an `Array`. Create an un-circularized version of
      that `Array` - replace each of its values with an index that indexes
      the original value.
      */
      var arr = [];
      items.push({ orig: item, calc: arr }); // Note both `orig` AND `calc`.
      for (var i = 0; i < item.length; i++)
        arr.push(U.straighten0(item[i], items));
      
    } else {
      
      /*
      STEP 2.3) `item` is a simple inline value. We don't need to make any
      modifications to it, as inline values have no references (let alone
      circular references).
      */
      items.push({ orig: item, calc: item });
      
    }
        
    return ind;
  },
  unstraighten: function(items) {
    /*
    Re-circularizes un-circularized data! Used for undoing the effects of
    `U.straighten`. This process will use a particular marker (`unbuilt`) to
    show values that haven't yet been calculated. This is better than using
    `null`, because that would break in the case that the literal value is
    `null`.
    */
    var unbuilt = { UNBUILT: true };
    var initialArr = [];
    // Fill `initialArr` with `unbuilt` references
    for (var i = 0; i < items.length; i++) initialArr.push(unbuilt);
    return U.unstraighten0(items, 0, initialArr, unbuilt);
  },
  unstraighten0: function(items, ind, built, unbuilt) {
    /*
    The "meat" of the re-circularization process. Returns an Object, Array,
    or inline value. The return value may contain circular references.
    */
    if (built[ind] !== unbuilt) return built[ind];
    
    var item = items[ind];
    var value = null;
    
    /*
    Similar to `straighten`, check the type. Handle Object, Array, and inline
    values separately.
    */
    
    if (U.isObj(item, Object)) {
      
      // value is an ordinary object
      var obj = built[ind] = {};
      for (var k in item)
        obj[k] =  U.unstraighten0(items, item[k], built, unbuilt);
      return obj;
      
    } else if (U.isObj(item, Array)) {
      
      // value is an array
      var arr = built[ind] = [];
      for (var i = 0; i < item.length; i++)
        arr.push(U.unstraighten0(items, item[i], built, unbuilt));
      return arr;
      
    }
    
    built[ind] = item;
    return item;
  },
  thingToString: function(thing) {
    /*
    Elegant convenience function to convert any structure (circular or not)
    to a string! Now that this function is available, you can ignore
    `straighten` and `unstraighten`, and the headaches they may cause.
    */
    var st = U.straighten(thing);  
    return JSON.stringify(st);
  },
  stringToThing: function(string) {
    /*
    Elegant convenience function to reverse the effect of `U.thingToString`. 
    */
    return U.unstraighten(JSON.parse(string));
  }
};
 
var circular = {
  val: 'haha',
  val2: [ 'hey', 'ho', 'hee' ],
  doesNullWork: null
};
circular.circle1 = circular;
circular.confusing = {
  circular: circular,
  value: circular.val2
};

console.log('Does JSON.stringify work??');
try {
  var str = JSON.stringify(circular);
  console.log('JSON.stringify works!!');
} catch(err) {
  console.log('JSON.stringify doesn\'t work!');
}

console.log('');
console.log('Does U.thingToString work??');
try {
  var str = U.thingToString(circular);
  console.log('U.thingToString works!!');
  console.log('Its result looks like this:')
  console.log(str);
  console.log('And here\'s it converted back into an object:');
  var obj = U.stringToThing(str);
  for (var k in obj) {
    console.log('`obj` has key "' + k + '"');
  }
  console.log('Did `null` work?');
  if (obj.doesNullWork === null)
    console.log('yes!');
  else
    console.log('nope :(');
} catch(err) {
  console.error(err);
  console.log('U.thingToString doesn\'t work!');
}
&#13;
&#13;
&#13;

整个想法是通过将每个对象直接放入数组中来序列化一些圆形结构。

E.g。如果您有这样的对象:

{
    val: 'hello',
    anotherVal: 'hi',
    circular: << a reference to itself >>
}

然后U.straighten将产生这种结构:

[
    0: {
        val: 1,
        anotherVal: 2,
        circular: 0 // Note that it's become possible to refer to "self" by index! :D
    },
    1: 'hello',
    2: 'hi'
]

只需几个额外的笔记:

  • 我已经在各种情况下使用这些功能很长一段时间了! 非常不太可能存在隐藏的错误。

  • O(n ^ 2)运行时问题可能会因为将每个对象映射到唯一的哈希值(可以实现)而失败。 O(n ^ 2)性质的原因是线性搜索必须用于查找已经被环化的项目。由于此线性搜索在已经线性的过程中发生,因此运行时变为O(n ^ 2)

  • 这些方法实际提供少量压缩!相同的内联值在不同的索引处不会出现两次。内联值的所有相同实例将映射到同一索引。 E.g:

    {
        hi: 'hihihihihihihihihihihi-very-long-pls-compress',
        ha: 'hihihihihihihihihihihi-very-long-pls-compress'
    }
    

    成为(U.straighten之后):

    [
        0: {
            hi: 1,
            ha: 1
        },
        1: 'hihihihihihihihihihihi-very-long-pls-compress'
    ]
    
  • 最后,如果使用此代码并不清楚,则非常容易!您只需要查看U.thingToStringU.stringToThing即可。这些功能的使用与JSON.stringifyJSON.parse的使用完全相同。

    var circularObj = // Some big circular object you have
    var serialized = U.thingToString(circularObj);
    var unserialized = U.stringToThing(serialized);