PersistentTable - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Packages : PersistentTable

Overview of the PersistentTable package

 

Calling Sequence

Description

List of PersistentTable Package Commands

Caveats

Thread Safety

Examples

Compatibility

Calling Sequence

PersistentTable:-command(arguments)

command(arguments)

Description

• 

The PersistentTable package provides a connection object that behaves somewhat like a table, except it is (by default) backed by a file containing an SQLite table. As a consequence, any information stored in the table persists when Maple is shut down or restarted. Furthermore, there is some extra functionality for searching through the stored information.

• 

The interface is deliberately kept simple; it does not come close to giving access to all of SQLite's functionality. If you need more functionality, you can use the package Database:-SQLite. However, the PersistentTable package is easier to use for programmers who have experience with Maple, but not much experience with SQL. Conversely, if you don't need persistent storage or the extra searching functionality provided by this package, you are probably better off using a plain table.

List of PersistentTable Package Commands

• 

The commands in the PersistentTable package are:

Close

Count

Get

GetAll

GetKeys

Has

MaybeGet

Open

RawCommand

Set

 

 

• 

A PersistentTable object has a fixed number of columns with associated types, say k. The first column, or the first n columns for some positive integer n <= k, are taken to make up the primary key of the object. This means that when viewed as a key/value store, the object's keys are all expression sequences of n Maple expressions, and the values are expression sequences of n - k Maple expressions.

Caveats

• 

Storing values in columns with data type anything converts them to the so-called "dot m" format, as generated by the %m specifier for the sprintf command and as described in the Format,m help page. Retrieving them translates them back to Maple. If your value will not survive such a process (for example, if it involves locals from modules), use of persistent tables will lead to unexpected results.

• 

If float columns are used, be aware that the float operations done inside SQLite, and conversion to SQLite and back, do not necessarily comply with the rules with respect to precision that hold in Maple. Similarly, any constraints involving equality of expressions involving floating-point computation should be viewed with suspicion.

Thread Safety

• 

Internally, if there are multiple connection objects for the same file at one time in a single Maple session (presumably, though not necessarily, with a different prefix), and the file is specified in the same way for all invocations of Open (in particular, the string is the same after being run through FileTools:-AbsolutePath), only a single connection object is created through SQLite:-Open. It is required that either all of these connections are read-only, or none of them are. (Connections to different files can, of course, have different read-only status.)  Referring to the same file through different paths is not advised. SQLite enforces certain restrictions as to opening the same file in different concurrent Maple sessions; this is, therefore, also not advised.

• 

This is the only thread safety issue - persistent tables are thread safe as of Maple 2021 otherwise.

• 

For more information on thread safety, see index/threadsafe.

Examples

withPersistentTable&colon;

Generate three file names that we can overwrite.

path1FileTools:-JoinPathFileTools:-TemporaryDirectory&comma;foo.mks

path1/tmp/mpldoc2/foo.mks

(1)

path2FileTools:-JoinPathFileTools:-TemporaryDirectory&comma;bar.mks

path2/tmp/mpldoc2/bar.mks

(2)

path3FileTools:-JoinPathFileTools:-TemporaryDirectory&comma;baz.mks

path3/tmp/mpldoc2/baz.mks

(3)

Make sure these files do not currently exist.

FileTools:-RemoveopselectFileTools:-Exists&comma;path1&comma;path2&comma;path3

Create the first persistent table: entries are indexed by two integers and the value is an arbitrary Maple expression.

store1Openpath1&comma;style=k1::integer&comma;k2::integer&comma;v::anything&comma;primarykey=2

store1<< 3-column persistent table at /tmp/mpldoc2/foo.mks >>

(4)

The expression that is the value can be, for example, a list.

Setstore1&comma;3&comma;5&comma;2&comma;4&comma;6&comma;1&comma;3

One can access this value using the Get command or indexing.

Getstore1&comma;3&comma;5

2&comma;4&comma;6&comma;1&comma;3

(5)

store13,5

2&comma;4&comma;6&comma;1&comma;3

(6)

The Has and Count commands give us information about the existence of entries.

Hasstore1&comma;3&comma;5

true

(7)

Hasstore1&comma;4&comma;5

false

(8)

Countstore1&comma;k1=3

1

(9)

Countstore1&comma;k1=4

0

(10)

Setstore1&comma;3&comma;5&comma;2&comma;4

store13,5

2&comma;4

(11)

store13,6x&comma;y

store13,6x&comma;y

(12)

Countstore1&comma;k1=3

2

(13)

We can use the RawCommand command to manage transactions.

RawCommandstore1&comma;BEGIN TRANSACTION

store13,26

store13,26

(14)

Countstore1&comma;k1=3

3

(15)

RawCommandstore1&comma;ROLLBACK TRANSACTION

Countstore1&comma;k1=3

2

(16)

store13,26

store13,26

(17)

Countstore1&comma;k1=3

3

(18)

The Get command generates an error if you try to access an entry that does not exist. Indexing is the same. The MaybeGet command instead returns a default value that you specify.

MaybeGetstore1&comma;3&comma;6&comma;FAIL

x&comma;y

(19)

MaybeGetstore1&comma;3&comma;3&comma;FAIL

FAIL

(20)

store2Openpath2&comma;style=k1::integer&comma;k2::integer&comma;v::anything&comma;b::boolean&comma;primarykey=2

store2<< 4-column persistent table at /tmp/mpldoc2/bar.mks >>

(21)

Getstore2&comma;3&comma;5

Error, (in PersistentTable:-Get) bad index into PersistentTable

store21,4

Error, bad index into PersistentTable

