/
CDC-1.2b/
CDC-1.2b/src/
parent $misc
object $db

var $root child_index 2
var $root owners [$db]
var $root owned [$db]
var $root fertile 0
var $root inited 1
var $root manager $db
var $root writable [$db]
var $root readable ['parameters, 'methods, 'code]
var $root dbref 'db
var $db database #[]
var $root info ["Nothing more than a dictionary of names and objects."]

method init_db
    .perms(caller(), $root);
    database = #[];
.

method database
    return database;
.

method value_changed
    arg key, new_value;
    
    // change the value of a key.
    (> .remove(key) <);
    (> .insert(key, new_value) <);
.

method remove
    arg key;
    
    // remove a key/value from the database
    //THIS: is breaking the user_db (quick comment fix :)
    // .perms(sender(), 'writer);
    database = dict_del(database, key);
.

method exact_match
    arg key;
    var match;
    
    // get an exact match of a key, return the value
    match = (| database[key] |);
    if (match == ~keynf)
        throw(~matchnf, "No object by that key exists in the database.");
    return match;
.

method match_begin
    arg key;
    var matches, entry;
    
    // use match_begin of the key, return the value
    matches = [(| .exact_match(key) |)];
    if (!(matches[1])) {
        matches = [];
        for entry in (database) {
            if (match_begin(entry[1], key))
                matches = [@matches, entry[2]];
        }
    }
    if (matches) {
        if (listlen(matches) == 1)
            return matches[1];
        else
            throw(~ambig, "More than one object matches that key.", matches);
    } else {
        throw(~matchnf, "No entries in the database match that key.");
    }
.

method insert
    arg key, value;
    
    // insert a key/value to the database
    //  .perms(sender(), 'writer);
    database = dict_add(database, key, value);
.

method uninit_db
    .perms(caller(), $user);
    database = 0;
.

method key_changed
    arg old_key, new_key;
    var val;
    
    // change the value of a key.
    val = database[old_key];
    .remove(old_key);
    .insert(new_key, val);
.