예제 #1
0
 private byte[] Serialize(PostingsFile item)
 {
     using (var stream = new MemoryStream())
     {
         FileBase.Serializer.Serialize(stream, item);
         return(stream.ToArray());
     }
 }
예제 #2
0
        public void Put(PostingsFile item)
        {
            if (_postingsFiles == null)
            {
                _postingsFiles = new Dictionary <string, PostingsFile>();
            }

            _postingsFiles[item.Token] = item;
        }
예제 #3
0
        public bool TryGet(string token, out PostingsFile item)
        {
            InitReadSession();

            PostingsFile pf;

            if (_postingsFiles.TryGetValue(token, out pf))
            {
                item = pf;
                return(true);
            }

            var timer = new Stopwatch();

            timer.Start();
            var fileName = Path.Combine(_directory, _containerId + ".pc");

            if (!File.Exists(fileName))
            {
                item = null;
                return(false);
            }
            using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new StreamReader(fs, Encoding.ASCII))
                {
                    var    readerToken = string.Empty;
                    string line;
                    string data = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        readerToken = line.Substring(0, line.IndexOf(':'));
                        if (readerToken == token)
                        {
                            data = line;
                            break;
                        }
                    }

                    if (data == null)
                    {
                        item = null;
                        return(false);
                    }
                    var base64 = data.Substring(readerToken.Length + 1);
                    var bytes  = Convert.FromBase64String(base64);
                    using (var memStream = new MemoryStream(bytes))
                    {
                        var obj = Deserialize(memStream);
                        _postingsFiles[token] = obj;
                        Log.DebugFormat("extracted {0} from {1} in {2}", obj, fileName, timer.Elapsed);
                        item = obj;
                        return(true);
                    }
                }
        }