SQLite4
Check-in [c4135bffd7]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the beginnings of a simpler embedded b-tree backend. Doesn't do anything yet.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c4135bffd78a3a0a2e65ba2098998d6b0f20e510
User & Date: dan 2013-09-16 18:05:29
Context
2013-09-16
20:25
Further work on b-tree backend. check-in: a1399dac00 user: dan tags: trunk
18:05
Add the beginnings of a simpler embedded b-tree backend. Doesn't do anything yet. check-in: c4135bffd7 user: dan tags: trunk
2013-07-31
17:43
Extra tests for sqlite_kvstore. check-in: 9ce1a04efd user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to main.mk.

69
70
71
72
73
74
75

76
77
78
79
80
81
82
..
90
91
92
93
94
95
96

97
98
99
100
101
102
103
LIBOBJ+= vdbe.o parse.o \
         alter.o analyze.o attach.o auth.o \
         build.o \
         callback.o complete.o ctime.o date.o delete.o env.o expr.o \
         fault.o fkey.o fts5.o fts5func.o \
         func.o global.o hash.o \
         icu.o insert.o kv.o kvlsm.o kvmem.o legacy.o \

         lsm_ckpt.o lsm_file.o lsm_log.o lsm_main.o lsm_mem.o lsm_mutex.o \
         lsm_shared.o lsm_str.o lsm_sorted.o lsm_tree.o \
         lsm_unix.o lsm_varint.o \
         main.o malloc.o math.o mem.o mem0.o mem2.o mem3.o mem5.o \
         mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
         opcodes.o os.o \
         pragma.o prepare.o printf.o \
................................................................................
# All of the source code files.
#
SRC = \
  $(TOP)/src/alter.c \
  $(TOP)/src/analyze.c \
  $(TOP)/src/attach.c \
  $(TOP)/src/auth.c \

  $(TOP)/src/build.c \
  $(TOP)/src/callback.c \
  $(TOP)/src/complete.c \
  $(TOP)/src/ctime.c \
  $(TOP)/src/date.c \
  $(TOP)/src/delete.c \
  $(TOP)/src/env.c \







>







 







>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
LIBOBJ+= vdbe.o parse.o \
         alter.o analyze.o attach.o auth.o \
         build.o \
         callback.o complete.o ctime.o date.o delete.o env.o expr.o \
         fault.o fkey.o fts5.o fts5func.o \
         func.o global.o hash.o \
         icu.o insert.o kv.o kvlsm.o kvmem.o legacy.o \
	 bt_unix.o \
         lsm_ckpt.o lsm_file.o lsm_log.o lsm_main.o lsm_mem.o lsm_mutex.o \
         lsm_shared.o lsm_str.o lsm_sorted.o lsm_tree.o \
         lsm_unix.o lsm_varint.o \
         main.o malloc.o math.o mem.o mem0.o mem2.o mem3.o mem5.o \
         mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
         opcodes.o os.o \
         pragma.o prepare.o printf.o \
................................................................................
# All of the source code files.
#
SRC = \
  $(TOP)/src/alter.c \
  $(TOP)/src/analyze.c \
  $(TOP)/src/attach.c \
  $(TOP)/src/auth.c \
  $(TOP)/src/bt_unix.c \
  $(TOP)/src/build.c \
  $(TOP)/src/callback.c \
  $(TOP)/src/complete.c \
  $(TOP)/src/ctime.c \
  $(TOP)/src/date.c \
  $(TOP)/src/delete.c \
  $(TOP)/src/env.c \

Added src/bt.h.





















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
** 2013 September 14
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
*/

#include <sqlite4.h>

typedef struct bt_db bt_db;
typedef struct bt_cursor bt_cursor;

/* 
** Values that may be passed as the 4th parameter to BtCsrSeek() 
*/
#define BT_SEEK_LEFAST   -2
#define BT_SEEK_LE       -1
#define BT_SEEK_EQ        0
#define BT_SEEK_GE        1

/*
** Open and close a database connection.
*/
int sqlite4BtNew(sqlite4_env*, int nExtra, bt_db **ppDb);
int sqlite4BtClose(bt_db*);
void *sqlite4BtExtra(bt_db*);

/*
** Attach a database connection to a database file.
*/
int sqlite4BtOpen(bt_db*, const char *zFilename);

/*
** Begin(), Commit() and Revert() methods. These functions work in the
** same way as the corresponding sqlite4_kv_methods functions. As follows:
**
** Part of the state maintained by a database handle is the transaction
** level - a non-negative integer. If the transaction level is 0, then
** no read or write transaction is currently open. If the transaction
** level is 1, then a read-only transaction is open. If the transaction
** level is 2 or greater, then (L-1) nested write transactions are open,
** where L is the transaction level. Summary:
**
**   L==0    (no transaction is open)
**   L==1    (a read-only transaction is open)
**   L>=2    ((L-1) nested write transactions are open)
**
** If successful, all three methods set the transaction level to the value
** passed as the second argument. They differ as follows:
**
** Begin(): If (L>=iLevel), this is a no-op. Otherwise, open the read and/or
**          write transactions required to set L to iLevel.
**
** Commit(): If (L<=iLevel), this is a no-op. Otherwise, close as many
**           transactions as required to set L to iLevel. Commit any write
**           transactions closed by this action.
**
** Rollback(): If (L<=iLevel), this is a no-op. Otherwise, close as many
**             transactions as required to set L to iLevel. Roll back any 
**             write transactions closed by this action.
**
** Revert(): Revert(i) is the same as Rollback(i) except that if i>=2, then
**           following the rollback the innermost nested write transaction
**           is also rolled back (but not closed).
**
**    SAVEPOINT one;              -- Begin(2)
**      SAVEPOINT two;            -- Begin(3)
**      ROLLBACK TO one;          -- Revert(2)
**      SAVEPOINT two;            -- Begin(3)
**        SAVEPOINT three;        -- Begin(4)
**        ROLLBACK TO three;      -- Revert(4)
**        RELEASE three;          -- Commit(3)
**    RELEASE one;                -- Commit(1)
**
** The TransactionLevel() method returns the current transaction level.
*/
int sqlite4BtBegin(bt_db*, int iLevel);
int sqlite4BtCommit(bt_db*, int iLevel);
int sqlite4BtRevert(bt_db*, int iLevel);
int sqlite4BtRollback(bt_db*, int iLevel);
int sqlite4BtTransactionLevel(bt_db*);

/*
** Open and close a database cursor.
*/
int sqlite4BtCsrOpen(bt_db*, int nExtra, bt_cursor **ppCsr);
int sqlite4BtCsrClose(bt_cursor *pCsr);
void *sqlite4BtCsrExtra(bt_cursor *pCsr);

/*
** Return values:
**
**   SQLITE4_OK:
**   SQLITE4_NOTFOUND:
**   SQLITE4_INEXACT:
**   other:
*/
int sqlite4BtCsrSeek(bt_cursor *pCsr, const void *pK, int nK, int eSeek);

int sqlite4BtCsrFirst(bt_cursor *pCsr);
int sqlite4BtCsrLast(bt_cursor *pCsr);

int sqlite4BtCsrNext(bt_cursor *pCsr);
int sqlite4BtCsrPrev(bt_cursor *pCsr);

int sqlite4BtCsrValid(bt_cursor *pCsr);
int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK);
int sqlite4BtCsrData(bt_cursor *pCsr, int, int, const void **ppV, int *pnV);

int sqlite4BtReplace(bt_db*, const void *pK, int nK, const void *pV, int nV);
int sqlite4BtDelete(bt_cursor*);

int sqlite4BtSetCookie(bt_db*, unsigned int iVal);
int sqlite4BtGetCookie(bt_db*, unsigned int *piVal);

Added src/btInt.h.

































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
** 2013 September 14
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
*/

#include "bt.h"

typedef sqlite4_int64 i64;
typedef unsigned int u32;
typedef unsigned char u8;

/*************************************************************************
** Interface to bt_pager.c functionality.
*/
typedef struct BtPage BtPage;
typedef struct BtPager BtPager;

/*
** Open and close a pager database connection.
*/
int sqlite4BtPagerNew(sqlite4_env*, int nExtra, BtPager **pp);
int sqlite4BtPagerClose(BtPager*);
void *sqlite4BtPagerExtra(BtPager*);

/*
** Attach a database file to a pager object.
*/
int sqlite4BtPagerOpen(BtPager*, const char *zFilename);

/*
** Transactions. These methods are more or less the same as their 
** counterparts in bt.h.
*/
int sqlite4BtPagerBegin(BtPager*, int iLevel);
int sqlite4BtPagerCommit(BtPager*, int iLevel);
int sqlite4BtPagerRevert(BtPager*, int iLevel);
int sqlite4BtPagerRollback(BtPager*, int iLevel);
int sqlite4BtPagerTransactionLevel(BtPager*);

/*
** Query for the database page size. Requires an open read transaction.
*/
int sqlite4BtPagerPagesize(BtPager*, int *pnByte);

/* 
** Query for the root page number. Requires an open read transaction.
*/
int sqlite4BtPagerRootpgno(BtPager*, u32 *piRoot);

/*
** Read, write and trim existing database pages.
*/
int sqlite4BtPageGet(BtPager*, u32 pgno, BtPage **ppPage);
int sqlite4BtPageWrite(BtPage*);
int sqlite4BtPageTrim(BtPage*);
int sqlite4BtPageRelease(BtPage*);

/*
** Allocate a new database page and return a writable reference to it.
*/
int sqlite4BtPageAllocate(BtPager*, BtPage **ppPage);

/*
** Query page references.
*/
u32 sqlite4BtPagePgno(BtPage*);
void *sqlite4BtPageData(BtPage*);

