func newBpTree(blocksize uint32, filename string, keysize uint32, fields []uint32) (*BpTree, bool) {
	self := new(BpTree)
	// 4 MB buffer with a block size of 4096 bytes
	if bf, ok := NewBlockFile(filename, NewLFU(1000)); !ok {
		fmt.Println("could not create block file")
		return nil, false
	} else {
		self.bf = bf
	}
	self.blocksize = blocksize
	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
}
func testingNewBTree(blocksize uint32) (*BTree, bool) {
	filename := "test.btree"
	keysize := uint32(4)
	fields := ([]uint32{1, 1, 2})
	self := new(BTree)
	// 4 MB buffer with a block size of 4096 bytes
	if bf, ok := file.NewBlockFile(filename, NewLFU(1000)); !ok {
		fmt.Println("could not create block file")
		return nil, false
	} else {
		self.bf = bf
	}
	file.OPENFLAG = os.O_RDWR | os.O_CREAT
	if dim, ok := NewBlockDimensions(RECORDS|POINTERS, blocksize, keysize, 8, fields); !ok {
		fmt.Println("Block Dimensions invalid")
		return nil, false
	} else {
		self.node = dim
	}
	//     fmt.Println("keys per block", self.node.KeysPerBlock())
	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())
	}
	clean := func(self *BTree) { self.bf.Close() }
	runtime.SetFinalizer(self, clean)
	return self, true
}
Example #3
0
func NewBTree(filename string, keysize uint32, fields []uint32) (*BTree, bool) {
	self := new(BTree)
	// 4 MB buffer with a block size of 4096 bytes
	if bf, ok := NewBlockFile(filename, 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
}