Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,13 @@ jobs:
DOLTLITE_COMPAT_PREBUILT: 1
run: bash test/doltlite_compat_test.sh build/doltlite

# The newest prior release reads version 12 and plays the old engine.
- name: Migrate the version 12 corpus
run: |
old=$(find .compat-cache -mindepth 2 -maxdepth 2 -name doltlite -path '*/v*' | sort -V | tail -n 1)
test -x "$old"
bash test/doltlite_migrate_test.sh build/doltlite "$old"

- name: Contract tests
run: |
bash test/sqlite_compatibility_contract_test.sh build/doltlite
Expand Down
2 changes: 1 addition & 1 deletion packaging/rust/smoke-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ fn main() -> std::process::ExitCode {
.expect("create saved");
drop(db);
let valid = std::fs::read(&db_path).unwrap();
for cut in [168, 500, 1000] {
for cut in [192, 500, 1000] {
for flags in [
OpenFlags::READ_ONLY,
OpenFlags::READ_WRITE,
Expand Down
1 change: 0 additions & 1 deletion src/btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ i64 sqlite3BtreeIntegerKey(BtCursor*);
i64 sqlite3BtreeSqlRowid(BtCursor*);
int sqlite3BtreeSqlRowidMoveto(BtCursor*, i64, int*);
void sqlite3BtreeClearSqlRowidCache(Btree*, Pgno);
int sqlite3BtreeProllyIndexHasWideNumericDesc(Btree*, Pgno, int*);
int sqlite3BtreeProllyIndexHasNocaseNul(
Btree*, Pgno, int, const char *const*, int*
);
Expand Down
78 changes: 72 additions & 6 deletions src/chunk_refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ void csFreeRemotes(ChunkStore *cs){
cs->refs.nRemotes = 0;
}

void csFreeUpstreams(ChunkStore *cs){
int k;
for(k=0; k<cs->refs.nUpstreams; k++){
sqlite3_free(cs->refs.aUpstreams[k].zName);
sqlite3_free(cs->refs.aUpstreams[k].zRemote);
sqlite3_free(cs->refs.aUpstreams[k].zBranch);
}
sqlite3_free(cs->refs.aUpstreams);
cs->refs.aUpstreams = 0;
cs->refs.nUpstreams = 0;
}

void csFreeTracking(ChunkStore *cs){
int k;
for(k=0; k<cs->refs.nTracking; k++){
Expand Down Expand Up @@ -316,6 +328,7 @@ void csFreeRefsState(ChunkStore *cs){
csFreeRemotes(cs);
csFreeTracking(cs);
csFreeSequences(cs);
csFreeUpstreams(cs);
sqlite3_free(cs->refs.zDefaultBranch);
cs->refs.zDefaultBranch = 0;
cs->refs.bDefaultExplicit = 0;
Expand Down Expand Up @@ -422,7 +435,7 @@ static int refsAllocArray(void **ppArray, int n, sqlite3_uint64 nElem){
return SQLITE_OK;
}

int csDecodeRefsV7(
int csDecodeRefsV8(
const u8 *data,
int nData,
RefsTable *pRefs,
Expand All @@ -438,6 +451,7 @@ int csDecodeRefsV7(
int nRemotes;
int nTracking;
int nSequences;
int nUpstreams;
int hasDefaultBranch = 0;
int i;
int rc;
Expand All @@ -446,7 +460,7 @@ int csDecodeRefsV7(
reader.p = data;
reader.n = nData;
rc = refsTake(&reader, 1, &pVersion);
if( rc!=SQLITE_OK || pVersion[0]!=7 ) return SQLITE_CORRUPT;
if( rc!=SQLITE_OK || pVersion[0]!=CS_REFS_FORMAT ) return SQLITE_CORRUPT;
rc = refsReadStringView(&reader,
pRefs ? &pRefs->zDefaultBranch : 0,
&pDefaultBranch, &nDefaultBranch);
Expand Down Expand Up @@ -573,14 +587,30 @@ int csDecodeRefsV7(
}
if( rc!=SQLITE_OK ) return rc;
}

rc = refsReadCount(&reader, 12, &nUpstreams);
if( rc!=SQLITE_OK ) return rc;
if( pRefs ){
rc = refsAllocArray((void**)&pRefs->aUpstreams, nUpstreams,
sizeof(UpstreamRef));
if( rc!=SQLITE_OK ) return rc;
pRefs->nUpstreams = nUpstreams;
}
for(i=0; i<nUpstreams; i++){
UpstreamRef *pUp = pRefs ? &pRefs->aUpstreams[i] : 0;
rc = refsReadString(&reader, pUp ? &pUp->zName : 0);
if( rc==SQLITE_OK ) rc = refsReadString(&reader, pUp ? &pUp->zRemote : 0);
if( rc==SQLITE_OK ) rc = refsReadString(&reader, pUp ? &pUp->zBranch : 0);
if( rc!=SQLITE_OK ) return rc;
}
return reader.n==0 ? SQLITE_OK : SQLITE_CORRUPT;
}

int csDeserializeRefs(ChunkStore *cs, const u8 *data, int nData){
ChunkStore tmp;
int rc;
memset(&tmp, 0, sizeof(tmp));
rc = csDecodeRefsV7(data, nData, &tmp.refs, 0, 0);
rc = csDecodeRefsV8(data, nData, &tmp.refs, 0, 0);
if( rc!=SQLITE_OK ){
csFreeRefsState(&tmp);
return rc;
Expand All @@ -593,7 +623,7 @@ int csDeserializeRefs(ChunkStore *cs, const u8 *data, int nData){
int csDeserializeRefsIntoTemp(ChunkStore *pTmp, const u8 *data, int nData){
int rc;
memset(pTmp, 0, sizeof(*pTmp));
rc = csDecodeRefsV7(data, nData, &pTmp->refs, 0, 0);
rc = csDecodeRefsV8(data, nData, &pTmp->refs, 0, 0);
if( rc!=SQLITE_OK ) csFreeRefsState(pTmp);
return rc;
}
Expand Down Expand Up @@ -657,6 +687,11 @@ static int refsMergeRemoteEqual(const RemoteRef *a, const RemoteRef *b){
return refsMergeStrEqual(a->zUrl, b->zUrl);
}

static int refsMergeUpstreamEqual(const UpstreamRef *a, const UpstreamRef *b){
return refsMergeStrEqual(a->zRemote, b->zRemote)
&& refsMergeStrEqual(a->zBranch, b->zBranch);
}

static int refsMergeTrackingEqual(const TrackingBranch *a, const TrackingBranch *b){
return refsMergeHashEqual(&a->commitHash, &b->commitHash);
}
Expand Down Expand Up @@ -822,6 +857,27 @@ static void refsMergeRemoteFree(void *pEntry){
sqlite3_free(r->zUrl);
}

static int refsMergeUpstreamFind(const void *aBase, int n, const void *pEntry){
return csFindNamedRef(aBase, n, sizeof(UpstreamRef), *(char* const*)pEntry);
}

static int refsMergeUpstreamCopy(void *pDst, const void *pSrc){
const UpstreamRef *s = (const UpstreamRef*)pSrc;
UpstreamRef *d = (UpstreamRef*)pDst;
memset(d, 0, sizeof(*d));
d->zName = sqlite3_mprintf("%s", s->zName);
d->zRemote = sqlite3_mprintf("%s", s->zRemote);
d->zBranch = sqlite3_mprintf("%s", s->zBranch);
return d->zName==0 || d->zRemote==0 || d->zBranch==0;
}

static void refsMergeUpstreamFree(void *pEntry){
UpstreamRef *r = (UpstreamRef*)pEntry;
sqlite3_free(r->zName);
sqlite3_free(r->zRemote);
sqlite3_free(r->zBranch);
}

static int refsMergeTrackingCopy(void *pDst, const void *pSrc){
const TrackingBranch *s = (const TrackingBranch*)pSrc;
TrackingBranch *d = (TrackingBranch*)pDst;
Expand All @@ -843,7 +899,7 @@ int csMergeSavedRefsOntoDisk(
const SavedRefsState *pLocal,
const RefsTable *pBase
){
RefsMergeCat aCat[4];
RefsMergeCat aCat[5];
int pass, i, rc;

memset(aCat, 0, sizeof(aCat));
Expand Down Expand Up @@ -887,6 +943,16 @@ int csMergeSavedRefsOntoDisk(
aCat[3].xFreeEntry = refsMergeTrackingFree;
aCat[3].xFind = refsMergeTrackingFind;

aCat[4].aLocal = pLocal->aUpstreams; aCat[4].nLocal = pLocal->nUpstreams;
aCat[4].aBase = pBase->aUpstreams; aCat[4].nBase = pBase->nUpstreams;
aCat[4].paDisk = (void**)&cs->refs.aUpstreams;
aCat[4].pnDisk = &cs->refs.nUpstreams;
aCat[4].stride = (int)sizeof(UpstreamRef);
aCat[4].xEqual = (int(*)(const void*,const void*))refsMergeUpstreamEqual;
aCat[4].xCopyInto = refsMergeUpstreamCopy;
aCat[4].xFreeEntry = refsMergeUpstreamFree;
aCat[4].xFind = refsMergeUpstreamFind;

{
const char *zL = pLocal->zDefaultBranch;
const char *zB = pBase->zDefaultBranch;
Expand All @@ -903,7 +969,7 @@ int csMergeSavedRefsOntoDisk(
}

for(pass=0; pass<2; pass++){
for(i=0; i<4; i++){
for(i=0; i<5; i++){
rc = refsMergeCategory(&aCat[i], pass==0);
if( rc!=SQLITE_OK ) return rc;
}
Expand Down
19 changes: 18 additions & 1 deletion src/chunk_refs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef struct TagRef TagRef;
typedef struct RemoteRef RemoteRef;
typedef struct TrackingBranch TrackingBranch;
typedef struct SequenceRef SequenceRef;
typedef struct UpstreamRef UpstreamRef;
typedef struct RefsTable RefsTable;
typedef int (*CsRefsHashCb)(void *ctx, const ProllyHash *pHash);

Expand Down Expand Up @@ -39,6 +40,13 @@ struct TrackingBranch {
ProllyHash commitHash;
};

/* Local configuration, like remotes: a push never carries it. */
struct UpstreamRef {
char *zName;
char *zRemote;
char *zBranch;
};

/* Non-versioned AUTOINCREMENT high-water, shared across branches. */
struct SequenceRef {
char *zTableName;
Expand All @@ -62,6 +70,8 @@ struct RefsTable {
int nTracking;
SequenceRef *aSequences;
int nSequences;
UpstreamRef *aUpstreams;
int nUpstreams;
};

void refsTableGetBranches(const RefsTable *rt, int *pn, const BranchRef **par);
Expand Down Expand Up @@ -95,6 +105,8 @@ struct SavedRefsState {
int nTracking;
SequenceRef *aSequences;
int nSequences;
UpstreamRef *aUpstreams;
int nUpstreams;
};

#define REFS_OWNED_COPY(D, S) do { \
Expand All @@ -110,6 +122,8 @@ struct SavedRefsState {
(D).nTracking = (S).nTracking; \
(D).aSequences = (S).aSequences; \
(D).nSequences = (S).nSequences; \
(D).aUpstreams = (S).aUpstreams; \
(D).nUpstreams = (S).nUpstreams; \
} while(0)

#define REFS_OWNED_CLEAR(D) do { \
Expand All @@ -125,6 +139,8 @@ struct SavedRefsState {
(D).nTracking = 0; \
(D).aSequences = 0; \
(D).nSequences = 0; \
(D).aUpstreams = 0; \
(D).nUpstreams = 0; \
} while(0)

struct ChunkStore;
Expand All @@ -143,6 +159,7 @@ int csEnsureDefaultBranch(struct ChunkStore *cs);
void csFreeBranches(struct ChunkStore *cs);
void csFreeTags(struct ChunkStore *cs);
void csFreeRemotes(struct ChunkStore *cs);
void csFreeUpstreams(struct ChunkStore *cs);
void csFreeTracking(struct ChunkStore *cs);
void csFreeSequences(struct ChunkStore *cs);
/* Savepoint rollback has to put back the counter a rolled-back
Expand All @@ -168,7 +185,7 @@ void csRestoreCommittedRefsHash(struct ChunkStore *cs);
void csDetachSavedRefsState(struct ChunkStore *cs, SavedRefsState *pSaved);
int csDeserializeRefs(struct ChunkStore *cs, const u8 *data, int nData);
int csDeserializeRefsIntoTemp(struct ChunkStore *pTmp, const u8 *data, int nData);
int csDecodeRefsV7(const u8 *data, int nData, RefsTable *pRefs,
int csDecodeRefsV8(const u8 *data, int nData, RefsTable *pRefs,
CsRefsHashCb xHash, void *pCtx);

#endif
23 changes: 19 additions & 4 deletions src/chunk_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ void csSerializeManifest(const ChunkStore *cs, u8 *aBuf){

CS_WRITE_I64(aBuf + CS_MANIFEST_WAL_OFFSET_OFF, cs->wal.iWalOffset);
memcpy(aBuf + CS_MANIFEST_REFS_HASH_OFF, cs->refs.refsHash.data, PROLLY_HASH_SIZE);
memcpy(aBuf + CS_MANIFEST_STORE_ID_OFF, cs->aStoreId, CS_STORE_ID_SIZE);
}

static void csManifestHashOffset(blake3_hasher *pHasher, i64 iOffset){
Expand Down Expand Up @@ -637,6 +638,7 @@ static int csReadManifest(ChunkStore *cs){

cs->wal.iWalOffset = CS_READ_I64(aBuf + CS_MANIFEST_WAL_OFFSET_OFF);
memcpy(cs->refs.refsHash.data, aBuf + CS_MANIFEST_REFS_HASH_OFF, PROLLY_HASH_SIZE);
memcpy(cs->aStoreId, aBuf + CS_MANIFEST_STORE_ID_OFF, CS_STORE_ID_SIZE);

/* A WAL offset inside live data makes replay rewind and reclaim rows.
** Replay bounds it against the file size when it stats the file. */
Expand Down Expand Up @@ -726,6 +728,8 @@ static int csOpen(
int emptyFile = 0;

memset(cs, 0, sizeof(*cs));
/* Kept only if this handle creates the file; an existing manifest's wins. */
sqlite3_randomness(CS_STORE_ID_SIZE, cs->aStoreId);
csIndexCacheSetBudget(cs, 200*1024);
if( !pVfs ){
pVfs = sqlite3_vfs_find(0);
Expand Down Expand Up @@ -1365,6 +1369,7 @@ int chunkStoreClose(ChunkStore *cs){
csFreeBranches(cs);
csFreeTags(cs);
csFreeRemotes(cs);
csFreeUpstreams(cs);
csFreeTracking(cs);
csFreeSequences(cs);
csDropTxnSequences(cs);
Expand Down Expand Up @@ -1895,7 +1900,8 @@ void csFillChunkHdr(u8 *p, const ProllyHash *pHash, u32 size){
CS_WRITE_U32(p + CS_WAL_CHUNK_LEN_OFF, size);
}

#define CS_COMMIT_V2 2
#define CS_COMMIT_V3 3
#define CS_COMMIT_HDR_SIZE 4
#define CS_COMMIT_MAX_PARENTS 8
#define CS_ANCESTOR_WALK_MAX 65536

Expand All @@ -1909,8 +1915,9 @@ static int csDecodeCommitParents(
const u8 *pEnd = data + nData;
int nPar, i;
*pnParents = 0;
if( nData < 2 + PROLLY_HASH_SIZE + 8 + 6 ) return 0;
if( *p++ != CS_COMMIT_V2 ) return 0;
if( nData < CS_COMMIT_HDR_SIZE + 1 + PROLLY_HASH_SIZE + 8 + 6 ) return 0;
if( p[0]!='D' || p[1]!='C' || p[2]!='M' || p[3]!=CS_COMMIT_V3 ) return 0;
p += CS_COMMIT_HDR_SIZE;
nPar = *p++;
if( nPar > CS_COMMIT_MAX_PARENTS ) return 0;
if( pEnd - p < nPar*PROLLY_HASH_SIZE + PROLLY_HASH_SIZE + 8 + 6 ) return 0;
Expand All @@ -1927,7 +1934,14 @@ static int csDecodeCommitParents(
if( pEnd - p < len ) return 0;
p += len;
}
if( p != pEnd ) return 0;
while( p != pEnd ){
u32 nExt;
if( pEnd - p < 5 ) return 0;
nExt = (u32)p[1] | ((u32)p[2]<<8) | ((u32)p[3]<<16) | ((u32)p[4]<<24);
p += 5;
if( nExt > (u32)(pEnd - p) ) return 0;
p += nExt;
}
*pnParents = nPar;
return 1;
}
Expand Down Expand Up @@ -2813,6 +2827,7 @@ void chunkStoreClearRefs(ChunkStore *cs){
csFreeBranches(cs);
csFreeTags(cs);
csFreeRemotes(cs);
csFreeUpstreams(cs);
csFreeTracking(cs);
csFreeSequences(cs);
csDropTxnSequences(cs);
Expand Down
15 changes: 13 additions & 2 deletions src/chunk_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#include "prolly_stats.h"

#define CHUNK_STORE_MAGIC 0x444C5443
#define CHUNK_STORE_VERSION 12
#define CHUNK_MANIFEST_SIZE 168
#define CHUNK_STORE_VERSION 13
#define CS_REFS_FORMAT 8
#define CHUNK_MANIFEST_SIZE 192
#define CHUNK_INDEX_ENTRY_SIZE 32

#define CS_READ_U32(p) ( \
Expand Down Expand Up @@ -82,6 +83,10 @@
#define CS_MANIFEST_SELF_HASH_OFF 124
#define CS_MANIFEST_CHECKPOINT_HASH_OFF 144
#define CS_MANIFEST_CHECKPOINT_COUNT_OFF 164
/* Minted when the file is created and carried by every later manifest,
** including the one gc writes. Tells this file from another at its path. */
#define CS_MANIFEST_STORE_ID_OFF 168
#define CS_STORE_ID_SIZE 16

#define CS_MANIFEST_HASH_LEGACY 0
#define CS_MANIFEST_HASH_OK 1
Expand Down Expand Up @@ -350,6 +355,7 @@ struct CsWsSession {
struct ChunkStore {
ChunkFile file;
RefsTable refs;
u8 aStoreId[CS_STORE_ID_SIZE];
/* Counters as of the first mutation since the last commit. ROLLBACK TO
** the savepoint that opened a transaction reaches no btree savepoint, so
** this is the only copy of what that rollback has to put back. */
Expand Down Expand Up @@ -568,6 +574,11 @@ int chunkStoreFindTag(ChunkStore *cs, const char *zName, ProllyHash *pCommit);

int chunkStoreAddRemote(ChunkStore *cs, const char *zName, const char *zUrl);
int chunkStoreDeleteRemote(ChunkStore *cs, const char *zName);
int chunkStoreFindUpstream(ChunkStore *cs, const char *zName,
const char **pzRemote, const char **pzBranch);
int chunkStoreSetUpstream(ChunkStore *cs, const char *zName,
const char *zRemote, const char *zBranch);
int chunkStoreDeleteUpstream(ChunkStore *cs, const char *zName);
int chunkStoreFindRemote(ChunkStore *cs, const char *zName, const char **pzUrl);

int chunkStoreDeleteTracking(ChunkStore *cs, const char *zRemote,
Expand Down
Loading
Loading