?? tcl.html
字號:
<html><head><title>Metakit for Tcl</title></head><body bgcolor="#ffffff"><a name="top"></a>
<a href="http://www.scriptics.com/"><img src="tcl_files/tcl.gif" align="left" border="0" height="75" width="51"></a>
<a href="http://www.equi4.com/"><img src="tcl_files/e4s.gif" align="right" border="0" height="35" vspace="3" width="97"></a>
<center>
<h2> Metakit for Tcl</h2>
<i>The structured database which fits in the palm of your hand </i>
<p>
[ <a href="#Overview">Overview</a>
| <a href="#Terminology">Terminology</a>
| <a href="#inst">Installation</a>
| <a href="#start">Getting started</a>
| <a href="#ref">Mk4tcl Reference</a>
]
</p></center>
<p>
<b>Buzzwords</b> - <a href="http://www.equi4.com/metakit/">Metakit</a>
is an embeddable database which runs on Unix, Windows, Macintosh, and
other platforms. It lets you build applications which store their data
efficiently, in a portable way, and which will not need a complex
runtime installation. In terms of the data model, Metakit takes the
middle ground between RDBMS, OODBMS, and flat-file databases - yet it
is quite different from each of them.
</p><p>
<b>Technology</b> - Everything is stored variable-sized yet with
efficient positional row access. Changing an existing datafile
structure is as simple as re-opening it with that new structure. All
changes are transacted. You can mix and match software written in C++,
Python, and Tcl. Things can't get much more flexible...
</p><p>
<b>Tcl/Tk</b> - The extension for <a href="http://www.tcl.tk/">Tcl</a>
is called "Mk4tcl". It is being used in a number of commercial
projects, for in-house use as well as in commercially distributed
products.
</p><p>
<b>Mk4tcl 2.4.9.6</b> - is a final/production release. The <a href="http://www.equi4.com/metakit.html">homepage</a>
points to a download area with pre-compiled shared libraries for Unix,
Windows, and Macintosh. The Metakit source distribution includes this
documentation, the Mk4tcl C++ source code, a small Tcl test suite, a
"mkshow.tcl" utility which lets you examine data in any Metakit
datafile from the command line, and a few more goodies.
</p><p>
<b>License and support</b> - Metakit 2 and up are distributed under the liberal
X/MIT-style open source license. Commercial support is available through an Enterprise License. See the <a href="http://www.equi4.com/mklicense.html">license</a> page for details.
</p><p>
<b>Credits</b> - Are due to Mark Roseman for providing the initial
incentive and feedback, and to Matt Newman for a range of suggestions
and ideas. Evidently, Mk4tcl could not exist without the Tcl/Tk
scripting platform and its superb extensibility.
</p><p>
<b>Updates</b> - The latest version of this document is at
<a href="http://www.equi4.com/metakit/tcl.html">http://www.equi4.com/metakit/tcl.html</a>.
<a name="Overview"><hr size="1"></a></p><h2>Overview</h2>
Metakit is a machine- and language-independent toolkit for storing and managing
structured data. This is a description of the <i>Mk4tcl</i> extension, which
allows you to create, access, and manipulate Metakit datafiles using Tcl.
Here is a Tcl script which selects, sorts,
and displays some previously stored results:
<pre> mk::file open db phonebook.dat -readonly
foreach i [mk::select db.persons -glob name "Jon*" -sort date] {
puts "Found [mk::get db.persons!$i name phone date]"
}</pre>
This script illustrates how easy it is to access stored data from Tcl.
What it does not show, however, is that numeric data can be stored in binary
format (yet remain fully portable), that datafiles can contain complex (nested)
datastructures, that the structure of datafiles can be adjusted at any time,
and that all modifications use the commit / rollback transaction model.
<p>
In actual use, Metakit resembles more an array manipulation
package than a database - with the main access mechanism being
<i>'by position'</i>, not by primary key.
The Tcl interface does not yet cover
all operations provided by the complete C++ interface of Metakit,
but as the <i>mk::select</i> command illsutrates, it
does include quite flexible forms of searching and sorting.
</p><p>
</p><p><a name="Terminology"><hr size="1"></a></p><h2>Terminology</h2>
There are several ways to say the same thing, depending on where you're
coming from. For example, the terms <i>table</i>, <i>list</i>, <i>collection</i>,
<i>array</i>, <i>sequence</i>, and <i>vector</i> all
denote a more or less similar concept.
To help avoid confusion, Metakit uses a simple
(but hopefully precise) terminology.
<p>
The terms adopted by Metakit can be summarized as follows:
</p><p>
</p><ul>
<li>A <b>view</b> is an indexable collection of <b>rows</b>
(a <i>table</i> of <i>records</i>, an <i>array</i> of <i>elements</i>).
</li><li>An <b>index</b> is a position in a <i>view</i>, used to specify a <i>row</i>
(the first row is at index zero).
</li><li>Each view has an ordered
set of <b>properties</b>, used to refer to the data values of each row.
</li><li>In Metakit, each (<i>view</i>, <i>index</i>, <i>property</i>) combination denotes
a single data value.
</li><li>A different way to describe this combination would be:
(<i>matrix</i>, <i>row-index</i>, <i>column-id</i>).
</li><li>Data values can be strings, numeric, untyped data,
or a nested view, called a <b>subview</b>.
</li><li>A <b>cursor</b> is a reference to a specific row in a specific view,
i.e. a (<i>view</i>, <i>index</i>) tuple.
</li></ul>
<p>
The <i>Mk4tcl</i> extension adds several notational conventions:
</p><p>
</p><ul>
<li>A <b>tag</b> is an identifier used to refer to an open datafile.
</li><li>Top-level views are specified as <b>tag.viewname</b>.
</li><li>Row <b>N</b> in such a view can be specified as <b>tag.viewname!N</b>.
</li><li>Subviews extend this notation, e.g. <b>tag.viewname!N.subview</b>.
</li><li>Sub-rows continue in the same way, e.g. <b>tag.viewname!N.subview!M</b>.
</li><li>The specification of a view (either top-level or subview) is called a <b>path</b>.
</li><li>Thus, both <i>tag.viewname</i> and <i>tag.viewname!N.subview</i> are paths.
</li><li>In <i>Mk4tcl</i>, a cursor placed at the Nth row is equivalent to the string "<b>path!N</b>".
</li><li>A trailing row index is allowed and ignored wherever a path is expected.
</li><li>As a result, cursors are allowed (and frequently used) as path arguments.
</li></ul>
<p>
A few more comments about the semantics of Metakit:
</p><p>
</p><ul>
<li>Views are <i>homogenous</i>: each row in a view contains the same type of
information.
</li><li>This also implies that all subviews within the same view always
have the same structure.
</li><li>Rows are either part of a view on file, or <i>temporary</i> (gone
when no longer referenced).
</li><li>A cursor need not point to an existing row
(its current position may be out of range).
</li></ul>
<p>
<a name="inst"><hr size="1"></a></p><h2>Installation</h2>
<ol>
<li>Download the latest version from <a href="http://www.equi4.com/pub/mk/">http://www.equi4.com/pub/mk/</a>
</li><li>On Unix, rename the appropriate compiled extension to "Mk4tcl.so" (on Win/Mac, use the corresponding file)
</li><li>Do a small test, by running "demo.tcl". If all is well, you should get some self-explanatory output
</li><li>Place the extension somewhere on Tcl's package search path (or just leave it in ".")
</li></ol>
<p>
<a name="start"><hr size="1"></a></p><h2>Getting started</h2>
Create a datafile:
<blockquote><pre>package require Mk4tcl
mk::file open db datafile.mk</pre></blockquote>
Create a view (this is the Metakit term for "table"):
<blockquote><pre>set vw [mk::view layout db.people {first last shoesize:I}]</pre></blockquote>
Add two rows (this is the Metakit term for "record"):
<blockquote><pre>mk::row append $vw first "John" last "Lennon" shoesize 44
mk::row append $vw first "Flash" last "Gordon" shoesize 42</pre></blockquote>
Commit the changes to file:
<blockquote><pre>mk::file commit db</pre></blockquote>
Show a list of all people:
<blockquote><pre>mk::loop c $vw {puts [mk::get $c first last shoesize]}</pre></blockquote>
Show a list of all people, sorted by last name:
<blockquote><pre>foreach r [mk::select $vw -sort last] {puts [mk::get $vw!$r]}</pre></blockquote>
Show a list of all people with first name 'John':
<blockquote><pre>foreach r [mk::select $vw first "John"] {puts [mk::get $vw!$r]}</pre></blockquote>
<p>
<a name="ref"><hr size="1"></a>
</p><dl><dt></dt><h2>Mk4tcl Reference</h2><dd>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><a href="#mk_file">mk::file</a></td><td width="20"></td><td>Opening, closing, and saving datafiles</td>
</tr><tr><td><a href="#mk_view">mk::view</a></td><td width="20"></td><td>View structure and size operations</td>
</tr><tr><td><a href="#mk_cursor">mk::cursor</a></td><td width="20"></td><td>Cursor variables for positioning</td>
</tr><tr><td><a href="#mk_row">mk::row</a></td><td width="20"></td><td>Create, insert, and delete rows</td>
</tr><tr><td><a href="#mk_get">mk::get</a></td><td width="20"></td><td>Fetch values</td>
</tr><tr><td><a href="#mk_set">mk::set</a></td><td width="20"></td><td>Store values</td>
</tr><tr><td><a href="#mk_loop">mk::loop</a></td><td width="20"></td><td>Iterate over the rows of a view</td>
</tr><tr><td><a href="#mkselect">mk::select</a></td><td width="20"></td><td>Selection and sorting</td>
</tr><tr><td><a href="#mk_channel">mk::channel</a></td><td width="20"></td><td>Channel interface (new in 1.2)</td>
</tr></tbody></table><br>
<p></p></dd><dt><a name="mk_file"><hr size="1"></a></dt><h2>mk::file</h2><dd><h3>Opening, closing, and saving datafiles</h3>
<p></p></dd><dt>SYNOPSIS</dt><dd><b>mk::file</b> <b>open</b> <br>
<b>mk::file</b> <b>open</b> <i>tag</i> <br>
<b>mk::file</b> <b>open</b> <i>tag</i> <i>filename</i> ?-readonly? ?-nocommit? ?-extend? ?-shared? <br>
<b>mk::file</b> <b>views</b> <i>tag</i> <br>
<b>mk::file</b> <b>close</b> <i>tag</i> <br>
<b>mk::file</b> <b>commit</b> <i>tag</i> ?-full? <br>
<b>mk::file</b> <b>rollback</b> <i>tag</i> ?-full? <br>
<b>mk::file</b> <b>load</b> <i>tag</i> <i>channel</i> <br>
<b>mk::file</b> <b>save</b> <i>tag</i> <i>channel</i> <br>
<b>mk::file</b> <b>aside</b> <i>tag</i> <i>tag2</i> <br>
<b>mk::file</b> <b>autocommit</b> <i>tag</i> <br>
<p></p></dd><dt>DESCRIPTION</dt><dd>
The <i>mk::file</i> command is used to open and close Metakit datafiles.
It is also used to force pending changes to disk (<i>commit</i>),
to cancel the last changes (<i>rollback</i>), and to send/receive the entire
contents of a datafile over a Tcl <i>channel</i>, including sockets
(<i>load/save</i>).
<p>
Without arguments, '<b>mk::file open</b>' returns the list of tags and filenames of all datasets which are currently open (of the form <i>tag1 name1 tag2 name2 ...</i>).
</p><p>
The '<b>mk::file open</b>' command associates a datafile with a
unique symbolic <i>tag</i>. A tag must consist of alphanumeric characters,
and is used in the other commands to refer to a specfic open datafile.
If <i>filename</i> is omitted, a temporary in-memory dataset is created (which cannot use commit, but which you could save to an I/O channel).
When a datafile is closed, all pending changes will be written to file,
unless the <b>-nocommit</b> option is specified. In that case, only an
explicit commit will save changes. To open a file only for reading,
use the <b>-readonly</b> option.
Datafiles can be opened read-only by any number
of readers, or by a single writer (no other combinations are allowed).
There is an additional mode, specified by the <b>-extend</b> option: in this
case changes are always written at the end of the datafile. This allows
modifications by one writer without affecting readers. Readers can adjust
to new changes made that way by doing a "rollback" (see below). The term
is slightly confusing in this case, since it really is a "roll-forward" ...
The <b>-shared</b> option causes an open datafile to be visible in every
Tcl interpreter, with thread locking as needed. The datafile is still tied
to the current interpreter and will be closed when that interpreter is
terminated.
</p><p>
The '<b>mk::file views</b>' command returns a list with the views
currently defined in the open datafile associated with <i>tag</i>.
You can use the <i>'mk::view layout'</i> command to determine the current
structure of each view.
</p><p>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -