{"id":3582,"date":"2020-09-01T14:16:10","date_gmt":"2020-09-01T06:16:10","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=3582"},"modified":"2020-09-01T14:16:10","modified_gmt":"2020-09-01T06:16:10","slug":"sqlite%e6%9f%a5%e8%af%a2%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%ef%bc%88%e5%ae%9e%e6%b5%8b%e9%a1%b9%e7%9b%ae%e4%b8%ad%e8%8a%82%e7%9c%81%e4%b8%80%e5%8d%8a%e6%97%b6%e9%97%b4%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/qq%e7%ba%a2%e5%8c%85%e7%9a%84%e5%91%93%e8%af%ad\/3582\/","title":{"rendered":"sqlite\u67e5\u8be2\u6027\u80fd\u4f18\u5316\uff08\u5b9e\u6d4b\u9879\u76ee\u4e2d\u8282\u7701\u4e00\u534a\u65f6\u95f4\uff09"},"content":{"rendered":"\n<p style=\"font-size:24px\">\u901a\u8fc7\u4f18\u5316\u8bfb\u53d6\u63d0\u5347\u67e5\u8be2\u6548\u7387<\/p>\n\n\n<p style=\"font-size:24px\">\u53ea\u9700\u8981\u5728<strong>sqlite.c<\/strong>\u52a0\u5165\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>#define MAP_BUFFER_LEN 1024 * 1024 * 10\n#include &lt;Windows.h&gt;\ntypedef struct _FILE_MAPING\n{\n    HANDLE fileH;\n    HANDLE mapFileH;\n    char* map_buffer;\n    SIZE_T mapsize;\n    DWORD64 buffer_offset;\n    SIZE_T filesize;\n    int flag;\n}*PFILE_MAPING, FILE_MAPING;\nSIZE_T mGetFileSize(PFILE_MAPING p);\n    void reles(PFILE_MAPING p)\n    {\n        if (p-&gt;map_buffer)\n            UnmapViewOfFile(p-&gt;map_buffer);\n        if (p-&gt;mapFileH)\n            CloseHandle(p-&gt;mapFileH);\n        if (p-&gt;fileH)\n            CloseHandle(p-&gt;fileH);\n        memset(p, 0, sizeof(FILE_MAPING));\n        free(p);\n    }\n    int Init_Maping(PFILE_MAPING* p,wchar_t* path, DWORD access, DWORD sharemode, DWORD CreationDisposition, DWORD Flags)\n    {\n        (*p) = (PFILE_MAPING)malloc(sizeof(FILE_MAPING));\n         memset((*p),0, sizeof(FILE_MAPING));\n        (*p)-&gt;fileH = CreateFileW(path,\n            access\/*GENERIC_READ | GENERIC_WRITE*\/,\n            sharemode \/*FILE_SHARE_READ| GENERIC_WRITE*\/,\n            NULL,\n            CreationDisposition\/*OPEN_EXISTING*\/,\n            Flags\/*FILE_ATTRIBUTE_NORMAL*\/,\n            NULL);\n        if ((*p)-&gt;fileH == INVALID_HANDLE_VALUE)\n        {\n            int erro = GetLastError();\n            return 0;\n        }\n        (*p)-&gt;mapFileH = CreateFileMapping((*p)-&gt;fileH, NULL, PAGE_READONLY\/*PAGE_READWRITE*\/, 0, 0, 0);\n        if ((*p)-&gt;mapFileH == NULL)\n        {\n            int erro = GetLastError();\n            return 0;\n        }\n        return 1;\n    }\n    int ReadFile_Maping(PFILE_MAPING p,DWORD64 _offset, DWORD64 len, char** buffer, DWORD* readed, OVERLAPPED* poverlapped)\n    {\n        if (p == NULL || p-&gt;mapFileH == NULL || !len)\n        {\n            return ReadFile(p-&gt;fileH,*buffer, len, readed, poverlapped);\n        }\n        \/\/if (len &gt; MAP_BUFFER_LEN)\n        \/\/    return 0;\n        SIZE_T llen = mGetFileSize(p);\n        if (p-&gt;map_buffer &amp;&amp; _offset &gt;= p-&gt;buffer_offset &amp;&amp; (_offset + len) &lt; (p-&gt;mapsize + p-&gt;buffer_offset))\n        {\n            \/\/*buffer = map_buffer + offset - buffer_offset;\n            memcpy(*buffer, p-&gt;map_buffer + _offset - p-&gt;buffer_offset, len);\n        }\n        else\n        {\n            if (p-&gt;map_buffer)\n            {\n                UnmapViewOfFile(p-&gt;map_buffer);\n                p-&gt;map_buffer = NULL;\n                p-&gt;buffer_offset = NULL;\n                p-&gt;mapsize = NULL;\n            }\n            static DWORD dwAllocationGranularity = 0;\n            if (!p-&gt;flag)\n            {\n                p-&gt;flag = 1;\n                SYSTEM_INFO sinf = { 0 };\n                GetSystemInfo(&amp;sinf);\n                dwAllocationGranularity = sinf.dwAllocationGranularity;\n            }\n            DWORD64 needmapoffset = 0;\n            if(_offset)\n                needmapoffset = _offset - _offset % dwAllocationGranularity;\n            SIZE_T mapsize = MAP_BUFFER_LEN;\n            if (len &gt; MAP_BUFFER_LEN)\n\t\t\t\tmapsize = (SIZE_T)len;\n\t\t\tif (needmapoffset + mapsize &gt; llen)\n\t\t\t\tmapsize = llen - needmapoffset;\n            p-&gt;map_buffer = (char*)MapViewOfFile(p-&gt;mapFileH, FILE_MAP_READ, *(DWORD*)((char*)&amp;needmapoffset + 4), (DWORD)(needmapoffset &amp; 0xFFFFFFFF), mapsize);\n\t\t\tif (!p-&gt;map_buffer)\n\t\t\t{\n\t\t\t\t\/\/return 0;\n\t\t\t\treturn ReadFile(p-&gt;fileH, *buffer, len, readed, poverlapped);\n\t\t\t}\n\t\t\tif (_offset &gt; llen)\n\t\t\t{\n\t\t\t\treturn ReadFile(p-&gt;fileH, *buffer, len, readed, poverlapped);\n\t\t\t}\n            p-&gt;buffer_offset = needmapoffset;\n            p-&gt;mapsize = mapsize;\n            \/\/*buffer = map_buffer + offset - buffer_offset;\n\t\t\tmemcpy(*buffer, p-&gt;map_buffer + _offset - needmapoffset, len);\n        }\n        *readed = len;\n        return 1;\n    }\n    SIZE_T mGetFileSize(PFILE_MAPING p)\n    {\n        if (p-&gt;filesize)\n            return  p-&gt;filesize;\n        if (p-&gt;fileH)\n        {\n            *(DWORD*)&amp;p-&gt;filesize = GetFileSize(p-&gt;fileH, (DWORD*)((char*)&amp;p-&gt;filesize + 4));\n            return  p-&gt;filesize;\n        }\n        return 0;\n    }\n<\/code><\/pre>\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2020\/09\/image-8-1024x417.png\" alt=\"\" class=\"wp-image-3584\" width=\"1253\" height=\"510\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-8-1024x417.png 1024w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-8-300x122.png 300w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-8-768x313.png 768w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-8.png 1125w\" sizes=\"(max-width: 1253px) 100vw, 1253px\" \/><\/figure>\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2020\/09\/image-9-1024x287.png\" alt=\"\" class=\"wp-image-3585\" width=\"1356\" height=\"380\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-9-1024x287.png 1024w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-9-300x84.png 300w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-9-768x215.png 768w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/09\/image-9.png 1174w\" sizes=\"(max-width: 1356px) 100vw, 1356px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u4f18\u5316\u8bfb\u53d6\u63d0\u5347\u67e5\u8be2\u6548\u7387 \u53ea\u9700\u8981\u5728sqlite.c\u52a0\u5165\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"views":5443,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3582"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3582"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3582\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3582"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}