简易数据库实现 UNIX环境高级编程(APUE)第二十章 A Database Library

将课程的源代码 使用C++写了一部分

LINUX WINDOW均可运行

#ifndef MYDB_H
#define MYDB_H

#include <iostream>
#include <fstream>
#include <string>

#define NHASH_DEF   137    /* default hash table size */
#define FREE_LINK  NHASH_DEF
#define PTR_SZ      8
#define MAGIC_WORDS "MyDB_Start:\n"
#define SEPARATOE    "#"



class MyDB{
public:
    MyDB(const char* pathName) : hashOffset(0),
    dataOffset(0),writeDataLength(0),idxOffset(0),writeIdxLength(0) 
    {
        DBidxName = pathName;
        DBidxName += ".idx";
        DBdataName = pathName;
        DBdataName += ".dat";
    }
    void CloseDB(){
            idxFile.close();
            datFile.close();
        }
    ~MyDB(){ CloseDB();}

    bool Init();
    unsigned long HashFunc(const char *key);
    bool WriteData(const char* data);
    bool WriteIndex(const char* key);
    size_t GetIndexOffsetBykey(const char* key);
    long ReadPtr(std::fstream& fs, size_t offset);
    bool Writeptr(std::fstream& fs, size_t offset, const std::string& writeString);
    void TestFunc(const char* searchKey);
    size_t SearchLinkForKey(size_t offset, const char* key);
    std::string Query(const char* key);
private:

    bool DoWriteIndex(const char* key, const size_t nextOffset);
    bool OpenFileOrExit(std::fstream& fs, const std::string& fileName);
    size_t hashOffset;
    size_t dataOffset;
    size_t writeDataLength;
    unsigned long idxOffset;
    size_t writeIdxLength;
    MyDB& operator=(const MyDB&);
    MyDB(const MyDB&);
    std::string DBidxName;
    std::string DBdataName;
    std::fstream idxFile;
    std::fstream datFile;
};

#endif // MYDB_H
mydb.h
#include <string>
#include "mydb.h"

void MyDB::TestFunc(const char* key)
{


}

std::string MyDB::Query(const char* key)
{
    size_t offset = GetIndexOffsetBykey(key);
    if (0 == offset)
        return "";

    size_t nextPtr = ReadPtr(idxFile, offset);
    size_t indexLength = ReadPtr(idxFile, offset + PTR_SZ);

    char* indexstr = new char[indexLength + 1];
    idxFile.read(indexstr, indexLength);
    indexstr[indexLength] = '\0';
    std::string indexString(indexstr);
    delete[] indexstr;

    std::string keyString = indexString.substr(0, indexString.find_first_of(SEPARATOE));

    return keyString;
}

bool MyDB::OpenFileOrExit(std::fstream& fs, const std::string& fileName) {
    fs.open(fileName, std::ios::in | std::ios::out | std::ios::binary);
    if (!fs.is_open()) {
        fs.open(fileName, std::ios::in | std::ios::out | std::ios::binary | std::ios::app);
        fs.close();
        fs.open(fileName, std::ios::in | std::ios::out | std::ios::binary);
        if (!fs.is_open()) {
            std::cerr << "open idxFile error.exit!" << std::endl;
            exit(-1);
        }
    }
    return true;
}

bool MyDB::Init() {
    OpenFileOrExit(idxFile,DBidxName);
    OpenFileOrExit(datFile, DBdataName);

    idxFile.seekg(0, std::ios_base::end);
    size_t len = idxFile.tellg();
    hashOffset = 0;

    if (len != 0)
        return true;

    //进行初始化
    idxFile.seekp(0, std::ios::beg);

    std::string ptrstr(PTR_SZ, ' ');
    ptrstr[PTR_SZ - 1] = '0';
    //std::cout << ptrstr << std::endl;
    for (int i = 0; i < NHASH_DEF + 1; i++)
        idxFile << ptrstr;
    idxFile << '\n';
    idxFile.flush();
    return true;
}

unsigned long
MyDB::HashFunc(const char *key)
{
    unsigned long   hval = 0;
    char        c;
    int            i;

    for (i = 1; (c = *key++) != 0; i++)
        hval += c * i;
    return(hval % NHASH_DEF);
}

