private bool Update2 <T>(string tablename, string key, T item, EntityTableMeta meta) where T : new() { string tablefile = GetTableFile(tablename); Dictionary <long, EntityTableIndexItem> arr = null; Tuple <long, long> offset = null; int indexpos = 0; var locker = GetKeyLocker(tablename, string.Empty); lock (locker) { if (!keyindexdic[tablename].TryGetValue(key, out arr)) { throw new Exception(string.Format("更新失败,key为{0}的记录数为0", key)); } string keyindexfile = GetKeyFile(tablename); EntityTableIndexItem indexitem = (EntityTableIndexItem)arr.Last().Value; using (ObjTextWriter keywriter = ObjTextWriter.CreateWriter(keyindexfile, ObjTextReaderWriterEncodeType.entitybuf)) //ObjTextWriter keywriter = GetWriter(keyindexfile); { indexitem.Del = true; keywriter.AppendObject(indexitem); } var keyvalue = item.Eval(meta.KeyProperty); //var keyvalue=meta.KeyProperty.GetValueMethed(item); if (keyvalue == null) { throw new Exception("key值不能为空"); } var tableitem = new EntityTableItem <T>(item); tableitem.Flag = (byte)EntityTableItemFlag.Ok; using (ObjTextWriter otw = ObjTextWriter.CreateWriter(tablefile, ObjTextReaderWriterEncodeType.entitybuf)) //ObjTextWriter otw = GetWriter(tablefile); { offset = otw.PreAppendObject(tableitem, (s1, s2) => { if (s1.Length <= indexitem.len) { Console.WriteLine("修改->覆盖"); return(otw.Override(indexitem.Offset, s1)); } return(null); }); } Dictionary <long, EntityTableIndexItem> al = null; var keyidc = keyindexdic[tablename]; if (!keyidc.TryGetValue(keyvalue.ToString(), out al)) { lock (keyidc) { if (!keyidc.TryGetValue(keyvalue.ToString(), out al)) { al = new Dictionary <long, EntityTableIndexItem>(); keyidc.TryAdd(keyvalue.ToString(), al); } } } EntityTableIndexItem newkey = new EntityTableIndexItem { Key = keyvalue.ToString(), Offset = offset.Item1, len = (int)(offset.Item2 - offset.Item1), Del = false }; if (newkey.Offset == indexitem.Offset) { newkey.len = indexitem.len; } arr[indexpos] = newkey; using (ObjTextWriter keyidxwriter = ObjTextWriter.CreateWriter(keyindexfile, ObjTextReaderWriterEncodeType.entitybuf)) //ObjTextWriter keyidxwriter = GetWriter(keyindexfile); //lock(keyidxwriter) { keyidxwriter.AppendObject(newkey); } foreach (var idx in meta.Indexs) { string indexfile = GetIndexFile(tablename, idx); var indexvalue = item.Eval(meta.IndexProperties[idx]); var newindex = new EntityTableIndexItem { Key = indexvalue == null ? string.Empty : indexvalue.ToString(), Offset = offset.Item1, len = (int)(offset.Item2 - offset.Item1), Del = false }; using (ObjTextWriter idxwriter = ObjTextWriter.CreateWriter(indexfile, ObjTextReaderWriterEncodeType.entitybuf)) //ObjTextWriter idxwriter = GetWriter(indexfile); //lock(idxwriter) { idxwriter.AppendObject(newindex); } } Console.WriteLine("写入成功:" + keyvalue + "->" + offset); } return(true); }
private bool Insert2 <T>(string tablename, IEnumerable <T> items, EntityTableMeta meta) where T : new() { string tablefile = GetTableFile(tablename); var locker = GetKeyLocker(tablename, string.Empty); lock (locker) { ObjTextWriter otw = ObjTextWriter.CreateWriter(tablefile, ObjTextReaderWriterEncodeType.entitybuf); string keyindexfile = GetKeyFile(tablename); ObjTextWriter keywriter = ObjTextWriter.CreateWriter(keyindexfile, ObjTextReaderWriterEncodeType.entitybuf); Dictionary <string, ObjTextWriter> idxwriterdic = new Dictionary <string, ObjTextWriter>(); try { foreach (var item in items) { var keyvalue = item.Eval(meta.KeyProperty); //var keyvalue = meta.KeyProperty.GetValueMethed(item); if (keyvalue == null) { throw new Exception("key值不能为空"); } var keystr = keyvalue.ToString(); var keylocker = GetKeyLocker(tablename, keystr); Dictionary <long, EntityTableIndexItem> arr = null; lock (keylocker) { if (keyindexdic[tablename].TryGetValue(keystr, out arr)) { throw new Exception(string.Format("key:{0}不可重复", keystr)); } } var tableitem = new EntityTableItem <T>(item); tableitem.Flag = (byte)EntityTableItemFlag.Ok; //using (ObjTextWriter otw = ObjTextWriter.CreateWriter(tablefile, ObjTextReaderWriterEncodeType.entitybuf)) //var otw = GetWriter(tablefile); //lock(otw) { var offset = otw.AppendObject(tableitem); Dictionary <long, EntityTableIndexItem> al = null; var idc = keyindexdic[tablename]; if (!idc.TryGetValue(keystr, out al)) { lock (idc) { if (!idc.TryGetValue(keystr, out al)) { al = new Dictionary <long, EntityTableIndexItem>(); idc.TryAdd(keystr, al); } } } var newkey = new EntityTableIndexItem { Key = keystr, Offset = offset.Item1, len = (int)(offset.Item2 - offset.Item1) }; lock (al) { al.Add(newkey.Offset, newkey); } //string keyindexfile = GetKeyFile(tablename); //using (ObjTextWriter keywriter = ObjTextWriter.CreateWriter(keyindexfile, ObjTextReaderWriterEncodeType.entitybuf)) //ObjTextWriter keywriter = GetWriter(keyindexfile); //lock(keywriter) { keywriter.AppendObject(newkey); } foreach (var idx in meta.Indexs) { string indexfile = GetIndexFile(tablename, idx); var indexvalue = item.Eval(meta.IndexProperties[idx]); var newindex = new EntityTableIndexItem { Key = indexvalue == null ? string.Empty : indexvalue.ToString(), Offset = offset.Item1, len = (int)(offset.Item2 - offset.Item1) }; //using (ObjTextWriter idxwriter = ObjTextWriter.CreateWriter(indexfile, ObjTextReaderWriterEncodeType.entitybuf)) //ObjTextWriter idxwriter = GetWriter(indexfile); //lock(idxwriter) { ObjTextWriter idxwriter = null; if (!idxwriterdic.TryGetValue(indexfile, out idxwriter)) { idxwriter = ObjTextWriter.CreateWriter(indexfile, ObjTextReaderWriterEncodeType.entitybuf); idxwriterdic.Add(indexfile, idxwriter); } idxwriter.AppendObject(newindex); } } } } } finally { otw.Dispose(); keywriter.Dispose(); foreach (var kv in idxwriterdic) { kv.Value.Dispose(); } } } return(true); }