/* 
** Read/write the schema cookie value.
*/
int sqlite4BtPagerSetCookie(BtPager*, u32 iVal);
int sqlite4BtPagerGetCookie(BtPager*, u32 *piVal);
/*
** End of bt_pager.c interface.
*************************************************************************/

/*************************************************************************
** File-system interface.
*/
typedef struct bt_env bt_env;
typedef struct bt_file bt_file;

struct bt_env {
  void *pVfsCtx;
  int (*xFullpath)(bt_env*, const char *, char *, int *);
  int (*xOpen)(bt_env*, const char *, int flags, bt_file **);
  int (*xRead)(bt_file *, i64, void *, int);
  int (*xWrite)(bt_file *, i64, void *, int);
  int (*xTruncate)(bt_file *, i64);
  int (*xSync)(bt_file *);
  int (*xSectorSize)(bt_file *);
  int (*xClose)(bt_file *);
  int (*xUnlink)(bt_env*, const char *);
  int (*xLock)(bt_file*, int, int);
  int (*xTestLock)(bt_file*, int, int, int);
  int (*xShmMap)(bt_file*, int, int, void **);
  void (*xShmBarrier)(void);
  int (*xShmUnmap)(bt_file*, int);
};

/* Flags for the 3rd argument to xOpen */
#define BT_OPEN_READONLY 0x0001

/* Candidate values for the 3rd argument to bt_env.xLock() */
#define BT_LOCK_UNLOCK 0
#define BT_LOCK_SHARED 1
#define BT_LOCK_EXCL   2

/* Size of shared-memory chunks - 48KB. */
#define BT_SHM_CHUNK_SIZE (48*1024)

/*
** End of file system interface.
*************************************************************************/




Added src/bt_main.c.































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
** 2013 September 14
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
*/

#include "bt.h"

int sqlite4BtNew(lsm_env *pEnv, int nExtra, bt_db **ppDb){
  return SQLITE4_OK;
}

int sqlite4BtClose(bt_db *db){
  return SQLITE4_OK;
}

void *sqlite4BtExtra(bt_db *db){
  return SQLITE4_OK;
}

int sqlite4BtOpen(bt_db *db, const char *zFilename){
  return SQLITE4_OK;
}

int sqlite4BtBegin(bt_db *db, int iLevel){
  return SQLITE4_OK;
}

int sqlite4BtCommit(bt_db *db, int iLevel){
  return SQLITE4_OK;
}

int sqlite4BtRevert(bt_db *db, int iLevel){
  return SQLITE4_OK;
}

int sqlite4BtRollback(bt_db *db, int iLevel){
  return SQLITE4_OK;
}

int sqlite4BtTransactionLevel(bt_db *db){
  return SQLITE4_OK;
}

int sqlite4BtCsrOpen(bt_db *db, int nExtra, bt_cursor **ppCsr){
  return SQLITE4_OK;
}

int sqlite4BtCsrClose(bt_cursor *pCsr){
  return SQLITE4_OK;
}

void *sqlite4BtCsrExtra(bt_cursor *pCsr){
}

int sqlite4BtCsrSeek(bt_cursor *pCsr, const void *pK, int nK, int eSeek){
  return SQLITE4_OK;
}

int sqlite4BtCsrFirst(bt_cursor *pCsr){
  return SQLITE4_OK;
}

int sqlite4BtCsrLast(bt_cursor *pCsr){
  return SQLITE4_OK;
}

int sqlite4BtCsrNext(bt_cursor *pCsr){
  return SQLITE4_OK;
}

int sqlite4BtCsrPrev(bt_cursor *pCsr){
  return SQLITE4_OK;
}

int sqlite4BtCsrValid(bt_cursor *pCsr){
  return 0;
}

int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK){
  return SQLITE4_OK;
}

int sqlite4BtCsrData(bt_cursor *pCsr, int, int, const void **ppV, int *pnV){
  return SQLITE4_OK;
}

int sqlite4BtReplace(bt_db *db, const void *pK, int nK, const void *pV, int nV){
  return SQLITE4_OK;
}

int sqlite4BtDelete(bt_cursor *pCsr){
  return SQLITE4_OK;
}

int sqlite4BtSetCookie(bt_db *db, unsigned int iVal){
  return 0;
}

int sqlite4BtGetCookie(bt_db *db, unsigned int *piVal){
  return 0;
}

Added src/bt_unix.c.























































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
** 2011-12-03
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** Unix-specific run-time environment implementation for bt.
*/
#if defined(__GNUC__) || defined(__TINYC__)
/* workaround for ftruncate() visibility on gcc. */
# ifndef _XOPEN_SOURCE
#  define _XOPEN_SOURCE 500
# endif
#endif

#include <unistd.h>
#include <sys/types.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>

#include <unistd.h>
#include <errno.h>

#include <sys/mman.h>
#include "btInt.h"

/* There is no fdatasync() call on Android */
#ifdef __ANDROID__
# define fdatasync(x) fsync(x)
#endif

/*
** The malloc/free functions used for allocations made within this file.
** There are relatively few such allocations.
*/
static void *btMalloc(size_t n){ 
  return malloc(n); 
}
static void btFree(void *p){ 
  free(p); 
}
static void *btRealloc(void *p, size_t n){ 
  return realloc(p, n); 
}
static void *btReallocOrFree(void *p, size_t n){
  void *pNew = realloc(p, n); 
  if( pNew==0 ) free(p);
  return pNew;
}

#ifdef NDEBUG
# define btErrorBkpt(x) x
#else
static int btErrorBkpt(int rc){
  return rc;
}
#endif

/*
** An open file is an instance of the following object
*/
typedef struct PosixFile PosixFile;
struct PosixFile {
  bt_env *pEnv;                  /* The run-time environment */
  const char *zName;              /* Full path to file */
  int fd;                         /* The open file descriptor */
  int shmfd;                      /* Shared memory file-descriptor */
  int nShm;                       /* Number of entries in array apShm[] */
  void **apShm;                   /* Array of 32K shared memory segments */
};

static char *posixShmFile(PosixFile *p){
  char *zShm;
  int nName = strlen(p->zName);
  zShm = (char*)btMalloc(nName+4+1);
  if( zShm ){
    memcpy(zShm, p->zName, nName);
    memcpy(&zShm[nName], "-shm", 5);
  }
  return zShm;
}

static int btPosixOsOpen(
  bt_env *pEnv,
  const char *zFile,
  int flags,
  bt_file **ppFile
){
  int rc = SQLITE4_OK;
  PosixFile *p;

  p = (PosixFile*)btMalloc(sizeof(PosixFile));
  if( p==0 ){
    rc = SQLITE4_NOMEM;
  }else{
    int bReadonly = (flags & BT_OPEN_READONLY);
    int oflags = (bReadonly ? O_RDONLY : (O_RDWR|O_CREAT));
    memset(p, 0, sizeof(PosixFile));
    p->zName = zFile;
    p->pEnv = pEnv;
    p->fd = open(zFile, oflags, 0644);
    if( p->fd<0 ){
      btFree(p);
      p = 0;
      rc = btErrorBkpt(SQLITE4_IOERR);
    }
  }

  *ppFile = (bt_file *)p;
  return rc;
}

static int btPosixOsWrite(
  bt_file *pFile,                 /* File to write to */
  i64 iOff,                       /* Offset to write to */
  void *pData,                    /* Write data from this buffer */
  int nData                       /* Bytes of data to write */
){
  int rc = SQLITE4_OK;
  PosixFile *p = (PosixFile *)pFile;
  off_t offset;

  offset = lseek(p->fd, (off_t)iOff, SEEK_SET);
  if( offset!=iOff ){
    rc = btErrorBkpt(SQLITE4_IOERR);
  }else{
    ssize_t prc = write(p->fd, pData, (size_t)nData);
    if( prc<0 ) rc = btErrorBkpt(SQLITE4_IOERR);
  }

  return rc;
}

static int btPosixOsTruncate(
  bt_file *pFile,                /* File to write to */
  i64 nSize                      /* Size to truncate file to */
){
  PosixFile *p = (PosixFile *)pFile;
  int rc = SQLITE4_OK;                /* Return code */
  int prc;                        /* Posix Return Code */
  struct stat sStat;              /* Result of fstat() invocation */
  
  prc = fstat(p->fd, &sStat);
  if( prc==0 && sStat.st_size>nSize ){
    prc = ftruncate(p->fd, (off_t)nSize);
  }
  if( prc<0 ) rc = btErrorBkpt(SQLITE4_IOERR);

  return rc;
}

static int btPosixOsRead(
  bt_file *pFile,                /* File to read from */
  i64 iOff,                      /* Offset to read from */
  void *pData,                    /* Read data into this buffer */
  int nData                       /* Bytes of data to read */
){
  int rc = SQLITE4_OK;
  PosixFile *p = (PosixFile *)pFile;
  off_t offset;

  offset = lseek(p->fd, (off_t)iOff, SEEK_SET);
  if( offset!=iOff ){
    rc = btErrorBkpt(SQLITE4_IOERR);
  }else{
    ssize_t prc = read(p->fd, pData, (size_t)nData);
    if( prc<0 ){ 
      rc = btErrorBkpt(SQLITE4_IOERR);
    }else if( prc<nData ){
      memset(&((u8 *)pData)[prc], 0, nData - prc);
    }

  }

  return rc;
}

