Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

chore(core): add LRU for Styles,add test (#297)

Dushusir6bb96cf43ba1
diff --git a/packages/core/src/sheets/__tests__/styles.spec.ts b/packages/core/src/sheets/__tests__/styles.spec.ts
new file mode 100644
index 0000000000..cb2bb8bcda
--- /dev/null
+++ b/packages/core/src/sheets/__tests__/styles.spec.ts
@@ -0,0 +1,61 @@
+import { describe, expect, it } from 'vitest';
+
+import { Styles } from '../styles';
+
+describe('Test Styles', () => {
+ it('Init styles, set style', () => {
+ const styles = new Styles(
+ {
+ '1': { fs: 12 },
+ '2': { fs: 14 },
+ },
+ 2
+ );
+
+ // get style by id
+ let style = styles.get('1');
+ expect(style).toEqual({ fs: 12 });
+
+ // get style by data
+ style = styles.get({ fs: 12 });
+ expect(style).toEqual({ fs: 12 });
+
+ // add style
+ const styleNull = styles.setValue(null);
+ expect(styleNull).toBeUndefined();
+
+ const styleId = styles.setValue({ fs: 16 });
+
+ // search style not in cache
+ const searchId = styles.search({ fs: 12 });
+ expect(searchId).toBe('1');
+
+ // Add duplicate style, search style in cache
+ const styleIdDuplicate = styles.setValue({ fs: 16 });
+ expect(styleIdDuplicate).toBe(styleId);
+
+ // each function
+ let count = 0;
+ styles.each((style) => {
+ count++;
+ });
+ expect(count).toBe(3);
+
+ // toJSON function
+ const stylesJSON = styles.toJSON();
+ expect(stylesJSON).toEqual({
+ '1': { fs: 12 },
+ '2': { fs: 14 },
+ [styleId as string]: { fs: 16 },
+ });
+
+ // getStyleByCell function
+ const cellStyleId = { s: '1' };
+ style = styles.getStyleByCell(cellStyleId);
+ expect(style).toEqual({ fs: 12 });
+
+ const cellStyleObject = { s: { fs: 12 } };
+ style = styles.getStyleByCell(cellStyleObject);
+ expect(style).toEqual({ fs: 12 });
+ });
+});
diff --git a/packages/core/src/sheets/styles.ts b/packages/core/src/sheets/styles.ts
index 41850c20ae..25eec307e9 100644
--- a/packages/core/src/sheets/styles.ts
+++ b/packages/core/src/sheets/styles.ts
@@ -3,14 +3,19 @@ import { ICellData, IStyleData } from '../Types/Interfaces';
/**
* Styles in a workbook, cells locate styles based on style IDs
+ *
+ * TODO@Dushusir: Cachemap needs to follow style to clear cleared following styles
*/
export class Styles {
private _styles: IKeyType<Nullable<IStyleData>>;
private _cacheMap = new Map<string, string>();
- constructor(styles: IKeyType<Nullable<IStyleData>> = {}) {
+ private _maxCacheSize: number;
+
+ constructor(styles: IKeyType<Nullable<IStyleData>> = {}, maxCacheSize = 100) {
this._styles = styles;
+ this._maxCacheSize = maxCacheSize;
this._generateCacheMap();
}
@@ -26,25 +31,27 @@ export class Styles {
}
search(data: IStyleData): string {
- const { _styles } = this;
-
- // for (const id in _styles) {
- // if (Tools.diffValue(_styles[id], data)) {
- // return id;
- // }
- // }
-
// Take from cache
const styleObject = JSON.stringify(data);
if (this._cacheMap.has(styleObject)) {
- return this._cacheMap.get(styleObject) as string;
+ const id = this._cacheMap.get(styleObject) as string;
+ // Move the accessed entry to the end of the Map to represent its recent usage
+ this._cacheMap.delete(styleObject);
+ this._cacheMap.set(styleObject, id);
+ return id;
+ }
+
+ // Check if the data exists in _styles and not in _cacheMap
+ const existingId = this.getExistingStyleId(data);
+ if (existingId) {
+ return existingId;
}
return '-1';
}
get(id: string | Nullable<IStyleData>): Nullable<IStyleData> {
- id = String(id);
if (typeof id !== 'string') return id;
+ id = String(id);
return this._styles[id];
}
@@ -55,6 +62,13 @@ export class Styles {
const styleObject = JSON.stringify(data);
this._cacheMap.set(styleObject, id);
+ // Check if cache size exceeds the maximum limit
+ if (this._cacheMap.size > this._maxCacheSize) {
+ // Remove the least recently used entry (the first entry in the Map)
+ const firstEntry = this._cacheMap.entries().next().value;
+ this._cacheMap.delete(firstEntry[0]);
+ }
+
return id;
}
@@ -83,10 +97,25 @@ export class Styles {
}
private _generateCacheMap(): void {
- const { _styles } = this;
+ const { _styles, _cacheMap } = this;
+ let count = 0;
for (const id in _styles) {
const styleObject = JSON.stringify(_styles[id]);
- this._cacheMap.set(styleObject, id);
+ _cacheMap.set(styleObject, id);
+ count++;
+ if (count >= this._maxCacheSize) {
+ break;
+ }
+ }
+ }
+
+ private getExistingStyleId(data: IStyleData): Nullable<string> {
+ const { _styles } = this;
+ for (const id in _styles) {
+ if (Tools.diffValue(_styles[id], data)) {
+ return id;
+ }
}
+ return null;
}
}