bool MyDB::Writeptr(std::fstream& fs,size_t offset,const std::string& writeString) {
    fs.seekp(offset, std::ios::beg);
    fs << writeString;
    return true;
}

long MyDB::ReadPtr(std::fstream& fs,size_t offset) {
    long ret = 0;

    char ptrstr[PTR_SZ + 1];
    fs.seekg(offset, std::ios::beg);
    fs.read(ptrstr, PTR_SZ);
    ptrstr[PTR_SZ] = '\0';

    ret = atol(ptrstr);
    return ret;
}

size_t MyDB::SearchLinkForKey(size_t offset, const char* key)
{
    size_t currentPtr = offset;
    
    do {
        size_t nextPtr = ReadPtr(idxFile, currentPtr);
        size_t indexLength = ReadPtr(idxFile, currentPtr + PTR_SZ);

        char* indexstr = new char[indexLength + 1];
        idxFile.read(indexstr, indexLength);
        indexstr[indexLength] = '\0';
        std::string indexString(indexstr);
        delete[] indexstr;

        std::string keyString = indexString.substr(0, indexString.find_first_of(SEPARATOE));
        if (keyString == std::string(key))
            break;
        currentPtr = nextPtr;
    } while (0 != currentPtr);

    return currentPtr;

}


size_t MyDB::GetIndexOffsetBykey(const char* key) {
    size_t indexOff = 0;
    unsigned long hashKeyValue = HashFunc(key);

    size_t linkOffset = hashKeyValue*PTR_SZ + hashOffset;
    size_t idxOffset = ReadPtr(idxFile,linkOffset);
    if (0 == idxOffset)
        return 0;
    //有链表  便历链表 查看是否已有关键字
    size_t offset = SearchLinkForKey(idxOffset,key);

    return offset;
}

bool MyDB::DoWriteIndex(const char* key,const size_t nextOffset) {
    idxFile.seekp(0, std::ios::end);
    idxOffset = idxFile.tellp();

    std::string s = std::to_string(nextOffset);
    if (s.size() > PTR_SZ) {
        return false;
    }

    std::string writeString(PTR_SZ - s.size(), ' ');
    writeString += s;
    
    char ptrstr[PTR_SZ + 1];
    sprintf(ptrstr, "%*d", PTR_SZ,0);

    std::string idxString(writeString);
    idxString += ptrstr;
    idxString += key;
    idxString += SEPARATOE;
    idxString += std::to_string(dataOffset);
    idxString += SEPARATOE;
    idxString += std::to_string(writeDataLength);
    idxString += "\n";

    unsigned long size = idxString.size() - strlen(ptrstr) * 2;
    sprintf(ptrstr, "%*d", PTR_SZ, size);
    idxString.replace(PTR_SZ, PTR_SZ, ptrstr);
    //std::cout << idxString << std::endl;
    idxFile << idxString;
    //idxFile.flush();

    return true;
}

bool MyDB::WriteIndex(const char* key)
{
    unsigned long hashKeyValue = HashFunc(key);
    size_t linkOffset = hashKeyValue*PTR_SZ + hashOffset;
    size_t idxInLinkHeadoffset = ReadPtr(idxFile, linkOffset);

    if (0 == idxInLinkHeadoffset) {
        DoWriteIndex(key, 0);
        std::string s = std::to_string(idxOffset);
        if (s.size() <= PTR_SZ) {
            std::string writeString(PTR_SZ - s.size(), ' ');
            writeString += s;
            Writeptr(idxFile, linkOffset, writeString);
            return true;
        }
        else {
            return false;
        }
    }
    else {
        //在链表中寻找
        size_t offset = GetIndexOffsetBykey(key);
        if (0 != offset) {
            //寻找到则直接返回
            return  true;
        }
        DoWriteIndex(key, idxInLinkHeadoffset);
        std::string s = std::to_string(idxOffset);
        if (s.size() <= PTR_SZ) {
            std::string writeString(PTR_SZ - s.size(), ' ');
            writeString += s;
            Writeptr(idxFile, linkOffset, writeString);
            return true;
        }
        else {
            return false;
        }

    }

    return true;
}

bool MyDB::WriteData(const char* data)
{
    try {
        writeDataLength = strlen(data);
        datFile.seekp(0, std::ios::end);
        dataOffset = datFile.tellp();
        datFile << data;
    }
    catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    return true;
}
mydb.cpp

