forked from dipseth/dataproc-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric-converter.ts
More file actions
223 lines (201 loc) · 5.48 KB
/
generic-converter.ts
File metadata and controls
223 lines (201 loc) · 5.48 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Generic Type-to-Qdrant Conversion System
* Core interfaces and TypeScript utility types for automatic field detection and mapping
*/
import {
QdrantPayloadBase,
QdrantQueryResultPayload,
QdrantClusterPayload,
QdrantJobPayload,
} from './qdrant-payload.js';
import { QdrantStorageMetadata } from './response-filter.js';
import { QueryResultData, ExtendedClusterData, DataprocJob } from './dataproc-responses.js';
/**
* Generic converter interface for type-safe conversions
*/
export interface GenericConverter<TSource, TTarget extends QdrantPayloadBase> {
convert(source: TSource, metadata: QdrantStorageMetadata): Promise<TTarget>;
validate(source: TSource): boolean;
getCompressionFields(): (keyof TSource)[];
}
/**
* Configuration for field-level compression with security considerations
*/
export interface CompressionFieldConfig<T> {
fields: (keyof T)[];
sizeThreshold?: number;
compressionType?: 'gzip' | 'deflate';
securityLevel?: 'none' | 'basic' | 'encrypted'; // Future security consideration
}
/**
* Field transformation functions for custom data processing
*/
export type FieldTransformations<T> = {
[K in keyof T]?: (value: T[K]) => any;
};
/**
* Metadata injection rules for automatic field generation
*/
export interface MetadataInjectionRules {
autoTimestamp?: boolean;
autoUUID?: boolean;
customFields?: Record<string, () => any>;
}
/**
* Comprehensive conversion configuration
*/
export interface ConversionConfig<TSource> {
fieldMappings?: Partial<Record<keyof TSource, string>>;
compressionRules?: CompressionFieldConfig<TSource>;
transformations?: FieldTransformations<TSource>;
metadata?: MetadataInjectionRules;
}
/**
* Compressible field wrapper for large data
*/
export type CompressibleField<T> = {
data: T;
isCompressed?: boolean;
compressionType?: 'gzip' | 'deflate';
originalSize?: number;
compressedSize?: number;
};
/**
* Advanced mapped type for automatic field detection
* Determines which fields should be compressed based on their types
*/
export type ExtractQdrantFields<T> = {
[K in keyof T]: T[K] extends string | number | boolean | null | undefined
? T[K]
: T[K] extends Array<any>
? CompressibleField<T[K]>
: T[K] extends object
? CompressibleField<T[K]>
: T[K];
};
/**
* Conditional type for automatic payload type selection
* Infers the correct Qdrant payload type based on source data type
*/
export type InferQdrantPayload<T> = T extends QueryResultData
? QdrantQueryResultPayload
: T extends ExtendedClusterData
? QdrantClusterPayload
: T extends DataprocJob
? QdrantJobPayload
: QdrantPayloadBase;
/**
* Utility type to extract keys that should be compressed
*/
export type CompressibleKeys<T> = {
[K in keyof T]: T[K] extends Array<any> ? K : T[K] extends object ? K : never;
}[keyof T];
/**
* Type-safe field mapping utility
*/
export type FieldMapping<TSource, TTarget> = {
[K in keyof TSource]?: keyof TTarget;
};
/**
* Result of field analysis for automatic detection
*/
export interface FieldAnalysisResult<T> {
compressibleFields: (keyof T)[];
primitiveFields: (keyof T)[];
objectFields: (keyof T)[];
arrayFields: (keyof T)[];
estimatedSizes: Record<keyof T, number>;
}
/**
* Conversion result with metadata
*/
export interface ConversionResult<T extends QdrantPayloadBase> {
payload: T;
metadata: {
fieldsProcessed: number;
fieldsCompressed: number;
totalOriginalSize: number;
totalCompressedSize: number;
compressionRatio: number;
processingTime: number;
};
}
/**
* Type guard utilities for runtime type checking
*/
export interface TypeGuards {
isQueryResultData(obj: unknown): obj is QueryResultData;
isExtendedClusterData(obj: unknown): obj is ExtendedClusterData;
isDataprocJob(obj: unknown): obj is DataprocJob;
}
/**
* Configuration registry for type-specific conversion rules
*/
export interface ConversionRegistry {
register<T>(
typeName: string,
config: ConversionConfig<T>,
typeGuard: (obj: unknown) => obj is T
): void;
getConfig<T>(typeName: string): ConversionConfig<T> | undefined;
detectType(obj: unknown): string | undefined;
}
/**
* Advanced utility type for deep field extraction
*/
export type DeepExtractFields<T, Depth extends number = 3> = Depth extends 0
? never
: T extends object
? {
[K in keyof T]: T[K] extends object ? DeepExtractFields<T[K], Prev<Depth>> : K;
}[keyof T]
: never;
/**
* Helper type for depth counting in recursive types
*/
type Prev<T extends number> = T extends 0
? never
: T extends 1
? 0
: T extends 2
? 1
: T extends 3
? 2
: T extends 4
? 3
: T extends 5
? 4
: never;
/**
* Type-safe conversion function signature
*/
export type ConvertFunction<TSource, TTarget extends QdrantPayloadBase> = (
source: TSource,
metadata: QdrantStorageMetadata,
config?: ConversionConfig<TSource>
) => Promise<ConversionResult<TTarget>>;
/**
* Validation result for source data
*/
export interface ValidationResult {
isValid: boolean;
errors: string[];
warnings: string[];
suggestions: string[];
}
/**
* Performance metrics for conversion operations
*/
export interface ConversionMetrics {
totalConversions: number;
averageProcessingTime: number;
averageCompressionRatio: number;
fieldCompressionStats: Record<
string,
{
timesCompressed: number;
averageCompressionRatio: number;
totalSizeSaved: number;
}
>;
}