blockFile

Save data in chunked in blocks in a file for creating databases
git clone https://noulin.net/git/blockFile.git
Log | Files | Refs | LICENSE

blockFileInternal.h (2554B)


      1 #pragma once
      2 
      3 static blockFileFunctionst *blockFileF = NULL;
      4 
      5 /* TODO declare structs for private data and add a void pointer to the private data in the class declaration  */
      6 
      7 // file format
      8 // block 0 reserved > first 8 bytes: blockSize, next byte: block index size 32bits (0) or 64bits (1)
      9 // block 1 data
     10 // block 2 data
     11 // ...
     12 
     13 typedef struct {
     14   u64 blockSize;
     15   uint8_t  blockIndexSize:1; // 0 32bits - 1 64bits TODO 32 only for now
     16 } block0;
     17 
     18 // block format
     19 // byte 0:
     20 //  bit 0: 0 block in a chain - 1 first block in chain
     21 //  bit 1: 0 not compressed   - 1 compressed with lz4
     22 // dataSize u64         - size of the document, only first block
     23 // nextBlock u32 or u64 - 0 means no next block
     24 // (blCount  u64 - how many block for this document)
     25 // decompressedSize u32 - only when document is compressed, only first block
     26 // data
     27 //
     28 // First block
     29 // 1 or 3
     30 // dataSize
     31 // nextBlock
     32 // decompressedSize
     33 // data
     34 //
     35 // Other blocks
     36 // 0
     37 // nextBlock
     38 // data
     39 
     40 #define PACKED __attribute__((__packed__))
     41 
     42 typedef struct PACKED {
     43   u8 chain:1; // 0 block in a chain - 1 first block in chain
     44   u8 z:1;     // 0 not compressed   - 1 compressed with lz4
     45   u64 nextBlock;
     46   // end of header in chain block
     47   u64 dataSize;
     48   // end of header in not compressed document
     49   uint32_t decompressedSize;
     50 } blockt;
     51 
     52 struct PACKED firstBlockHeader {
     53   u8 chain:1; // 0 block in a chain - 1 first block in chain
     54   u8 z:1;     // 0 not compressed   - 1 compressed with lz4
     55   u64 nextBlock;
     56   u64 dataSize;
     57 };
     58 
     59 struct PACKED firstBlockHeaderCompressed {
     60   u8 chain:1; // 0 block in a chain - 1 first block in chain
     61   u8 z:1;     // 0 not compressed   - 1 compressed with lz4
     62   u64 nextBlock;
     63   u64 dataSize;
     64   uint32_t decompressedSize;
     65 };
     66 
     67 struct PACKED blockheader {
     68   u8 chain:1; // 0 block in a chain - 1 first block in chain
     69   u8 z:1;     // 0 not compressed   - 1 compressed with lz4
     70   u64 nextBlock;
     71 };
     72 
     73 
     74 
     75 // API
     76 // open         open file
     77 // load         load all documents in file
     78 // add          add data, return first block
     79 // get          get data by block
     80 // remove       remove data
     81 // delete       delete file
     82 // close        close file
     83 
     84 #define uI u32
     85 
     86 dArrayT(freeBlocks, uI);
     87 
     88 typedef enum {READWRITE, APPEND} fmodet;
     89 
     90 struct privateBlockFile {
     91   u64         count; // block count
     92   u64         blockSize;
     93   freeBlocks  freeBlocks;
     94   const char *name;
     95   char       *freeName;
     96   FILE       *f;
     97   fmodet      fmode;
     98   FILE       *freeF;
     99   flagsBlockFilet defaultFlags;
    100 };
    101 
    102 // save freeBlocks in a file
    103 
    104 #define fil (*(self->file))
    105 #define BLOCKSIZE 64
    106