⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testblob.cpp

📁 全新的纯内存式实时数据库
💻 CPP
字号:
#include "testblob.h"McoSqlEngine engine;void insertFile(int id, char* name) {     File file;    file.name = name;    file.id = id;    FILE* f = fopen(name, "rb");    fseek(f, 0, SEEK_END);    long content_length = ftell(f);        char* content = new char[content_length];    fseek(f, 0, SEEK_SET);    fread(content, 1, content_length, f);    fclose(f);    ScalarArray<char> blob(content, content_length);    file.blob = &blob;    // Build vector of file line offsets    ScalarArray<int> lines(10);    lines.append(0);    for (int i = 0; i < content_length; i++) {         if (content[i] == '\n') {             lines.append(i+1);        }    }    file.lines = &lines;        if (id & 1) {         // insert record using traditional SQL syntax        engine.executeStatement("insert into File values (%i,%s,%v,%v)", id, name, &blob, &lines);    } else {         // insert record using eXtremeSQL structure mapping        engine.executeStatement("insert into File %r", &file);    }            delete[] content;}void fetchFile(int id, char* name) {     File file;    QueryResult result(engine.executeQuery("select * from File where name=%s", name));    Cursor* cursor = result->records();    Record* rec = cursor->next();    result->extract(rec, &file, sizeof(File));    assert(file.id == id);    assert(strcmp(file.name, name) == 0);    FILE* f = fopen(name, "rb");    ScalarArray<int> lines(100);    lines.append(0);    Blob* blob = (Blob*)file.blob;    int offs = 0;    while (true) {         char buf[2][1024];        int len = blob->get(buf[0], sizeof(buf[0]));        if (len == 0) {             assert(fread(buf[1], 1, 1, f) == 0);            break;        }        fread(buf[1], 1, len, f);        assert(memcmp(buf[0], buf[1], len) == 0);        for (int i = 0; i < len; i++) {             if (buf[0][i] == '\n') {                 lines.append(offs + i + 1);            }        }        offs += len;    }    fclose(f);    Array* arr = (Array*)rec->get(3); // the same as file.lines    assert(arr->size() == lines.size());    assert(file.lines->size() == lines.size());    int* buf = new int[arr->size()];    arr->getBody(buf, 0, arr->size());    assert(memcmp(buf, lines.pointer(), lines.size()*sizeof(int)) == 0);    file.lines->getBody(buf, 0, file.lines->size());    assert(memcmp(buf, lines.pointer(), lines.size()*sizeof(int)) == 0);    }    int main(){    engine.open("testblobdb", testblobdb_get_dictionary(), DATABASE_SIZE, PAGE_SIZE, MAP_ADDRESS);#ifndef _VXWORKS    insertFile(1, "testblob.cpp");    insertFile(2, "testblob.h");    fetchFile(1, "testblob.cpp");    fetchFile(2, "testblob.h");#else    insertFile(1, "/tgtsvr/testblob.cpp");    insertFile(2, "/tgtsvr/testblob.h");    fetchFile(1, "/tgtsvr/testblob.cpp");    fetchFile(2, "/tgtsvr/testblob.h");#endif    engine.close();    printf("Test passed\n");    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -