<% // -*- java -*- /* Copyright (c) 2005 Tony Garnock-Jones Copyright (c) 2005 LShift Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // The JSONDB namespace contains functions for working with an // extremely simple and toy filesystem-based database, similar to a // Python pickle database. // // A single file on the filesystem holds a serialized JSON object. // JSONDB.load retrieves that object from a named file; JSONDB.save // stores an object into such a file. // // The most interesting function here is JSONDB.transaction, which // uses ASP's Application.Lock()/.UnLock() functions to isolate a // transaction on the toy database. See below for details. // var JSONDB = { fs: Server.CreateObject("Scripting.FileSystemObject"), load: function (relpath) { var dbpath = Server.MapPath(relpath); try { var f = JSONDB.fs.OpenTextFile(dbpath, 1); var db = JSON.parse(f.ReadAll()); f.Close(); return db; } catch (e) { return undefined; } }, save: function (relpath, db) { var dbpath = Server.MapPath(relpath); var f = JSONDB.fs.CreateTextFile(dbpath); f.WriteLine(JSON.stringify(db)); f.Close(); }, // The JSONDB.transaction function first locks the application to // ensure serial access to the database, and then loads the // database, hands it into the handler function it was given, // collects the result, and saves the database back to disk before // returning the collected result. // // If the database doesn't exist on disk, the creator function is // called to build an initial database snapshot. // // Note that if any exceptions are thrown, the changes made to the // database are /not/ saved back to disk: this gives a very nice // simple transactional store for server-side AJAJ applications. // // Note also that we take good care to call Application.UnLock() // even if the transaction fails. // transaction: function (relpath, creator, handler) { try { Application.Lock(); var db = JSONDB.load(relpath); if (!db) { db = creator(); } var result = handler(db); JSONDB.save(relpath, db); return result; } finally { Application.UnLock(); } } }; %>