Recommended json data structure for storing chat log in a text file

I have a chat application where I need to keep a chat log for individual contacts. I am currently using a simple array as shown below to store in a TXT file.

var messages = [{_id:1, message : "Hello"}, {_id:$1, message : "Hello", }];

I see below problems with him if he grows.

  • Going back takes so much processing to convert it to an array from txt format.

  • Does so much cache.

But I feel this makes search messages easier. I would like to know if there are better alternatives to this structure.

Note. The reason for the preferred .txt file over indexed DB or webSQL is because I don't want to deal with storage restrictions.

+4
2

_id message plain array [id, message] object , . , . .

UPDATE:

, :

[[1,'message1'],[2,'message2'],[1,'message3']]

, , - :

var file = JSON.parse(fs.readFileSync('filepath.txt'));
for (var i = 0; i < file.length; i++) {
    messages.push({id: file[i][0], message: file[i][1]};
}

, , 2014-10-26.txt , , .

, , , read TTL , . , .

+6

, " json" File API, , - , "" . , .

" ", , , ( , .)

, {userid:XXX, message:"YYY"} .

monkeyinsight, JSON : [XXX,"YYY"]

: 1. 2. ,

var message_index["msg001.json","msg002.json", ... ];

, , 1000

var messages=[[userid,message], ...];

message_index, - .

, JSON , .

var messages=":1:message one:2:message two:3:another message:4:last message";

var getMsg= function( id, msgs ){
    var key= ":"+id+":";
    var ikey= msgs.search(key);
    var message={'id':id, 'message':""};
    if( ikey >= 0 ){
        var start= ikey+key.length;
        var end= msgs.indexOf(":", start );
        if( end > start ){
            message.message= msgs.substr(start, end-start); 
        }else{ 
            message.message= msgs.substr(start); 
        }
    }
    return message;
}

var m= getMsg(4, messages);   // returns: {id: 4, message: "last message"}

4 (, ": 1: a" vs "[1, 'a']," )

, , (':' ) .

+3

All Articles