func newBpTree(blocksize uint32, path string, keysize uint32, fields []uint32) (*BpTree, bool) {
	self := new(BpTree)
	self.lock = new(sync.Mutex)
	// 4 MB buffer with a block size of 4096 bytes
	if bf, ok := NewBlockFile(path, NewLFU(1000)); !ok {
		fmt.Println("could not create block file")
		return nil, false
	} else {
		self.bf = bf
	}
	self.blocksize = blocksize
	OPENFLAG = os.O_RDWR | os.O_CREATE
	if inter, ok := NewBlockDimensions(POINTERS|EQUAPTRS|NODUP, self.blocksize, keysize, 8, nil); !ok {
		fmt.Println("Block Dimensions invalid")
		return nil, false
	} else {
		self.internal = inter
	}

	if leaf, ok := NewBlockDimensions(RECORDS|EXTRAPTR, self.blocksize, keysize, 8, fields); !ok {
		fmt.Println("Block Dimensions invalid")
		return nil, false
	} else {
		self.external = leaf
	}

	if !self.bf.Open() {
		fmt.Println("Couldn't open file")
		return nil, false
	}
	if s, open := self.bf.Size(); open && s == 0 {
		// This is a new file the size is zero
		self.bf.Allocate(self.blocksize)
		b, ok := NewKeyBlock(self.bf, self.external)
		if !ok {
			self.bf.Close()
			fmt.Println("Could not create the root block")
			return nil, false
		}
		if !b.SerializeToFile() {
			self.bf.Close()
			fmt.Println("Could not serialize root block to file")
			return nil, false
		}
		self.info = treeinfo.New(self.bf, 1, b.Position())
	} else {
		self.info = treeinfo.Load(self.bf)
	}
	runtime.SetFinalizer(self, func(self *BpTree) { self.bf.Close() })
	return self, true
}
Exemple #2
0
func NewBTree(path string, keysize uint32, fields []uint32) (*BTree, bool) {
	self := new(BTree)
	// 4 MB buffer with a block size of 4096 bytes
	if bf, ok := NewBlockFile(path, NewLFU(1000)); !ok {
		fmt.Println("could not create block file")
		return nil, false
	} else {
		self.bf = bf
	}
	if dim, ok := NewBlockDimensions(RECORDS|POINTERS, treeinfo.BLOCKSIZE, keysize, 8, fields); !ok {
		fmt.Println("Block Dimensions invalid")
		return nil, false
	} else {
		self.node = dim
	}
	if !self.bf.Open() {
		fmt.Println("Couldn't open file")
		return nil, false
	}
	if s, open := self.bf.Size(); open && s == 0 {
		// This is a new file the size is zero
		self.bf.Allocate(self.node.BlockSize)
		b, ok := NewKeyBlock(self.bf, self.node)
		if !ok {
			self.bf.Close()
			fmt.Println("Could not create the root block")
			return nil, false
		}
		if !b.SerializeToFile() {
			self.bf.Close()
			fmt.Println("Could not serialize root block to file")
			return nil, false
		}
		self.info = treeinfo.New(self.bf, 1, b.Position())
	} else {
		self.info = treeinfo.Load(self.bf)
	}
	runtime.SetFinalizer(self,
		func(self *BTree) { self.bf.Close() })
	return self, true
}