-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Hierarchical Partition Key Support
- Loading branch information
Showing
20 changed files
with
301 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { PartitionKeyDefinition } from "./PartitionKeyDefinition"; | ||
|
||
export type PartitionKey = PartitionKeyDefinition | string | number | unknown; | ||
export type PartitionKey = PrimitivePartitionKeyValue | PrimitivePartitionKeyValue[]; | ||
|
||
/** | ||
* Internal Representation Of Partition Key. | ||
*/ | ||
export type PartitionKeyInternal = PrimitivePartitionKeyValue[]; | ||
|
||
/** | ||
* A primitive Partition Key value. | ||
*/ | ||
export type PrimitivePartitionKeyValue = | ||
| string | ||
| number | ||
| boolean | ||
| NullPartitionType | ||
| NonePartitionKey; | ||
|
||
/** | ||
* The returned object represents a partition key value that allows creating and accessing items | ||
* with a null value for the partition key. | ||
*/ | ||
export type NullPartitionType = null; | ||
export const NullPartitionKeyLiteral: NullPartitionType = null; | ||
|
||
/** | ||
* The returned object represents a partition key value that allows creating and accessing items | ||
* without a value for partition key | ||
*/ | ||
export type NonePartitionKey = { | ||
[K in any]: never; | ||
}; | ||
export const NonePartitionKeyLiteral: NonePartitionKey = {}; | ||
|
||
/** | ||
* Maps PartitionKey to InternalPartitionKey. | ||
* @param partitionKey | ||
* @returns | ||
*/ | ||
export function mapPartitionToInternal(partitionKey: PartitionKey): PartitionKeyInternal { | ||
if (Array.isArray(partitionKey)) return partitionKey; | ||
else return [partitionKey]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
sdk/cosmosdb/cosmos/src/documents/PartitionKeyDefinitionVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
export enum PartitionKeyDefinitionVersion { | ||
V1 = 1, | ||
V2 = 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
export enum PartitionKeyKind { | ||
Hash = "Hash", | ||
MultiHash = "MultiHash" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,67 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { AzureLogger, createClientLogger } from "@azure/logger"; | ||
import { parsePath } from "./common"; | ||
import { PartitionKey, PartitionKeyDefinition } from "./documents"; | ||
import { NonePartitionKeyLiteral, NullPartitionKeyLiteral, PartitionKeyDefinition, PartitionKeyInternal, PrimitivePartitionKeyValue } from "./documents"; | ||
|
||
const logger: AzureLogger = createClientLogger("extractPartitionKey"); | ||
|
||
/** | ||
* Function to extract PartitionKey based on {@link PartitionKeyDefinition} | ||
* from an object. | ||
* Retuns | ||
* 1. {@link PartitionKeyInternal[]} - if extraction is successful. | ||
* 2. {@link undefined} - if either {@link partitionKeyDefinition} is not well formed | ||
* or an unsupported partitionkey type is encountered. | ||
* @hidden | ||
*/ | ||
export function extractPartitionKey( | ||
document: unknown, | ||
partitionKeyDefinition: PartitionKeyDefinition | ||
): PartitionKey[] { | ||
partitionKeyDefinition?: PartitionKeyDefinition | ||
): PartitionKeyInternal | undefined { | ||
if ( | ||
partitionKeyDefinition && | ||
partitionKeyDefinition.paths && | ||
partitionKeyDefinition.paths.length > 0 | ||
) { | ||
const partitionKey: PartitionKey[] = []; | ||
const partitionKeys: PrimitivePartitionKeyValue[] = []; | ||
partitionKeyDefinition.paths.forEach((path: string) => { | ||
const pathParts = parsePath(path); | ||
const pathParts: string[] = parsePath(path); | ||
let obj = document; | ||
for (const part of pathParts) { | ||
if (typeof obj === "object" && part in obj) { | ||
if (typeof obj === "object" && obj !==null && part in obj) { | ||
obj = (obj as Record<string, unknown>)[part]; | ||
} else { | ||
obj = undefined; | ||
break; | ||
} | ||
} | ||
partitionKey.push(obj); | ||
if(typeof obj === "string" || typeof obj === "number" || typeof obj === "boolean" ) { | ||
partitionKeys.push(obj); | ||
} else if(obj === NullPartitionKeyLiteral) { | ||
partitionKeys.push(NullPartitionKeyLiteral); | ||
} else if(obj === undefined || JSON.stringify(obj) === JSON.stringify(NonePartitionKeyLiteral)) { | ||
if(partitionKeyDefinition.systemKey === true) { | ||
return [] | ||
} | ||
partitionKeys.push(NonePartitionKeyLiteral); | ||
} else { | ||
logger.warning('Unsupported PartitionKey found.'); | ||
return undefined; | ||
} | ||
}); | ||
if (partitionKey.length === 1 && partitionKey[0] === undefined) { | ||
return undefinedPartitionKey(partitionKeyDefinition); | ||
} | ||
return partitionKey; | ||
return partitionKeys; | ||
} | ||
logger.warning("Unexpected Partition Key Definition Found."); | ||
return undefined; | ||
} | ||
/** | ||
* @hidden | ||
*/ | ||
export function undefinedPartitionKey(partitionKeyDefinition: PartitionKeyDefinition): unknown[] { | ||
export function undefinedPartitionKey(partitionKeyDefinition: PartitionKeyDefinition): PartitionKeyInternal { | ||
if (partitionKeyDefinition.systemKey === true) { | ||
return []; | ||
} else { | ||
return [{}]; | ||
return partitionKeyDefinition.paths.map(() => NonePartitionKeyLiteral); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.