func TestSimpleJSONDB_InitializesDataFile(t *testing.T) { firstDataBlock := make([]byte, 10) blocksBitMapBlock := make([]byte, dbio.DATABLOCK_SIZE) bTreeRootBlock := make([]byte, 2) fakeDataFile := utils.NewFakeDataFileWithBlocks([][]byte{ firstDataBlock, blocksBitMapBlock, nil, nil, bTreeRootBlock, }) jsondb.NewWithDataFile(fakeDataFile) if !utils.SlicesEqual(firstDataBlock[0:2], []byte{0x00, 0x03}) { t.Error("Did not set the next available data block pointer to 3") } if !utils.SlicesEqual(firstDataBlock[2:4], []byte{0x00, 0x03}) { t.Error("Did not set the first record block pointer to 3") } blocksBitMap := dbio.NewBitMapFromBytes(blocksBitMapBlock) for i := 0; i < 4; i++ { val, err := blocksBitMap.Get(i) if err != nil { t.Fatal(err) } if !val { t.Errorf("Expected block %d to be flagged as used", i) } } }
func (dbm *dataBlocksMap) tupleForBlockID(dataBlockID uint16) (dbio.BitMap, int) { blockOffset := dataBlockID / dbio.DATABLOCK_SIZE flagOffset := dataBlockID % dbio.DATABLOCK_SIZE block, err := dbm.dataBuffer.FetchBlock(DATA_BLOCK_MAP_FIRST_BLOCK + blockOffset) if err != nil { panic(err) } return dbio.NewBitMapFromBytes(block.Data), int(flagOffset) }
func (dbm *dataBlocksMap) AllInUse() bool { for blockIndex := uint16(0); blockIndex < DATA_BLOCK_MAP_BLOCKS_COUNT; blockIndex++ { block, err := dbm.dataBuffer.FetchBlock(DATA_BLOCK_MAP_FIRST_BLOCK + blockIndex) if err != nil { panic(err) } bitMap := dbio.NewBitMapFromBytes(block.Data) for i := 0; i < dbio.DATABLOCK_SIZE; i++ { if isInUse, err := bitMap.Get(i); err != nil { panic(err) } else if !isInUse { return false } } } return true }
func (dbm *dataBlocksMap) FirstFree() uint16 { for blockIndex := uint16(0); blockIndex < DATA_BLOCK_MAP_BLOCKS_COUNT; blockIndex++ { block, err := dbm.dataBuffer.FetchBlock(DATA_BLOCK_MAP_FIRST_BLOCK + blockIndex) if err != nil { panic(err) } bitMap := dbio.NewBitMapFromBytes(block.Data) for i := 0; i < dbio.DATABLOCK_SIZE; i++ { isInUse, err := bitMap.Get(i) if err != nil { panic(err) } if !isInUse { return uint16(i) + blockIndex*dbio.DATABLOCK_SIZE } } } return 0 }