opusdev/vector-similarity-api
1
1import { BSONError } from './error';2import type { Int32 } from './int_32';3import { Long } from './long';4import { type InspectFn, defaultInspect } from './parser/utils';5 6/** @public */7export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';8/** @public */9export type LongWithoutOverrides = new (10 low: unknown,11 high?: number | boolean,12 unsigned?: boolean13) => {14 [P in Exclude<keyof Long, TimestampOverrides>]: Long[P];15};16/** @public */17export const LongWithoutOverridesClass: LongWithoutOverrides =18 Long as unknown as LongWithoutOverrides;19 20/** @public */21export interface TimestampExtended {22 $timestamp: {23 t: number;24 i: number;25 };26}27 28/**29 * @public30 * @category BSONType31 *32 * A special type for _internal_ MongoDB use and is **not** associated with the regular Date type.33 */34export class Timestamp extends LongWithoutOverridesClass {35 get _bsontype(): 'Timestamp' {36 return 'Timestamp';37 }38 39 static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;40 41 /**42 * An incrementing ordinal for operations within a given second.43 */44 get i(): number {45 return this.low >>> 0;46 }47 48 /**49 * A `time_t` value measuring seconds since the Unix epoch50 */51 get t(): number {52 return this.high >>> 0;53 }54 55 /**56 * @param int - A 64-bit bigint representing the Timestamp.57 */58 constructor(int: bigint);59 /**60 * @param long - A 64-bit Long representing the Timestamp.61 */62 constructor(long: Long);63 /**64 * @param value - A pair of two values indicating timestamp and increment.65 */66 constructor(value: { t: number; i: number });67 constructor(low?: bigint | Long | { t: number | Int32; i: number | Int32 }) {68 if (low == null) {69 super(0, 0, true);70 } else if (typeof low === 'bigint') {71 super(low, true);72 } else if (Long.isLong(low)) {73 super(low.low, low.high, true);74 } else if (typeof low === 'object' && 't' in low && 'i' in low) {75 if (typeof low.t !== 'number' && (typeof low.t !== 'object' || low.t._bsontype !== 'Int32')) {76 throw new BSONError('Timestamp constructed from { t, i } must provide t as a number');77 }78 if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {79 throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');80 }81 const t = Number(low.t);82 const i = Number(low.i);83 if (t < 0 || Number.isNaN(t)) {84 throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');85 }86 if (i < 0 || Number.isNaN(i)) {87 throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');88 }89 if (t > 0xffff_ffff) {90 throw new BSONError(91 'Timestamp constructed from { t, i } must provide t equal or less than uint32 max'92 );93 }94 if (i > 0xffff_ffff) {95 throw new BSONError(96 'Timestamp constructed from { t, i } must provide i equal or less than uint32 max'97 );98 }99 100 super(i, t, true);101 } else {102 throw new BSONError(103 'A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }'104 );105 }106 }107 108 toJSON(): { $timestamp: string } {109 return {110 $timestamp: this.toString()111 };112 }113 114 /** Returns a Timestamp represented by the given (32-bit) integer value. */115 static fromInt(value: number): Timestamp {116 return new Timestamp(Long.fromInt(value, true));117 }118 119 /** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */120 static fromNumber(value: number): Timestamp {121 return new Timestamp(Long.fromNumber(value, true));122 }123 124 /**125 * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.126 *127 * @param lowBits - the low 32-bits.128 * @param highBits - the high 32-bits.129 */130 static fromBits(lowBits: number, highBits: number): Timestamp {131 return new Timestamp({ i: lowBits, t: highBits });132 }133 134 /**135 * Returns a Timestamp from the given string, optionally using the given radix.136 *137 * @param str - the textual representation of the Timestamp.138 * @param optRadix - the radix in which the text is written.139 */140 static fromString(str: string, optRadix: number): Timestamp {141 return new Timestamp(Long.fromString(str, true, optRadix));142 }143 144 /** @internal */145 toExtendedJSON(): TimestampExtended {146 return { $timestamp: { t: this.t, i: this.i } };147 }148 149 /** @internal */150 static fromExtendedJSON(doc: TimestampExtended): Timestamp {151 // The Long check is necessary because extended JSON has different behavior given the size of the input number152 const i = Long.isLong(doc.$timestamp.i)153 ? doc.$timestamp.i.getLowBitsUnsigned() // Need to fetch the least significant 32 bits154 : doc.$timestamp.i;155 const t = Long.isLong(doc.$timestamp.t)156 ? doc.$timestamp.t.getLowBitsUnsigned() // Need to fetch the least significant 32 bits157 : doc.$timestamp.t;158 return new Timestamp({ t, i });159 }160 161 inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {162 inspect ??= defaultInspect;163 const t = inspect(this.t, options);164 const i = inspect(this.i, options);165 return `new Timestamp({ t: ${t}, i: ${i} })`;166 }167}168 