relpipe-data/examples-guile-multiple-relations.xml
author František Kučera <franta-hg@frantovo.cz>
Mon, 21 Feb 2022 00:43:11 +0100
branchv_0
changeset 329 5bc2bb8b7946
parent 316 d7ae02390fac
permissions -rw-r--r--
Release v0.18

<stránka
	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
	
	<nadpis>Processing multiple relations with Scheme</nadpis>
	<perex>filter some relations and others keep unaffected</perex>
	<m:pořadí-příkladu>01800</m:pořadí-příkladu>

	<text xmlns="http://www.w3.org/1999/xhtml">
		
		<p>
			The <code>relpipe-tr-scheme</code> is capable to process multiple relations in a single pass.
			So we can filter some relations and let others flow unaffected through this pipeline step.
		</p>

		<m:pre jazyk="bash"><![CDATA[# define some sample relational functions:
r1() { relpipe-in-cli generate seq 1 i integer 1 2 3; }             # seq
r2() { relpipe-in-fstab; }                                          # fstab
r3() { find /usr/share/sounds/ -print0 | relpipe-in-filesystem; }   # filesystem

# put them together in a single stream function:
sample-data() { r1; r2; r3; }

# let them flow through our Scheme transformation:
sample-data \
	| relpipe-tr-scheme \
		--relation fstab \
			--where '(or (string= $type "btrfs") (string-prefix? "/mnt/" $mount_point) )' \
		--relation filesystem \
			--where '(and (> $size 8000) (< $size 9000) )' \
	| relpipe-out-tabular]]></m:pre>
	
		<p>Such script will generate something like this:</p>
		
		<m:pre jazyk="text"><![CDATA[seq:
 ╭─────────────╮
 │ i (integer) │
 ├─────────────┤
 │           1 │
 │           2 │
 │           3 │
 ╰─────────────╯
Record count: 3

fstab:
 ╭─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬───────────────────────────────────────┬────────────────┬────────────────╮
 │ scheme (string) │ device                      (string) │ mount_point (string) │ type (string) │ options                      (string) │ dump (integer) │ pass (integer) │
 ├─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼───────────────────────────────────────┼────────────────┼────────────────┤
 │                 │ /dev/sde                             │ /mnt/data            │ ext4          │ relatime,user_xattr,errors=remount-ro │              0 │              2 │
 │ UUID            │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home                │ btrfs         │ relatime                              │              0 │              2 │
 │                 │ /dev/mapper/sdf_crypt                │ /mnt/private         │ xfs           │ relatime                              │              0 │              2 │
 ╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴───────────────────────────────────────┴────────────────┴────────────────╯
Record count: 3

filesystem:
 ╭───────────────────────────────────────────────────────────────────────────┬───────────────┬────────────────┬────────────────┬────────────────╮
 │ path                                                             (string) │ type (string) │ size (integer) │ owner (string) │ group (string) │
 ├───────────────────────────────────────────────────────────────────────────┼───────────────┼────────────────┼────────────────┼────────────────┤
 │ /usr/share/sounds/Oxygen-Window-All-Desktops-Not.ogg                      │ f             │           8363 │ root           │ root           │
 │ /usr/share/sounds/KDE-Window-All-Desktops-Not.ogg                         │ f             │           8363 │ root           │ root           │
 │ /usr/share/sounds/Oxygen-Window-Close.ogg                                 │ f             │           8865 │ root           │ root           │
 │ /usr/share/sounds/KDE-Window-Close.ogg                                    │ f             │           8865 │ root           │ root           │
 │ /usr/share/sounds/KDE-Window-All-Desktops.ogg                             │ f             │           8712 │ root           │ root           │
 │ /usr/share/sounds/Oxygen-Window-All-Desktops.ogg                          │ f             │           8712 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/power-plug.oga                       │ l             │           8748 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/bell.oga                             │ f             │           8495 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/network-connectivity-established.oga │ l             │           8748 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/device-removed.oga                   │ f             │           8500 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/device-added.oga                     │ f             │           8748 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/power-unplug.oga                     │ l             │           8500 │ root           │ root           │
 │ /usr/share/sounds/freedesktop/stereo/network-connectivity-lost.oga        │ l             │           8500 │ root           │ root           │
 ╰───────────────────────────────────────────────────────────────────────────┴───────────────┴────────────────┴────────────────┴────────────────╯
Record count: 13]]></m:pre>


		<p>
			In Scheme, we have filtered the <code>fstab</code> and <code>filesystem</code> relations
			while the <code>seq</code> relation was kept intact.
		</p>
		
		<p>
			The <code>--relation</code> option accepts a regular expression.
			So it is possible to process more than one relation with it (having all needed attributes in each, of course). 
			If more <code>--relation</code> options (e.g. <code>'f.*'</code> and <code>fstab</code>) matches the same relation in the stream, only the first wins and transforms that relation.
		</p>
		
		<p>
			If we define some variables in the Scheme context, they will stay there – so we can pass data across relations.
			Thus we can do even JOIN, if we really want.
		</p>

		
	</text>

</stránka>