Skip to content

Commit 9c9bb11

Browse files
committed
remove table name cache
1 parent c9b7afa commit 9c9bb11

9 files changed

+19
-122
lines changed

Package.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ let package = Package(
88
],
99
dependencies: [
1010
// 🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.
11-
.package(url: "https://github.com/vapor/core.git", .branch("nio")),
11+
.package(url: "https://github.com/vapor/core.git", .branch("master")),
1212

1313
// 🔑 Hashing (BCrypt, SHA, HMAC, etc), encryption, and randomness.
14-
.package(url: "https://github.com/vapor/crypto.git", .branch("nio")),
14+
.package(url: "https://github.com/vapor/crypto.git", .branch("master")),
1515

1616
// 🗄 Core services for creating database integrations.
17-
.package(url: "https://github.com/vapor/database-kit.git", .branch("nio")),
17+
.package(url: "https://github.com/vapor/database-kit.git", .branch("master")),
1818

1919
// 📦 Dependency injection / inversion of control framework.
20-
.package(url: "https://github.com/vapor/service.git", .branch("nio")),
20+
.package(url: "https://github.com/vapor/service.git", .branch("master")),
2121

2222
// Event-driven network application framework for high performance protocol servers & clients, non-blocking.
2323
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),

Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift

+1-7
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,7 @@ extension PostgreSQLConnection {
5858
guard let row = currentRow else {
5959
throw PostgreSQLError(identifier: "query", reason: "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription.", source: .capture())
6060
}
61-
print("before parse")
62-
let parsed = try row.parse(
63-
data: data,
64-
formatCodes: resultFormat.formatCodes,
65-
tableNameCache: self.tableNameCache
66-
)
67-
print("after parse")
61+
let parsed = try row.parse(data: data, formatCodes: resultFormat.formatCodes)
6862
try onRow(parsed)
6963
case .close: break
7064
default: throw PostgreSQLError(identifier: "query", reason: "Unexpected message during PostgreSQLParseRequest: \(message)", source: .capture())

Sources/PostgreSQL/Connection/PostgreSQLConnection+SimpleQuery.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ extension PostgreSQLConnection {
2525
guard let row = currentRow else {
2626
throw PostgreSQLError(identifier: "simpleQuery", reason: "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription.", source: .capture())
2727
}
28-
let parsed = try row.parse(
29-
data: data,
30-
formatCodes: row.fields.map { $0.formatCode },
31-
tableNameCache: self.tableNameCache
32-
)
28+
let parsed = try row.parse(data: data, formatCodes: row.fields.map { $0.formatCode })
3329
onRow(parsed)
3430
case .close: break // query over, waiting for `readyForQuery`
3531
default: throw PostgreSQLError(identifier: "simpleQuery", reason: "Unexpected message during PostgreSQLQuery: \(message)", source: .capture())

Sources/PostgreSQL/Connection/PostgreSQLConnection.swift

-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public final class PostgreSQLConnection {
1717
/// If non-nil, will log queries.
1818
public var logger: PostgreSQLLogger?
1919

20-
/// Caches oid -> table name data.
21-
internal var tableNameCache: PostgreSQLTableNameCache?
22-
2320
/// Returns a new unique portal name.
2421
internal var nextPortalName: String {
2522
defer { uniqueNameCounter = uniqueNameCounter &+ 1 }
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
/// Represents a PostgreSQL column.
2-
public struct PostgreSQLColumn: Hashable {
3-
/// See `Hashable.hashValue`
4-
public var hashValue: Int {
5-
return ((table ?? "_") + "." + name).hashValue
6-
}
7-
2+
public struct PostgreSQLColumn: Hashable, Equatable {
83
/// The table this column belongs to.
9-
public var table: String?
4+
public var tableOID: Int32
105

116
/// The column's name.
127
public var name: String
138
}
149

1510
extension PostgreSQLColumn: CustomStringConvertible {
1611
public var description: String {
17-
if let table = table {
18-
return "\(table)(\(name))"
19-
} else {
20-
return "\(name)"
21-
}
12+
return "<\(tableOID)>.(\(name))"
2213
}
2314
}
2415

@@ -35,50 +26,7 @@ extension Dictionary where Key == PostgreSQLColumn {
3526

3627
/// Access a `Value` from this dictionary keyed by `PostgreSQLColumn`s
3728
/// using a field (column) name and entity (table) name.
38-
public func value(forTable table: String, atColumn column: String) -> Value? {
39-
return self[PostgreSQLColumn(table: table, name: column)]
40-
}
41-
}
42-
43-
import Async
44-
45-
final class PostgreSQLTableNameCache {
46-
var connection: Future<PostgreSQLConnection>
47-
var cache: [Int32: String?]
48-
var working: Bool
49-
50-
init(connection: Future<PostgreSQLConnection>) {
51-
self.connection = connection
52-
self.cache = [:]
53-
self.working = false
54-
}
55-
56-
func tableName(oid: Int32) throws -> String? {
57-
if oid == 0 {
58-
return nil
59-
}
60-
61-
if working {
62-
cache[oid] = "pg_class"
63-
return "pg_class"
64-
}
65-
working = true
66-
defer { working = false }
67-
if let existing = cache[oid] {
68-
return existing
69-
} else {
70-
let res = try connection
71-
.wait()
72-
.simpleQuery("select relname from pg_class where oid = \(oid)")
73-
.wait()
74-
let new: String?
75-
if res.count > 0 {
76-
new = try res[0].firstValue(forColumn: "relname")!.decode(String.self)
77-
} else {
78-
new = nil
79-
}
80-
cache[oid] = new
81-
return new
82-
}
29+
public func value(forTableOID tableOID: Int32, atColumn column: String) -> Value? {
30+
return self[PostgreSQLColumn(tableOID: tableOID, name: column)]
8331
}
8432
}

Sources/PostgreSQL/Database/PostgreSQLDatabase.swift

-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ public final class PostgreSQLDatabase: Database {
88
/// If non-nil, will log queries.
99
public var logger: PostgreSQLLogger?
1010

11-
/// Caches oid -> table name data.
12-
internal var tableNameCache: PostgreSQLTableNameCache?
13-
1411
/// Creates a new `PostgreSQLDatabase`.
1512
public init(config: PostgreSQLDatabaseConfig, on worker: Worker) {
1613
self.config = config
17-
self.tableNameCache = PostgreSQLTableNameCache(connection: makeConnection(on: worker))
1814
}
1915

2016
/// See `Database.makeConnection()`
@@ -25,7 +21,6 @@ public final class PostgreSQLDatabase: Database {
2521
print("[PostgreSQL] \(error)")
2622
}.flatMap(to: PostgreSQLConnection.self) { client in
2723
client.logger = self.logger
28-
client.tableNameCache = self.tableNameCache
2924
return client.authenticate(
3025
username: config.username,
3126
database: config.database,

Sources/PostgreSQL/Message/PostgreSQLRowDescription.swift

+2-9
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,15 @@ struct PostgreSQLRowDescription: Decodable {
2222
extension PostgreSQLRowDescription {
2323
/// Parses a `PostgreSQLDataRow` using the metadata from this row description.
2424
/// Important to pass formatCodes in since the format codes in the field are likely not correct (if from a describe request)
25-
public func parse(
26-
data: PostgreSQLDataRow,
27-
formatCodes: [PostgreSQLFormatCode],
28-
tableNameCache: PostgreSQLTableNameCache?
29-
) throws -> [PostgreSQLColumn: PostgreSQLData] {
25+
public func parse(data: PostgreSQLDataRow, formatCodes: [PostgreSQLFormatCode]) throws -> [PostgreSQLColumn: PostgreSQLData] {
3026
return try .init(uniqueKeysWithValues: fields.enumerated().map { (i, field) in
3127
let formatCode: PostgreSQLFormatCode
3228
switch formatCodes.count {
3329
case 0: formatCode = .text
3430
case 1: formatCode = formatCodes[0]
3531
default: formatCode = formatCodes[i]
3632
}
37-
let key = try PostgreSQLColumn(
38-
table: tableNameCache?.tableName(oid: field.tableObjectID),
39-
name: field.name
40-
)
33+
let key = PostgreSQLColumn(tableOID: field.tableObjectID, name: field.name)
4134
let value = try data.columns[i].parse(dataType: field.dataType, format: formatCode)
4235
return (key, value)
4336
})

Sources/PostgreSQL/PostgreSQLProvider.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Async
12
import Service
23

34
/// Provides base `PostgreSQL` services such as database and connection.
@@ -19,7 +20,9 @@ public final class PostgreSQLProvider: Provider {
1920
}
2021

2122
/// See `Provider.boot`
22-
public func boot(_ worker: Container) throws { }
23+
public func didBoot(_ worker: Container) throws -> Future<Void> {
24+
return .done(on: worker)
25+
}
2326
}
2427

2528
/// MARK: Services
@@ -33,6 +36,6 @@ extension PostgreSQLDatabaseConfig: ServiceType {
3336
extension PostgreSQLDatabase: ServiceType {
3437
/// See `ServiceType.makeService(for:)`
3538
public static func makeService(for worker: Container) throws -> PostgreSQLDatabase {
36-
return try .init(config: worker.make(for: PostgreSQLDatabase.self), on: worker)
39+
return try .init(config: worker.make(), on: worker)
3740
}
3841
}

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+1-30
Original file line numberDiff line numberDiff line change
@@ -342,34 +342,6 @@ class PostgreSQLConnectionTests: XCTestCase {
342342
_ = try categories.wait()
343343
}
344344

345-
func testTableNameCache() throws {
346-
let hostname: String
347-
#if Xcode
348-
hostname = try Process.execute("docker-machine", "ip")
349-
#else
350-
hostname = "localhost"
351-
#endif
352-
let config = PostgreSQLDatabaseConfig.init(
353-
hostname: hostname,
354-
port: 5432,
355-
username: "vapor_username",
356-
database: "vapor_database",
357-
password: nil
358-
)
359-
let main = MultiThreadedEventLoopGroup(numThreads: 1)
360-
defer { try! main.syncShutdownGracefully() }
361-
let sub = MultiThreadedEventLoopGroup(numThreads: 1)
362-
defer { try! sub.syncShutdownGracefully() }
363-
364-
let database = PostgreSQLDatabase(config: config, on: main)
365-
let client = try database.makeConnection(on: sub).wait()
366-
let query = """
367-
select * from "pg_type" where "typlen" = $1 or "typlen" = $2
368-
"""
369-
let rows = try client.query(query, [1, 2]).wait()
370-
XCTAssertEqual(rows[0].keys.first?.table, "pg_type")
371-
}
372-
373345
static var allTests = [
374346
("testVersion", testVersion),
375347
("testSelectTypes", testSelectTypes),
@@ -379,7 +351,6 @@ class PostgreSQLConnectionTests: XCTestCase {
379351
("testStruct", testStruct),
380352
("testNull", testNull),
381353
("testGH24", testGH24),
382-
("testTableNameCache", testTableNameCache),
383354
]
384355
}
385356

@@ -388,7 +359,7 @@ extension PostgreSQLConnection {
388359
static func makeTest() throws -> PostgreSQLConnection {
389360
let hostname: String
390361
#if Xcode
391-
hostname = try Process.execute("docker-machine", "ip")
362+
hostname = (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100"
392363
#else
393364
hostname = "localhost"
394365
#endif

0 commit comments

Comments
 (0)