Rev 3432 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
Implementation notes for those who want to modify the codeA. sql buffers1. Much of the logic is concerned with constructing the SQL to be executed.These sql's are stored in one of the global buffers (g_sql_buf).2. Currently, there are 4 of them (controlled by constant NBUFS insrc/sqlite_dataframe.h).3. It is assumed that R is running on a single thread, so this approach willwork.4. Initially, all buffers are 1024 characters long. The buffer sizes arestored in the global int array g_sql_buf_sz.5. When writing strings to a buf with variable length, use_expand_buf([index of buffer], [approx size]). If approx size is greaterthan the buffer's length, it will be reallocated with twice the currentsize.6. Functions use a naming convention with respect to the buffer it uses.Top-level functions may use any of the buffers. Those with nameending in a number are expected to use buffers a buffer with that numberas index, and those "below" it. For example, the function USE_SDF1 mayuse buffers 1 and 2, so those calling it should not store data in thosebuffers that they will need after the function call. Initially I was ableto make do with 3 buffers (0, 1 & 2), but somewhere I had to add anotherbuffer (w/c makes it 4). In most cases, the buffers "below" a number areonly those buffers up to 2 (e.g., USE_SDF1 uses only buffers 1 & 2) so3 can be used by the calling function.B. MAX_ATTACHED, USE_SDF1, UNUSE_SDF21. The number of DB attached to the SQLite engine is limited by the valueof the macro MAX_ATTACHED in SQLite.2. By default, MAX_ATTACHED is 10. This package modifies it to 31. Thisis the maximum, according to the SQLite docs. SQLite uses bitfieldssomewhere, where the nth bit probably represents the nth attached db.3. SQLiteDF produces a lot of SDF. Most operations creates SDF whereintermediate values are stored.4. On startup, it opens workspace.db in the current directory. Then itattaches SDF's listed in workspace.db.5. The package "juggles" with db's attached by using USE_SDF1 and UNUSE_SDF2.6. USE_SDF1 checks if a SDF is loaded. If not, and there is a free "slot,"it is attached. If there is no free slot, then the least used among thoseloaded is detached to make room for this one. The use count of the SDFloaded is increased.7. On USE_SDF1, there is an option to "protect" it from being unloaded. Thereare cases where a function uses 2 SDF and the second USE_SDF may unloadthe 1st. In general, top-level functions that will use 2 SDF's, includingthose that will store results in an SDF, will have to set the protectstatement. The protect options sets a bit in the workspace db to flag thatit should not be detached.8. When the protect option is used, make sure to do UNUSE_SDF2. This clearsthe flag in workspace.db.9. _prepare_attach2() contains the logic for detaching SDF's.C. transactions1. when doing bulk modifications (insert/update/delete), enclose them in atransaction, otherwise it will be real slow (see SQLite benchmarks inSQLite site).2. Statements that are sqlite_step()-ed inside a transaction, even if they aresqlite_prepare()-ed before a BEGIN.3. You may use macros _sqlite_begin and _sqlite_commit. When __SQLITE_DEBUG__is defined, file and lines where the BEGIN or COMMIT occurred. Besides thatit is convenient.