my web-application should able store , update (also load) json data on server. however, data may contain big arrays every time saved new entry appended.
my solution:
send updates server key-path within json data.
currently i'm sending data xmlhttprequest jquery, this
/** * asynchronously writes file on server (via php-script). * @param {string} file complete filename (path/to/file.ext) * @param content content should written. may js object. * @param {array} updatepath (optional), json only. not entire file written, * given path within object updated. default path supposed contain array , * content appended it. * @param {string} key (optional) in combination updatepath. if key provided, content written * field named parameters content @ data located @ updatepath old content. * * @returns {promise} */ io.write = function (file, content, updatepath, key) { if (utils.isobject(content)) content = json.stringify(content, null, "\t"); file = io.parsepath(file); var data = {f: file, t: content}; if (typeof updatepath !== "undefined") { if (array.isarray(updatepath)) updatepath = updatepath.join('.'); data.a = updatepath; if (typeof key !== "undefined") data.k = key; } return new promise(function (resolve, reject) { $.ajax({ type: 'post', url: io.url.write, data: data, success: function (data) { data = data.split("\n"); if (data[0] == "ok") resolve(data[1]); else reject(new error((data[0] == "error" ? "php error:\n" : "") + data.slice(1).join("\n"))); }, cache: false, error: function (j, t, e) { reject(e); //throw new error("error writing file '" + file + "'\n" + json.stringify(j) + " " + e); } }); }); }; on server, php script manages rest this:
- recieves data , checks if valid
- check if given file path writable
- if file exists , .json
- read , decode json
- return error on invalid json
- if there no update path given
- just write data
- if there update path given
- return error if update path in json data can't traversed (or file didn't exist)
- update data @ update-path
- write pretty-printed json file
however i'm not happy , problems kept coming last weeks.
my questions
- generally: how approach problem? alternative suggestions, databases? libraries help?
note: prefer solutions, use php or standart apache stuff. - one problem was, sometimes, multiple writes on same file triggered. avoid used promises (wrapped because read jquerys deferred stuff isnt promise/a compliant) client side, dont feel 100% sure working. there (file) lock in php works across multiple requests?
- every , json files break , not clear me how reproduce problem. @ time breaks, don't have history of happened. general debugging strategies client/server saving/loading process this?
- i wrote comet enable web server diffs on updates of json data structures. same reason. server keeps few version of json document , serves client different version of json document update need reason version of json data.
maybe reuse of code, written in c++ , coffeescript: https://github.com/torstenrobitzki/sioux
- if have concurrent write accesses data structure, sure, ever writes file has right version of file in mind when reading file?
Comments
Post a Comment