zlib: Don't use PASTE for INTMAX error messages
[jimtcl.git] / examples / metakit.tcl
blob0d544d1a86cae7a6b86406e4c3e2c74133005f5c
1 package require mk
3 # These will become subcommands of every view handle
5 # Looping using cursors
6 proc {mk.view each} {view arrayVar script} {
7 upvar 1 $arrayVar array
8 for {set cur $view!0} {[cursor valid $cur]} {cursor incr cur} {
9 set array [cursor get $cur]
10 uplevel 1 $script
14 # Shortcuts to avoid cursors for one-time operations
15 proc {mk.view set} {view pos args} {
16 tailcall cursor set $view!$pos {*}$args
18 proc {mk.view append} {view args} {
19 tailcall cursor set $view!end+1 {*}$args
21 proc {mk.view insert} {view pos args} {
22 # Note that this only inserts fresh rows and doesn't set any data
23 tailcall cursor insert $view!$pos {*}$args
26 # Dump a view to stdout
27 proc {mk.view dump} {view} {
28 $view each row {puts " $row"}
31 # -----------------------------------------------------------------------------
33 # Open an in-memory database
34 set db [storage]
36 # Specify the view structure, creating new views and restructuring existing
37 # ones as necessary
38 $db structure firstview {key string first string}
39 $db structure secondview {key string second string}
41 # Open them.
42 [$db view firstview] as fstview
43 # Or equivalently (using pipeline notation)
44 $db view secondview | as sndview
46 # Use the helpers defined above to populate the first view
47 $fstview set 0 key foo first bar
48 $fstview append key hello first world
49 $fstview insert 0
50 $fstview set 0 key metakit first example
52 # Or use cursors directly. A end-X/end+X cursor moves automatically when
53 # the view size changes.
54 set cur $sndview!end+1
55 cursor set $cur key foo second baz
56 cursor set $cur key hello second goodbye
57 cursor set $cur key silly second examples
59 puts "First view:"
60 $fstview dump
61 puts "Second view:"
62 $sndview dump
64 puts "\nNow trying view operations. Note that all the binary operations"
65 puts "are left-biased when it comes to conflicting property values.\n"
67 puts "Join on key:" ;# Common subset of the two outer joins below
68 $fstview join $sndview key | dump
69 puts "Outer join on key:" ;# Will yield more rows than an inner join
70 $fstview join $sndview -outer key | dump
71 puts "Outer join on key, in reverse order:"
72 $sndview join $fstview -outer key | dump
74 puts "Cartesian product:"
75 $fstview product $sndview | dump
77 puts "Pairing:"
78 $fstview pair $sndview | dump
79 puts "Pairing, in reverse order:"
80 $sndview pair $fstview | dump
82 puts "Complex pipeline (fetch rows 3,5,.. from the cartesian product and sort"
83 puts "them on the 'first' property):"
84 $fstview product $sndview | range 3 end 2 | sort first | dump
85 # Slice step defaults to 1. Sorting may be performed on several properties at
86 # a time, prepending a "-" (minus sign) will cause the sort order to be reversed.
88 puts "Another one (fetch the unique key values from the cartesian product):"
89 $fstview product $sndview | project key | unique | dump
90 # Use "without" to remove certain properties.
92 puts "Keys in the cartesian product not in the reverse pairing:"
93 [$fstview product $sndview | project key | unique] minus [$sndview pair $fstview | unique] | dump
94 # Union "union", intersection "intersect" and symmetric difference "different"
95 # are also available. They all work only if the rows are unique.
97 puts "Create a subview:"
98 $fstview product $sndview | group subv key | as complexview | dump
99 # Not so informative as subviews are not displayed properly. Several grouping
100 # properties may be specified.
101 puts "Get its values for row #0:"
102 cursor get $complexview!0 subv | dump
103 puts "And flatten it back:"
104 $complexview flatten subv | dump
106 puts "Remove a row:"
107 cursor remove $sndview!1
108 $sndview dump
109 # Several rows may be removed at once by specifying a row count
110 puts "Clear the view:"
111 $sndview resize 0
112 $sndview dump