优化创建实体时的历史记录,(延迟写入和避免冗余)

pull/450/MERGE
ChenX 5 years ago
parent 59c7c1def2
commit 5544e3ea02

@ -1,6 +1,7 @@
import { Factory } from './CADFactory'; import { Factory } from './CADFactory';
import { CADFiler } from './CADFiler'; import { CADFiler } from './CADFiler';
import { CADObject } from './CADObject'; import { CADObject } from './CADObject';
import { CreateObjectData } from './CreateObjectData';
import { EraseEntityData } from "./EraseEntityData"; import { EraseEntityData } from "./EraseEntityData";
import { HistorycRecord } from './HistorycRecord'; import { HistorycRecord } from './HistorycRecord';
import { ObjectAllDataHistoryRecord } from './ObjectAllDataHistoryRecord'; import { ObjectAllDataHistoryRecord } from './ObjectAllDataHistoryRecord';
@ -12,71 +13,71 @@ import { ObjectId } from './ObjectId';
@Factory @Factory
export class CommandHistoryRecord extends CADObject export class CommandHistoryRecord extends CADObject
{ {
constructor(cmdName?: string) constructor(public CommandName = "")
{ {
super(); super();
this.commandName = cmdName;
}
//命令名称
private commandName: string;
get CommandName()
{
return this.commandName;
} }
//历史记录表 //历史记录表
private historyCol = new Map<ObjectId, HistorycRecord[]>(); private _HistoryList = new Map<ObjectId, HistorycRecord[]>();
private _CreateObjects = new Map<CADObject, HistorycRecord>();
get HistoryList(): Map<ObjectId, HistorycRecord[]> get HistoryList(): Map<ObjectId, HistorycRecord[]>
{ {
return this.historyCol; return this._HistoryList;
} }
private GetObjectHistoryList(id: ObjectId) private GetObjectHistoryList(id: ObjectId)
{ {
if (!this.historyCol.has(id)) if (!this._HistoryList.has(id))
{ this._HistoryList.set(id, []);
this.historyCol.set(id, []); return this._HistoryList.get(id);
}
return this.historyCol.get(id);
} }
EndCommand() EndCommand()
{ {
for (let [id, recs] of this.historyCol) for (let [id, hrs] of this._HistoryList)
{ {
let rec = this.GetObjectAllDataRecord(recs); let hr = this.GetObjectAllDataRecord(hrs);
if (rec) if (hr)
{ hr.WriteRedo();
rec.WriteRedo(); }
}
for (let [, hr] of this._CreateObjects)
{
let h = hr.redoData as CreateObjectData;
h.Save();
} }
} }
//获得对象的记录
GetObjectAllDataRecord(historyList: HistorycRecord[]): ObjectAllDataHistoryRecord //获取对象快照记录(如果有的话)
GetObjectAllDataRecord(historyList: HistorycRecord[]): ObjectAllDataHistoryRecord | undefined
{ {
if (historyList.length > 0) if (historyList.length > 0)
{ {
let rec = historyList[historyList.length - 1]; let hr = historyList[historyList.length - 1];
if (rec instanceof ObjectAllDataHistoryRecord) if (hr instanceof ObjectAllDataHistoryRecord)
{ return hr;
return rec;
}
} }
} }
//对象写入数据
//对象写入历史记录
WriteObjectHistoryPath(obj: CADObject, history: HistorycRecord) WriteObjectHistoryPath(obj: CADObject, history: HistorycRecord)
{ {
let his = this.GetObjectHistoryList(obj.Id); if (this._CreateObjects.has(obj))
if (this.GetObjectAllDataRecord(his))
{
//console.log("优化掉重复的全部数据");
return; return;
}
let hrs = this.GetObjectHistoryList(obj.Id);
if (this.GetObjectAllDataRecord(hrs))
return;
if (history instanceof ObjectAllDataHistoryRecord && history.undoData === undefined) if (history instanceof ObjectAllDataHistoryRecord && history.undoData === undefined)
{
history.WriteUndo(); history.WriteUndo();
} else if (history.redoData instanceof CreateObjectData)
his.push(history); this._CreateObjects.set(history.redoData.Object, history);
hrs.push(history);
} }
CreateObjectHistory(obj: CADObject): ObjectAllDataHistoryRecord CreateObjectHistory(obj: CADObject): ObjectAllDataHistoryRecord
{ {
if (!obj.Id) if (!obj.Id)
@ -84,10 +85,11 @@ export class CommandHistoryRecord extends CADObject
console.warn("错误!CreateObjectHistory"); console.warn("错误!CreateObjectHistory");
return; return;
} }
let rec = new ObjectAllDataHistoryRecord(obj.Id); let hr = new ObjectAllDataHistoryRecord(obj.Id);
this.WriteObjectHistoryPath(obj, rec); this.WriteObjectHistoryPath(obj, hr);
return rec; return hr;
} }
CreateEraseHistory(obj: CADObject, isErase: boolean) CreateEraseHistory(obj: CADObject, isErase: boolean)
{ {
let hr = new HistorycRecord(); let hr = new HistorycRecord();
@ -104,20 +106,20 @@ export class CommandHistoryRecord extends CADObject
ReadFile(file: CADFiler) ReadFile(file: CADFiler)
{ {
let ver = file.Read(); let ver = file.Read();
this.commandName = file.Read(); this.CommandName = file.Read();
let cout = file.Read(); let cout = file.Read();
this.historyCol.clear(); this._HistoryList.clear();
for (let i = 0; i < cout; i++) for (let i = 0; i < cout; i++)
{ {
let id = file.ReadObjectId(); let id = file.ReadObjectId();
let length = file.Read(); let length = file.Read();
let recs = []; let hrs: HistorycRecord[] = [];
this.historyCol.set(id, recs); this._HistoryList.set(id, hrs);
for (let j = 0; j < length; j++) for (let j = 0; j < length; j++)
{ {
let rec = file.ReadObject(); let hr = file.ReadObject() as HistorycRecord;
recs.push(rec); hrs.push(hr);
} }
} }
} }
@ -125,15 +127,15 @@ export class CommandHistoryRecord extends CADObject
WriteFile(file: CADFiler) WriteFile(file: CADFiler)
{ {
file.Write(1); file.Write(1);
file.Write(this.commandName); file.Write(this.CommandName);
file.Write(this.historyCol.size); file.Write(this._HistoryList.size);
for (let [id, recs] of this.historyCol) for (let [id, hrs] of this._HistoryList)
{ {
file.WriteObjectId(id); file.WriteObjectId(id);
file.Write(recs.length); file.Write(hrs.length);
for (let rec of recs) for (let hr of hrs)
{ {
file.WriteObject(rec); file.WriteObject(hr);
} }
} }
} }

@ -6,16 +6,20 @@ import { Database } from './Database';
@Factory @Factory
export class CreateObjectData extends CADObject export class CreateObjectData extends CADObject
{ {
private CADFiler: CADFiler; private CADFiler: CADFiler = new CADFiler();
constructor(obj?: CADObject) constructor(public Object?: CADObject)
{ {
super(); super();
this.CADFiler = new CADFiler();
if (obj)
this.CADFiler.WriteObject(obj);
} }
getObject(db: Database): CADObject //记录数据,在命令结束的时候记录
Save()
{
if (this.Object)
this.CADFiler.WriteObject(this.Object);
}
GetObject(db: Database): CADObject
{ {
this.CADFiler.Reset(); this.CADFiler.Reset();
this.CADFiler.database = db; this.CADFiler.database = db;

@ -302,9 +302,8 @@ export class Database
let newRecord = filer.ReadObject() as SymbolTableRecord; let newRecord = filer.ReadObject() as SymbolTableRecord;
newRecord.Owner = undefined; newRecord.Owner = undefined;
newRecord.Name = name; newRecord.Name = name;
owner.Add(newRecord, false);
this.AllocationObjectId(newRecord); this.AllocationObjectId(newRecord);
owner.Add(newRecord, false);
idMap.set(object.Id, newRecord.Id); idMap.set(object.Id, newRecord.Id);
} }
else if (status === DuplicateRecordCloning.Replace) else if (status === DuplicateRecordCloning.Replace)

@ -101,7 +101,7 @@ export class ObjectCollection<T extends CADObject> extends CADObject
{ {
if (undoData instanceof CreateObjectData) if (undoData instanceof CreateObjectData)
{ {
let obj = undoData.getObject(this._db) as T; let obj = undoData.GetObject(this._db) as T;
this.Objects.push(obj); this.Objects.push(obj);
this.AppendEvent(obj); this.AppendEvent(obj);
} }

Loading…
Cancel
Save