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

Overview
Comment:Further work on b-tree backend.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a1399dac00d62b8dcf163abf85d3bc85957ecd01
User & Date: dan 2013-09-16 20:25:41.212
Context
2013-09-17
20:26
Add new file bt_pager.c. check-in: c8eb0a0cf4 user: dan tags: trunk
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
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/btInt.h.
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
** 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.
*************************************************************************/











|
|
|
|
|
|
|
|
|



|














>
>
>







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
** File-system interface.
*/
typedef struct bt_env bt_env;
typedef struct bt_file bt_file;

struct bt_env {
  void *pVfsCtx;
  int (*xFullpath)(sqlite4_env*,bt_env*, const char *, char **);
  int (*xOpen)(sqlite4_env*,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)(sqlite4_env*,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)(bt_file*);
  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)

/* Find the default VFS */
bt_env *sqlite4BtEnvDefault(void);

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




Changes to src/bt_main.c.
9
10
11
12
13
14
15




16
17
18
19
20
21
22
**    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;







>
>
>
>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
*/

#include "bt.h"

struct bt_db {
  BtPager *pPager;
};

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

int sqlite4BtClose(bt_db *db){
  return SQLITE4_OK;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
}

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){







|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
}

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

int sqlite4BtTransactionLevel(bt_db *db){
  return sqlite4BtPagerTransactionLevel(db->pPager);
}

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

int sqlite4BtCsrClose(bt_cursor *pCsr){
Changes to src/bt_unix.c.
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
#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 */







>
|



















>








|

|






>


|





|







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
#endif

/*
** An open file is an instance of the following object
*/
typedef struct PosixFile PosixFile;
struct PosixFile {
  sqlite4_env *pSqlEnv;
  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(
  sqlite4_env *pSqlEnv,
  bt_env *pEnv,
  const char *zFile,
  int flags,
  bt_file **ppFile
){
  int rc = SQLITE4_OK;
  PosixFile *p;

  p = (PosixFile*)sqlite4_malloc(pSqlEnv, sizeof(PosixFile));
  if( p==0 ){
    rc = btErrorBkpt(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->pSqlEnv = pSqlEnv;
    p->fd = open(zFile, oflags, 0644);
    if( p->fd<0 ){
      sqlite4_free(pSqlEnv, 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 */
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
}

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;
}








>


|
<

|
>
>
|


<
<

|

|
|
>

|

>
|
>
|
>
|
>
|
>
|
>
|
>
>
>
|
|
|
|
>
>
|
<
|
|
<
<



>
|
|







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
}

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

static int btPosixOsFullpath(
  sqlite4_env *pSqlEnv,
  bt_env *pEnv,
  const char *zName,
  char **pzOut

){
  int rc = SQLITE4_OK;
  char *zOut = 0;
  char *zCwd = 0;
  int nCwd = 0;

  if( zName[0]!='/' ){


    int nTmp = 512;
    char *zTmp = (char*)sqlite4_malloc(pSqlEnv, nTmp);
    while( zTmp ){
      zCwd = getcwd(zTmp, nTmp);
      if( zCwd || errno!=ERANGE ) break;
      sqlite4_free(pSqlEnv, zTmp);
      nTmp = nTmp*2;
      zTmp = sqlite4_malloc(pSqlEnv, nTmp);
    }
    if( zTmp==0 ){
      rc = btErrorBkpt(SQLITE4_NOMEM);
    }else if( zCwd==0 ){
      rc = btErrorBkpt(SQLITE4_IOERR);
    }else{
      assert( zCwd==zTmp );
      nCwd = strlen(zCwd);
    }
  }

  if( rc==SQLITE4_OK ){
    int nReq = nCwd + 1 + strlen(zName) + 1;
    zOut = sqlite4_malloc(pSqlEnv, nReq);
    if( zOut ){
      int nName = strlen(zName);
      if( nCwd ){
        memcpy(zOut, zCwd, nCwd);
        zOut[nCwd] = '/';
        memcpy(&zOut[nCwd+1], zName, nName+1);
      }else{
        memcpy(zOut, zName, nName+1);
      }

    }else{
      rc = btErrorBkpt(SQLITE4_NOMEM);


    }
  }

  sqlite4_free(pSqlEnv, zCwd);
  *pzOut = zOut;
  return rc;
}

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

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    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++){







|







380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
    if( p->apShm[iChunk]==0 ) return btErrorBkpt(SQLITE4_IOERR);
  }

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

void btPosixOsShmBarrier(bt_file *pFile){
}

int btPosixOsShmUnmap(bt_file *pFile, int bDelete){
  PosixFile *p = (PosixFile *)pFile;
  if( p->shmfd>0 ){
    int i;
    for(i=0; i<p->nShm; i++){
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
   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 */







|







414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
   btPosixOsShmUnmap(pFile, 0);
   close(p->fd);
   btFree(p->apShm);
   btFree(p);
   return SQLITE4_OK;
}

bt_env *sqlite4BtEnvDefault(void){
  static bt_env posix_env = {
    0,                            /* pVfsCtx */
    btPosixOsFullpath,            /* xFullpath */
    btPosixOsOpen,                /* xOpen */
    btPosixOsRead,                /* xRead */
    btPosixOsWrite,               /* xWrite */
    btPosixOsTruncate,            /* xTruncate */