测试代码

// DB0_4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <assert.h>
#include "mydb.h"



int main()
{
    MyDB db("123");
    db.Init();
    for (int i = 0; i < 500; ++i) {
        std::string s = std::to_string(i);
        std::string writeIdxString("indexWrite");
        std::string writeDatString("dataWrite");
        writeIdxString += s;
        writeDatString += s;
        db.WriteData(writeDatString.c_str());
        db.WriteIndex(writeIdxString.c_str());
    }
    std::string queryKeyString1("indexWrite0");
    std::string queryKeyString2("indexWrite1");
    std::string queryKeyString3("indexWrite10");
    std::string queryKeyString4("indexWrite100");
    std::string queryKeyString5("indexWrite_");
    std::string queryKeyString6("indexWritex");
    std::string queryKeyString7("ShouldNotFind");
    assert(db.Query(queryKeyString1.c_str())  == queryKeyString1);
    assert(db.Query(queryKeyString2.c_str()) == queryKeyString2);
    assert(db.Query(queryKeyString3.c_str()) == queryKeyString3);
    assert(db.Query(queryKeyString4.c_str()) == queryKeyString4);


    assert(db.Query(queryKeyString5.c_str()) != queryKeyString5);
    assert(db.Query(queryKeyString6.c_str()) != queryKeyString6);
    assert(db.Query(queryKeyString7.c_str()) != queryKeyString7);

    std::cout << "fine" << std::endl;
    return 0;
}
main.cpp

代码运行后 文件效果

 

  1     5300    4958       0   17992   17650   19778   19436   19094   16282   12862   12520    9100    8758    5338    4996   18372   18030   17688   19816   19474   16662   16320   12900   12558    9138    8796    5376   18752   18410   18068   17726   19854   17042   16700   16358   12938   12596    9176    8834   19132   18790   18448   18106   17764   17422   17080   16738   16396   12976   12634    9214   19512   19170   18828   18486   18144   17802   17460   17118   16776   16434   13014    9594    6174   19550   19208   18866   18524   18182   17840   17498   17156   16814   13394    9974    6554    2740   19588   19246   18904   18562   18220   17878   17536   17194   13774   10354    6934    2776    2452   19626   19284   18942   18600   18258   17916   17574   14154   10734    7314    2812    2488    1206   19664   19322   18980   18638   18296   17954   14534   11114    7694    4882    1240       0       0   19702   19360   19018   18676   18334   14914   11494    8682    5262    4920       0       0   17612   19740   19398   19056   18714   15294   12482    9062    8720       0
  2        0      17indexWrite0#0#10
  3        0      18indexWrite1#10#10
  4        0      18indexWrite2#20#10
  5        0      18indexWrite3#30#10
  6        0      18indexWrite4#40#10
  7        0      18indexWrite5#50#10
  8        0      18indexWrite6#60#10
  9        0      18indexWrite7#70#10
 10        0      18indexWrite8#80#10
 11        0      18indexWrite9#90#10
 12        0      20indexWrite10#100#11
 13        0      20indexWrite11#111#11
 14        0      20indexWrite12#122#11
 15        0      20indexWrite13#133#11
 16        0      20indexWrite14#144#11
 17     1410      20indexWrite15#155#11
 18        0      20indexWrite16#166#11
 19        0      20indexWrite17#177#11
 20        0      20indexWrite18#188#11
 21        0      20indexWrite19#199#11
 22        0      20indexWrite20#210#11
 23        0      20indexWrite21#221#11
 24        0      20indexWrite22#232#11
 25        0      20indexWrite23#243#11
 26        0      20indexWrite24#254#11
 27        0      20indexWrite25#265#11
 28        0      20indexWrite26#276#11
 29        0      20indexWrite27#287#11
 30        0      20indexWrite28#298#11
 31        0      20indexWrite29#309#11
 32        0      20indexWrite30#320#11
 33        0      20indexWrite31#331#11
 34        0      20indexWrite32#342#11
 35        0      20indexWrite33#353#11
 36        0      20indexWrite34#364#11
 37        0      20indexWrite35#375#11
 38        0      20indexWrite36#386#11
 39        0      20indexWrite37#397#11
 40        0      20indexWrite38#408#11
 41        0      20indexWrite39#419#11
 42        0      20indexWrite40#430#11
 43        0      20indexWrite41#441#11
 44        0      20indexWrite42#452#11
 45        0      20indexWrite43#463#11
 46        0      20indexWrite44#474#11
 47        0      20indexWrite45#485#11
 48        0      20indexWrite46#496#11
 49        0      20indexWrite47#507#11
 50        0      20indexWrite48#518#11
 51        0      20indexWrite49#529#11
 52        0      20indexWrite50#540#11
 53        0      20indexWrite51#551#11
 54        0      20indexWrite52#562#11
 55        0      20indexWrite53#573#11
 56        0      20indexWrite54#584#11
 57        0      20indexWrite55#595#11
 58        0      20indexWrite56#606#11
 59        0      20indexWrite57#617#11
 60        0      20indexWrite58#628#11
 61        0      20indexWrite59#639#11
 62        0      20indexWrite60#650#11
 63        0      20indexWrite61#661#11
 64        0      20indexWrite62#672#11
 65        0      20indexWrite63#683#11
 66        0      20indexWrite64#694#11
 67        0      20indexWrite65#705#11
 68        0      20indexWrite66#716#11
 69        0      20indexWrite67#727#11
 70        0      20indexWrite68#738#11
 71        0      20indexWrite69#749#11
 72        0      20indexWrite70#760#11
 73        0      20indexWrite71#771#11
 74        0      20indexWrite72#782#11
 75        0      20indexWrite73#793#11
 76        0      20indexWrite74#804#11
 77        0      20indexWrite75#815#11
 78        0      20indexWrite76#826#11
 79        0      20indexWrite77#837#11
 80        0      20indexWrite78#848#11
 81        0      20indexWrite79#859#11
 82        0      20indexWrite80#870#11
 83        0      20indexWrite81#881#11
 84        0      20indexWrite82#892#11
 85        0      20indexWrite83#903#11
 86        0      20indexWrite84#914#11
 87     1444      20indexWrite85#925#11
 88     1480      20indexWrite86#936#11
 89     1516      20indexWrite87#947#11
 90     1552      20indexWrite88#958#11
 91     1588      20indexWrite89#969#11
 92        0      20indexWrite90#980#11
 93        0      20indexWrite91#991#11
 94        0      21indexWrite92#1002#11
 95        0      21indexWrite93#1013#11
 96        0      21indexWrite94#1024#11
 97     1804      21indexWrite95#1035#11
 98     1840      21indexWrite96#1046#11
 99     1876      21indexWrite97#1057#11
