forked from realm/realm-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectId.swift
141 lines (125 loc) · 5.4 KB
/
ObjectId.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
import Foundation
import Realm
/**
A 12-byte (probably) unique object identifier.
ObjectIds are similar to a GUID or a UUID, and can be used to uniquely identify objects without a centralized ID generator. An ObjectID consists of:
1. A 4 byte timestamp measuring the creation time of the ObjectId in seconds since the Unix epoch.
2. A 5 byte random value
3. A 3 byte counter, initialized to a random value.
ObjectIds are intended to be fast to generate. Sorting by an ObjectId field will typically result in the objects being sorted in creation order.
*/
@objc(RealmSwiftObjectId)
public final class ObjectId: RLMObjectId, Decodable, @unchecked Sendable {
// MARK: Initializers
/// Creates a new zero-initialized ObjectId.
public override required init() {
super.init()
}
// swiftlint:disable unneeded_override
/// Creates a new randomly-initialized ObjectId.
public override static func generate() -> ObjectId {
super.generate()
}
// swiftlint:enable unneeded_override
/// Creates a new ObjectId from the given 24-byte hexadecimal string.
///
/// Throws if the string is not 24 characters or contains any characters other than 0-9a-fA-F.
/// - Parameter string: The string to parse.
public override required init(string: String) throws {
try super.init(string: string)
}
/// Creates a new ObjectId using the given date, machine identifier, process identifier.
///
/// - Parameters:
/// - timestamp: A timestamp as NSDate.
/// - machineId: The machine identifier.
/// - processId: The process identifier.
public required init(timestamp: Date, machineId: Int, processId: Int) {
super.init(timestamp: timestamp,
machineIdentifier: Int32(machineId),
processIdentifier: Int32(processId))
}
/// Creates a new ObjectId from the given 24-byte hexadecimal static string.
///
/// Aborts if the string is not 24 characters or contains any characters other than 0-9a-fA-F. Use the initializer which takes a String to handle invalid strings at runtime.
public required init(_ str: StaticString) {
// swiftlint:disable:next optional_data_string_conversion
try! super.init(string: str.withUTF8Buffer { String(decoding: $0, as: UTF8.self) })
}
/// Creates a new ObjectId by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
try super.init(string: container.decode(String.self))
}
}
extension ObjectId: Encodable {
/// Encodes this ObjectId into the given encoder.
///
/// This function throws an error if the given encoder is unable to encode a string.
///
/// - Parameter encoder: The encoder to write data to.
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(stringValue)
}
}
extension ObjectId: Comparable {
/// Returns a Boolean value indicating whether the value of the first
/// argument is less than that of the second argument.
///
/// - Parameters:
/// - lhs: An ObjectId value to compare.
/// - rhs: Another ObjectId value to compare.
public static func < (lhs: ObjectId, rhs: ObjectId) -> Bool {
lhs.isLessThan(rhs)
}
/// Returns a Boolean value indicating whether the ObjectId of the first
/// argument is less than or equal to that of the second argument.
///
/// - Parameters:
/// - lhs: An ObjectId value to compare.
/// - rhs: Another ObjectId value to compare.
public static func <= (lhs: ObjectId, rhs: ObjectId) -> Bool {
lhs.isLessThanOrEqual(to: rhs)
}
/// Returns a Boolean value indicating whether the ObjectId of the first
/// argument is greater than or equal to that of the second argument.
///
/// - Parameters:
/// - lhs: An ObjectId value to compare.
/// - rhs: Another ObjectId value to compare.
public static func >= (lhs: ObjectId, rhs: ObjectId) -> Bool {
lhs.isGreaterThanOrEqual(to: rhs)
}
/// Returns a Boolean value indicating whether the ObjectId of the first
/// argument is greater than that of the second argument.
///
/// - Parameters:
/// - lhs: An ObjectId value to compare.
/// - rhs: Another ObjectId value to compare.
public static func > (lhs: ObjectId, rhs: ObjectId) -> Bool {
lhs.isGreaterThan(rhs)
}
}