static int btPosixOsSync(bt_file *pFile){
  int rc = SQLITE4_OK;

#ifndef LSM_NO_SYNC
  PosixFile *p = (PosixFile *)pFile;
  int prc = 0;

  if( p->pMap ){
    prc = msync(p->pMap, p->nMap, MS_SYNC);
  }
  if( prc==0 ) prc = fdatasync(p->fd);
  if( prc<0 ) rc = btErrorBkpt(SQLITE4_IOERR);
#else
  (void)pFile;
#endif

  return rc;
}

static int btPosixOsSectorSize(bt_file *pFile){
  return 512;
}

static int btPosixOsFullpath(
  bt_env *pEnv,
  const char *zName,
  char *zOut,
  int *pnOut
){
  int nBuf = *pnOut;
  int nReq;

  if( zName[0]!='/' ){
    char *z;
    char *zTmp;
    int nTmp = 512;
    zTmp = (char*)btMalloc(nTmp);
    while( zTmp ){
      z = getcwd(zTmp, nTmp);
      if( z || errno!=ERANGE ) break;
      nTmp = nTmp*2;
      zTmp = btReallocOrFree(zTmp, nTmp);
    }
    if( zTmp==0 ) return btErrorBkpt(SQLITE4_NOMEM);
    if( z==0 ) return btErrorBkpt(SQLITE4_IOERR);
    assert( z==zTmp );

    nTmp = strlen(zTmp);
    nReq = nTmp + 1 + strlen(zName) + 1;
    if( nReq<=nBuf ){
      memcpy(zOut, zTmp, nTmp);
      zOut[nTmp] = '/';
      memcpy(&zOut[nTmp+1], zName, strlen(zName)+1);
    }
    btFree(zTmp);
  }else{
    nReq = strlen(zName)+1;
    if( nReq<=nBuf ){
      memcpy(zOut, zName, strlen(zName)+1);
    }
  }

  *pnOut = nReq;
  return SQLITE4_OK;
}

static int btPosixOsUnlink(bt_env *pEnv, const char *zFile){
  int prc = unlink(zFile);
  return prc ? btErrorBkpt(SQLITE4_IOERR) : SQLITE4_OK;
}

int btPosixOsLock(bt_file *pFile, int iLock, int eType){
  int rc = SQLITE4_OK;
  PosixFile *p = (PosixFile *)pFile;
  static const short aType[3] = { F_UNLCK, F_RDLCK, F_WRLCK };
  struct flock lock;

  assert( aType[BT_LOCK_UNLOCK]==F_UNLCK );
  assert( aType[BT_LOCK_SHARED]==F_RDLCK );
  assert( aType[BT_LOCK_EXCL]==F_WRLCK );
  assert( eType>=0 && eType<(sizeof(aType)/sizeof(aType[0])) );
  assert( iLock>0 && iLock<=32 );

  memset(&lock, 0, sizeof(lock));
  lock.l_whence = SEEK_SET;
  lock.l_len = 1;
  lock.l_type = aType[eType];
  lock.l_start = (4096-iLock);

  if( fcntl(p->fd, F_SETLK, &lock) ){
    int e = errno;
    if( e==EACCES || e==EAGAIN ){
      rc = SQLITE4_BUSY;
    }else{
      rc = btErrorBkpt(SQLITE4_IOERR);
    }
  }

  return rc;
}

int btPosixOsTestLock(bt_file *pFile, int iLock, int nLock, int eType){
  int rc = SQLITE4_OK;
  PosixFile *p = (PosixFile *)pFile;
  static const short aType[3] = { 0, F_RDLCK, F_WRLCK };
  struct flock lock;

  assert( eType==BT_LOCK_SHARED || eType==BT_LOCK_EXCL );
  assert( aType[BT_LOCK_SHARED]==F_RDLCK );
  assert( aType[BT_LOCK_EXCL]==F_WRLCK );
  assert( eType>=0 && eType<(sizeof(aType)/sizeof(aType[0])) );
  assert( iLock>0 && iLock<=32 );

  memset(&lock, 0, sizeof(lock));
  lock.l_whence = SEEK_SET;
  lock.l_len = nLock;
  lock.l_type = aType[eType];
  lock.l_start = (4096-iLock);

  if( fcntl(p->fd, F_GETLK, &lock) ){
    rc = btErrorBkpt(SQLITE4_IOERR);
  }else if( lock.l_type!=F_UNLCK ){
    rc = SQLITE4_BUSY;
  }

  return rc;
}

