public override void OnChangeDocuments(DocumentMeta meta, DocumentContext context)
        {
            int count = context.Documents.Count;


            if (context.Operation == OperationType.Delete)
            {
                for (int i = 0; i < count; i++)
                {
                    var primaryOf = meta.SubmitChanges(context.Documents[i], OperationType.Delete);

                    Cache.Remove(meta.TypeOf, primaryOf);
                }
            }
            else
            {
                var parser = GetDocumentParser(meta.TypeOf, context.Operation);

                for (int i = 0; i < count; i++)
                {
                    Document document = context.Documents[i];

                    if (document.KeyOf == null)
                    {
                        document.KeyOf = new string[] { };
                    }

                    if (Option.SupportDocumentParser && parser != null)
                    {
                        parser.Parse(this, meta, document, context.Storage);
                    }

                    meta.SubmitChanges(document, context.Operation);

                    Cache.Set(document, meta.Cache.Expire);
                }
            }
        }
        public override bool LoadDocumentType(string typeOf, bool isInitialize = false)
        {
            var schema = GetDocumentType(typeOf);
            var meta   = new DocumentMeta(typeOf, schema, Provider, Option);

            if (isInitialize && meta.Cache.Lazy)
            {
                return(false);
            }

            meta.Partitions.Add(0, 0);

            var query = new DocumentQuery(typeOf);

            query.Skip = 0;
            query.Take = Option.SqlProviderDataReaderPageSize;

            var parser = GetDocumentParser(meta.TypeOf, OperationType.Load);

            using (var reader = new PostgreReader(Provider, Option, query, Logger))
            {
                while (reader.Read())
                {
                    foreach (var row in reader.Rows)
                    {
                        var document = GetDocumentFromSource(ref meta, row, 0);

                        if (Option.SupportDocumentParser && parser != null)
                        {
                            parser.Parse(this, meta, document, null);
                        }

                        meta.SubmitChanges(document, OperationType.Load);

                        Cache.Set(document, meta.Cache.Expire);

                        document.Dispose();
                    }

                    reader.NextPage();
                }

                Cache.Set(meta, meta.Cache.Expire);
            }

            return(true);
        }
Exemple #3
0
        public override bool LoadDocumentType(string typeOf, bool isInitialize = false)
        {
            Logger?.Debug(ScopeType.Engine, $"Load TypeOf: {typeOf}");

            var documentType = GetDocumentType(typeOf);
            var meta         = new DocumentMeta(typeOf, documentType, Provider, Option);

            if (isInitialize && meta.Cache.Lazy)
            {
                return(true);
            }

            var files = new List <DocumentStorage>();

            #region Files
            var directoryInfo = new DirectoryInfo(meta.Directory);
            if (directoryInfo.Exists)
            {
                var extentions     = Option.Extention;
                var directoryFiles = directoryInfo.GetFiles(extentions, SearchOption.TopDirectoryOnly);

                meta.HasData = directoryFiles.Length > 0;

                var parts = new List <int>();
                foreach (var file in directoryFiles)
                {
                    var storage = new DocumentStorage(meta.TypeOf, Provider?.BaseDirectory, file);

                    if (storage.Partition > meta.Partitions.Current)
                    {
                        meta.Partitions.Current = storage.Partition;
                    }

                    if (!parts.Contains(storage.Partition))
                    {
                        parts.Add(storage.Partition);
                    }

                    files.Add(storage);
                }

                parts = parts.OrderBy(p => p).ToList();

                foreach (var part in parts)
                {
                    meta.Partitions.Add(part, 0);
                }

                meta.Partitions.Next = meta.Partitions.Current + 1;
            }
            #endregion

            if (files.Count == 0)
            {
                Logger?.Debug(ScopeType.Engine, "LoadDocumentType files.count = 0");

                return(false);
            }

            Logger?.Debug(ScopeType.Engine, $"TypeOf:{typeOf} number of files : {files.Count}");

            int bufferSize = Option.BufferSize;
            var serializer = new JsonSerializer();
            var parser     = GetDocumentParser(meta.TypeOf, OperationType.Load);

            foreach (DocumentStorage file in files)
            {
                Logger?.Debug(ScopeType.Engine, $"Read TypeOf: {file.TypeOf} - File : {file.Target}");

                try
                {
                    using (FileStream fs = new FileStream(file.Target, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize))
                        using (StreamReader sr = new StreamReader(fs, Encoding.UTF8, true, bufferSize))
                            using (JsonReader reader = new JsonTextReader(sr))
                            {
                                reader.SupportMultipleContent = false;

                                while (reader.Read())
                                {
                                    if (reader.TokenType == JsonToken.StartObject)
                                    {
                                        var document = GetDocumentFromSource(ref meta, serializer.Deserialize <JObject>(reader), file.Partition);

                                        if (Option.SupportDocumentParser && parser != null)
                                        {
                                            parser.Parse(this, meta, document, file);
                                        }

                                        meta.SubmitChanges(document, OperationType.Load);

                                        Cache.Set(document, meta.Cache.Expire);

                                        document.Dispose();
                                    }
                                }
                            }
                }
                catch (Exception ex)
                {
                    Logger?.Fatal($"Exception Read TypeOf: {file.TypeOf} File : {file.Target}", ex);
                }
            }

            Cache.Set(meta, meta.Cache.Expire);
            Cache.Set(new DocumentSequence(typeOf, meta.Sequence));

            documentType.Dispose();
            meta.Dispose();

            return(true);
        }