Intentionally committing crap code.
Signed-off-by: Adam Rocska <adam.rocska@adams.solutions>
This commit is contained in:
parent
7094fa0a5d
commit
4fe0745b4c
|
@ -2,5 +2,5 @@ import Foundation
|
|||
import enum IndexReader.IpAddress
|
||||
import struct MetadataReader.Metadata
|
||||
|
||||
public typealias IpAddress = Index.IpAddress
|
||||
public typealias IpAddress = IndexReader.IpAddress
|
||||
public typealias DbMetadata = Metadata
|
|
@ -4,9 +4,6 @@ import enum IndexReader.IpAddress
|
|||
import protocol DataSection.DataSection
|
||||
import protocol IndexReader.Index
|
||||
import struct MetadataReader.Metadata
|
||||
import class MetadataReader.Reader
|
||||
import class IndexReader.InMemoryIndex
|
||||
import class DataSection.InMemoryDataSection
|
||||
|
||||
public class InMemoryReader<SearchIndex>: Reader where SearchIndex: IndexReader.Index {
|
||||
|
||||
|
@ -20,14 +17,6 @@ public class InMemoryReader<SearchIndex>: Reader where SearchIndex: IndexReader.
|
|||
self.dataSection = dataSection
|
||||
}
|
||||
|
||||
convenience init(_ inputStream: @escaping @autoclosure () -> InputStream) throws {
|
||||
let reader = MetadataReader.Reader(windowSize: 2048)
|
||||
guard let metadata = reader.read(inputStream()) else { throw ReaderError.cantExtractMetadata }
|
||||
let inMemoryIndex: SearchIndex = InMemoryIndex<UInt>(metadata: metadata, stream: inputStream())
|
||||
let inMemoryDataSection = InMemoryDataSection(metadata: metadata, stream: inputStream())
|
||||
self.init(index: inMemoryIndex, dataSection: inMemoryDataSection, metadata: metadata)
|
||||
}
|
||||
|
||||
public func get(_ ip: IpAddress) -> [String: Payload]? {
|
||||
guard let lookup = index.lookup(ip) else { return nil }
|
||||
guard let lookupResult = dataSection.lookup(pointer: Int(lookup)) else { return nil }
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
import Foundation
|
||||
import class DataSection.InMemoryDataSection
|
||||
import class IndexReader.InMemoryIndex
|
||||
import class MetadataReader.Reader
|
||||
|
||||
class ReaderFactory {
|
||||
func makeInMemoryReader(_ inputStream: @escaping () -> InputStream) throws -> Reader {
|
||||
let reader = MetadataReader.Reader(windowSize: 2048)
|
||||
guard let metadata = reader.read(inputStream()) else { throw ReaderError.cantExtractMetadata }
|
||||
let inMemoryIndex = InMemoryIndex<UInt>(metadata: metadata, stream: inputStream())
|
||||
let inMemoryDataSection = InMemoryDataSection(metadata: metadata, stream: inputStream())
|
||||
return InMemoryReader(index: inMemoryIndex, dataSection: inMemoryDataSection, metadata: metadata)
|
||||
}
|
||||
}
|
|
@ -79,6 +79,21 @@ class InMemoryReaderTest: XCTestCase {
|
|||
XCTAssertTrue(mockDataSectionCalled)
|
||||
}
|
||||
|
||||
func testOverall() throws {
|
||||
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/DBReaderTests/Resources/GeoLite2-Country_20200421/GeoLite2-Country.mmdb"
|
||||
)!
|
||||
}
|
||||
let reader = try factory.makeInMemoryReader(streamFactory)
|
||||
// measure {
|
||||
// reader.get(IpAddress.v4("80.99.18.166"))
|
||||
// }
|
||||
print(reader.get(IpAddress.v4("80.99.18.166")))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class MockSearchIndex: Index {
|
||||
|
|
Loading…
Reference in New Issue