Beispiel #1
0
        protected void Dispose(bool isdispose)
        {
            if (isdispose)
            {
                _backtimer.Dispose();
                if (_queueWriter != null)
                {
                    _queueWriter.Flush();
                    _queueWriter.Dispose();
                }

                if (_queueReader != null)
                {
                    _queueReader.Dispose();
                }

                SaveConfig();

                _isdispose = 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);
        }