EsentPersistentStorage_IdentifierTable.cs 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Versions;
using Roslyn.Utilities;

13
namespace Microsoft.CodeAnalysis.Esent
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
    internal partial class EsentPersistentStorage : ISyntaxTreeInfoPersistentStorage
    {
        private const string IdentifierSetVersion = "<IdentifierSetVersion>";
        private const string IdentifierSetSerializationVersion = "1";
        private const int NotSupported = -1;
        private const int FlushThreshold = 5000;

        private int? _identifierSetVersionId;

        private bool TryGetUniqueIdentifierId(string identifier, out int id)
        {
            id = default(int);

            if (string.IsNullOrWhiteSpace(identifier))
            {
                return false;
            }

            try
            {
                id = _esentStorage.GetUniqueIdentifierId(identifier);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private bool TryGetIdentifierSetVersionId(out int id)
        {
            if (_identifierSetVersionId.HasValue)
            {
                id = _identifierSetVersionId.Value;
                return true;
            }

            if (TryGetUniqueIdentifierId(IdentifierSetVersion, out id))
            {
                _identifierSetVersionId = id;
                return true;
            }

            return false;
        }

61
        private VersionStamp GetIdentifierSetVersion(EsentStorage.Key key)
62 63 64 65 66 67
        {
            if (!PersistenceEnabled)
            {
                return VersionStamp.Default;
            }

C
CyrusNajmabadi 已提交
68
            if (!TryGetIdentifierSetVersionId(out var identifierId))
69 70 71 72 73
            {
                return VersionStamp.Default;
            }

            // TODO: verify that project is in solution associated with the storage
74
            return EsentExceptionWrapper(key, identifierId, GetIdentifierSetVersion, CancellationToken.None);
75 76
        }

77
        private VersionStamp GetIdentifierSetVersion(EsentStorage.Key key, int identifierId, object unused1, object unused2, CancellationToken cancellationToken)
78 79
        {
            using (var accessor = _esentStorage.GetIdentifierLocationTableAccessor())
80
            using (var stream = accessor.GetReadStream(key, identifierId))
81 82 83 84 85 86
            {
                if (stream == null)
                {
                    return VersionStamp.Default;
                }

87
                using (var reader = new ObjectReader(stream))
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
                {
                    return VersionStamp.ReadFrom(reader);
                }
            }
        }

        public bool ReadIdentifierPositions(Document document, VersionStamp syntaxVersion, string identifier, List<int> positions, CancellationToken cancellationToken)
        {
            Contract.ThrowIfTrue(string.IsNullOrWhiteSpace(identifier));

            if (!PersistenceEnabled)
            {
                return false;
            }

C
CyrusNajmabadi 已提交
103
            if (!TryGetProjectAndDocumentKey(document, out var key))
104 105 106 107
            {
                return false;
            }

108
            var persistedVersion = GetIdentifierSetVersion(key);
109 110 111 112 113
            if (!document.CanReusePersistedSyntaxTreeVersion(syntaxVersion, persistedVersion))
            {
                return false;
            }

C
CyrusNajmabadi 已提交
114
            if (!TryGetUniqueIdentifierId(identifier, out var identifierId))
115 116 117 118
            {
                return false;
            }

119
            return EsentExceptionWrapper(key, identifierId, positions, ReadIdentifierPositions, cancellationToken);
120 121
        }

122
        private bool ReadIdentifierPositions(EsentStorage.Key key, int identifierId, List<int> positions, object unused, CancellationToken cancellationToken)
123 124
        {
            using (var accessor = _esentStorage.GetIdentifierLocationTableAccessor())
125
            using (var stream = accessor.GetReadStream(key, identifierId))
126 127 128 129 130 131 132
            {
                if (stream == null)
                {
                    // no such identifier exist.
                    return true;
                }

133
                using (var reader = new ObjectReader(stream))
134 135 136 137 138 139 140 141 142 143 144 145
                {
                    var formatVersion = reader.ReadString();
                    if (formatVersion != IdentifierSetSerializationVersion)
                    {
                        return false;
                    }

                    return ReadFrom(reader, positions, cancellationToken);
                }
            }
        }

146
        private bool DeleteIdentifierLocations(EsentStorage.Key key, CancellationToken cancellationToken)
147 148 149
        {
            using (var accessor = _esentStorage.GetIdentifierLocationTableAccessor())
            {
150
                accessor.Delete(key, cancellationToken);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
                return accessor.ApplyChanges();
            }
        }

        private void Free(Dictionary<string, List<int>> map)
        {
            if (map == null)
            {
                return;
            }

            foreach (var value in map.Values)
            {
                if (value == null)
                {
                    continue;
                }

                SharedPools.BigDefault<List<int>>().ClearAndFree(value);
            }

            SharedPools.StringIgnoreCaseDictionary<List<int>>().ClearAndFree(map);
        }

        private bool ReadFrom(ObjectReader reader, List<int> result, CancellationToken cancellationToken)
        {
            var count = reader.ReadInt32();
            if (count < 0)
            {
                return false;
            }

            for (int i = 0; i < count; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();
                result.Add(reader.ReadInt32());
            }

            return true;
        }

        private void WriteList(ObjectWriter writer, List<int> positions)
        {
            if (positions.Count > FlushThreshold)
            {
                writer.WriteInt32(NotSupported);
                return;
            }

            writer.WriteInt32(positions.Count);

            foreach (var position in positions)
            {
                writer.WriteInt32(position);
            }
        }

        private static Dictionary<string, List<int>> CreateIdentifierLocations(Document document, SyntaxNode root, CancellationToken cancellationToken)
        {
            var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();

            var identifierMap = SharedPools.StringIgnoreCaseDictionary<List<int>>().AllocateAndClear();
            foreach (var token in root.DescendantTokens(descendIntoTrivia: true))
            {
                if (token.IsMissing || token.Span.Length == 0)
                {
                    continue;
                }

                if (syntaxFacts.IsIdentifier(token) || syntaxFacts.IsGlobalNamespaceKeyword(token))
                {
                    var valueText = token.ValueText;
                    identifierMap.GetOrAdd(valueText, _ => SharedPools.BigDefault<List<int>>().AllocateAndClear()).Add(token.Span.Start);
                }
            }

            return identifierMap;
        }
    }
}