tie_growfile.tcl

Go to the documentation of this file.
00001 /*  tie_growfile.tcl --*/
00002 /* */
00003 /*  Data source: Files.*/
00004 /* */
00005 /*  Copyright (c) 2006 Andreas Kupries <andreas_kupries@users.sourceforge.net>*/
00006 /* */
00007 /*  See the file "license.terms" for information on usage and redistribution*/
00008 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00009 /*  */
00010 /*  RCS: @(#) $Id: tie_growfile.tcl,v 1.1 2006/03/08 04:55:58 andreas_kupries Exp $*/
00011 
00012 /*  ### ### ### ######### ######### #########*/
00013 /*  Requisites*/
00014 
00015 package require snit
00016 package require tie
00017 
00018 /*  ### ### ### ######### ######### #########*/
00019 /*  Implementation*/
00020 
00021 snit::type ::tie::std::growfile {
00022     /*  ### ### ### ######### ######### #########*/
00023     /*  Notes*/
00024 
00025     /*  This data source is geared towards the storage of arrays which*/
00026     /*  will never shrink over time. Data is always appended to the*/
00027     /*  files associated with this driver. Nothing is ever*/
00028     /*  removed. Compaction does not happen either, so modification of*/
00029     /*  array entries will keep the old information around in the history.*/
00030 
00031     /*  ### ### ### ######### ######### #########*/
00032     /*  Specials*/
00033 
00034     pragma -hastyperet s no
00035     pragma -hasinfo        no
00036     pragma -simpledispatch yes
00037 
00038     # ### ### ### ######### ######### #########
00039     ## API : Construction & Destruction
00040 
00041     constructor (type thepath) {
00042     # Locate and open the journal file.
00043 
00044     set path [file normalize $thepath]
00045     if {[file exists $path]} {
00046         set chan [open $path {RDWR EXCL APPEND}]
00047     } else {
00048         set chan [open $path {RDWR EXCL CREAT APPEND}]
00049     }
00050     fconfigure $chan -buffering none -encoding utf-8
00051     return
00052     }
00053 
00054     destructor {
00055     /*  Release the channel to the journal file, should it be open.*/
00056     if {$chan ne ""} {close $chan}
00057     return
00058     }
00059 
00060     /*  ### ### ### ######### ######### #########*/
00061     /*  API : Data source methods*/
00062 
00063     ret  get () {
00064     if {![file size $path]} {return {}}
00065     $self LoadJournal
00066     return [array get cache]
00067     }
00068 
00069     ret  names () {
00070     if {![file size $path]} {return {}}
00071     $self LoadJournal
00072     return [array names cache]
00073     }
00074 
00075     ret  size () {
00076     if {![file size $path]} {return 0}
00077     $self LoadJournal
00078     return [array size cache]
00079     }
00080 
00081     ret  getv (type index) {
00082     if {![file size $path]} {
00083         return -code error "can't read \"$index\": no such variable"
00084     }
00085     $self LoadJournal
00086     return $cache($index)
00087     }
00088 
00089     ret  set (type dict) {
00090     puts  -nonewline $chan $dict
00091     puts  -nonewline $chan { }
00092     flush            $chan
00093     return
00094     }
00095 
00096     ret  setv (type index , type value) {
00097     puts  -nonewline $chan [list $index $value]
00098     puts  -nonewline $chan { }
00099     flush            $chan
00100     return
00101     }
00102 
00103     ret  unset (optional pattern =*) {
00104     return -code error \
00105         "Deletion of entries is not allowed by this data source"
00106     }
00107 
00108     ret  unsetv (type index) {
00109     return -code error \
00110         "Deletion of entries is not allowed by this data source"
00111     }
00112 
00113     /*  ### ### ### ######### ######### #########*/
00114     /*  Internal : Instance data*/
00115 
00116     variable chan {} ; /*  Channel to write the journal.*/
00117     variable path {} ; /*  Path to journal file.*/
00118 
00119     /*  Journal loading, and cache.*/
00120 
00121     variable count 0         ; /*  #Operations in the journal.*/
00122     variable cvalid 0        ; /*  Validity of the cache.*/
00123     variable cache -array {} ; /*  Cache for journal*/
00124 
00125     /*  Management of the cache: See notes at beginning.*/
00126 
00127     /*  ### ### ### ######### ######### #########*/
00128     /*  Internal: Loading from the journal.*/
00129 
00130     ret  LoadJournal () {
00131     if {$cvalid} return
00132     set cvalid 1
00133 
00134     set in [open $path r]
00135     array set cache [read $in]
00136     close $in
00137     return
00138     }
00139 
00140     /*  ### ### ### ######### ######### #########*/
00141 }
00142 
00143 /*  ### ### ### ######### ######### #########*/
00144 /*  Ready to go*/
00145 
00146 ::tie::register ::tie::std::growfile as growfile
00147 package provide   tie::std::growfile 1.0
00148 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1