// adds a block to persistent store as the last block of inodeid, // setting its blockid to the generated ID, and returning the generated ID and error func (nd *NameData) AddBlock(b maggiefs.Block, inodeid uint64) (uint64, error) { newBlockId, err := nd.GetIncrCounter(COUNTER_BLOCK, 1) b.Id = newBlockId _, err = nd.Mutate(inodeid, func(i *maggiefs.Inode) error { i.Blocks = append(i.Blocks, b) return nil }) if err != nil { return 0, err } return b.Id, nil }
func (ns *NameServer) AddBlock(nodeid uint64, length uint32) (newBlock maggiefs.Block, err error) { i, err := ns.nd.GetInode(nodeid) if err != nil { return maggiefs.Block{}, nil } // check which hosts we want vols, err := ns.rm.volumesForNewBlock(nil) if err != nil { return maggiefs.Block{}, err } volIds := make([]uint32, len(vols)) for idx, v := range vols { volIds[idx] = v.VolId } var startPos uint64 = 0 if len(i.Blocks) > 0 { lastBlock := i.Blocks[len(i.Blocks)-1] startPos = lastBlock.EndPos + 1 } else { startPos = 0 } endPos := startPos + uint64(length) - 1 // allocate block and id b := maggiefs.Block{ Id: 0, Inodeid: i.Inodeid, Version: 0, StartPos: startPos, EndPos: endPos, Volumes: volIds, } newId, err := ns.nd.AddBlock(b, i.Inodeid) b.Id = newId //fmt.Printf("nameserver addblock returning new block %+v\n",b) if err != nil { return maggiefs.Block{}, err } // replicate block to datanodes err = ns.rm.AddBlock(b) return b, err }