int btPosixOsShmMap(bt_file *pFile, int iChunk, int sz, void **ppShm){
  PosixFile *p = (PosixFile *)pFile;

  *ppShm = 0;
  assert( sz==BT_SHM_CHUNK_SIZE );
  if( iChunk>=p->nShm ){
    int i;
    void **apNew;
    int nNew = iChunk+1;
    off_t nReq = nNew * BT_SHM_CHUNK_SIZE;
    struct stat sStat;

    /* If the shared-memory file has not been opened, open it now. */
    if( p->shmfd<=0 ){
      char *zShm = posixShmFile(p);
      if( !zShm ) return btErrorBkpt(SQLITE4_NOMEM);
      p->shmfd = open(zShm, O_RDWR|O_CREAT, 0644);
      btFree(zShm);
      if( p->shmfd<0 ){ 
        return btErrorBkpt(SQLITE4_IOERR);
      }
    }

    /* If the shared-memory file is not large enough to contain the 
    ** requested chunk, cause it to grow.  */
    if( fstat(p->shmfd, &sStat) ){
      return btErrorBkpt(SQLITE4_IOERR);
    }
    if( sStat.st_size<nReq ){
      if( ftruncate(p->shmfd, nReq) ){
        return btErrorBkpt(SQLITE4_IOERR);
      }
    }

    apNew = (void**)btRealloc(p->apShm, sizeof(void *) * nNew);
    if( !apNew ) return btErrorBkpt(SQLITE4_NOMEM);
    for(i=p->nShm; i<nNew; i++){
      apNew[i] = 0;
    }
    p->apShm = apNew;
    p->nShm = nNew;
  }

  if( p->apShm[iChunk]==0 ){
    p->apShm[iChunk] = mmap(0, BT_SHM_CHUNK_SIZE, 
        PROT_READ|PROT_WRITE, MAP_SHARED, p->shmfd, iChunk*BT_SHM_CHUNK_SIZE
    );
    if( p->apShm[iChunk]==0 ) return btErrorBkpt(SQLITE4_IOERR);
  }

  *ppShm = p->apShm[iChunk];
  return SQLITE4_OK;
}

void btPosixOsShmBarrier(void){
}

int btPosixOsShmUnmap(bt_file *pFile, int bDelete){
  PosixFile *p = (PosixFile *)pFile;
  if( p->shmfd>0 ){
    int i;
    for(i=0; i<p->nShm; i++){
      if( p->apShm[i] ){
        munmap(p->apShm[i], BT_SHM_CHUNK_SIZE);
        p->apShm[i] = 0;
      }
    }
    close(p->shmfd);
    p->shmfd = 0;
    if( bDelete ){
      char *zShm = posixShmFile(p);
      if( zShm ) unlink(zShm);
      btFree(zShm);
    }
  }
  return SQLITE4_OK;
}


static int btPosixOsClose(bt_file *pFile){
   PosixFile *p = (PosixFile *)pFile;
   btPosixOsShmUnmap(pFile, 0);
   close(p->fd);
   btFree(p->apShm);
   btFree(p);
   return SQLITE4_OK;
}

bt_env *bt_default_env(void){
  static bt_env posix_env = {
    0,                            /* pVfsCtx */
    btPosixOsFullpath,            /* xFullpath */
    btPosixOsOpen,                /* xOpen */
    btPosixOsRead,                /* xRead */
    btPosixOsWrite,               /* xWrite */
    btPosixOsTruncate,            /* xTruncate */
    btPosixOsSync,                /* xSync */
    btPosixOsSectorSize,          /* xSectorSize */
    btPosixOsClose,               /* xClose */
    btPosixOsUnlink,              /* xUnlink */
    btPosixOsLock,                /* xLock */
    btPosixOsTestLock,            /* xTestLock */
    btPosixOsShmMap,              /* xShmMap */
    btPosixOsShmBarrier,          /* xShmBarrier */
    btPosixOsShmUnmap             /* xShmUnmap */
  };
  return &posix_env;
}

Added src/kvbt.c.

















































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
** 2013 September 14
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** A b-tree key/value storage subsystem that presents the interfadce
** defined by kv.h
*/

#include "sqliteInt.h"
#include "bt.h"

/* Forward declarations of objects */
typedef struct KVBt KVBt;
typedef struct KVBtCsr KVBtCsr;

/*
** An instance of an open connection to a bt_db store.  A subclass of KVStore.
*/
struct KVBt {
  KVStore base;                   /* Base class, must be first */
  bt_db *pDb;                     /* Database handle */
};

/*
** An instance of an open cursor pointing into an LSM store.  A subclass
** of KVCursor.
*/
struct KVBtCsr {
  KVCursor base;                  /* Base class. Must be first */
  bt_cursor *pCsr;                /* LSM cursor handle */
};
  
/*
** Begin a transaction or subtransaction.
*/
static int btBegin(KVStore *pKVStore, int iLevel){
  KVBt *p = (KVBt *)pKVStore;
  int rc;
  rc = sqlite4BtBegin(p->pDb, iLevel);
  pKVStore->iTransLevel = sqlite4BtTransactionLevel(p->pDb);
  return rc;
}

/*
** Commit a transaction or subtransaction.
*/
static int btCommitPhaseOne(KVStore *pKVStore, int iLevel){
  return SQLITE4_OK;
}
static int btCommitPhaseTwo(KVStore *pKVStore, int iLevel){
  KVBt *p = (KVBt *)pKVStore;
  int rc;
  rc = sqlite4BtCommit(p->pDb, iLevel);
  pKVStore->iTransLevel = sqlite4BtTransactionLevel(p->pDb);
  return rc;
}

/*
** Rollback a transaction or subtransaction.
*/
static int btRollback(KVStore *pKVStore, int iLevel){
  KVBt *p = (KVBt *)pKVStore;
  int rc;
  rc = sqlite4BtRollback(p->pDb, iLevel);
  pKVStore->iTransLevel = sqlite4BtTransactionLevel(p->pDb);
  return rc;
}

/*
** Revert a transaction back to what it was when it started.
*/
static int btRevert(KVStore *pKVStore, int iLevel){
  KVBt *p = (KVBt *)pKVStore;
  int rc;
  rc = sqlite4BtRevert(p->pDb, iLevel);
  pKVStore->iTransLevel = sqlite4BtTransactionLevel(p->pDb);
  return rc;
}

/*
** Implementation of the xReplace(X, aKey, nKey, aData, nData) method.
*/
static int btReplace(
  KVStore *pKVStore,
  const KVByteArray *aKey, KVSize nKey,
  const KVByteArray *aData, KVSize nData
){
  KVBt *p = (KVBt *)pKVStore;
  return sqlite4BtReplace(p->pDb, aKey, nKey, aData, nData);
}

/*
** Create a new cursor object.
*/
static int btOpenCursor(KVStore *pKVStore, KVCursor **ppKVCursor){
  KVBt *p = (KVBt *)pKVStore;
  int rc = SQLITE4_OK;
  bt_cursor *pCsr;
  KVBtCsr *pBtcsr;

  rc = sqlite4BtCsrOpen(p->pDb, sizeof(KVBtCsr), &pCsr);
  if( rc!=SQLITE4_OK ){
    pBtcsr = 0;
  }else{
    pBtcsr = (KVBtCsr*)sqlite4BtCsrExtra(pCsr);
    memset(pBtcsr, 0, sizeof(KVBtCsr));
    pBtcsr->base.pStore = pKVStore;
    pBtcsr->base.pStoreVfunc = pKVStore->pStoreVfunc;
    pBtcsr->pCsr = pCsr;
  }

  *ppKVCursor = (KVCursor*)pBtcsr;
  return rc;
}

/*
** Reset a cursor
*/
static int btReset(KVCursor *pKVCursor){
  return SQLITE4_OK;
}

/*
** Destroy a cursor object
*/
static int btCloseCursor(KVCursor *pKVCursor){
  KVBtCsr *pBtcsr = (KVBtCsr *)pKVCursor;
  sqlite4BtCsrClose(pBtcsr->pCsr);
  return SQLITE4_OK;
}

/*
** Move a cursor to the next non-deleted node.
*/
static int btNextEntry(KVCursor *pKVCursor){
  KVBtCsr *pBtcsr = (KVBtCsr *)pKVCursor;
  return sqlite4BtCsrNext(pBtcsr->pCsr);
}

/*
** Move a cursor to the previous non-deleted node.
*/
static int btPrevEntry(KVCursor *pKVCursor){
  KVBtCsr *pBtcsr = (KVBtCsr *)pKVCursor;
  return sqlite4BtCsrPrev(pBtcsr->pCsr);
}

/*
** Seek a cursor.
*/
static int btSeek(
  KVCursor *pKVCursor, 
  const KVByteArray *aKey,
  KVSize nKey,
  int dir
){
  int rc;
  KVBtCsr *pCsr = (KVBtCsr *)pKVCursor;

  assert( dir==0 || dir==1 || dir==-1 || dir==-2 );
  assert( BT_SEEK_EQ==0 && BT_SEEK_GE==1 && BT_SEEK_LE==-1 );
  assert( BT_SEEK_LEFAST==-2 );

  return sqlite4BtCsrSeek(pCsr->pCsr, (void *)aKey, nKey, dir);
}

/*
** Delete the entry that the cursor is pointing to.
**
** Though the entry is "deleted", it still continues to exist as a
** phantom.  Subsequent xNext or xPrev calls will work, as will
** calls to xKey and xData, thought the result from xKey and xData
** are undefined.
*/
static int btDelete(KVCursor *pKVCursor){
  KVBtCsr *pBtcsr = (KVBtCsr *)pKVCursor;
  return sqlite4BtDelete(pBtcsr);
}

/*
** Return the key of the node the cursor is pointing to.
*/
static int btKey(
  KVCursor *pKVCursor,         /* The cursor whose key is desired */
  const KVByteArray **paKey,   /* Make this point to the key */
  KVSize *pN                   /* Make this point to the size of the key */
){
  KVBtCsr *pCsr = (KVBtCsr *)pKVCursor;
  return sqlite4BtCsrKey(pCsr->pCsr, (const void **)paKey, (int *)pN);
}

