Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] database cache as tree of weak pointers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-02-29 (01:37)
From: Ingo Bormuth <Bormuth@w...>
Subject: [Caml-list] database cache as tree of weak pointers
  I need to implement a cache for a tree like database (directory service).
  So I thought about using objects linked together by weak pointers.
        module Str_Map = Map.Make (String);;
        class node (parent: node option) = object (self)
          val mutable node_map = Str_Map.empty
          method add (name: string) (other: node) =
            let weak_ptr = Weak.create 1 in
            Weak.set weak_ptr 0 (Some other);
            node_map <- Str_Map.add name weak_ptr node_map;
        let a = ref (new node None) ;;
        let b = ref (new node (Some !a)) ;;
        let c = ref (new node (Some !a)) ;;
        !a#add "B_IN_A" !b ;;
        !a#add "C_IN_A" !c ;;
  Every node has a reference to its parent node which ensures that
  its parent node will not be taken away by the GC. So every node
  stayes as long in memory until all its child nodes have been
  taken away.
  node_map maps the names of child nodes to their weak pointer.
  So I can access every node simply by following its path in the tree.
  I know every name is mapped to an array holding just oneweak pointer.
  That doesn't look very nice but I don't see an alternative.
  Is it possible to define the module Str_Map inside the node class ?
  Is this way really going to work propperly ?
  Is there a more elegant solution ?
  Is parent really a reference ?
  I really don't want to steal you time. I'm comming from C++ being
  relatively new to oCaml. It took me a long time to think of that
  little code snippet and I feel I need some feedback on wheather the
  way of thinking if the one intended by the language design.
  Thank you in advance - Ingo Bormuth
Extra-Konto: 2,50 %* Zinsen p. a. ab dem ersten Euro! Nur hier mit 25
Euro-Tankgutschein & ExtraPramie!

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: