Skip to content

Commit f0d05fd

Browse files
authored
adds public api docs (#148)
1 parent 8de1d14 commit f0d05fd

15 files changed

+202
-58
lines changed

Sources/Valkey/Cluster/HashSlot.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct HashSlot: Hashable, Sendable {
5252
/// The minimum valid hash slot value (0).
5353
public static let min = HashSlot(.hashSlotMin)
5454

55-
/// The total number of hash slots in a Valkey cluster (16,384).
55+
/// The total number of hash slots allowed in a Valkey cluster (16,384).
5656
public static let count: Int = 16384
5757

5858
private var _raw: UInt16
@@ -69,23 +69,34 @@ extension HashSlot: Comparable {
6969
}
7070

7171
extension HashSlot: RawRepresentable {
72+
/// The type that represents the raw value of a hash slot.
7273
public typealias RawValue = UInt16
7374

75+
/// Creates a hash slot based on the raw value you provide for valid hash slot values.
76+
/// - Parameter rawValue: The raw value of the hash slot.
7477
public init?(rawValue: UInt16) {
7578
guard rawValue <= HashSlot.max.rawValue else { return nil }
7679
self._raw = rawValue
7780
}
7881

82+
/// Creates a hash slot based on the raw value you provide for valid hash slot values, or returns nil if the raw value isn't within the valid range.
83+
/// - Parameter rawValue: The raw value of the hash slot.
84+
///
85+
/// The raw value must be within ``HashSlot/min`` (`0`) to ``HashSlot/max`` (`16383`) to initialize.
86+
/// If you pass in an Int value beyond that range, the initializer returns `nil`.
7987
public init?(rawValue: Int) {
8088
guard HashSlot.min.rawValue <= rawValue, rawValue <= HashSlot.max.rawValue else { return nil }
8189
self._raw = UInt16(rawValue)
8290
}
8391

92+
/// Creates a hash slot based on the raw value you provide for valid hash slot values.
93+
/// - Parameter rawValue: The raw value of the hash slot.
8494
public init?(rawValue: Int64) {
8595
guard HashSlot.min.rawValue <= rawValue, rawValue <= HashSlot.max.rawValue else { return nil }
8696
self._raw = UInt16(rawValue)
8797
}
8898

99+
/// The raw value of a hash slot.
89100
public var rawValue: UInt16 { self._raw }
90101
}
91102

@@ -130,8 +141,8 @@ extension HashSlot {
130141
/// The algorithm calculates the CRC16 of either the entire key or a specific part of the key
131142
/// enclosed in curly braces (`{...}`), then performs modulo 16384 to get the slot number.
132143
///
133-
/// - Parameter key: The key used in a Valkey command
134-
/// - Returns: A HashSlot representing where this key would be stored in the cluster
144+
/// - Parameter key: The key used in a Valkey command.
145+
/// - Returns: A HashSlot representing where this key would be stored in the cluster.
135146
@inlinable
136147
public init(key: some BidirectionalCollection<UInt8>) {
137148
// Banging is safe because the modulo ensures we are in range
@@ -140,8 +151,8 @@ extension HashSlot {
140151

141152
/// Creates a hash slot for a Valkey key.
142153
///
143-
/// - Parameter key: The Valkey key for which to calculate the hash slot
144-
/// - Returns: A HashSlot representing where this key would be stored in the cluster
154+
/// - Parameter key: The Valkey key for which to calculate the hash slot.
155+
/// - Returns: A HashSlot instance that represents where this key would be stored in the cluster.
145156
@inlinable
146157
public init(key: ValkeyKey) {
147158
switch key._storage {
@@ -157,12 +168,12 @@ extension HashSlot {
157168
/// Computes the portion of the key that should be used for hash slot calculation.
158169
///
159170
/// Follows the Valkey hash tag specification:
160-
/// - If the key contains a pattern like "{...}", only the content between the braces is used for hashing
161-
/// - If the pattern is empty "{}", or doesn't exist, the entire key is used
162-
/// - Only the first occurrence of "{...}" is considered
171+
/// - If the key contains a pattern like "{...}", only the content between the braces is used for hashing.
172+
/// - If the pattern is empty "{}", or doesn't exist, the entire key is used.
173+
/// - Only the first occurrence of "{...}" is considered.
163174
///
164-
/// - Parameter keyUTF8View: The UTF8 view of key for your operation
165-
/// - Returns: A substring UTF8 view that will be used in the CRC16 computation
175+
/// - Parameter keyUTF8View: The UTF-8 view of key for your operation.
176+
/// - Returns: A UTF-8 sequence to use in the CRC16 computation to compute a hash slot.
166177
@inlinable
167178
package static func hashTag<Bytes: BidirectionalCollection<UInt8>>(forKey keyUTF8View: Bytes) -> Bytes.SubSequence {
168179
var firstOpenCurly: Bytes.Index?
@@ -278,8 +289,8 @@ extension HashSlot {
278289
///
279290
/// This is the specific CRC16 implementation used by Valkey/Redis for hash slot calculation.
280291
///
281-
/// - Parameter bytes: A sequence of bytes to compute the CRC16 value for
282-
/// - Returns: The computed CRC16 value
292+
/// - Parameter bytes: A sequence of bytes used to compute the CRC16 value.
293+
/// - Returns: The computed CRC16 value.
283294
@inlinable
284295
package static func crc16<Bytes: Sequence>(_ bytes: Bytes) -> UInt16 where Bytes.Element == UInt8 {
285296
var crc: UInt16 = 0

Sources/Valkey/Cluster/ValkeyClusterClient.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import Synchronization
2020

2121
/// A client for interacting with a Valkey cluster.
2222
///
23-
/// ``ValkeyClusterClient`` provides a high-level interface for communicating with a Valkey cluster.
23+
/// `ValkeyClusterClient` provides a high-level interface for communicating with a Valkey cluster.
2424
/// It handles cluster topology discovery, command routing, and automatic connection management
2525
/// across multiple Valkey server nodes.
2626
///
2727
/// The client supports:
28-
/// - Automatic cluster topology discovery and maintenance
29-
/// - Command routing to the appropriate node based on key hash slots
30-
/// - Handling of MOVED responses for proper cluster resharding
31-
/// - Connection pooling and failover
32-
/// - Circuit breaking during cluster disruptions
28+
/// - Automatic cluster topology discovery and maintenance.
29+
/// - Command routing to the appropriate node based on key hash slots.
30+
/// - Handling of MOVED responses for proper cluster resharding.
31+
/// - Connection pooling and failover.
32+
/// - Circuit breaking during cluster disruptions.
3333
///
3434
/// Example usage:
3535
/// ```swift

Sources/Valkey/Commands/Custom/ClusterCustomCommands.swift

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ package struct ValkeyClusterParseError: Error, Equatable {
4141
}
4242
}
4343

44+
/// A description of a Valkey cluster.
45+
///
46+
/// A description is return when you call ``ValkeyConnectionProtocol/clusterShards()``.
4447
public struct ValkeyClusterDescription: Hashable, Sendable, RESPTokenDecodable {
45-
/// Details for a node within a cluster shard
48+
/// Details for a node within a cluster shard.
4649
public struct Node: Hashable, Sendable {
47-
/// Replication role of a given node within a shard (primary or replica)
50+
/// Replication role of a given node within a shard (primary or replica).
4851
public struct Role: Sendable, Hashable, RawRepresentable {
52+
/// The node is primary.
4953
public static let primary = Role(base: .primary)
54+
/// The node is a replica.
5055
public static let replica = Role(base: .replica)
5156

5257
public init?(rawValue: String) {
@@ -75,8 +80,11 @@ public struct ValkeyClusterDescription: Hashable, Sendable, RESPTokenDecodable {
7580

7681
/// Node's health status
7782
public struct Health: Sendable, Hashable, RawRepresentable {
83+
/// The node is online.
7884
public static let online = Health(base: .online)
85+
/// The node is in a failed state.
7986
public static let failed = Health(base: .failed)
87+
/// The node is loading.
8088
public static let loading = Health(base: .loading)
8189

8290
public init?(rawValue: String) {
@@ -103,16 +111,36 @@ public struct ValkeyClusterDescription: Hashable, Sendable, RESPTokenDecodable {
103111
}
104112
}
105113

114+
/// The ID of the node
106115
public var id: String
116+
/// The port
107117
public var port: Int?
118+
/// The TLS port
108119
public var tlsPort: Int?
120+
/// The IP address
109121
public var ip: String
122+
/// The hostname
110123
public var hostname: String?
124+
/// The endpoint
111125
public var endpoint: String
126+
/// The role of the node
112127
public var role: Role
128+
/// The replication offset for the node
113129
public var replicationOffset: Int
130+
/// The health of the node
114131
public var health: Health
115132

133+
/// Creates a new node
134+
/// - Parameters:
135+
/// - id: The node ID
136+
/// - port: The port
137+
/// - tlsPort: The TLS port
138+
/// - ip: The IP address
139+
/// - hostname: The hostname
140+
/// - endpoint: The endpoint
141+
/// - role: The node role
142+
/// - replicationOffset: The replication offset
143+
/// - health: The node health
116144
public init(
117145
id: String,
118146
port: Int?,
@@ -136,18 +164,28 @@ public struct ValkeyClusterDescription: Hashable, Sendable, RESPTokenDecodable {
136164
}
137165
}
138166

167+
/// A portion of a valkey cluster
139168
public struct Shard: Hashable, Sendable {
169+
/// The slots represented in the shard.
140170
public var slots: HashSlots
171+
/// The nodes that make up the shard.
141172
public var nodes: [Node]
142173

174+
/// Create a new shard.
175+
/// - Parameters:
176+
/// - slots: The slots in the shard.
177+
/// - nodes: The nodes in the shard.
143178
public init(slots: HashSlots, nodes: [Node]) {
144179
self.slots = slots
145180
self.nodes = nodes
146181
}
147182
}
148183

184+
/// The individual portions of a valkey cluster, known as shards.
149185
public var shards: [Shard]
150186

187+
/// Creates a cluster description from the response token you provide.
188+
/// - Parameter respToken: The response token.
151189
public init(fromRESP respToken: RESPToken) throws {
152190
do {
153191
self = try Self.makeClusterDescription(respToken: respToken)
@@ -156,6 +194,8 @@ public struct ValkeyClusterDescription: Hashable, Sendable, RESPTokenDecodable {
156194
}
157195
}
158196

197+
/// Creates a cluster description from a list of shards you provide.
198+
/// - Parameter shards: The shards that make up the cluster.
159199
public init(_ shards: [ValkeyClusterDescription.Shard]) {
160200
self.shards = shards
161201
}

Sources/Valkey/Connection/ValkeyConnectionConfiguration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ public struct ValkeyConnectionConfiguration: Sendable {
8787
}
8888

8989
/// Optional authentication credentials for accessing the Valkey server.
90+
///
9091
/// Set this property when connecting to a server that requires authentication.
9192
public var authentication: Authentication?
9293

9394
/// TLS configuration for the connection.
95+
///
9496
/// Use `.disable` for unencrypted connections or `.enable(...)` for secure connections.
9597
public var tls: TLS
9698

Sources/Valkey/Documentation.docc/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ try await valkeyClient.withConnection { connection in
5050

5151
- ``ValkeyClient``
5252
- ``ValkeyClientConfiguration``
53+
- ``ValkeyConnectionProtocol``
5354
- ``ValkeyServerAddress``
5455
- ``ValkeyConnection``
55-
- ``ValkeyConnectionProtocol``
56+
- ``ValkeyConnectionConfiguration``
5657

5758
### Commands
5859

Sources/Valkey/RESP/RESPError.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import NIOCore
2222
/// 2. You are contacting an untrusted backend
2323
///
2424
public struct RESPParsingError: Error {
25+
/// The error code associated with an error parsing a response.
2526
public struct Code: Hashable, Sendable, CustomStringConvertible {
2627
private enum Base {
2728
case invalidLeadingByte
@@ -41,14 +42,23 @@ public struct RESPParsingError: Error {
4142
self.base = base
4243
}
4344

45+
/// The leading byte is invalid.
4446
public static let invalidLeadingByte = Self.init(.invalidLeadingByte)
47+
/// The data is invalid.
4548
public static let invalidData = Self.init(.invalidData)
49+
/// The nested aggregate types are too deeply nested.
4650
public static let tooDeeplyNestedAggregatedTypes = Self.init(.tooDeeplyNestedAggregatedTypes)
51+
/// A colon is missing within a verbatim string
4752
public static let missingColonInVerbatimString = Self.init(.missingColonInVerbatimString)
53+
/// Can't parse the data as an integer.
4854
public static let canNotParseInteger = Self.init(.canNotParseInteger)
55+
/// Can't parse the data as a Double.
4956
public static let canNotParseDouble = Self.init(.canNotParseDouble)
57+
/// Can't parse the data as a big number.
5058
public static let canNotParseBigNumber = Self.init(.canNotParseBigNumber)
59+
/// The type is unexpected.
5160
public static let unexpectedType = Self.init(.unexpectedType)
61+
/// There is an invalid element count.
5262
public static let invalidElementCount = Self.init(.invalidElementCount)
5363

5464
public var description: String {
@@ -75,7 +85,10 @@ public struct RESPParsingError: Error {
7585
}
7686
}
7787

88+
/// The error code
7889
public var code: Code
90+
91+
/// The byte buffer that failed to parse.
7992
public var buffer: ByteBuffer
8093

8194
@usableFromInline

Sources/Valkey/RESP/RESPRenderable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import NIOCore
1616

17-
/// Type that can be rendered into a RESP buffer
17+
/// A type that can be rendered into a RESP buffer.
1818
public protocol RESPRenderable {
1919
var respEntries: Int { get }
2020

Sources/Valkey/RESP/RESPStringRenderable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import FoundationEssentials
2020
import Foundation
2121
#endif
2222

23-
/// Type that can be rendered as a single bulk string
23+
/// A type that can be rendered as a single bulk string.
2424
public protocol RESPStringRenderable: Sendable, Hashable {
2525
func encode(into commandEncoder: inout ValkeyCommandEncoder)
2626
}

Sources/Valkey/RESP/RESPToken.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414

1515
import NIOCore
1616

17+
/// The response from a Valkey server.
18+
///
19+
/// A response token is a type that provides a view into the underlying byte buffer returned from the Valkey server.
1720
public struct RESPToken: Hashable, Sendable {
21+
/// A list of response tokens.
1822
public struct Array: Sequence, Sendable, Hashable {
1923
public typealias Element = RESPToken
2024

25+
/// The count of items in the list.
2126
public let count: Int
2227
let buffer: ByteBuffer
2328

@@ -45,6 +50,7 @@ public struct RESPToken: Hashable, Sendable {
4550
}
4651
}
4752

53+
/// A dictionary of response tokens mapped by a response token.
4854
public struct Map: Sequence, Sendable, Hashable {
4955
public typealias Element = (key: RESPToken, value: RESPToken)
5056

@@ -80,21 +86,37 @@ public struct RESPToken: Hashable, Sendable {
8086
}
8187
}
8288

89+
/// A response token value.
8390
public enum Value: Hashable, Sendable {
91+
/// A simple string
8492
case simpleString(ByteBuffer)
93+
/// A simple error
8594
case simpleError(ByteBuffer)
95+
/// A bulk string
8696
case bulkString(ByteBuffer)
97+
/// A bulk error
8798
case bulkError(ByteBuffer)
99+
/// A verbatim string
88100
case verbatimString(ByteBuffer)
101+
/// An integer number
89102
case number(Int64)
103+
/// A floating point number
90104
case double(Double)
105+
/// A Boolean value
91106
case boolean(Bool)
107+
/// Null
92108
case null
109+
/// A big number
93110
case bigNumber(ByteBuffer)
111+
/// An array
94112
case array(Array)
113+
/// An attribute
95114
case attribute(Map)
115+
/// A map
96116
case map(Map)
117+
/// A set
97118
case set(Array)
119+
/// A pushed value
98120
case push(Array)
99121
}
100122

0 commit comments

Comments
 (0)