~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Open Mash Cross Reference
mash/tcl/common/mash-softtable.tcl

Component: ~ [ mash ] ~ [ apps ] ~ [ gsm ] ~ [ lib ] ~ [ otcl ] ~ [ srm ] ~ [ tcl8.3 ] ~ [ tclcl ] ~ [ tk8.3 ] ~ [ tutorials ] ~

  1 import MashSoftState
  2 
  3 Class MashSoftTable
  4 
  5 #----------------------------------------------------------------------
  6 # Class:
  7 #   MashSoftTable
  8 # Description:
  9 #   MashSoftTable abstracts a soft-state table.  Each entry in the table
 10 #   is a key-value pair.   Entires expired after a timeout value, if it
 11 #   is not refreshed.
 12 #----------------------------------------------------------------------
 13 
 14 #----------------------------------------------------------------------
 15 # Method:
 16 #   MashSoftTable init
 17 # Description:
 18 #   Initialize timeout value for all soft-states in the table.
 19 # Arguments:
 20 #   timeout --
 21 #     Timeout value for each entry.
 22 #----------------------------------------------------------------------
 23 MashSoftTable instproc init { timeout } {
 24     $self instvar timeout_
 25     set timeout_ $timeout
 26 }
 27 
 28 
 29 #----------------------------------------------------------------------
 30 # Method:
 31 #   MashSoftTable set
 32 # Description:
 33 #   Set the table entry in the table associated with key to value.
 34 #----------------------------------------------------------------------
 35 MashSoftTable instproc set {key value} {
 36     $self instvar table_ timeout_
 37     if [info exists table_($key)] {
 38         $table_($key) set_value $value
 39     } else {
 40         set table_($key) [new MashSoftState $value $timeout_ "$self delete $key"]
 41     }
 42 }
 43 
 44 
 45 #----------------------------------------------------------------------
 46 # Method:
 47 #   MashSoftTable get
 48 # Description:
 49 #   Return the table entry with specified key.  An empty string is 
 50 #   returned if the entry does not exist, or has expired.
 51 #----------------------------------------------------------------------
 52 MashSoftTable instproc get {key} {
 53     $self instvar table_ 
 54     if {[info exists table_($key)] && ![$table_($key) is_expired]} {
 55         return [$table_($key) get_value]
 56     } else {
 57         return ""
 58     }
 59 }
 60 
 61 
 62 #----------------------------------------------------------------------
 63 # Method:
 64 #   MashSoftTable has
 65 # Description:
 66 #   Return true the table entry with specified key exists and has not
 67 #   expired.
 68 #----------------------------------------------------------------------
 69 MashSoftTable instproc has {key} {
 70     $self instvar table_ 
 71     if {[info exists table_($key)] && ![$table_($key) is_expired]} {
 72         return 1
 73     } else {
 74         return 0
 75     }
 76 }
 77 
 78 
 79 #----------------------------------------------------------------------
 80 # Method:
 81 #   MashSoftTable refresh
 82 # Description:
 83 #   Refresh an entry in the soft-state table
 84 #----------------------------------------------------------------------
 85 MashSoftTable instproc refresh {key} {
 86     $self instvar table_ 
 87     if {[info exists table_($key)] && ![$table_($key) is_expired]} {
 88         $table_($key) refresh
 89     } 
 90 }
 91 
 92 
 93 
 94 #----------------------------------------------------------------------
 95 # Method:
 96 #   MashSoftTable delete
 97 # Description:
 98 #   Delete the table entry with specified key
 99 #----------------------------------------------------------------------
100 MashSoftTable instproc delete {key} {
101     $self instvar table_
102     delete $table_($key)
103     unset table_($key)
104 }
105 
106 
107 #----------------------------------------------------------------------
108 # Method:
109 #   MashSoftTable destroy
110 # Description:
111 #   Delete the soft-state table.
112 #----------------------------------------------------------------------
113 MashSoftTable instproc destroy {} {
114     $self instvar table_
115     foreach key [array names table_] {
116         delete $table_($key) 
117     }
118     $self next
119 }
120 
121 
122 Class MashSoftTable/Adaptive -superclass MashSoftTable
123 
124 #----------------------------------------------------------------------
125 # Method:
126 #   MashSoftTable/Adaptive init
127 # Description:
128 #   Set the table entry in the table associated with key to value.
129 #----------------------------------------------------------------------
130 MashSoftTable/Adaptive instproc init {timeout {n 8}} {
131     $self next $timeout
132     $self instvar n_
133     set n_ $n
134 }
135 
136 
137 #----------------------------------------------------------------------
138 # Method:
139 #   MashSoftTable/Adaptive set
140 # Description:
141 #   Set the table entry in the table associated with key to value.
142 #----------------------------------------------------------------------
143 MashSoftTable/Adaptive instproc set {key value} {
144     $self instvar table_ timeout_ n_
145     if [info exists table_($key)] {
146         $table_($key) set_value $value
147     } else {
148         set table_($key) [new MashSoftState/Adaptive \
149             $value $timeout_ "$self delete $key" $n_]
150     }
151 }
152 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.