100     1912      21indexWrite98#1068#11
101     1948      21indexWrite99#1079#11
102        0      22indexWrite100#1090#12
103     4000      22indexWrite101#1102#12
104     3712      22indexWrite102#1114#12
105     3424      22indexWrite103#1126#12
106     3136      22indexWrite104#1138#12
107     2848      22indexWrite105#1150#12
108        0      22indexWrite106#1162#12
109        0      22indexWrite107#1174#12
110        0      22indexWrite108#1186#12
111     2920      22indexWrite109#1198#12
112     4324      22indexWrite110#1210#12
113     4036      22indexWrite111#1222#12
114     3748      22indexWrite112#1234#12
115     3460      22indexWrite113#1246#12
116     3172      22indexWrite114#1258#12
117     1274      22indexWrite115#1270#12
118        0      22indexWrite116#1282#12
119        0      22indexWrite117#1294#12
120     3244      22indexWrite118#1306#12
121     2956      22indexWrite119#1318#12
122     4360      22indexWrite120#1330#12
123     4072      22indexWrite121#1342#12
124     3784      22indexWrite122#1354#12
125     3496      22indexWrite123#1366#12
126     3208      22indexWrite124#1378#12
127        0      22indexWrite125#1390#12
128        0      22indexWrite126#1402#12
129        0      22indexWrite127#1414#12
130     3280      22indexWrite128#1426#12
131     2992      22indexWrite129#1438#12
132     4396      22indexWrite130#1450#12
133     4108      22indexWrite131#1462#12
134     3820      22indexWrite132#1474#12
135     3532      22indexWrite133#1486#12
136     1308      22indexWrite134#1498#12
137        0      22indexWrite135#1510#12
138        0      22indexWrite136#1522#12
139     3604      22indexWrite137#1534#12
140     3316      22indexWrite138#1546#12
141     3028      22indexWrite139#1558#12
142     4433      22indexWrite140#1570#12
143     4144      22indexWrite141#1582#12
144     3856      22indexWrite142#1594#12
145     3568      22indexWrite143#1606#12
146        0      22indexWrite144#1618#12
147        0      22indexWrite145#1630#12
148        0      22indexWrite146#1642#12
149     3640      22indexWrite147#1654#12
150     3352      22indexWrite148#1666#12
151     3064      22indexWrite149#1678#12
152     4470      22indexWrite150#1690#12
153     4180      22indexWrite151#1702#12
154     3892      22indexWrite152#1714#12
155     1342      22indexWrite153#1726#12
156        0      22indexWrite154#1738#12
157        0      22indexWrite155#1750#12
158     3964      22indexWrite156#1762#12
159     3676      22indexWrite157#1774#12
160     3388      22indexWrite158#1786#12
161     3100      22indexWrite159#1798#12
162     4507      22indexWrite160#1810#12
163     4216      22indexWrite161#1822#12
164     3928      22indexWrite162#1834#12
165        0      22indexWrite163#1846#12
166        0      22indexWrite164#1858#12
167     4692      22indexWrite165#1870#12
168     4730      22indexWrite166#1882#12
169     4768      22indexWrite167#1894#12
170     4806      22indexWrite168#1906#12
171     4844      22indexWrite169#1918#12
172     4544      22indexWrite170#1930#12
173     4252      22indexWrite171#1942#12
174     1376      22indexWrite172#1954#12
175        0      22indexWrite173#1966#12
176        0      22indexWrite174#1978#12
177     5072      22indexWrite175#1990#12
178     5110      22indexWrite176#2002#12
179     5148      22indexWrite177#2014#12
180     5186      22indexWrite178#2026#12
181     5224      22indexWrite179#2038#12
182     4581      22indexWrite180#2050#12
183     4288      22indexWrite181#2062#12
184        0      22indexWrite182#2074#12
185        0      22indexWrite183#2086#12
186        0      22indexWrite184#2098#12
187     5452      22indexWrite185#2110#12
188     5490      22indexWrite186#2122#12
189     5528      22indexWrite187#2134#12
190     5566      22indexWrite188#2146#12
191     5604      22indexWrite189#2158#12
192     4618      22indexWrite190#2170#12
193     1624      22indexWrite191#2182#12
194        0      22indexWrite192#2194#12
195        0      22indexWrite193#2206#12
196        0      22indexWrite194#2218#12
197     5832      22indexWrite195#2230#12
198     5870      22indexWrite196#2242#12
199     5908      22indexWrite197#2254#12
200     5946      22indexWrite198#2266#12
201     5984      22indexWrite199#2278#12
202     7884      22indexWrite200#2290#12
203     7922      22indexWrite201#2302#12
204     7960      22indexWrite202#2314#12
205     7998      22indexWrite203#2326#12
206     8036      22indexWrite204#2338#12
207     8074      22indexWrite205#2350#12
208     5642      22indexWrite206#2362#12
209     5680      22indexWrite207#2374#12
210     5718      22indexWrite208#2386#12
211     5756      22indexWrite209#2398#12
212     8264      22indexWrite210#2410#12
213     8302      22indexWrite211#2422#12
214     8340      22indexWrite212#2434#12
215     8378      22indexWrite213#2446#12
216     8416      22indexWrite214#2458#12
217     8454      22indexWrite215#2470#12
218     6022      22indexWrite216#2482#12
219     6060      22indexWrite217#2494#12
220     6098      22indexWrite218#2506#12
221     6136      22indexWrite219#2518#12
222        0      22indexWrite220#2530#12
223     6212      22indexWrite221#2542#12
224     6250      22indexWrite222#2554#12
225     6288      22indexWrite223#2566#12
226     6326      22indexWrite224#2578#12
227     6364      22indexWrite225#2590#12
228     6402      22indexWrite226#2602#12
229     6440      22indexWrite227#2614#12
230     6478      22indexWrite228#2626#12
231     6516      22indexWrite229#2638#12
232        0      22indexWrite230#2650#12
233     6592      22indexWrite231#2662#12
234     6630      22indexWrite232#2674#12
235     6668      22indexWrite233#2686#12
236     6706      22indexWrite234#2698#12
237     6744      22indexWrite235#2710#12
238     6782      22indexWrite236#2722#12
239     6820      22indexWrite237#2734#12
240     6858      22indexWrite238#2746#12
241     6896      22indexWrite239#2758#12
242        0      22indexWrite240#2770#12
243     6972      22indexWrite241#2782#12
244     7010      22indexWrite242#2794#12
245     7048      22indexWrite243#2806#12
246     7086      22indexWrite244#2818#12
247     7124      22indexWrite245#2830#12
248     7162      22indexWrite246#2842#12
249     7200      22indexWrite247#2854#12
250     7238      22indexWrite248#2866#12
251     7276      22indexWrite249#2878#12
252        0      22indexWrite250#2890#12
253     7352      22indexWrite251#2902#12
254     7390      22indexWrite252#2914#12
255     7428      22indexWrite253#2926#12
256     7466      22indexWrite254#2938#12
257     7504      22indexWrite255#2950#12
258     7542      22indexWrite256#2962#12
259     7580      22indexWrite257#2974#12
260     7618      22indexWrite258#2986#12
261     7656      22indexWrite259#2998#12
262     2164      22indexWrite260#3010#12
263     7732      22indexWrite261#3022#12
264     7770      22indexWrite262#3034#12
265     7808      22indexWrite263#3046#12
266     7846      22indexWrite264#3058#12
267     8492      22indexWrite265#3070#12
268     8530      22indexWrite266#3082#12
269     8568      22indexWrite267#3094#12
270     8606      22indexWrite268#3106#12
271     8644      22indexWrite269#3118#12
272     2200      22indexWrite270#3130#12
273     8112      22indexWrite271#3142#12
274     8150      22indexWrite272#3154#12
275     8188      22indexWrite273#3166#12
276     8226      22indexWrite274#3178#12
277     8872      22indexWrite275#3190#12
278     8910      22indexWrite276#3202#12
279     8948      22indexWrite277#3214#12
280     8986      22indexWrite278#3226#12
281     9024      22indexWrite279#3238#12
282     2236      22indexWrite280#3250#12
283     4655      22indexWrite281#3262#12
284     1660      22indexWrite282#3274#12
285        0      22indexWrite283#3286#12
286     1105      22indexWrite284#3298#12
287     9252      22indexWrite285#3310#12
288     9290      22indexWrite286#3322#12
289     9328      22indexWrite287#3334#12
290     9366      22indexWrite288#3346#12
291     9404      22indexWrite289#3358#12
292     2272      22indexWrite290#3370#12
293     1984      22indexWrite291#3382#12
294     1696      22indexWrite292#3394#12
295        0      22indexWrite293#3406#12
296        0      22indexWrite294#3418#12
297     9632      22indexWrite295#3430#12
298     9670      22indexWrite296#3442#12
299     9708      22indexWrite297#3454#12
300     9746      22indexWrite298#3466#12
301     9784      22indexWrite299#3478#12
302    11684      22indexWrite300#3490#12
303    11722      22indexWrite301#3502#12
304    11760      22indexWrite302#3514#12
305    11798      22indexWrite303#3526#12
306    11836      22indexWrite304#3538#12
307    11874      22indexWrite305#3550#12
308     9442      22indexWrite306#3562#12
309     9480      22indexWrite307#3574#12
310     9518      22indexWrite308#3586#12
311     9556      22indexWrite309#3598#12
312    12064      22indexWrite310#3610#12
313    12102      22indexWrite311#3622#12
314    12140      22indexWrite312#3634#12
315    12178      22indexWrite313#3646#12
316    12216      22indexWrite314#3658#12
317    12254      22indexWrite315#3670#12
318     9822      22indexWrite316#3682#12
319     9860      22indexWrite317#3694#12
320     9898      22indexWrite318#3706#12
321     9936      22indexWrite319#3718#12
322        0      22indexWrite320#3730#12
323    10012      22indexWrite321#3742#12
324    10050      22indexWrite322#3754#12
325    10088      22indexWrite323#3766#12
326    10126      22indexWrite324#3778#12
327    10164      22indexWrite325#3790#12
328    10202      22indexWrite326#3802#12
329    10240      22indexWrite327#3814#12
330    10278      22indexWrite328#3826#12
331    10316      22indexWrite329#3838#12
332        0      22indexWrite330#3850#12
333    10392      22indexWrite331#3862#12
334    10430      22indexWrite332#3874#12
335    10468      22indexWrite333#3886#12
336    10506      22indexWrite334#3898#12
337    10544      22indexWrite335#3910#12
338    10582      22indexWrite336#3922#12
339    10620      22indexWrite337#3934#12
340    10658      22indexWrite338#3946#12
341    10696      22indexWrite339#3958#12
342        0      22indexWrite340#3970#12
343    10772      22indexWrite341#3982#12
344    10810      22indexWrite342#3994#12
345    10848      22indexWrite343#4006#12
346    10886      22indexWrite344#4018#12
347    10924      22indexWrite345#4030#12
348    10962      22indexWrite346#4042#12
349    11000      22indexWrite347#4054#12
350    11038      22indexWrite348#4066#12
351    11076      22indexWrite349#4078#12
352        0      22indexWrite350#4090#12
353    11152      22indexWrite351#4102#12
354    11190      22indexWrite352#4114#12
355    11228      22indexWrite353#4126#12
356    11266      22indexWrite354#4138#12
357    11304      22indexWrite355#4150#12
358    11342      22indexWrite356#4162#12
359    11380      22indexWrite357#4174#12
360    11418      22indexWrite358#4186#12
361    11456      22indexWrite359#4198#12
362     2524      22indexWrite360#4210#12
363    11532      22indexWrite361#4222#12
364    11570      22indexWrite362#4234#12
365    11608      22indexWrite363#4246#12
366    11646      22indexWrite364#4258#12
367    12292      22indexWrite365#4270#12
368    12330      22indexWrite366#4282#12
369    12368      22indexWrite367#4294#12
370    12406      22indexWrite368#4306#12
371    12444      22indexWrite369#4318#12
372     2560      22indexWrite370#4330#12
373    11912      22indexWrite371#4342#12
374    11950      22indexWrite372#4354#12
375    11988      22indexWrite373#4366#12
376    12026      22indexWrite374#4378#12
377    12672      22indexWrite375#4390#12
378    12710      22indexWrite376#4402#12
379    12748      22indexWrite377#4414#12
380    12786      22indexWrite378#4426#12
381    12824      22indexWrite379#4438#12
382     2596      22indexWrite380#4450#12
383     2308      22indexWrite381#4462#12
384     2020      22indexWrite382#4474#12
385     1732      22indexWrite383#4486#12
386     1138      22indexWrite384#4498#12
387    13052      22indexWrite385#4510#12
388    13090      22indexWrite386#4522#12
389    13128      22indexWrite387#4534#12
390    13166      22indexWrite388#4546#12
391    13204      22indexWrite389#4558#12
392     2632      22indexWrite390#4570#12
393     2344      22indexWrite391#4582#12
394     2056      22indexWrite392#4594#12
395     1768      22indexWrite393#4606#12
396        0      22indexWrite394#4618#12
397    13432      22indexWrite395#4630#12
398    13470      22indexWrite396#4642#12
399    13508      22indexWrite397#4654#12
400    13546      22indexWrite398#4666#12
401    13584      22indexWrite399#4678#12
402    15484      22indexWrite400#4690#12
403    15522      22indexWrite401#4702#12
404    15560      22indexWrite402#4714#12
405    15598      22indexWrite403#4726#12
406    15636      22indexWrite404#4738#12
407    15674      22indexWrite405#4750#12
408    13242      22indexWrite406#4762#12
409    13280      22indexWrite407#4774#12
410    13318      22indexWrite408#4786#12
411    13356      22indexWrite409#4798#12
412    15864      22indexWrite410#4810#12
413    15902      22indexWrite411#4822#12
414    15940      22indexWrite412#4834#12
415    15978      22indexWrite413#4846#12
416    16016      22indexWrite414#4858#12
417    16054      22indexWrite415#4870#12
418    13622      22indexWrite416#4882#12
419    13660      22indexWrite417#4894#12
420    13698      22indexWrite418#4906#12
421    13736      22indexWrite419#4918#12
422        0      22indexWrite420#4930#12
423    13812      22indexWrite421#4942#12
424    13850      22indexWrite422#4954#12
425    13888      22indexWrite423#4966#12
426    13926      22indexWrite424#4978#12
427    13964      22indexWrite425#4990#12
428    14002      22indexWrite426#5002#12
429    14040      22indexWrite427#5014#12
430    14078      22indexWrite428#5026#12
431    14116      22indexWrite429#5038#12
432        0      22indexWrite430#5050#12
433    14192      22indexWrite431#5062#12
434    14230      22indexWrite432#5074#12
435    14268      22indexWrite433#5086#12
436    14306      22indexWrite434#5098#12
437    14344      22indexWrite435#5110#12
438    14382      22indexWrite436#5122#12
439    14420      22indexWrite437#5134#12
440    14458      22indexWrite438#5146#12
441    14496      22indexWrite439#5158#12
442        0      22indexWrite440#5170#12
443    14572      22indexWrite441#5182#12
444    14610      22indexWrite442#5194#12
445    14648      22indexWrite443#5206#12
446    14686      22indexWrite444#5218#12
447    14724      22indexWrite445#5230#12
448    14762      22indexWrite446#5242#12
449    14800      22indexWrite447#5254#12
450    14838      22indexWrite448#5266#12
451    14876      22indexWrite449#5278#12
452        0      22indexWrite450#5290#12
453    14952      22indexWrite451#5302#12
454    14990      22indexWrite452#5314#12
455    15028      22indexWrite453#5326#12
456    15066      22indexWrite454#5338#12
457    15104      22indexWrite455#5350#12
458    15142      22indexWrite456#5362#12
459    15180      22indexWrite457#5374#12
460    15218      22indexWrite458#5386#12
461    15256      22indexWrite459#5398#12
462     2884      22indexWrite460#5410#12
463    15332      22indexWrite461#5422#12
464    15370      22indexWrite462#5434#12
465    15408      22indexWrite463#5446#12
466    15446      22indexWrite464#5458#12
467    16092      22indexWrite465#5470#12
468    16130      22indexWrite466#5482#12
469    16168      22indexWrite467#5494#12
470    16206      22indexWrite468#5506#12
471    16244      22indexWrite469#5518#12
472     5034      22indexWrite470#5530#12
473    15712      22indexWrite471#5542#12
474    15750      22indexWrite472#5554#12
475    15788      22indexWrite473#5566#12
476    15826      22indexWrite474#5578#12
477    16472      22indexWrite475#5590#12
478    16510      22indexWrite476#5602#12
479    16548      22indexWrite477#5614#12
480    16586      22indexWrite478#5626#12
481    16624      22indexWrite479#5638#12
482     5414      22indexWrite480#5650#12
483     2668      22indexWrite481#5662#12
484     2380      22indexWrite482#5674#12
485     2092      22indexWrite483#5686#12
486     1172      22indexWrite484#5698#12
487    16852      22indexWrite485#5710#12
488    16890      22indexWrite486#5722#12
489    16928      22indexWrite487#5734#12
490    16966      22indexWrite488#5746#12
491    17004      22indexWrite489#5758#12
492     5794      22indexWrite490#5770#12
493     2704      22indexWrite491#5782#12
494     2416      22indexWrite492#5794#12
495     2128      22indexWrite493#5806#12
496        0      22indexWrite494#5818#12
497    17232      22indexWrite495#5830#12
498    17270      22indexWrite496#5842#12
499    17308      22indexWrite497#5854#12
500    17346      22indexWrite498#5866#12
501    17384      22indexWrite499#5878#12
View Code

 

 

 

大体思路如下:

使用两个文件分开存储 key 和value

value存储在data文件中 中规中矩的存储 不多描述

key值存储在index文件中 使用素数(137)个桶来存储key值

0  0  0  0  0  0.........0  0   <-----137 个桶

当输入一个 kv 值时,比如输入 ''key" "value"

经过哈希运算 key的哈希值为2 则在第3个桶(计数规则从0开始,0,1,2)记录key写入index文件的偏移值

则index文件内容如下

0  0  1137  0  0  0.......0  0

0 8key#0#5

 

表示在第3个桶内有内容  由于仅有一个记录 所以INDEX的一开始为0  若有其他记录则记录桶内下个元素在文件里的偏移  后面的8 表示接下来的字节加'\n'共长8个字节

后面的内容则是key值与data在data文件里的起始位置和长度

 

posted on 2017-07-25 11:26  itdef  阅读(880)  评论(0编辑  收藏  举报

导航