Last active
August 29, 2015 14:28
-
-
Save SergejJurecko/68979ff6460806581ad5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "lmdb.h" | |
#include <string.h> | |
#include <stdio.h> | |
#include <ctype.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
# define DBG(X, ...) fprintf(stderr,X "\r\n",##__VA_ARGS__) ; | |
#define u8 unsigned char | |
#define u64 uint64_t | |
#define i64 int64_t | |
#define u32 unsigned int | |
typedef struct lmdb | |
{ | |
MDB_env *menv; | |
MDB_txn *txn; | |
MDB_dbi pagesdb; | |
MDB_cursor *cursorPages; | |
}lmdb; | |
static int pagesdb_cmp(const MDB_val *a, const MDB_val *b) | |
{ | |
u64 aActor; | |
u64 bActor; | |
u32 aPgno; | |
u32 bPgno; | |
int diff; | |
memcpy(&aActor,a->mv_data,sizeof(u64)); | |
memcpy(&bActor,b->mv_data,sizeof(u64)); | |
diff = aActor - bActor; | |
if (diff == 0) | |
{ | |
memcpy(&aPgno,(u8*)a->mv_data + sizeof(u64),sizeof(u32)); | |
memcpy(&bPgno,(u8*)b->mv_data + sizeof(u64),sizeof(u32)); | |
return aPgno - bPgno; | |
} | |
return diff; | |
} | |
static int pagesdb_val_cmp(const MDB_val *a, const MDB_val *b) | |
{ | |
u64 aEvterm,aEvnum; | |
u64 bEvterm,bEvnum; | |
u8 aCounter, bCounter; | |
int diff; | |
memcpy(&aEvterm, a->mv_data, sizeof(u64)); | |
memcpy(&bEvterm, b->mv_data, sizeof(u64)); | |
diff = aEvterm - bEvterm; | |
if (diff == 0) | |
{ | |
memcpy(&aEvnum, (u8*)a->mv_data+sizeof(u64), sizeof(u64)); | |
memcpy(&bEvnum, (u8*)b->mv_data+sizeof(u64), sizeof(u64)); | |
diff = aEvnum - bEvnum; | |
if (diff == 0) | |
{ | |
aCounter = ((u8*)a->mv_data)[sizeof(u64)*2]; | |
bCounter = ((u8*)b->mv_data)[sizeof(u64)*2]; | |
return aCounter - bCounter; | |
} | |
return diff; | |
} | |
return diff; | |
} | |
static int open_env(lmdb *lm, const char *pth, int flags) | |
{ | |
if (mdb_env_create(&lm->menv) != MDB_SUCCESS) | |
return -1; | |
if (mdb_env_set_maxdbs(lm->menv,5) != MDB_SUCCESS) | |
return -1; | |
if (mdb_env_open(lm->menv, pth, MDB_NOSUBDIR | flags, 0664) != MDB_SUCCESS) | |
return -1; | |
if (mdb_txn_begin(lm->menv, NULL, flags, &lm->txn) != MDB_SUCCESS) | |
return -1; | |
if (mdb_dbi_open(lm->txn, "pages", MDB_DUPSORT, &lm->pagesdb) != MDB_SUCCESS) | |
return -1; | |
if (mdb_set_compare(lm->txn, lm->pagesdb, pagesdb_cmp) != MDB_SUCCESS) | |
return -1; | |
if (mdb_set_dupsort(lm->txn, lm->pagesdb, pagesdb_val_cmp) != MDB_SUCCESS) | |
return -1; | |
if (mdb_cursor_open(lm->txn, lm->pagesdb, &lm->cursorPages) != MDB_SUCCESS) | |
return -1; | |
return 0; | |
} | |
int main() | |
{ | |
lmdb rd; | |
u8 pagesKeyBuf[sizeof(u64)+sizeof(u32)]; | |
MDB_val pgKey = {0,NULL}, pgVal = {0,NULL}; | |
u64 evnum,evterm,aindex = 126; | |
u32 pgno = 65; | |
int mrc; | |
memcpy(pagesKeyBuf, &aindex,sizeof(u64)); | |
memcpy(pagesKeyBuf + sizeof(u64), &pgno, sizeof(u32)); | |
pgKey.mv_data = pagesKeyBuf; | |
pgKey.mv_size = sizeof(pagesKeyBuf); | |
if (open_env(&rd, "lmdb", 0) == -1) | |
{ | |
DBG("Unable to open source environment"); | |
return 0; | |
} | |
if (mdb_cursor_get(rd.cursorPages,&pgKey,&pgVal,MDB_SET) != MDB_SUCCESS) | |
{ | |
return 0; | |
} | |
if (mdb_cursor_get(rd.cursorPages,&pgKey,&pgVal,MDB_FIRST_DUP) != MDB_SUCCESS) | |
return 0; | |
do{ | |
MDB_val pgDelKey = {0,NULL}, pgDelVal = {0,NULL}; | |
mdb_cursor_get(rd.cursorPages,&pgDelKey,&pgDelVal,MDB_GET_CURRENT); | |
memcpy(&evterm, pgDelVal.mv_data, sizeof(u64)); | |
memcpy(&evnum, (u8*)pgDelVal.mv_data+sizeof(u64),sizeof(u64)); | |
DBG("Evnum=%llu",evnum); | |
// if (evnum > 5538) | |
if (evnum == 5538) | |
{ | |
mrc = mdb_cursor_del(rd.cursorPages,0); | |
if (mrc != MDB_SUCCESS) | |
{ | |
DBG("Unable to delete page on cursor! err=%d, evnum=%llu",mrc,evnum); | |
return 1; | |
} | |
DBG("Deleted"); | |
} | |
}while (mdb_cursor_get(rd.cursorPages,&pgKey,&pgVal,MDB_NEXT_DUP) == MDB_SUCCESS); | |
mdb_txn_commit(rd.txn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment