Skip to content

Commit

Permalink
Remove isLeaf and isRoot from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
emma-k-alexandra committed Jul 25, 2019
1 parent da54025 commit 75a9e10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
26 changes: 10 additions & 16 deletions Sources/BTree/BTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct BTree<Key: Comparable & Codable, Value: Codable> {
self.storage = try Storage(path: storagePath)

if self.storage.isEmpty() {
self.root = BTreeNode(minimumDegree: minimumDegree, isLeaf: true, isRoot: true, isLoaded: true, storage: self.storage)
self.root = BTreeNode(minimumDegree: minimumDegree, isRoot: true, isLoaded: true, storage: self.storage)

try self.root.save()

Expand Down Expand Up @@ -65,7 +65,7 @@ public struct BTree<Key: Comparable & Codable, Value: Codable> {
if self.root.isFull {
var root = self.root

self.root = BTreeNode(minimumDegree: root.minimumDegree, isLeaf: false, isRoot: true, isLoaded: true, storage: self.storage)
self.root = BTreeNode(minimumDegree: root.minimumDegree, isRoot: true, isLoaded: true, storage: self.storage)
root.isRoot = false

self.root.children.append(root)
Expand Down Expand Up @@ -97,7 +97,7 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {
public var minimumDegree: Int

/// If this node is a leaf
public var isLeaf: Bool
public var isLeaf: Bool { self.children.count == 0 }

/// If this node is full of elements.
public var isFull: Bool { self.elements.count == (2 * self.minimumDegree - 1) }
Expand All @@ -120,21 +120,21 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {
case children
case minimumDegree
case isLeaf
case isRoot
}

// MARK: Creation

/// Initializer used to load this node from disk.
public init(from decoder: Decoder) throws {
self.isRoot = false
self.isLoaded = true

let values = try decoder.container(keyedBy: CodingKeys.self)

self.elements = try values.decode(Array<BTreeElement>.self, forKey: .elements)

self.minimumDegree = try values.decode(Int.self, forKey: .minimumDegree)
self.isLeaf = try values.decode(Bool.self, forKey: .isLeaf)
self.isRoot = try values.decode(Bool.self, forKey: .isRoot)


let decodedChildren = try values.decode([String].self, forKey: .children)

self.children = try decodedChildren.map({ (childOffsetString) -> BTreeNode in
Expand All @@ -143,15 +143,13 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {

}

var child = BTreeNode(minimumDegree: self.minimumDegree, isLeaf: self.isLeaf, isRoot: false)
var child = BTreeNode(minimumDegree: self.minimumDegree, isRoot: false)
child.offset = childOffset

return child

})

self.isLoaded = true

}

/// Set up a new node
Expand All @@ -160,9 +158,8 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {
/// - parameter isLeaf: If this node is a leaf
/// - parameter isLoaded: If this node is loaded from storage.
/// - parameter storage: The storage engine used by this node.
public init(minimumDegree: Int, isLeaf: Bool, isRoot: Bool, isLoaded: Bool = false, storage: Storage<Key, Value>? = nil) {
public init(minimumDegree: Int, isRoot: Bool, isLoaded: Bool = false, storage: Storage<Key, Value>? = nil) {
self.minimumDegree = minimumDegree
self.isLeaf = isLeaf
self.isLoaded = isLoaded
self.isRoot = isRoot
self.storage = storage
Expand Down Expand Up @@ -256,7 +253,7 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {
/// - parameter childIndex: The index of the child to split
/// - throws: If unable to load any nodes used in this split, or if storage engine is unable to write to disk.
public mutating func split(at childIndex: Int) throws {
var newChild = BTreeNode(minimumDegree: self.minimumDegree, isLeaf: self.children[childIndex].isLeaf, isRoot: false, isLoaded: true, storage: self.storage!)
var newChild = BTreeNode(minimumDegree: self.minimumDegree, isRoot: false, isLoaded: true, storage: self.storage!)

let elementsToTransferRange = self.minimumDegree...
newChild.elements = Array(self.children[childIndex].elements[elementsToTransferRange])
Expand Down Expand Up @@ -292,8 +289,6 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {
try container.encode(self.elements, forKey: .elements)
try container.encode(self.children.map { $0.offset!.toPaddedString() }, forKey: .children)
try container.encode(self.minimumDegree, forKey: .minimumDegree)
try container.encode(self.isLeaf, forKey: .isLeaf)
try container.encode(self.isRoot, forKey: .isRoot)

}

Expand Down Expand Up @@ -330,7 +325,6 @@ public struct BTreeNode<Key: Comparable & Codable, Value: Codable>: Codable {

self.elements = node.elements
self.children = node.children
self.isLeaf = node.isLeaf
self.minimumDegree = node.minimumDegree
self.isLoaded = true

Expand Down
20 changes: 11 additions & 9 deletions Tests/BTreeTests/BTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class BTreeTests: XCTestCase {
storagePath.appendPathComponent("testBTreeNodeInit.db")

let storage = try! Storage<TestKey, TestValue>(path: storagePath)
let node = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, storage: storage)
let node = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, storage: storage)

XCTAssertEqual(node.minimumDegree, 2)
XCTAssertEqual(node.isLeaf, true)
Expand All @@ -55,7 +55,7 @@ final class BTreeTests: XCTestCase {
storagePath.appendPathComponent("testBTreeNodeInit.db")

let storage = try! Storage<TestKey, TestValue>(path: storagePath)
var node = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, isLoaded: true, storage: storage)
var node = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, isLoaded: true, storage: storage)

let offset = try! storage.saveRoot(node)
node.offset = offset
Expand Down Expand Up @@ -172,7 +172,9 @@ final class BTreeTests: XCTestCase {

XCTAssertEqual(try! tree.find(element10.key)?.value , element10.value.value)

try? FileManager.default.removeItem(at: tempDirectory)
print(tempDirectory)

// try? FileManager.default.removeItem(at: tempDirectory)

}

Expand All @@ -195,7 +197,7 @@ final class BTreeTests: XCTestCase {

let storage = try! Storage<TestKey, TestValue>(path: tempDirectory)

let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, isLoaded: true)
let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, isLoaded: true)

XCTAssertNoThrow(try storage.saveRoot(rootNode))

Expand All @@ -209,7 +211,7 @@ final class BTreeTests: XCTestCase {

let storage = try! Storage<TestKey, TestValue>(path: tempDirectory)

let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, isLoaded: true)
let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, isLoaded: true)

XCTAssertNoThrow(try storage.saveRoot(rootNode))

Expand All @@ -225,7 +227,7 @@ final class BTreeTests: XCTestCase {

let storage = try! Storage<TestKey, TestValue>(path: tempDirectory)

var rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, isLoaded: true)
var rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, isLoaded: true)

let offset = try! storage.saveRoot(rootNode)
rootNode.offset = offset
Expand All @@ -244,11 +246,11 @@ final class BTreeTests: XCTestCase {

let storage = try! Storage<TestKey, TestValue>(path: storagePath)

let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, isLoaded: true)
let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, isLoaded: true)

let _ = try! storage.saveRoot(rootNode)

let nodeToAppend = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: false, isLoaded: true)
let nodeToAppend = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: false, isLoaded: true)

XCTAssertNoThrow(try storage.append(nodeToAppend))

Expand All @@ -262,7 +264,7 @@ final class BTreeTests: XCTestCase {

let storage = try! Storage<TestKey, TestValue>(path: storagePath)

let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isLeaf: true, isRoot: true, isLoaded: true)
let rootNode = BTreeNode<TestKey, TestValue>(minimumDegree: 2, isRoot: true, isLoaded: true)

let offset = try! storage.saveRoot(rootNode)

Expand Down

0 comments on commit 75a9e10

Please sign in to comment.