@@ -8,8 +8,10 @@ import {
8
8
ServerConfig
9
9
} from './schemaregistry-client' ;
10
10
import stringify from "json-stringify-deterministic" ;
11
+ import { v4 } from "uuid" ;
11
12
import { ClientConfig } from "./rest-service" ;
12
13
import { RestError } from "./rest-error" ;
14
+ import { SchemaId } from "./serde/serde" ;
13
15
14
16
interface VersionCacheEntry {
15
17
version : number ;
@@ -45,6 +47,7 @@ class MockClient implements Client {
45
47
private clientConfig ?: ClientConfig ;
46
48
private infoToSchemaCache : Map < string , MetadataCacheEntry > ;
47
49
private idToSchemaCache : Map < string , InfoCacheEntry > ;
50
+ private guidToSchemaCache : Map < string , InfoCacheEntry > ;
48
51
private schemaToVersionCache : Map < string , VersionCacheEntry > ;
49
52
private configCache : Map < string , ServerConfig > ;
50
53
private counter : Counter ;
@@ -53,6 +56,7 @@ class MockClient implements Client {
53
56
this . clientConfig = config
54
57
this . infoToSchemaCache = new Map ( ) ;
55
58
this . idToSchemaCache = new Map ( ) ;
59
+ this . guidToSchemaCache = new Map ( ) ;
56
60
this . schemaToVersionCache = new Map ( ) ;
57
61
this . configCache = new Map ( ) ;
58
62
this . counter = new Counter ( ) ;
@@ -67,7 +71,7 @@ class MockClient implements Client {
67
71
if ( ! metadata ) {
68
72
throw new RestError ( "Failed to register schema" , 422 , 42200 ) ;
69
73
}
70
- return metadata . id ;
74
+ return metadata . id ! ;
71
75
}
72
76
73
77
async registerFullResponse ( subject : string , schema : SchemaInfo , normalize : boolean = false ) : Promise < SchemaMetadata > {
@@ -78,20 +82,19 @@ class MockClient implements Client {
78
82
return cacheEntry . metadata ;
79
83
}
80
84
81
- const id = await this . getIDFromRegistry ( subject , schema ) ;
82
- if ( id === - 1 ) {
85
+ const schemaId = await this . getIDFromRegistry ( subject , schema ) ;
86
+ if ( schemaId . id === - 1 ) {
83
87
throw new RestError ( "Failed to retrieve schema ID from registry" , 422 , 42200 ) ;
84
88
}
85
89
86
- const metadata : SchemaMetadata = { ... schema , id } ;
90
+ const metadata : SchemaMetadata = { id : schemaId . id ! , guid : schemaId . guid ! , ... schema } ;
87
91
this . infoToSchemaCache . set ( cacheKey , { metadata, softDeleted : false } ) ;
88
92
89
93
return metadata ;
90
94
}
91
95
92
- private async getIDFromRegistry ( subject : string , schema : SchemaInfo ) : Promise < number > {
96
+ private async getIDFromRegistry ( subject : string , schema : SchemaInfo ) : Promise < SchemaId > {
93
97
let id = - 1 ;
94
-
95
98
for ( const [ key , value ] of this . idToSchemaCache . entries ( ) ) {
96
99
const parsedKey = JSON . parse ( key ) ;
97
100
if ( parsedKey . subject === subject && this . schemasEqual ( value . info , schema ) ) {
@@ -100,14 +103,24 @@ class MockClient implements Client {
100
103
}
101
104
}
102
105
106
+ let guid = "" ;
107
+ for ( const [ key , value ] of this . guidToSchemaCache . entries ( ) ) {
108
+ if ( this . schemasEqual ( value . info , schema ) ) {
109
+ guid = key ;
110
+ break ;
111
+ }
112
+ }
113
+
103
114
await this . generateVersion ( subject , schema ) ;
104
115
if ( id < 0 ) {
105
116
id = this . counter . increment ( ) ;
106
117
const idCacheKey = stringify ( { subject, id } ) ;
107
118
this . idToSchemaCache . set ( idCacheKey , { info : schema , softDeleted : false } ) ;
119
+ guid = v4 ( )
120
+ this . guidToSchemaCache . set ( guid , { info : schema , softDeleted : false } ) ;
108
121
}
109
122
110
- return id ;
123
+ return new SchemaId ( "" , id , guid ) ;
111
124
}
112
125
113
126
private async generateVersion ( subject : string , schema : SchemaInfo ) : Promise < void > {
@@ -134,13 +147,27 @@ class MockClient implements Client {
134
147
return cacheEntry . info ;
135
148
}
136
149
150
+ async getByGuid ( guid : string , format ?: string ) : Promise < SchemaInfo > {
151
+ const cacheEntry = this . guidToSchemaCache . get ( guid ) ;
152
+
153
+ if ( ! cacheEntry || cacheEntry . softDeleted ) {
154
+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
155
+ }
156
+ return cacheEntry . info ;
157
+ }
158
+
137
159
async getId ( subject : string , schema : SchemaInfo ) : Promise < number > {
160
+ const metadata = await this . getIdFullResponse ( subject , schema ) ;
161
+ return metadata . id ! ;
162
+ }
163
+
164
+ async getIdFullResponse ( subject : string , schema : SchemaInfo ) : Promise < SchemaMetadata > {
138
165
const cacheKey = stringify ( { subject, schema : minimize ( schema ) } ) ;
139
166
const cacheEntry = this . infoToSchemaCache . get ( cacheKey ) ;
140
167
if ( ! cacheEntry || cacheEntry . softDeleted ) {
141
168
throw new RestError ( "Schema not found" , 404 , 40400 ) ;
142
169
}
143
- return cacheEntry . metadata . id ;
170
+ return cacheEntry . metadata ;
144
171
}
145
172
146
173
async getLatestSchemaMetadata ( subject : string , format ?: string ) : Promise < SchemaMetadata > {
@@ -158,6 +185,7 @@ class MockClient implements Client {
158
185
const parsedKey = JSON . parse ( key ) ;
159
186
if ( parsedKey . subject === subject && value . version === version ) {
160
187
json = parsedKey ;
188
+ break
161
189
}
162
190
}
163
191
@@ -170,14 +198,26 @@ class MockClient implements Client {
170
198
const parsedKey = JSON . parse ( key ) ;
171
199
if ( parsedKey . subject === subject && value . info . schema === json . schema . schema ) {
172
200
id = parsedKey . id ;
201
+ break
173
202
}
174
203
}
175
204
if ( id === - 1 ) {
176
205
throw new RestError ( "Schema not found" , 404 , 40400 ) ;
177
206
}
207
+ let guid : string = "" ;
208
+ for ( const [ key , value ] of this . guidToSchemaCache . entries ( ) ) {
209
+ if ( value . info . schema === json . schema . schema ) {
210
+ guid = key
211
+ break
212
+ }
213
+ }
214
+ if ( guid === "" ) {
215
+ throw new RestError ( "Schema not found" , 404 , 40400 ) ;
216
+ }
178
217
179
218
return {
180
219
id,
220
+ guid,
181
221
version,
182
222
subject,
183
223
...json . schema ,
@@ -226,6 +266,7 @@ class MockClient implements Client {
226
266
const parsedKey = JSON . parse ( key ) ;
227
267
if ( parsedKey . subject === subject && value . info . schema === latest . schema ) {
228
268
id = parsedKey . id ;
269
+ break
229
270
}
230
271
}
231
272
if ( id === - 1 ) {
0 commit comments