Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for query tagging #1133

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/driver/src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,13 @@ export class Client implements Executor {
);
}

withTag(tag: string | null): Client {
return new Client(
this.pool,
this.options.withSession(this.options.session.withTag(tag)),
);
}

withWarningHandler(handler: WarningHandler): Client {
return new Client(this.pool, this.options.withWarningHandler(handler));
}
Expand Down
10 changes: 8 additions & 2 deletions packages/driver/src/baseConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ export class BaseRawConnection {
options: QueryOptions | undefined,
language: Language,
) {
if (state.annotations.size >= 1 << 16) {
throw new errors.InternalClientError("too many annotations");
}
wb.writeUInt16(state.annotations.size);
for (const [name, value] of state.annotations) {
wb.writeString(name);
wb.writeString(value);
}
wb.writeFlags(0xffff_ffff, capabilitiesFlags);
wb.writeFlags(
0,
Expand Down Expand Up @@ -953,7 +961,6 @@ export class BaseRawConnection {
): Promise<ParseResult> {
const wb = new WriteMessageBuffer();
wb.beginMessage(chars.$P);
wb.writeUInt16(0); // no headers

this._encodeParseParams(
wb,
Expand Down Expand Up @@ -1080,7 +1087,6 @@ export class BaseRawConnection {
): Promise<errors.EdgeDBError[]> {
const wb = new WriteMessageBuffer();
wb.beginMessage(chars.$O);
wb.writeUInt16(0); // no headers

this._encodeParseParams(
wb,
Expand Down
45 changes: 39 additions & 6 deletions packages/driver/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as errors from "./errors";
import { utf8Encoder } from "./primitives/buffer";

export type BackoffFunction = (n: number) => number;

Expand Down Expand Up @@ -118,6 +119,8 @@ export class TransactionOptions {
}
}

const TAG_ANNOTATION_KEY = "tag";

export interface SessionOptions {
module?: string;
moduleAliases?: Record<string, string>;
Expand All @@ -138,6 +141,13 @@ export class Session {
readonly config: Record<string, any>;
readonly globals: Record<string, any>;

/** @internal */
annotations = new Map<string, string>();

get tag(): string | null {
return this.annotations.get(TAG_ANNOTATION_KEY) ?? null;
}

constructor({
module = "default",
moduleAliases = {},
Expand All @@ -150,33 +160,56 @@ export class Session {
this.globals = globals;
}

private _clone(mergeOptions: SessionOptions) {
const session = new Session({ ...this, ...mergeOptions });
session.annotations = this.annotations;
return session;
}

withModuleAliases({
module,
...aliases
}: {
[name: string]: string;
}): Session {
return new Session({
...this,
return this._clone({
module: module ?? this.module,
moduleAliases: { ...this.moduleAliases, ...aliases },
});
}

withConfig(config: { [name: string]: any }): Session {
return new Session({
...this,
return this._clone({
config: { ...this.config, ...config },
});
}

withGlobals(globals: { [name: string]: any }): Session {
return new Session({
...this,
return this._clone({
globals: { ...this.globals, ...globals },
});
}

withTag(tag: string | null): Session {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure exactly what api we want here? It looks like the rust client has a withTag api (also with reserved tag names) (https://github.com/edgedb/edgedb-rust/pull/364/files#diff-608add6cc9859de62fac8b605b75549b985354b28530c7c1b07180ccd86fd729R574), but the python implementation just exposes the underlying annotations? (https://github.com/edgedb/edgedb-python/pull/552/files#diff-ac82d0a568b6ac577eb791f28078350639f6ffd9b904254a17557bb7dff1fb62R416)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah maybe I should just stay with with_tag() in Python too (so that the same tag limitation can be applied). cc @1st1 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withQueryTag() it is then.

const session = new Session({ ...this });
session.annotations = new Map(this.annotations);
if (tag != null) {
if (tag.startsWith("edgedb/")) {
throw new errors.InterfaceError("reserved tag: edgedb/*");
}
if (tag.startsWith("gel/")) {
throw new errors.InterfaceError("reserved tag: gel/*");
}
if (utf8Encoder.encode(tag).length > 128) {
throw new errors.InterfaceError("tag too long (> 128 bytes)");
}
session.annotations.set(TAG_ANNOTATION_KEY, tag);
} else {
session.annotations.delete(TAG_ANNOTATION_KEY);
}
return session;
}

/** @internal */
_serialise() {
const state: SerializedSessionState = {};
Expand Down
Loading