Memory tables

Read(399) Label: memory table, memory, len, keys, row, find, index,

An entity table can be imported into the memory to become a memory table, which is used like a normal table sequence:

 

A

1

=file("D:/file/dw/employees.ctx")

2

=A1.create()

3

=A2.memory(;right(Name,6)=="Garcia")

4

=A3(4)

A2 opens composite table employees.ctx. In A3, T.memory(C,…;w) function filters out records that meets the condition w and generates a memory table containing employee records where the surname is Garcia. We can view a memory table as we view a table sequence. Here’s A3’s result:

Also like a table sequence, we can access records in a memory table by their sequence numbers. A4 gets the 4th record from the memory table, for example:

Besides using an entity table to generate a memory table, we can use cs.memory() function to generate one from a cursor:

 

A

1

=file("D:/file/dw/employees.ctx")

2

=A1.create()

3

=A2.cursor(EID,Dept,Name; right(Name,6)=="Garcia")

4

=A3. memory()

5

=A4.len()

A3 selects from the employee records where surname is Garcia to geneate a cursor. A4 generates a memory table from this cursor:

Since the cursor contains only some of the fields in etable, the memory table generated from the cursor contains same fields, too. In A5, T.len() function gets the number of records in the memory table:

Use T.keys(K…) function to set the primary key for a memory table. Then use T.find(k) function and k.row(T) function to query or find records:

 

A

1

=file("D:/file/dw/employees.ctx")

2

=A1.create()

3

=A2.memory(;right(Name,6)== "Garcia")

4

>A3.keys(EID)

5

=A3.find(8559)

6

=8559.row(A3)

Since the primary key of the memory table is EID, the reset of the key in A4 can be skipped. A5 and A6 respectively find the specified record according to the primary key value 8559 and get the same result:

Then we can use T.index() function to create an index for the memory table to increase the query efficiency.

And we can use update and delete functions to update the memory table. Their uses are similar as they are with an entity table:

 

A

B

1

=file("D:/file/dw/employees.ctx")

=A1.create().attach(etable)

2

=B1.memory(;right(Name,6)=="Garcia")

 

3

=[3,13,87].(~.row(A2))

=A2.select(Gender=="F").derive()

4

>A3(1).Gender="UNKNOWN"

>B3.run(Gender="Female")

5

>A2.delete(A3)

>A2.update(B3)

6

=B1.cursor(;right(Name,6)=="Garcia").fetch()

 

To find details of updating the memory table, we execute the cellset code step by step. Below is the memory table in A2:

A3 gets records where EID is 3, 13 and 87 from A2:

B3 selects records of female employees from A2 and copies the result with derive function:

A4 modifies the first record in A3:

Then we find that A2’s memory table is modified:

B4 modifies records in B3. But since B3’s table sequence is a copy, the modification won’t affect the original memory table. Execute B4 and we get data in B3 as follows:

Execute A5, and three records where EID is 83, 78 and 200 are deleted from B2’s memory table:

Execute B5 and records of female employees in the memory table are modified. Note that the record where EID is 13 returns back to the memory table:

A6 again fetches data from the memory table:

We find that the modification of the memory table doesn’t affect the corresponding entity table. That’s because a memory table is independent of the entity table from which it is generated.