通过优化读取提升查询效率
只需要在sqlite.c加入如下代码:
#define MAP_BUFFER_LEN 1024 * 1024 * 10
#include <Windows.h>
typedef struct _FILE_MAPING
{
HANDLE fileH;
HANDLE mapFileH;
char* map_buffer;
SIZE_T mapsize;
DWORD64 buffer_offset;
SIZE_T filesize;
int flag;
}*PFILE_MAPING, FILE_MAPING;
SIZE_T mGetFileSize(PFILE_MAPING p);
void reles(PFILE_MAPING p)
{
if (p->map_buffer)
UnmapViewOfFile(p->map_buffer);
if (p->mapFileH)
CloseHandle(p->mapFileH);
if (p->fileH)
CloseHandle(p->fileH);
memset(p, 0, sizeof(FILE_MAPING));
free(p);
}
int Init_Maping(PFILE_MAPING* p,wchar_t* path, DWORD access, DWORD sharemode, DWORD CreationDisposition, DWORD Flags)
{
(*p) = (PFILE_MAPING)malloc(sizeof(FILE_MAPING));
memset((*p),0, sizeof(FILE_MAPING));
(*p)->fileH = CreateFileW(path,
access/*GENERIC_READ | GENERIC_WRITE*/,
sharemode /*FILE_SHARE_READ| GENERIC_WRITE*/,
NULL,
CreationDisposition/*OPEN_EXISTING*/,
Flags/*FILE_ATTRIBUTE_NORMAL*/,
NULL);
if ((*p)->fileH == INVALID_HANDLE_VALUE)
{
int erro = GetLastError();
return 0;
}
(*p)->mapFileH = CreateFileMapping((*p)->fileH, NULL, PAGE_READONLY/*PAGE_READWRITE*/, 0, 0, 0);
if ((*p)->mapFileH == NULL)
{
int erro = GetLastError();
return 0;
}
return 1;
}
int ReadFile_Maping(PFILE_MAPING p,DWORD64 _offset, DWORD64 len, char** buffer, DWORD* readed, OVERLAPPED* poverlapped)
{
if (p == NULL || p->mapFileH == NULL || !len)
{
return ReadFile(p->fileH,*buffer, len, readed, poverlapped);
}
//if (len > MAP_BUFFER_LEN)
// return 0;
SIZE_T llen = mGetFileSize(p);
if (p->map_buffer && _offset >= p->buffer_offset && (_offset + len) < (p->mapsize + p->buffer_offset))
{
//*buffer = map_buffer + offset - buffer_offset;
memcpy(*buffer, p->map_buffer + _offset - p->buffer_offset, len);
}
else
{
if (p->map_buffer)
{
UnmapViewOfFile(p->map_buffer);
p->map_buffer = NULL;
p->buffer_offset = NULL;
p->mapsize = NULL;
}
static DWORD dwAllocationGranularity = 0;
if (!p->flag)
{
p->flag = 1;
SYSTEM_INFO sinf = { 0 };
GetSystemInfo(&sinf);
dwAllocationGranularity = sinf.dwAllocationGranularity;
}
DWORD64 needmapoffset = 0;
if(_offset)
needmapoffset = _offset - _offset % dwAllocationGranularity;
SIZE_T mapsize = MAP_BUFFER_LEN;
if (len > MAP_BUFFER_LEN)
mapsize = (SIZE_T)len;
if (needmapoffset + mapsize > llen)
mapsize = llen - needmapoffset;
p->map_buffer = (char*)MapViewOfFile(p->mapFileH, FILE_MAP_READ, *(DWORD*)((char*)&needmapoffset + 4), (DWORD)(needmapoffset & 0xFFFFFFFF), mapsize);
if (!p->map_buffer)
{
//return 0;
return ReadFile(p->fileH, *buffer, len, readed, poverlapped);
}
if (_offset > llen)
{
return ReadFile(p->fileH, *buffer, len, readed, poverlapped);
}
p->buffer_offset = needmapoffset;
p->mapsize = mapsize;
//*buffer = map_buffer + offset - buffer_offset;
memcpy(*buffer, p->map_buffer + _offset - needmapoffset, len);
}
*readed = len;
return 1;
}
SIZE_T mGetFileSize(PFILE_MAPING p)
{
if (p->filesize)
return p->filesize;
if (p->fileH)
{
*(DWORD*)&p->filesize = GetFileSize(p->fileH, (DWORD*)((char*)&p->filesize + 4));
return p->filesize;
}
return 0;
}
0 条评论