constant database multitool

EXAMPLE


I. Basic Operations

The distribution contains a plain text datafile of airport codes
in ./data/airport:

  $ head ./data/airport
  #       $NetBSD: airport,v 1.30.26.1 2009/01/16 23:03:27 bouyer Exp $
  #       @(#)airport     8.1 (Berkeley) 6/8/93
  #
  # Some of this information from http://www.mapping.com/airportcodes.html.
  #
  # Airport Code : Airport
  AAA:Anaa, French Polynesia
  AAB:Arrabury, Queensland, Australia
  AAC:Al Arish, Egypt
  AAE:Annaba (El Mellah), Algeria

Inspection of the file shows that it is directly parseable by ezcdb.
Just use the "getline" input format specifying a `:' (colon)
delimiter between key and value (this format also ignores the
commented lines beginning with `#'):

  $ ezcdb make -d: airport.cdb <./data/airport

In this kind of database, it wouldn't do at all if duplicate keys
existed.  Each airport code should refer to one and only one airport,
or who knows where your flight might end up.  Check for a unique key
constraint on the database:

  $ ezcdb dupes -q airport.cdb && echo "no dupes"
  no dupes

Now perform a key lookup on the airport code "PDX":

  $ ezcdb get PDX airport.cdb
  Portland International, Oregon, USA

Search all record values beginning with "portland", case insensitive
(the output is in the default format, ending with an empty line):

  $ ezcdb match -i "portland*" airport.cdb
  +3,35:PDX->Portland International, Oregon, USA
  +3,29:PTJ->Portland, Victoria, Australia
  +3,42:PWM->Portland International Jetport, Maine, USA
  +3,37:TTD->Portland (Troutdale Airport), OR, USA

Repeat the match query, this time with an equivalent regular
expression and the grep command.  Note the explicit anchoring in
this regular expression compared to the implicit anchoring in the
wildcard expresson above.  Pipeline the results directly into
another cdb:

  $ ezcdb grep -i "^portland" airport.cdb | ezcdb make portland.cdb

Check the new cdb file with a dump (the output format here is in
the "exchange" format):

  $ ezcdb dump -x portland.cdb
  3:35    PDX:Portland International, Oregon, USA
  3:29    PTJ:Portland, Victoria, Australia
  3:42    PWM:Portland International Jetport, Maine, USA
  3:37    TTD:Portland (Troutdale Airport), OR, USA

Such an itinerary would earn a lot of frequent-flyer miles.


II. Set Operations

The airport code for Entebbe, Uganda, is EBB, innit?

  $ ezcdb get EBB airport.cdb
  Kampala (Entebbe), Uganda

Suppose it is necessary to delete this record from the database.
One could open and edit the original plaintext file, then rebuild
the cdb from the edited file.  Or one could use set operations
directly on the command line as follows:

  $ echo "EBB:" | ezcdb make -d: delete.cdb && ezcdb purge airport_new.cdb airport.cdb delete.cdb

The "echo" command here is used simply to pipe in a single record
with key "EBB" (and empty value) that ezcdb make will compile into
delete.cdb.  Then airport.cdb and delete.cdb are combined with the
ezcdb purge command.  The result of the "purge" is airport_new.cdb,
containing all the original records from airport.cdb, except those
with matching keys found in delete.cdb.  For this example, then,
the "EBB" record will be purged from airport_new.cdb:

  $ ezcdb get EBB airport_new.cdb || echo "not found"
  not found

The result file argument of a set operation command can safely name
either of the operand files.  The result file will then replace
the operand file:

  $ echo "EBB:" | ezcdb make -d: delete.cdb && ezcdb purge airport.cdb airport.cdb delete.cdb

This example is the same as above, except that now the record has
been deleted from the airport.cdb file directly:

  $ ezcdb get EBB airport.cdb || echo "not found"
  not found

The same technique can be used to add a new record with the "merge"
set operation:

  $ echo "FUP:Far Uppsala International, Gnubia" | ezcdb make -d: add.cdb && \
  ezcdb merge airport.cdb airport.cdb add.cdb

The airport.cdb database has now been modified with the new airport:

  $ ezcdb get FUP airport.cdb
  Far Up