Skip to content

Commit

Permalink
Merge pull request #62 from bhargeymehta/bhargeymehta/concat-to-push
Browse files Browse the repository at this point in the history
Improve InMemoryProvider getRange function from O(N^2) to O(N)
  • Loading branch information
M1Les authored Sep 9, 2024
2 parents 1404aaf + 726aa2a commit 6b70241
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 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.47",
"version": "0.7.0",
"description": "A cross-browser object store library",
"author": "Mukundan Kavanur Kidambi <[email protected]>",
"scripts": {
Expand Down
67 changes: 49 additions & 18 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 @@ -741,6 +763,15 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
? this._indexTree.entriesReversed()
: this._indexTree.entries();
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 @@ -769,16 +800,16 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
}

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

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

0 comments on commit 6b70241

Please sign in to comment.