Red Databases

From Programming in RED
Jump to navigation Jump to search

Red can see its blocks as tables:

Take a data block like the following:

myblock: [

    key1 value1 value2 value3 key2 value1 value2 value3 key3 value1 value2 value3 key4 value1 value2 value3 key5 value1 value2 value3

]

Red can see it as a long list of data with no special grouping but want red to threat it as a table with fixed size records, use the /SKIP refinement. Skip can be used either in find or in select:


select/skip myblock 'key3 4 find/skip myblock 'key3 4

And Red will thread it as a groups of records of fixed size.


myblock: [

    key1 value1 value2 value3
    key2 value1 value2 value3
    key3 value1 value2 value3
    key4 value1 value2 value3
    key5 value1 value2 value3

]

In the actual implementation select/skip will search the key on the first column and also return the data in the adjacent one, value1 (but in further implementation could become more flexible)

To have full database functionality you can use the following code:

Find

find/skip (skip b <value>) key <record-size> >> key: 1 b: [0 0 0 1 1 1 2 2 2] find/skip (skip b 2) key 3 == [1 2 2 2] -- search by 3rd column of b (source)[1]


Select

myselect: func [data key len pos][

   pick find/skip data key len pos

] myselect mydata 'key1 4 1 (Source)[2]

Select an item >> copy/part find/skip myblock [key2 value1] 4 4 == [

   key2 value1 value2 value3

]

Find an item >> third find/skip myblock [key2 value1] 4 == value2 (Source)[3]