The GetAll command returns all rows satisfying some criterion. The GetKeys command returns information from the same rows, but returns only the primary key columns.

store21,4sqrt5,true

store21,45,true

(22)

GetAllstore2&comma;b=true

1&comma;4&comma;5&comma;true

(23)

GetKeysstore2&comma;b=true

1&comma;4

(24)

Closestore1&semi;Closestore2

You can't open two connections to the same file with different readonly settings, even if they have different prefixes:

store3Openpath3&comma;style=k::anything&comma;v::anything

store3<< 2-column persistent table at /tmp/mpldoc2/baz.mks >>

(25)

store4Openpath3&comma;prefix=duck&comma;style=k1::integer&comma;k2::integer&comma;v1::anything&comma;v2::integer&comma;v3::anything&comma;primarykey=2&comma;indexv2&comma;readonly=true

Error, (in PersistentTable:-Open) tried to connect to /tmp/mpldoc2/baz.mks, which is already open with a different read-only status

store4Openpath3&comma;prefix=duck&comma;style=k1::integer&comma;k2::integer&comma;v1::anything&comma;v2::integer&comma;v3::anything&comma;primarykey=2&comma;indexv2

store4<< 5-column persistent table at /tmp/mpldoc2/baz.mks with prefix duck_ >>

(26)

store5Openpath3&comma;prefix=drijfsijs&comma;style=k1::integer&comma;v1::float&comma;v2::float

store5<< 3-column persistent table at /tmp/mpldoc2/baz.mks with prefix drijfsijs_ >>

(27)

store31+xy

store31+xy

(28)

store33&comma;4y

store33&comma;4y

(29)

Hasstore3&comma;1

false

(30)

Hasstore3&comma;1+x

true

(31)

Hasstore4&comma;3&comma;4

false

(32)

store43,4duck,2,bill

store43,4duck,2,bill

(33)

store42,4goose,3,egg

store42,4goose,3,egg

(34)

Hasstore4&comma;3&comma;4

true

(35)

store31+x

y

(36)

store43,4

duck,2,bill

(37)

store42,4

goose,3,egg

(38)

GetAllstore4&comma;v2=3

2&comma;4&comma;goose&comma;3&comma;egg

(39)

GetAllstore4&comma;v2=4

(40)

Because the key and value are arbitrary Maple expressions, you cannot use inequalities when calling GetAll on store3: the comparison needs to be carried out inside the database engine, and it does not understand Maple expressions.

GetAllstore3&comma;k<5

Error, (in PersistentTable:-GetAll) invalid condition (k < 5): column k is of type anything, so it does not support inequalities

However, store4 supports arbitrary polynomial conditions involving its integer columns:

GetAllstore4&comma;k13<k2+v222

2&comma;4&comma;goose&comma;3&comma;egg

(41)

Setstore5&comma;3&comma;2.1&comma;3.5

Setstore5&comma;7&comma;Float&comma;7.4

GetAllstore5

3&comma;2.10000000000000009&comma;3.50000000000000000&comma;7&comma;Float&comma;7.40000000000000036

(42)

GetAllstore5&comma;k1<v1

7&comma;Float&comma;7.40000000000000036

(43)

Closestore3&semi;Closestore4&semi;Closestore5

restart

withPersistentTable&colon;

path1FileTools:-JoinPathFileTools:-TemporaryDirectory&comma;foo.mks

path1/tmp/mpldoc2/foo.mks

(44)

path2FileTools:-JoinPathFileTools:-TemporaryDirectory&comma;bar.mks

path2/tmp/mpldoc2/bar.mks

(45)

path3FileTools:-JoinPathFileTools:-TemporaryDirectory&comma;baz.mks

path3/tmp/mpldoc2/baz.mks

(46)

The files should still be present.

store1Openpath1

store1<< 3-column persistent table at /tmp/mpldoc2/foo.mks >>

(47)

store13,5

2&comma;4

(48)

Countstore1&comma;k1=3

3

(49)

Countstore1&comma;k1=4

0

(50)

MaybeGetstore1&comma;3&comma;6&comma;FAIL

x&comma;y

(51)

MaybeGetstore1&comma;3&comma;3&comma;FAIL

FAIL

(52)

store2Openpath2&comma;readonly

store2<< 4-column persistent table at /tmp/mpldoc2/bar.mks >>

(53)

store21,4

5,true

(54)

store21,46

Error, attempt to write a readonly database

GetAllstore2&comma;b=true

1&comma;4&comma;5&comma;true

(55)

Closestore1&semi;Closestore2

store3Openpath3

store3<< 2-column persistent table at /tmp/mpldoc2/baz.mks >>

(56)

store4Openpath3&comma;prefix=duck

store4<< 5-column persistent table at /tmp/mpldoc2/baz.mks with prefix duck_ >>

(57)

Hasstore3&comma;1

false

(58)

Hasstore3&comma;1+x

true

(59)

Hasstore4&comma;3&comma;4

true

(60)

store31+x

y

(61)

store43,4

duck,2,bill

(62)

store42,4

goose,3,egg

(63)

GetAllstore4&comma;v2=3

2&comma;4&comma;goose&comma;3&comma;egg

(64)

GetAllstore4&comma;v2=4

(65)

GetAllstore4&comma;k13<k2+v222

2&comma;4&comma;goose&comma;3&comma;egg

(66)

Closestore3&semi;Closestore4

FileTools:-Removepath1&comma;path2&comma;path3

Compatibility

• 

The PersistentTable package was introduced in Maple 2021.

• 

For more information on Maple 2021 changes, see Updates in Maple 2021.

See Also

Database:-SQLite

table