/*
** Return the data of the node the cursor is pointing to.
*/
static int btData(
  KVCursor *pKVCursor,         /* The cursor from which to take the data */
  KVSize ofst,                 /* Offset into the data to begin reading */
  KVSize n,                    /* Number of bytes requested */
  const KVByteArray **paData,  /* Pointer to the data written here */
  KVSize *pN                   /* Number of bytes delivered */
){
  KVBtCsr *pCsr = (KVBtCsr *)pKVCursor;
  return sqlite4BtCsrData(pCsr->pCsr, ofst, n, (const void**)paData, (int*)pN);
}

/*
** Destructor for the entire in-memory storage tree.
*/
static int btClose(KVStore *pKVStore){
  KVBt *p = (KVBt *)pKVStore;
  return sqlite4BtClose(p->pDb);
}

static int btControl(KVStore *pKVStore, int op, void *pArg){
  return SQLITE4_OK;
}

static int btGetMeta(KVStore *pKVStore, unsigned int *piVal){
  KVBt *p = (KVBt *)pKVStore;
  return sqlite4BtGetCookie(p->pDb, piVal);
}

static int btPutMeta(KVStore *pKVStore, unsigned int iVal){
  KVBt *p = (KVBt *)pKVStore;
  return sqlite4BtSetCookie(p->pDb, iVal);
}

static int btGetMethod(
  sqlite4_kvstore *pKVStore, 
  const char *zMethod, 
  void **ppArg,
  void (**pxFunc)(sqlite4_context *, int, sqlite4_value **),
  void (**pxDestroy)(void *)
){
  return SQLITE4_NOTFOUND;
}

int sqlite4OpenBtree(
  sqlite4_env *pEnv,              /* The environment to use */
  sqlite4_kvstore **ppKVStore,    /* OUT: New KV store returned here */
  const char *zFilename,          /* Name of database file to open */
  unsigned flags                  /* Bit flags */
){
  static const sqlite4_kv_methods bt_methods = {
    1,                            /* iVersion */
    sizeof(sqlite4_kv_methods),   /* szSelf */
    btReplace,                    /* xReplace */
    btOpenCursor,                 /* xOpenCursor */
    btSeek,                       /* xSeek */
    btNextEntry,                  /* xNext */
    btPrevEntry,                  /* xPrev */
    btDelete,                     /* xDelete */
    btKey,                        /* xKey */
    btData,                       /* xData */
    btReset,                      /* xReset */
    btCloseCursor,                /* xCloseCursor */
    btBegin,                      /* xBegin */
    btCommitPhaseOne,             /* xCommitPhaseOne */
    btCommitPhaseTwo,             /* xCommitPhaseTwo */
    btRollback,                   /* xRollback */
    btRevert,                     /* xRevert */
    btClose,                      /* xClose */
    btControl,                    /* xControl */
    btGetMeta,                    /* xGetMeta */
    btPutMeta,                    /* xPutMeta */
    btGetMethod                   /* xGetMethod */
  };

  KVBt *pNew = 0;
  bt_db *pDb = 0;

  rc = sqlite4BtNew(ENV, sizeof(KVBt), &pDb);
  if( rc==SQLITE4_OK ){
    pNew = (KVBt*)sqlite4BtExtra(pDb);
    pNew->base.pStoreVfunc = &bt_methods;
    pNew->base.pEnv = pEnv;
    pNew->pDb = pDb;
    rc = sqlite4BtOpen(pDb, zFilename);
  }

  if( rc!=SQLITE4_OK && pDb ){
    sqlite4BtClose(pDb);
    pNew = 0;
  }
  *ppKVStore = pNew;
  return rc;
}

Changes to tool/fixnumbers.tcl.

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  if {[string trim $line]=="<div id=end_of_toc></div>"} {
    if {$iSeenToc!=1} {error "end_of_toc mismatch"}
    set iSeenToc 2
    continue
  }
  if {$iSeenToc==1} continue

  if {[regexp {<h([123]).*>} $line -> iLevel]} {
    if {$iLevel==1 || $idx(1)>0} {
      incr idx($iLevel)
      for {set i [expr $iLevel+1]} {$i < 4} {incr i} { set idx($i) 0 }
      set num ""
      for {set i 1} {$i <= $iLevel} {incr i} { append num "$idx($i)." }

      regsub -all {<[^>]*>} $line "" tag







|







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  if {[string trim $line]=="<div id=end_of_toc></div>"} {
    if {$iSeenToc!=1} {error "end_of_toc mismatch"}
    set iSeenToc 2
    continue
  }
  if {$iSeenToc==1} continue

  if {[regexp {<h([1234]).*>} $line -> iLevel]} {
    if {$iLevel==1 || $idx(1)>0} {
      incr idx($iLevel)
      for {set i [expr $iLevel+1]} {$i < 4} {incr i} { set idx($i) 0 }
      set num ""
      for {set i 1} {$i <= $iLevel} {incr i} { append num "$idx($i)." }

      regsub -all {<[^>]*>} $line "" tag