diff --git a/Sources/DataSection/InMemoryDataSection.swift b/Sources/DataSection/InMemoryDataSection.swift index 5c1d842..e6efc8a 100644 --- a/Sources/DataSection/InMemoryDataSection.swift +++ b/Sources/DataSection/InMemoryDataSection.swift @@ -53,7 +53,7 @@ public class InMemoryDataSection: DataSection { } public func lookup(pointer: Int) -> [String: Payload]? { - guard let payload = decoder.read(at: pointer, resolvePointers: true) else { return nil } + guard let payload = decoder.read(at: pointer - Int(metadata.nodeCount) - 16, resolvePointers: true) else { return nil } guard case let Payload.map(result) = payload else { return nil } return result } diff --git a/Sources/IndexReader/InMemoryIndex.swift b/Sources/IndexReader/InMemoryIndex.swift index 9389c4f..05fdbb9 100644 --- a/Sources/IndexReader/InMemoryIndex.swift +++ b/Sources/IndexReader/InMemoryIndex.swift @@ -4,9 +4,9 @@ import struct MetadataReader.Metadata public class InMemoryIndex: Index where Pointer: UnsignedInteger, Pointer: FixedWidthInteger { private let metadata: Metadata - private let tree: [Pointer: Node] + private let tree: Data - init(metadata: Metadata, tree: [Pointer: Node]) { + init(metadata: Metadata, tree: Data) { self.metadata = metadata self.tree = tree } @@ -23,20 +23,14 @@ public class InMemoryIndex: Index where Pointer: UnsignedInteger, Point let buffer = UnsafeMutablePointer.allocate(capacity: searchTreeSize) let read = stream.read(buffer, maxLength: searchTreeSize) - var tree: [Pointer: Node] = [:] - let nodes = Data( + buffer.deallocate() + stream.close() +// precondition(tree.count == metadata.nodeCount) + self.init(metadata: metadata, tree: Data( bytesNoCopy: buffer, count: read, deallocator: .none - ) - .chunked(into: Int(metadata.nodeByteSize)) - .map({ Node($0) }) - for (pointer, node) in nodes.enumerated() { tree[Pointer(pointer)] = node } - - buffer.deallocate() - stream.close() - precondition(tree.count == metadata.nodeCount) - self.init(metadata: metadata, tree: tree) + )) } public func lookup(_ ip: IpAddress) -> Pointer? { @@ -50,9 +44,13 @@ public class InMemoryIndex: Index where Pointer: UnsignedInteger, Point } var pointer: Pointer = 0 + let nodeByteSize = Pointer.init(metadata.nodeByteSize) while pointer < metadata.nodeCount { guard let direction = stack.popLast() else { break } - guard let node = tree[pointer] else { return pointer } + let pointerInTree = pointer * nodeByteSize + let nodeCandidate = tree[pointerInTree..(nodeCandidate) +// guard let node = tree[pointer] else { return pointer } switch direction { case .left: pointer = node.left diff --git a/Tests/DBReaderTests/InMemoryReaderTest.swift b/Tests/DBReaderTests/InMemoryReaderTest.swift index 4e7a527..592f867 100644 --- a/Tests/DBReaderTests/InMemoryReaderTest.swift +++ b/Tests/DBReaderTests/InMemoryReaderTest.swift @@ -83,7 +83,8 @@ class InMemoryReaderTest: XCTestCase { let factory = ReaderFactory() let streamFactory: () -> InputStream = { InputStream( - fileAtPath: "/Users/rocskaadam/src/adam-rocska/src/GeoIP2-swift/Tests/ApiTests/Resources/GeoLite2-City_20200526/GeoLite2-City.mmdb" +// fileAtPath: "/Users/rocskaadam/src/adam-rocska/src/GeoIP2-swift/Tests/ApiTests/Resources/GeoLite2-City_20200526/GeoLite2-City.mmdb" + fileAtPath: "/Users/rocskaadam/src/adam-rocska/src/GeoIP2-swift/Tests/ApiTests/Resources/GeoLite2-ASN_20200526/GeoLite2-ASN.mmdb" // fileAtPath: "/Users/rocskaadam/src/adam-rocska/src/GeoIP2-swift/Tests/DBReaderTests/Resources/GeoLite2-Country_20200421/GeoLite2-Country.mmdb" )! }