Skip to content

Commit

Permalink
add live configs
Browse files Browse the repository at this point in the history
  • Loading branch information
bhargeymehta committed Sep 6, 2024
1 parent a85e71b commit 10f5532
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
6 changes: 5 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ export {
breakAndNormalizeSearchPhrase,
getFullTextIndexWordsForItem,
} from "./src/FullTextSearchHelpers";
export { InMemoryProvider, StoreData } from "./src/InMemoryProvider";
export {
InMemoryProvider,
StoreData,
ILiveConsumerConfigs,
} from "./src/InMemoryProvider";
export {
ItemType,
KeyComponentType,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/objectstoreprovider",
"version": "0.6.48",
"version": "0.7.0",
"description": "A cross-browser object store library",
"author": "Mukundan Kavanur Kidambi <[email protected]>",
"scripts": {
Expand Down
62 changes: 47 additions & 15 deletions src/InMemoryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export interface StoreData {
mapType?: OrderedMapType;
}

export interface ILiveConsumerConfigs {
usePushForGetRange: boolean;
};

export type GetLiveConsumerConfigsFn = () => ILiveConsumerConfigs;

const defaultLiveConsumerConfigs: ILiveConsumerConfigs = {
usePushForGetRange: false
};

export class InMemoryProvider extends DbProvider {
private _stores: Map<string, StoreData> = new Map();

Expand All @@ -73,7 +83,8 @@ export class InMemoryProvider extends DbProvider {
constructor(
mapType?: OrderedMapType,
supportsRollback = false,
logger?: IObjectStoreProviderLogger
logger?: IObjectStoreProviderLogger,
private getLiveConfigs?: GetLiveConsumerConfigsFn
) {
super();
this._mapType = mapType;
Expand Down Expand Up @@ -120,7 +131,8 @@ export class InMemoryProvider extends DbProvider {
token,
writeNeeded,
this._supportsRollback!,
this.logger
this.logger,
this.getLiveConfigs
)
);
}
Expand All @@ -146,7 +158,8 @@ class InMemoryTransaction implements DbTransaction {
private _transToken: TransactionToken,
private _writeNeeded: boolean,
private _supportsRollback: boolean,
private logger: IObjectStoreProviderLogger
private logger: IObjectStoreProviderLogger,
private getLiveConfigs?: GetLiveConsumerConfigsFn
) {
// Close the transaction on the next tick. By definition, anything is completed synchronously here, so after an event tick
// goes by, there can't have been anything pending.
Expand Down Expand Up @@ -217,7 +230,8 @@ class InMemoryTransaction implements DbTransaction {
const ims = new InMemoryStore(
this,
store,
this._writeNeeded && this._supportsRollback
this._writeNeeded && this._supportsRollback,
this.getLiveConfigs
);
this._stores.set(storeName, ims);
return ims;
Expand All @@ -237,7 +251,8 @@ class InMemoryStore implements DbStore {
constructor(
private _trans: InMemoryTransaction,
storeInfo: StoreData,
private _supportsRollback: boolean
private _supportsRollback: boolean,
private getLiveConfigs?: GetLiveConsumerConfigsFn
) {
this._storeSchema = storeInfo.schema;
if (this._supportsRollback) {
Expand Down Expand Up @@ -273,7 +288,8 @@ class InMemoryStore implements DbStore {
this._mergedData,
index,
this._storeSchema.primaryKeyPath,
this._mapType
this._mapType,
this.getLiveConfigs
)
);
});
Expand Down Expand Up @@ -394,7 +410,8 @@ class InMemoryStore implements DbStore {
this._mergedData,
undefined as any,
this._storeSchema.primaryKeyPath,
this._mapType
this._mapType,
this.getLiveConfigs
)
);
}
Expand All @@ -419,7 +436,8 @@ class InMemoryStore implements DbStore {
this._mergedData,
indexSchema,
this._storeSchema.primaryKeyPath,
this._mapType
this._mapType,
this.getLiveConfigs
)
);
}
Expand All @@ -441,7 +459,8 @@ class InMemoryStore implements DbStore {
this._mergedData,
index,
this._storeSchema.primaryKeyPath,
this._mapType
this._mapType,
this.getLiveConfigs
)
);
});
Expand Down Expand Up @@ -498,15 +517,18 @@ class InMemoryStore implements DbStore {
class InMemoryIndex extends DbIndexFTSFromRangeQueries {
private _indexTree: IOrderedMap<string, ItemType[]>;
private _trans?: InMemoryTransaction;
private getLiveConfigs: GetLiveConsumerConfigsFn;
constructor(
_mergedData: Map<string, ItemType>,
indexSchema: IndexSchema,
primaryKeyPath: KeyPathType,
mapType?: OrderedMapType
mapType?: OrderedMapType,
getLiveConfigs?: GetLiveConsumerConfigsFn
) {
super(indexSchema, primaryKeyPath);
this._indexTree = createOrderedMap(mapType);
this.put(values(_mergedData), true);
this.getLiveConfigs = getLiveConfigs ?? (() => defaultLiveConsumerConfigs);
}

public internal_SetTransaction(trans: InMemoryTransaction) {
Expand Down Expand Up @@ -740,7 +762,16 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
const iterator = reverse
? this._indexTree.entriesReversed()
: this._indexTree.entries();
const values = [] as ItemType[];
let values = [] as ItemType[];
const { usePushForGetRange } = this.getLiveConfigs();
const pushValues = (values: ItemType[], newValues: ItemType[]) => {
newValues.forEach(v => values.push(v));
return values;
};
const concatValues = (values: ItemType[], newValues: ItemType[]) => {
return values.concat(newValues);
};
const mergeFn = usePushForGetRange ? pushValues : concatValues;
for (const entry of iterator) {
const key = entry.key;
if (key === undefined) {
Expand Down Expand Up @@ -768,16 +799,17 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
break;
}

const pushToResult = (v: ItemType) => values.push(v);
if (this.isUniqueIndex()) {
(this._indexTree.get(key) as ItemType[]).forEach(pushToResult);
const newValues = this._indexTree.get(key) as ItemType[];
values = mergeFn(values, newValues);
} else {
this._getKeyValues(
const newValues = this._getKeyValues(
key,
limit - values.length,
Math.abs(offset),
reverse
).forEach(pushToResult);
);
values = mergeFn(values, newValues);

if (offset < 0) {
offset = 0;
Expand Down

0 comments on commit 10f5532

Please sign in to comment.