public StorageActionsAccessor(TableStorage storage, Reference<WriteBatch> writeBatch, Reference<SnapshotReader> snapshot, IdGenerator generator, IBufferPool bufferPool, OrderedPartCollection<AbstractFileCodec> fileCodecs)
            : base(snapshot, generator, bufferPool)
        {
            this.storage = storage;
            this.writeBatch = writeBatch;
	        this.fileCodecs = fileCodecs;
        }
		public static string GenerateText(TypeDeclaration type, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions)
		{
			var unit = new CompilationUnit();

			var namespaces = new HashSet<string>
			{
				typeof (SystemTime).Namespace,
				typeof (AbstractViewGenerator).Namespace,
				typeof (Enumerable).Namespace,
				typeof (IEnumerable<>).Namespace,
				typeof (IEnumerable).Namespace,
				typeof (int).Namespace,
				typeof (LinqOnDynamic).Namespace,
				typeof(Field).Namespace,
			};
			foreach (var extension in extensions)
			{
				foreach (var ns in extension.Value.GetNamespacesToImport())
				{
					namespaces.Add(ns);
				}
			}

			foreach (var ns in namespaces)
			{
				unit.AddChild(new Using(ns));
			}

			unit.AddChild(type);
			var output = new CSharpOutputVisitor();
			unit.AcceptVisitor(output, null);

			return output.Text;
		}
        private void StreamToClient(Stream stream, int pageSize, Etag etag, OrderedPartCollection<AbstractFileReadTrigger> readTriggers)
        {
            using (var cts = new CancellationTokenSource())
            using (var timeout = cts.TimeoutAfter(FileSystemsLandlord.SystemConfiguration.DatabaseOperationTimeout))
            using (var writer = new JsonTextWriter(new StreamWriter(stream)))
            {
                writer.WriteStartObject();
                writer.WritePropertyName("Results");
                writer.WriteStartArray();

                Storage.Batch(accessor =>
                {
                    var files = accessor.GetFilesAfter(etag, pageSize);
                    foreach (var file in files)
                    {
                        if (readTriggers.CanReadFile(file.FullPath, file.Metadata, ReadOperation.Load) == false)
                            continue;

                        timeout.Delay();
                        var doc = RavenJObject.FromObject(file);
                        doc.WriteTo(writer);

                        writer.WriteRaw(Environment.NewLine);
                    }
                });

                writer.WriteEndArray();
                writer.WriteEndObject();
                writer.Flush();
            }
        }
		public StorageActionsAccessor(TableColumnsCache tableColumnsCache, JET_INSTANCE instance, string databaseName, UuidGenerator uuidGenerator, OrderedPartCollection<AbstractFileCodec> fileCodecs)
		{
			this.tableColumnsCache = tableColumnsCache;
			this.uuidGenerator = uuidGenerator;
			this.fileCodecs = fileCodecs;
			try
			{
				session = new Session(instance);
				transaction = new Transaction(session);
				Api.JetOpenDatabase(session, databaseName, null, out database, OpenDatabaseGrbit.None);
			}
			catch (Exception original)
			{
				log.WarnException("Could not create accessor", original);
				try
				{
					Dispose();
				}
				catch (Exception e)
				{
					log.WarnException("Could not properly dispose accessor after exception in ctor.", e);
				}
				throw;
			}
		}
Exemple #5
0
		public DocumentStorageActions(
			JET_INSTANCE instance,
			string database,
			TableColumnsCache tableColumnsCache,
			OrderedPartCollection<AbstractDocumentCodec> documentCodecs,
			IUuidGenerator uuidGenerator,
			IDocumentCacher cacher,
			TransactionalStorage transactionalStorage)
		{
			this.tableColumnsCache = tableColumnsCache;
			this.documentCodecs = documentCodecs;
			this.uuidGenerator = uuidGenerator;
			this.cacher = cacher;
			this.transactionalStorage = transactionalStorage;
			try
			{
				session = new Session(instance);
				transaction = new Transaction(session);
				Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.None);
			}
			catch (Exception)
			{
				Dispose();
				throw;
			}
		}
        public void DocumentStorage_DocumentAdd_And_DocumentRead_WithCompression(string requestedStorage)
        {
            var documentCodecs = new OrderedPartCollection<AbstractDocumentCodec>();
            documentCodecs.Add(new DocumentCompression());

            DocumentStorage_DocumentAdd_And_DocumentRead_Internal(requestedStorage, "Foo/Bar", documentCodecs);
        }
Exemple #7
0
 public StorageActionsAccessor(TableStorage storage, Reference <WriteBatch> writeBatch, Reference <SnapshotReader> snapshot, IdGenerator generator, IBufferPool bufferPool, UuidGenerator uuidGenerator, OrderedPartCollection <AbstractFileCodec> fileCodecs)
     : base(snapshot, generator, bufferPool)
 {
     this.storage       = storage;
     this.writeBatch    = writeBatch;
     this.uuidGenerator = uuidGenerator;
     this.fileCodecs    = fileCodecs;
 }
        public MappedResultsStorageActions(TableStorage tableStorage, IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs, Reference<SnapshotReader> snapshot, Reference<WriteBatch> writeBatch, IBufferPool bufferPool)
			: base(snapshot, bufferPool)
		{
			this.tableStorage = tableStorage;
			this.generator = generator;
			this.documentCodecs = documentCodecs;
			this.writeBatch = writeBatch;
		}
Exemple #9
0
        public DocumentStorageActions(
            JET_INSTANCE instance,
            string database,
            TableColumnsCache tableColumnsCache,
            OrderedPartCollection <AbstractDocumentCodec> documentCodecs,
            IUuidGenerator uuidGenerator,
            IDocumentCacher cacher,
            EsentTransactionContext transactionContext,
            TransactionalStorage transactionalStorage)
        {
            this.tableColumnsCache    = tableColumnsCache;
            this.documentCodecs       = documentCodecs;
            this.uuidGenerator        = uuidGenerator;
            this.cacher               = cacher;
            this.transactionalStorage = transactionalStorage;
            this.transactionContext   = transactionContext;

            try
            {
                if (transactionContext == null)
                {
                    session     = new Session(instance);
                    transaction = new Transaction(session);
                    sessionAndTransactionDisposer = () =>
                    {
                        if (transaction != null)
                        {
                            transaction.Dispose();
                        }
                        if (session != null)
                        {
                            session.Dispose();
                        }
                    };
                }
                else
                {
                    session     = transactionContext.Session;
                    transaction = transactionContext.Transaction;
                    var disposable = transactionContext.EnterSessionContext();
                    sessionAndTransactionDisposer = disposable.Dispose;
                }
                Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.None);
            }
            catch (Exception ex)
            {
                logger.WarnException("Error when trying to open a new DocumentStorageActions", ex);
                try
                {
                    Dispose();
                }
                catch (Exception e)
                {
                    logger.WarnException("Error on dispose when the ctor threw an exception, resources may have leaked", e);
                }
                throw;
            }
        }
Exemple #10
0
        public static bool TryToCreateTransactionalStorage(InMemoryRavenConfiguration ravenConfiguration,
                                                           bool hasCompression, EncryptionConfiguration encryption, bool isRavenFs,
                                                           out ITransactionalStorage storage, out Database.FileSystem.Storage.ITransactionalStorage fileStorage)
        {
            storage     = null;
            fileStorage = null;
            if (File.Exists(Path.Combine(ravenConfiguration.DataDirectory, Voron.Impl.Constants.DatabaseFilename)))
            {
                if (isRavenFs)
                {
                    fileStorage = RavenFileSystem.CreateTransactionalStorage(ravenConfiguration);
                }
                else
                {
                    storage = ravenConfiguration.CreateTransactionalStorage(InMemoryRavenConfiguration.VoronTypeName, () => { }, () => { });
                }
            }
            else if (File.Exists(Path.Combine(ravenConfiguration.DataDirectory, "Data.jfm")))
            {
                if (isRavenFs)
                {
                    fileStorage = RavenFileSystem.CreateTransactionalStorage(ravenConfiguration);
                }
                else
                {
                    storage = ravenConfiguration.CreateTransactionalStorage(InMemoryRavenConfiguration.EsentTypeName, () => { }, () => { });
                }
            }
            if (storage == null && fileStorage == null)
            {
                return(false);
            }
            if (isRavenFs)
            {
                var filesOrderedPartCollection = new OrderedPartCollection <Database.FileSystem.Plugins.AbstractFileCodec>();
                fileStorage.Initialize(new UuidGenerator(), filesOrderedPartCollection);
                return(true);
            }
            var orderedPartCollection = new OrderedPartCollection <AbstractDocumentCodec>();

            if (encryption != null)
            {
                var documentEncryption = new DocumentEncryption();
                documentEncryption.SetSettings(new EncryptionSettings(encryption.EncryptionKey, encryption.SymmetricAlgorithmType,
                                                                      encryption.EncryptIndexes, encryption.PreferedEncryptionKeyBitsSize));
                orderedPartCollection.Add(documentEncryption);
            }
            if (hasCompression)
            {
                orderedPartCollection.Add(new DocumentCompression());
            }
            storage.Initialize(new SequentialUuidGenerator {
                EtagBase = 0
            }, orderedPartCollection);

            return(true);
        }
		public StorageOperationsTask(ITransactionalStorage storage, OrderedPartCollection<AbstractFileDeleteTrigger> deleteTriggers, IndexStorage search, INotificationPublisher notificationPublisher)
		{
			this.storage = storage;
			this.deleteTriggers = deleteTriggers;
			this.search = search;
			this.notificationPublisher = notificationPublisher;

			InitializeTimer();
		}
        public StorageOperationsTask(ITransactionalStorage storage, OrderedPartCollection <AbstractFileDeleteTrigger> deleteTriggers, IndexStorage search, INotificationPublisher notificationPublisher)
        {
            this.storage               = storage;
            this.deleteTriggers        = deleteTriggers;
            this.search                = search;
            this.notificationPublisher = notificationPublisher;

            InitializeTimer();
        }
Exemple #13
0
        public static Type Compile(string source, string name, string queryText, OrderedPartCollection <AbstractDynamicCompilationExtension> extensions, string basePath, InMemoryRavenConfiguration configuration)
        {
            source = source.Replace("AbstractIndexCreationTask.SpatialGenerate", "SpatialGenerate"); // HACK, should probably be on the client side
            var indexCacheDir = GetIndexCacheDir(configuration);

            if (Directory.Exists(indexCacheDir) == false)
            {
                Directory.CreateDirectory(indexCacheDir);
            }

            int numberHash;
            var indexFilePath = GetIndexFilePath(source, indexCacheDir, out numberHash);

            var locker = Locks.GetOrAdd(numberHash, s => new object());

            lock (locker)
            {
                var shouldCachedIndexBeRecompiled = ShouldIndexCacheBeRecompiled(indexFilePath);
                if (shouldCachedIndexBeRecompiled == false)
                {
                    // Look up the index in the in-memory cache.
                    CacheEntry entry;
                    if (cacheEntries.TryGetValue(source, out entry))
                    {
                        if (_logger.IsDebugEnabled)
                        {
                            _logger.Debug(string.Format("Cache entrie for index {0} in database {1} was found and incremented", name, configuration.DatabaseName));
                        }
                        Interlocked.Increment(ref entry.Usages);
                        return(entry.Type);
                    }

                    Type type;

                    if (TryGetDiskCacheResult(source, name, configuration, indexFilePath, out type))
                    {
                        if (_logger.IsDebugEnabled)
                        {
                            _logger.Debug(string.Format("Definition for index {0} in database {1} was found in compiled cache on disk (at {2}) and loaded to assembly", name, configuration.DatabaseName, configuration.CompiledIndexCacheDirectory));
                        }
                        return(type);
                    }
                }

                var result = DoActualCompilation(source, name, queryText, extensions, basePath, indexFilePath, configuration);

                // ReSharper disable once RedundantArgumentName
                AddResultToCache(source, result, shouldUpdateIfExists: shouldCachedIndexBeRecompiled);

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(string.Format("Index {0} in database {1} was compiled, cached and stored on disk (at {2}) and loaded to assembly", name, configuration.DatabaseName, configuration.CompiledIndexCacheDirectory));
                }
                return(result);
            }
        }
Exemple #14
0
 public DocumentsStorageActions(TableStorage storage,
                                IUuidGenerator generator,
                                OrderedPartCollection <AbstractDocumentCodec> documentCodecs,
                                IDocumentCacher documentCacher)
 {
     this.storage        = storage;
     this.generator      = generator;
     this.documentCodecs = documentCodecs;
     this.documentCacher = documentCacher;
 }
Exemple #15
0
 public DocumentRetriever(InMemoryRavenConfiguration configuration, IStorageActionsAccessor actions, OrderedPartCollection <AbstractReadTrigger> triggers,
                          Dictionary <string, RavenJToken> transformerParameters = null,
                          HashSet <string> itemsToInclude = null)
 {
     this.configuration         = configuration;
     this.actions               = actions;
     this.triggers              = triggers;
     this.transformerParameters = transformerParameters ?? new Dictionary <string, RavenJToken>();
     this.itemsToInclude        = itemsToInclude ?? new HashSet <string>();
 }
Exemple #16
0
        public static Type Compile(string source, string name, string queryText, OrderedPartCollection <AbstractDynamicCompilationExtension> extensions, string basePath)
        {
            var provider = new CSharpCodeProvider(new Dictionary <string, string> {
                { "CompilerVersion", "v4.0" }
            });
            var assemblies = new HashSet <string>
            {
                typeof(SystemTime).Assembly.Location,
                typeof(AbstractViewGenerator).Assembly.Location,
                typeof(NameValueCollection).Assembly.Location,
                typeof(Enumerable).Assembly.Location,
                typeof(Binder).Assembly.Location,
                typeof(Field).Assembly.Location
            };

            foreach (var extension in extensions)
            {
                foreach (var assembly in extension.Value.GetAssembliesToReference())
                {
                    assemblies.Add(assembly);
                }
            }
            var compilerParameters = new CompilerParameters
            {
                GenerateExecutable      = false,
                GenerateInMemory        = true,
                IncludeDebugInformation = false
            };

            if (basePath != null)
            {
                compilerParameters.TempFiles = new TempFileCollection(basePath, false);
            }

            foreach (var assembly in assemblies)
            {
                compilerParameters.ReferencedAssemblies.Add(assembly);
            }
            var compileAssemblyFromFile = provider.CompileAssemblyFromSource(compilerParameters, source);
            var results = compileAssemblyFromFile;

            if (results.Errors.HasErrors)
            {
                var sb = new StringBuilder()
                         .AppendLine("Source code:")
                         .AppendLine(queryText)
                         .AppendLine();
                foreach (CompilerError error in results.Errors)
                {
                    sb.AppendLine(error.ToString());
                }
                throw new InvalidOperationException(sb.ToString());
            }
            return(results.CompiledAssembly.GetType(name));
        }
 public ReadFileToDatabase(BufferPool bufferPool, ITransactionalStorage storage, OrderedPartCollection <AbstractFilePutTrigger> putTriggers, Stream inputStream, string filename, RavenJObject headers)
 {
     this.bufferPool  = bufferPool;
     this.inputStream = inputStream;
     this.storage     = storage;
     this.putTriggers = putTriggers;
     this.filename    = filename;
     this.headers     = headers;
     buffer           = bufferPool.TakeBuffer(StorageConstants.MaxPageSize);
     md5Hasher        = Encryptor.Current.CreateHash();
 }
 public DocumentRetriever(IStorageActionsAccessor actions, OrderedPartCollection <AbstractReadTrigger> triggers,
                          InFlightTransactionalState inFlightTransactionalState,
                          Dictionary <string, RavenJToken> transformerParameters = null,
                          HashSet <string> itemsToInclude = null)
 {
     this.actions  = actions;
     this.triggers = triggers;
     this.inFlightTransactionalState = inFlightTransactionalState;
     this.transformerParameters      = transformerParameters ?? new Dictionary <string, RavenJToken>();
     this.itemsToInclude             = itemsToInclude ?? new HashSet <string>();
 }
 public ReadFileToDatabase(BufferPool bufferPool, ITransactionalStorage storage, OrderedPartCollection<AbstractFilePutTrigger> putTriggers, Stream inputStream, string filename, RavenJObject headers)
 {
     this.bufferPool = bufferPool;
     this.inputStream = inputStream;
     this.storage = storage;
     this.putTriggers = putTriggers;
     this.filename = filename;
     this.headers = headers;
     buffer = bufferPool.TakeBuffer(StorageConstants.MaxPageSize);
     md5Hasher = Encryptor.Current.CreateHash();
 }
        public void Initialize(IUuidGenerator generator, OrderedPartCollection <AbstractDocumentCodec> documentCodecs, Action <string> putResourceMarker = null)
        {
            if (generator == null)
            {
                throw new ArgumentNullException("generator");
            }
            if (documentCodecs == null)
            {
                throw new ArgumentNullException("documentCodecs");
            }

            uuidGenerator   = generator;
            _documentCodecs = documentCodecs;

            Log.Info("Starting to initialize Voron storage. Path: " + configuration.DataDirectory);

            StorageEnvironmentOptions options = configuration.RunInMemory ?
                                                CreateMemoryStorageOptionsFromConfiguration(configuration) :
                                                CreateStorageOptionsFromConfiguration(configuration);

            options.OnScratchBufferSizeChanged += size =>
            {
                if (configuration.Storage.Voron.ScratchBufferSizeNotificationThreshold < 0)
                {
                    return;
                }

                if (size < configuration.Storage.Voron.ScratchBufferSizeNotificationThreshold * 1024L * 1024L)
                {
                    return;
                }

                RunTransactionalStorageNotificationHandlers();
            };

            tableStorage = new TableStorage(options, bufferPool);
            var schemaCreator = new SchemaCreator(configuration, tableStorage, Output, Log);

            schemaCreator.CreateSchema();
            schemaCreator.SetupDatabaseIdAndSchemaVersion();
            if (!configuration.Storage.PreventSchemaUpdate)
            {
                schemaCreator.UpdateSchemaIfNecessary();
            }

            SetupDatabaseId();

            if (putResourceMarker != null)
            {
                putResourceMarker(configuration.DataDirectory);
            }

            Log.Info("Voron storage initialized");
        }
Exemple #21
0
		public DocumentStorageActions(
			JET_INSTANCE instance,
			string database,
			TableColumnsCache tableColumnsCache,
			OrderedPartCollection<AbstractDocumentCodec> documentCodecs,
			IUuidGenerator uuidGenerator,
			IDocumentCacher cacher,
			EsentTransactionContext transactionContext,
			TransactionalStorage transactionalStorage)
		{
			this.tableColumnsCache = tableColumnsCache;
			this.documentCodecs = documentCodecs;
			this.uuidGenerator = uuidGenerator;
			this.cacher = cacher;
			this.transactionalStorage = transactionalStorage;
			this.transactionContext = transactionContext;

			try
			{
				if (transactionContext == null)
				{
					session = new Session(instance);
					transaction = new Transaction(session);
					sessionAndTransactionDisposer = () =>
					{
						if(transaction != null)
							transaction.Dispose();
						if(session != null)
							session.Dispose();
					};
				}
				else
				{
					session = transactionContext.Session;
					transaction = transactionContext.Transaction;
					var disposable = transactionContext.EnterSessionContext();
					sessionAndTransactionDisposer = disposable.Dispose;
				}
				Api.JetOpenDatabase(session, database, null, out dbid, OpenDatabaseGrbit.None);
			}
			catch (Exception ex)
			{
			    logger.WarnException("Error when trying to open a new DocumentStorageActions", ex);
			    try
			    {
			        Dispose();
			    }
			    catch (Exception e)
			    {
			        logger.WarnException("Error on dispose when the ctor threw an exception, resources may have leaked", e);
			    }
				throw;
			}
		}
        public MappedResultsStorageActions(TableStorage tableStorage, IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs, Reference<SnapshotReader> snapshot, 
			Reference<WriteBatch> writeBatch, IBufferPool bufferPool, IStorageActionsAccessor storageActionsAccessor, ConcurrentDictionary<int, RemainingReductionPerLevel> ScheduledReductionsPerViewAndLevel)
			: base(snapshot, bufferPool)
		{
			this.tableStorage = tableStorage;
			this.generator = generator;
			this.documentCodecs = documentCodecs;
			this.writeBatch = writeBatch;
	        this.storageActionsAccessor = storageActionsAccessor;
	        this.scheduledReductionsPerViewAndLevel = ScheduledReductionsPerViewAndLevel;
		}
Exemple #23
0
 public DynamicViewCompiler(string name, IndexDefinition indexDefinition, OrderedPartCollection <AbstractDynamicCompilationExtension> extensions, string basePath)
 {
     this.indexDefinition = indexDefinition;
     this.extensions      = extensions;
     this.basePath        = Path.Combine(basePath, "TemporaryIndexDefinitionsAsSource");
     if (Directory.Exists(this.basePath) == false)
     {
         Directory.CreateDirectory(this.basePath);
     }
     this.name = MonoHttpUtility.UrlEncode(name);
     RequiresSelectNewAnonymousType = true;
 }
 public StorageActionsAccessor(IUuidGenerator generator, OrderedPartCollection <AbstractDocumentCodec> documentCodecs, IDocumentCacher documentCacher, Reference <WriteBatch> writeBatchReference, Reference <SnapshotReader> snapshotReference, TableStorage storage, TransactionalStorage transactionalStorage, IBufferPool bufferPool)
 {
     Documents   = new DocumentsStorageActions(generator, documentCodecs, documentCacher, writeBatchReference, snapshotReference, storage, bufferPool);
     Indexing    = new IndexingStorageActions(storage, generator, snapshotReference, writeBatchReference, this, bufferPool);
     Queue       = new QueueStorageActions(storage, generator, snapshotReference, writeBatchReference, bufferPool);
     Lists       = new ListsStorageActions(storage, generator, snapshotReference, writeBatchReference, bufferPool);
     Tasks       = new TasksStorageActions(storage, generator, snapshotReference, writeBatchReference, bufferPool);
     Staleness   = new StalenessStorageActions(storage, snapshotReference, writeBatchReference, bufferPool);
     MapReduce   = new MappedResultsStorageActions(storage, generator, documentCodecs, snapshotReference, writeBatchReference, bufferPool);
     Attachments = new AttachmentsStorageActions(storage.Attachments, writeBatchReference, snapshotReference, generator, storage, transactionalStorage, bufferPool);
     General     = new GeneralStorageActions(storage, writeBatchReference, snapshotReference, bufferPool);
 }
		public StorageActionsAccessor(TableStorage storage, IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs, IDocumentCacher documentCacher)
		{
			General = new GeneralStorageActions(storage);
			Attachments = new AttachmentsStorageActions(storage, generator);
			Transactions = new TransactionStorageActions(storage, generator, documentCodecs);
			Documents = new DocumentsStorageActions(storage, Transactions, generator, documentCodecs, documentCacher);
			Indexing = new IndexingStorageActions(storage);
			MappedResults = new MappedResultsStorageAction(storage, generator);
			Queue = new QueueStorageActions(storage, generator);
			Tasks = new TasksStorageActions(storage, generator);
			Staleness = new StalenessStorageActions(storage);
		}
 public DynamicCompilerBase(InMemoryRavenConfiguration configuration, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions, string name, string basePath)
 {
     this.configuration = configuration;
     this.name = name;
     this.extensions = extensions;
     if (configuration.RunInMemory == false)
     {
         this.basePath = Path.Combine(basePath, "temp");
         if (Directory.Exists(this.basePath) == false)
             Directory.CreateDirectory(this.basePath);
     }
 }
        public bool Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection <AbstractDocumentCodec> documentCodecs)
        {
            try
            {
                DocumentCodecs = documentCodecs;
                generator      = uuidGenerator;
                var instanceParameters = new TransactionalStorageConfigurator(configuration).ConfigureInstance(instance, path);

                if (configuration.RunInUnreliableYetFastModeThatIsNotSuitableForProduction)
                {
                    instanceParameters = new InstanceParameters(instance)
                    {
                        CircularLog          = true,
                        Recovery             = false,
                        NoInformationEvent   = false,
                        CreatePathIfNotExist = true,
                        TempDirectory        = Path.Combine(path, "temp"),
                        SystemDirectory      = Path.Combine(path, "system"),
                        LogFileDirectory     = Path.Combine(path, "logs"),
                        MaxVerPages          = 256,
                        BaseName             = "RVN",
                        EventSource          = "Raven",
                        LogBuffers           = 8192,
                        LogFileSize          = 256,
                        MaxSessions          = TransactionalStorageConfigurator.MaxSessions,
                        MaxCursors           = 1024,
                        DbExtensionSize      = 128,
                        AlternateDatabaseRecoveryDirectory = path
                    };
                }

                log.Info(@"Esent Settings:
  MaxVerPages      = {0}
  CacheSizeMax     = {1}
  DatabasePageSize = {2}", instanceParameters.MaxVerPages, SystemParameters.CacheSizeMax, SystemParameters.DatabasePageSize);

                Api.JetInit(ref instance);

                var newDb = EnsureDatabaseIsCreatedAndAttachToDatabase();

                SetIdFromDb();

                tableColumnsCache.InitColumDictionaries(instance, database);

                return(newDb);
            }
            catch (Exception e)
            {
                Dispose();
                throw new InvalidOperationException("Could not open transactional storage: " + database, e);
            }
        }
Exemple #28
0
		public DynamicViewCompiler(string name, IndexDefinition indexDefinition, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions, string basePath, InMemoryRavenConfiguration configuration)
		{
			this.indexDefinition = indexDefinition;
			this.extensions = extensions;
			if (configuration.RunInMemory == false)
			{
				this.basePath = Path.Combine(basePath, "TemporaryIndexDefinitionsAsSource");
				if (Directory.Exists(this.basePath) == false)
					Directory.CreateDirectory(this.basePath);
			}
			this.name = MonoHttpUtility.UrlEncode(name);
		    RequiresSelectNewAnonymousType = true;
		}
Exemple #29
0
        public Query GetLuceneQuery(string index, IndexQuery query, OrderedPartCollection <AbstractIndexQueryTrigger> indexQueryTriggers)
        {
            Index value;

            if (indexes.TryGetValue(index, out value) == false)
            {
                log.Debug("Query on non existing index {0}", index);
                throw new InvalidOperationException("Index '" + index + "' does not exists");
            }
            var fieldsToFetch = new FieldsToFetch(new string[0], AggregationOperation.None, null);

            return(new Index.IndexQueryOperation(value, query, _ => false, fieldsToFetch, indexQueryTriggers).GetLuceneQuery());
        }
 public StorageActionsAccessor(TableStorage storage, IUuidGenerator generator, OrderedPartCollection <AbstractDocumentCodec> documentCodecs)
 {
     this.documentCodecs = documentCodecs;
     General             = new GeneralStorageActions(storage);
     Attachments         = new AttachmentsStorageActions(storage, generator);
     Transactions        = new TransactionStorageActions(storage, generator, documentCodecs);
     Documents           = new DocumentsStorageActions(storage, Transactions, generator, documentCodecs);
     Indexing            = new IndexingStorageActions(storage);
     MappedResults       = new MappedResultsStorageAction(storage, generator);
     Queue     = new QueueStorageActions(storage, generator);
     Tasks     = new TasksStorageActions(storage, generator);
     Staleness = new StalenessStorageActions(storage);
 }
Exemple #31
0
            public IndexQueryOperation(Index parent, IndexQuery indexQuery, Func <IndexQueryResult, bool> shouldIncludeInResults, FieldsToFetch fieldsToFetch, OrderedPartCollection <AbstractIndexQueryTrigger> indexQueryTriggers)
            {
                this.parent                 = parent;
                this.indexQuery             = indexQuery;
                this.shouldIncludeInResults = shouldIncludeInResults;
                this.fieldsToFetch          = fieldsToFetch;
                this.indexQueryTriggers     = indexQueryTriggers;

                if (fieldsToFetch.IsDistinctQuery)
                {
                    alreadyReturned = new HashSet <RavenJObject>(new RavenJTokenEqualityComparer());
                }
            }
Exemple #32
0
 public DynamicCompilerBase(InMemoryRavenConfiguration configuration, OrderedPartCollection <AbstractDynamicCompilationExtension> extensions, string name, string basePath)
 {
     this.configuration = configuration;
     this.name          = name;
     this.extensions    = extensions;
     if (configuration.RunInMemory == false)
     {
         this.basePath = Path.Combine(basePath, "temp");
         if (Directory.Exists(this.basePath) == false)
         {
             Directory.CreateDirectory(this.basePath);
         }
     }
 }
        public IndexDefinitionStorage(
            InMemoryRavenConfiguration configuration,
            ITransactionalStorage transactionalStorage,
            string path,
            OrderedPartCollection<AbstractDynamicCompilationExtension> extensions)
        {
            this.configuration = configuration;
            this.transactionalStorage = transactionalStorage;
            this.extensions = extensions; // this is used later in the ctor, so it must appears first
            this.path = Path.Combine(path, IndexDefDir);

            if (Directory.Exists(this.path) == false && configuration.RunInMemory == false)
                Directory.CreateDirectory(this.path);
        }
Exemple #34
0
        public void Initialize(UuidGenerator generator, OrderedPartCollection <AbstractFileCodec> codecs, Action <string> putResourceMarker = null)
        {
            if (codecs == null)
            {
                throw new ArgumentException("codecs");
            }

            uuidGenerator = generator;
            fileCodecs    = codecs;

            try
            {
                new TransactionalStorageConfigurator(configuration).ConfigureInstance(instance, path);
                bool lockTaken = false;
                //locking only the compaction didn't resolve the problem it seems this is the minimal amount of code that needs to be locked
                //to prevent errors from esent
                try
                {
                    Monitor.TryEnter(locker, 30 * 1000, ref lockTaken);
                    if (lockTaken == false)
                    {
                        throw new TimeoutException("Couldn't take FS lock for initializing a new FS (Esent bug requires us to lock the storage), we have waited for 30 seconds, aborting.");
                    }
                    Api.JetInit(ref instance);

                    EnsureDatabaseIsCreatedAndAttachToDatabase();
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(locker);
                    }
                }
                SetIdFromDb();

                tableColumnsCache.InitColumDictionaries(instance, database);

                if (putResourceMarker != null)
                {
                    putResourceMarker(path);
                }
            }
            catch (Exception e)
            {
                Dispose();
                throw new InvalidOperationException("Could not open transactional storage: " + database, e);
            }
        }
Exemple #35
0
        public static bool CanReadFile(this OrderedPartCollection <AbstractFileReadTrigger> triggers, string name, RavenJObject metadata, ReadOperation operation)
        {
            foreach (var trigger in triggers)
            {
                var result = trigger.Value.AllowRead(name, metadata, operation);
                if (result.Veto == ReadVetoResult.ReadAllow.Allow)
                {
                    continue;
                }

                Log.Debug("Trigger {0} asked us to ignore {1}", trigger.Value, name);
                return(false);
            }

            return(true);
        }
Exemple #36
0
        public IEnumerable <IndexQueryResult> Query(
            string index,
            IndexQuery query,
            Func <IndexQueryResult, bool> shouldIncludeInResults,
            FieldsToFetch fieldsToFetch,
            OrderedPartCollection <AbstractIndexQueryTrigger> indexQueryTriggers)
        {
            Index value;

            if (indexes.TryGetValue(index, out value) == false)
            {
                log.Debug("Query on non existing index {0}", index);
                throw new InvalidOperationException("Index " + index + " does not exists");
            }
            return(new Index.IndexQueryOperation(value, query, shouldIncludeInResults, fieldsToFetch, indexQueryTriggers).Query());
        }
Exemple #37
0
        public static string GenerateText(TypeDeclaration type,
                                          OrderedPartCollection <AbstractDynamicCompilationExtension> extensions,
                                          HashSet <string> namespaces = null)
        {
            var unit = new SyntaxTree();

            if (namespaces == null)
            {
                namespaces = new HashSet <string>
                {
                    typeof(SystemTime).Namespace,
                    typeof(AbstractViewGenerator).Namespace,
                    typeof(Enumerable).Namespace,
                    typeof(IEnumerable <>).Namespace,
                    typeof(IEnumerable).Namespace,
                    typeof(int).Namespace,
                    typeof(LinqOnDynamic).Namespace,
                    typeof(Field).Namespace,
                    typeof(CultureInfo).Namespace,
                    typeof(Regex).Namespace
                };
            }

            foreach (var extension in extensions)
            {
                foreach (var ns in extension.Value.GetNamespacesToImport())
                {
                    namespaces.Add(ns);
                }
            }

            foreach (var ns in namespaces)
            {
                unit.Members.Add(new UsingDeclaration(ns));
            }

            unit.Members.Add(new WindowsNewLine());

            unit.Members.Add(type);

            var stringWriter = new StringWriter();
            var output       = new CSharpOutputVisitor(stringWriter, FormattingOptionsFactory.CreateSharpDevelop());

            unit.AcceptVisitor(output);

            return(stringWriter.GetStringBuilder().ToString());
        }
Exemple #38
0
        public DocumentsStorageActions(IUuidGenerator uuidGenerator,
                                       OrderedPartCollection <AbstractDocumentCodec> documentCodecs,
                                       IDocumentCacher documentCacher,
                                       Reference <WriteBatch> writeBatch,
                                       Reference <SnapshotReader> snapshot,
                                       TableStorage tableStorage,
                                       IBufferPool bufferPool)
            : base(snapshot, bufferPool)
        {
            this.uuidGenerator  = uuidGenerator;
            this.documentCodecs = documentCodecs;
            this.documentCacher = documentCacher;
            this.writeBatch     = writeBatch;
            this.tableStorage   = tableStorage;

            metadataIndex = tableStorage.Documents.GetIndex(Tables.Documents.Indices.Metadata);
        }
        public static bool TryToCreateTransactionalStorage(InMemoryRavenConfiguration ravenConfiguration,
                                                           bool hasCompression, EncryptionConfiguration encryption, out ITransactionalStorage storage)
        {
            storage = null;
            if (File.Exists(Path.Combine(ravenConfiguration.DataDirectory, Voron.Impl.Constants.DatabaseFilename)))
            {
                storage = ravenConfiguration.CreateTransactionalStorage(InMemoryRavenConfiguration.VoronTypeName, () => { }, () => { });
            }
            else if (File.Exists(Path.Combine(ravenConfiguration.DataDirectory, "Data")))
            {
                storage = ravenConfiguration.CreateTransactionalStorage(InMemoryRavenConfiguration.EsentTypeName, () => { }, () => { });
            }

            if (storage == null)
            {
                return(false);
            }

            var orderedPartCollection = new OrderedPartCollection <AbstractDocumentCodec>();

            if (encryption != null)
            {
                var documentEncryption = new DocumentEncryption();
                documentEncryption.SetSettings(new EncryptionSettings(encryption.EncryptionKey, encryption.SymmetricAlgorithmType,
                                                                      encryption.EncryptIndexes, encryption.PreferedEncryptionKeyBitsSize));
                orderedPartCollection.Add(documentEncryption);
            }
            if (hasCompression)
            {
                orderedPartCollection.Add(new DocumentCompression());
            }

            uuidGenerator = new SequentialUuidGenerator();
            storage.Initialize(uuidGenerator, orderedPartCollection);

            /*
             * Added configuration steps
             */
            storage.Batch(actions =>
            {
                var nextIdentityValue  = actions.General.GetNextIdentityValue("Raven/Etag");
                uuidGenerator.EtagBase = nextIdentityValue;
            });

            return(true);
        }
		public static string GenerateText(TypeDeclaration type, 
			OrderedPartCollection<AbstractDynamicCompilationExtension> extensions,
			HashSet<string> namespaces = null)
		{
			var unit = new SyntaxTree();

			if (namespaces == null)
			{
				namespaces = new HashSet<string>
				{
					typeof (SystemTime).Namespace,
					typeof (AbstractViewGenerator).Namespace,
					typeof (Enumerable).Namespace,
					typeof (IEnumerable<>).Namespace,
					typeof (IEnumerable).Namespace,
					typeof (int).Namespace,
					typeof (LinqOnDynamic).Namespace,
					typeof (Field).Namespace,
					typeof (CultureInfo).Namespace,
					typeof (Regex).Namespace
				};
			}

			foreach (var extension in extensions)
			{
				foreach (var ns in extension.Value.GetNamespacesToImport())
				{
					namespaces.Add(ns);
				}
			}

			foreach (var ns in namespaces)
			{
				unit.Members.Add(new UsingDeclaration(ns));
			}

			unit.Members.Add(new WindowsNewLine());

			unit.Members.Add(type);

			var stringWriter = new StringWriter();
			var output = new CSharpOutputVisitor(stringWriter, FormattingOptionsFactory.CreateSharpDevelop());
			unit.AcceptVisitor(output);

			return stringWriter.GetStringBuilder().ToString();
		}
		public DocumentsStorageActions(IUuidGenerator uuidGenerator,
			OrderedPartCollection<AbstractDocumentCodec> documentCodecs,
			IDocumentCacher documentCacher,
			Reference<WriteBatch> writeBatch,
			Reference<SnapshotReader> snapshot,
            TableStorage tableStorage, 
            IBufferPool bufferPool)
			: base(snapshot, bufferPool)
		{
			this.uuidGenerator = uuidGenerator;
			this.documentCodecs = documentCodecs;
			this.documentCacher = documentCacher;
			this.writeBatch = writeBatch;
			this.tableStorage = tableStorage;

			metadataIndex = tableStorage.Documents.GetIndex(Tables.Documents.Indices.Metadata);
		}
Exemple #42
0
        public void Initialize(IUuidGenerator uuidGenerator, OrderedPartCollection <AbstractDocumentCodec> documentCodecs, Action <string> putResourceMarker = null, Action <object, Exception> onErrorAction = null)
        {
            try
            {
                DocumentCodecs = documentCodecs;
                generator      = uuidGenerator;

                InstanceParameters instanceParameters = new TransactionalStorageConfigurator(configuration, this).ConfigureInstance(instance, path);

                if (configuration.RunInUnreliableYetFastModeThatIsNotSuitableForProduction)
                {
                    instanceParameters.Recovery = false;
                }

                log.Info(@"Esent Settings:
  MaxVerPages      = {0}
  CacheSizeMax     = {1}
  DatabasePageSize = {2}", instanceParameters.MaxVerPages, SystemParameters.CacheSizeMax,
                         SystemParameters.DatabasePageSize);

                Api.JetInit(ref instance);

                EnsureDatabaseIsCreatedAndAttachToDatabase();

                SetIdFromDb();

                tableColumnsCache.InitColumDictionaries(instance, database);

                if (putResourceMarker != null)
                {
                    putResourceMarker(path);
                }
            }
            catch (Exception e)
            {
                onErrorAction?.Invoke(this, e);
                Dispose();
                var fileAccessException = e as EsentFileAccessDeniedException;
                if (fileAccessException == null)
                {
                    throw new InvalidOperationException("Could not open transactional storage: " + database, e);
                }
                throw new InvalidOperationException("Could not write to location: " + path + ". Make sure you have read/write permissions for this path.", e);
            }
        }
		public DynamicCompilerBase(InMemoryRavenConfiguration configuration, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions, string name, string basePath)
		{
			this.name = name;
			this.extensions = extensions;
			if (configuration.RunInMemory == false)
			{
				this.basePath = Path.Combine(basePath, "TemporaryIndexDefinitionsAsSource");
				if (Directory.Exists(this.basePath) == false)
				{
					Directory.CreateDirectory(this.basePath);
				}
				else
				{
					MaybeCleanupDirectory(this.basePath);
				}
			}
			this.name = MonoHttpUtility.UrlEncode(name);
		}
Exemple #44
0
        public IEnumerable <RavenJObject> IndexEntires(
            string index,
            IndexQuery query,
            OrderedPartCollection <AbstractIndexQueryTrigger> indexQueryTriggers,
            Reference <int> totalResults)
        {
            Index value;

            if (indexes.TryGetValue(index, out value) == false)
            {
                log.Debug("Query on non existing index '{0}'", index);
                throw new InvalidOperationException("Index '" + index + "' does not exists");
            }

            var indexQueryOperation = new Index.IndexQueryOperation(value, query, null, new FieldsToFetch(null, AggregationOperation.None, null), indexQueryTriggers);

            return(indexQueryOperation.IndexEntries(totalResults));
        }
		public IndexDefinitionStorage(
            InMemoryRavenConfiguration configuration,
            ITransactionalStorage transactionalStorage,
            string path,
            OrderedPartCollection<AbstractDynamicCompilationExtension> extensions)
        {
            this.configuration = configuration;
	        this.transactionalStorage = transactionalStorage;
	        this.extensions = extensions; // this is used later in the ctor, so it must appears first
            this.path = Path.Combine(path, IndexDefDir);

            if (Directory.Exists(this.path) == false && configuration.RunInMemory == false)
                Directory.CreateDirectory(this.path);

            if (configuration.RunInMemory == false)
                ReadFromDisk();

            newDefinitionsThisSession.Clear();
        }
		public IndexDefinitionStorage(
			InMemoryRavenConfiguration configuration,
			ITransactionalStorage transactionalStorage,
			string path,
			IEnumerable<AbstractViewGenerator> compiledGenerators,
			OrderedPartCollection<AbstractDynamicCompilationExtension> extensions)
		{
			this.configuration = configuration;
			this.extensions = extensions; // this is used later in the ctor, so it must appears first
			this.path = Path.Combine(path, IndexDefDir);

			if (Directory.Exists(this.path) == false && configuration.RunInMemory == false)
				Directory.CreateDirectory(this.path);

			if (configuration.RunInMemory == false)
				ReadIndexesFromDisk();

			//compiled view generators always overwrite dynamic views
			ReadIndexesFromCatalog(compiledGenerators, transactionalStorage);
		}
Exemple #47
0
        public static Type Compile(string source, string name, string queryText, OrderedPartCollection <AbstractDynamicCompilationExtension> extensions, string basePath, InMemoryRavenConfiguration configuration)
        {
            source = source.Replace("AbstractIndexCreationTask.SpatialGenerate", "SpatialGenerate");             // HACK, should probably be on the client side
            var indexCacheDir = GetIndexCacheDir(configuration);

            if (Directory.Exists(indexCacheDir) == false)
            {
                Directory.CreateDirectory(indexCacheDir);
            }

            var indexFilePath = GetIndexFilePath(source, indexCacheDir);

            var shouldCachedIndexBeRecompiled = ShouldIndexCacheBeRecompiled(indexFilePath);

            if (shouldCachedIndexBeRecompiled == false)
            {
                // Look up the index in the in-memory cache.
                CacheEntry entry;
                if (cacheEntries.TryGetValue(source, out entry))
                {
                    Interlocked.Increment(ref entry.Usages);
                    return(entry.Type);
                }

                Type type;

                if (TryGetDiskCacheResult(source, name, configuration, indexFilePath, out type))
                {
                    return(type);
                }
            }

            var result = DoActualCompilation(source, name, queryText, extensions, basePath, indexFilePath);

            // ReSharper disable once RedundantArgumentName
            AddResultToCache(source, result, shouldUpdateIfExists: shouldCachedIndexBeRecompiled);

            return(result);
        }
Exemple #48
0
        private void StreamQueryResultsToClient(Stream stream, string[] files, OrderedPartCollection <AbstractFileReadTrigger> readTriggers)
        {
            using (var cts = new CancellationTokenSource())
                using (var timeout = cts.TimeoutAfter(FileSystemsLandlord.SystemConfiguration.DatabaseOperationTimeout))
                    using (var writer = new JsonTextWriter(new StreamWriter(stream)))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Results");
                        writer.WriteStartArray();

                        Storage.Batch(accessor =>
                        {
                            foreach (var filename in files)
                            {
                                var fileHeader = accessor.ReadFile(filename);

                                if (fileHeader == null)
                                {
                                    continue;
                                }

                                if (readTriggers.CanReadFile(fileHeader.FullPath, fileHeader.Metadata, ReadOperation.Load) == false)
                                {
                                    continue;
                                }

                                timeout.Delay();
                                var doc = RavenJObject.FromObject(fileHeader);
                                doc.WriteTo(writer);

                                writer.WriteRaw(Environment.NewLine);
                            }
                        });

                        writer.WriteEndArray();
                        writer.WriteEndObject();
                        writer.Flush();
                    }
        }
Exemple #49
0
        public IEnumerable <IndexQueryResult> Query(
            string index,
            IndexQuery query,
            Func <IndexQueryResult, bool> shouldIncludeInResults,
            FieldsToFetch fieldsToFetch,
            OrderedPartCollection <AbstractIndexQueryTrigger> indexQueryTriggers)
        {
            Index value;

            if (indexes.TryGetValue(index, out value) == false)
            {
                log.Debug("Query on non existing index '{0}'", index);
                throw new InvalidOperationException("Index '" + index + "' does not exists");
            }

            var indexQueryOperation = new Index.IndexQueryOperation(value, query, shouldIncludeInResults, fieldsToFetch, indexQueryTriggers);

            if (query.Query != null && query.Query.Contains(Constants.IntersectSeperator))
            {
                return(indexQueryOperation.IntersectionQuery());
            }
            return(indexQueryOperation.Query());
        }
		public static Type Compile(string source, string name, string queryText, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions, string basePath)
		{
			var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
			var assemblies = new HashSet<string>
			{
				typeof (SystemTime).Assembly.Location,
				typeof (AbstractViewGenerator).Assembly.Location,
				typeof (NameValueCollection).Assembly.Location,
				typeof (Enumerable).Assembly.Location,
				typeof (Binder).Assembly.Location,
				typeof (Field).Assembly.Location
			};
			foreach (var extension in extensions)
			{
				foreach (var assembly in extension.Value.GetAssembliesToReference())
				{
					assemblies.Add(assembly);
				}
			}
			var compilerParameters = new CompilerParameters
			{
				GenerateExecutable = false,
				GenerateInMemory = true,
				IncludeDebugInformation = false
			};
			if (basePath != null)
				compilerParameters.TempFiles = new TempFileCollection(basePath, false);

			foreach (var assembly in assemblies)
			{
				compilerParameters.ReferencedAssemblies.Add(assembly);
			}
			var compileAssemblyFromFile = provider.CompileAssemblyFromSource(compilerParameters, source);
			var results = compileAssemblyFromFile;

			if (results.Errors.HasErrors)
			{
				var sb = new StringBuilder()
					.AppendLine("Source code:")
					.AppendLine(queryText)
					.AppendLine();
				foreach (CompilerError error in results.Errors)
				{
					sb.AppendLine(error.ToString());
				}
				throw new InvalidOperationException(sb.ToString());
			}
			return results.CompiledAssembly.GetType(name);
		}
		public MappedResultsStorageAction(TableStorage storage, IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs)
		{
			this.storage = storage;
			this.generator = generator;
			this.documentCodecs = documentCodecs;
		}
		public bool Initialize(IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs)
		{
			DocumentCodecs = documentCodecs;
			uuidGenerator = generator;
			if (configuration.RunInMemory  == false && Directory.Exists(configuration.DataDirectory) == false)
				Directory.CreateDirectory(configuration.DataDirectory);

			persistenceSource = configuration.RunInMemory
						  ? (IPersistentSource)new MemoryPersistentSource()
						  : new FileBasedPersistentSource(configuration.DataDirectory, "Raven", configuration.TransactionMode == TransactionMode.Safe);

			tableStroage = new TableStorage(persistenceSource);

			idleTimer = new Timer(MaybeOnIdle, null, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));

			tableStroage.Initialze();

			if (persistenceSource.CreatedNew)
			{
				Id = Guid.NewGuid();
				Batch(accessor => tableStroage.Details.Put("id", Id.ToByteArray()));
			}
			else
			{
				using(tableStroage.BeginTransaction())
				{
				var readResult = tableStroage.Details.Read("id");
				Id = new Guid(readResult.Data());
			}
			}

			return persistenceSource.CreatedNew;
		}
Exemple #53
0
		private static IEnumerable<string> FilterOutTasks(OrderedPartCollection<IStartupTask> tasks)
		{
			return tasks.Select(task => task.GetType().FullName).Where(t => !TasksToFilterOut.Contains(t)).ToList();
		}
		public static Type Compile(string source, string name, string queryText, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions, string basePath, InMemoryRavenConfiguration configuration)
		{
			source = source.Replace("AbstractIndexCreationTask.SpatialGenerate", "SpatialGenerate"); // HACK, should probably be on the client side
			var indexCacheDir = GetIndexCacheDir(configuration);
			if (Directory.Exists(indexCacheDir) == false)
			{
				Directory.CreateDirectory(indexCacheDir);
			}

			var indexFilePath = GetIndexFilePath(source, indexCacheDir);

			var shouldCachedIndexBeRecompiled = ShouldIndexCacheBeRecompiled(indexFilePath);
			if (shouldCachedIndexBeRecompiled == false)
			{
				// Look up the index in the in-memory cache.
				CacheEntry entry;
				if (cacheEntries.TryGetValue(source, out entry))
				{
					Interlocked.Increment(ref entry.Usages);
					return entry.Type;
				}

				Type type;

				if (TryGetDiskCacheResult(source, name, configuration, indexFilePath, out type))
					return type;
			}

			var result = DoActualCompilation(source, name, queryText, extensions, basePath, indexFilePath);

			// ReSharper disable once RedundantArgumentName
			AddResultToCache(source, result, shouldUpdateIfExists: shouldCachedIndexBeRecompiled);

			return result;
		}
		private static Type DoActualCompilation(string source, string name, string queryText, OrderedPartCollection<AbstractDynamicCompilationExtension> extensions,
												string basePath, string indexFilePath)
		{
			var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
			var currentAssembly = typeof(QueryParsingUtils).Assembly;

			var assemblies = new HashSet<string>
			{
				typeof (SystemTime).Assembly.Location,
				typeof (AbstractViewGenerator).Assembly.Location,
				typeof (NameValueCollection).Assembly.Location,
				typeof (Enumerable).Assembly.Location,
				typeof (Binder).Assembly.Location,
				AssemblyHelper.GetExtractedAssemblyLocationFor(typeof(Field), currentAssembly),
			};
			foreach (var extension in extensions)
			{
				foreach (var assembly in extension.Value.GetAssembliesToReference())
				{
					assemblies.Add(assembly);
				}
			}
			var compilerParameters = new CompilerParameters
			{
				GenerateExecutable = false,
				GenerateInMemory = false,
				IncludeDebugInformation = Debugger.IsAttached,
				OutputAssembly = indexFilePath
			};
			if (basePath != null)
				compilerParameters.TempFiles = new TempFileCollection(basePath, false);

			foreach (var assembly in assemblies)
			{
				compilerParameters.ReferencedAssemblies.Add(assembly);
			}

			CompilerResults compileAssemblyFromFile;
			if (indexFilePath != null)
			{
				var sourceFileName = indexFilePath + ".cs";
				File.WriteAllText(sourceFileName, source);
				compileAssemblyFromFile = provider.CompileAssemblyFromFile(compilerParameters, sourceFileName);
			}
			else
			{
				compileAssemblyFromFile = provider.CompileAssemblyFromSource(compilerParameters, source);
			}
			var results = compileAssemblyFromFile;

			if (results.Errors.HasErrors)
			{
				var sb = new StringBuilder()
					.AppendLine("Compilation Errors:")
					.AppendLine();

				foreach (CompilerError error in results.Errors)
				{
					sb.AppendFormat("Line {0}, Position {1}: Error {2} - {3}\n", error.Line, error.Column, error.ErrorNumber, error.ErrorText);
				}

				sb.AppendLine();

				sb.AppendLine("Source code:")
				  .AppendLine(queryText)
				  .AppendLine();

				throw new InvalidOperationException(sb.ToString());
			}

			var asm = Assembly.Load(File.ReadAllBytes(indexFilePath)); // avoid locking the file

			// ReSharper disable once AssignNullToNotNullAttribute
			File.SetCreationTime(indexFilePath, DateTime.UtcNow);

			CodeVerifier.AssertNoSecurityCriticalCalls(asm);

			Type result = asm.GetType(name);
			if (result == null)
				throw new InvalidOperationException(
					"Could not get compiled index type. This probably means that there is something wrong with the assembly load context.");
			return result;
		}
Exemple #56
0
		public IEnumerable<RavenJObject> IndexEntires(
			string indexName,
			IndexQuery query,
			List<string> reduceKeys,
			OrderedPartCollection<AbstractIndexQueryTrigger> indexQueryTriggers,
			Reference<int> totalResults)
		{
			Index value = TryIndexByName(indexName);
			if (value == null)
			{
				log.Debug("Query on non existing index '{0}'", indexName);
				throw new InvalidOperationException("Index '" + indexName + "' does not exists");
			}

			var indexQueryOperation = new Index.IndexQueryOperation(value, query, null, new FieldsToFetch(null, false, null), indexQueryTriggers, reduceKeys);
			return indexQueryOperation.IndexEntries(totalResults);
		}
Exemple #57
0
		public IEnumerable<IndexQueryResult> Query(string index, IndexQuery query, Func<IndexQueryResult, bool> shouldIncludeInResults, FieldsToFetch fieldsToFetch, OrderedPartCollection<AbstractIndexQueryTrigger> indexQueryTriggers, CancellationToken token)
		{
			Index value = TryIndexByName(index);
			if (value == null)
			{
				log.Debug("Query on non existing index '{0}'", index);
				throw new InvalidOperationException("Index '" + index + "' does not exists");
			}

			if ((value.Priority.HasFlag(IndexingPriority.Idle) || value.Priority.HasFlag(IndexingPriority.Abandoned)) &&
				value.Priority.HasFlag(IndexingPriority.Forced) == false)
			{
				documentDatabase.TransactionalStorage.Batch(accessor =>
				{
					value.Priority = IndexingPriority.Normal;
					try
					{
						accessor.Indexing.SetIndexPriority(value.indexId, IndexingPriority.Normal);
					}
					catch (Exception e)
					{
						if (accessor.IsWriteConflict(e) == false)
							throw;

						// we explciitly ignore write conflicts here, it is okay if we got set twice (two concurrent queries, or setting while indexing).
					}
					documentDatabase.WorkContext.ShouldNotifyAboutWork(() => "Idle index queried");
					documentDatabase.Notifications.RaiseNotifications(new IndexChangeNotification()
					{
						Name = value.PublicName,
						Type = IndexChangeTypes.IndexPromotedFromIdle
					});
				});
			}

			var indexQueryOperation = new Index.IndexQueryOperation(value, query, shouldIncludeInResults, fieldsToFetch, indexQueryTriggers);
			if (query.Query != null && query.Query.Contains(Constants.IntersectSeparator))
				return indexQueryOperation.IntersectionQuery(token);


			return indexQueryOperation.Query(token);
		}
Exemple #58
0
		public Query GetDocumentQuery(string index, IndexQuery query, OrderedPartCollection<AbstractIndexQueryTrigger> indexQueryTriggers)
		{
			var value = TryIndexByName(index);
			if (value == null)
			{
				log.Debug("Query on non existing index {0}", index);
				throw new InvalidOperationException("Index '" + index + "' does not exists");
			}
			var fieldsToFetch = new FieldsToFetch(new string[0], false, null);
			return new Index.IndexQueryOperation(value, query, _ => false, fieldsToFetch, indexQueryTriggers).GetDocumentQuery();
		}
		public void Initialize(IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs)
		{
		    if (generator == null) throw new ArgumentNullException("generator");
		    if (documentCodecs == null) throw new ArgumentNullException("documentCodecs");

		    uuidGenerator = generator;
		    _documentCodecs = documentCodecs;

		    StorageEnvironmentOptions options = configuration.RunInMemory ?
				CreateMemoryStorageOptionsFromConfiguration(configuration) :
		        CreateStorageOptionsFromConfiguration(configuration);

		    tableStorage = new TableStorage(options, bufferPool);
			var schemaCreator = new SchemaCreator(configuration, tableStorage, Output, Log);
			schemaCreator.CreateSchema();
			schemaCreator.SetupDatabaseIdAndSchemaVersion();
			schemaCreator.UpdateSchemaIfNecessary();

		    SetupDatabaseId();
		}
Exemple #60
0
			public IndexQueryOperation(Index parent, IndexQuery indexQuery, Func<IndexQueryResult, bool> shouldIncludeInResults,
										FieldsToFetch fieldsToFetch, OrderedPartCollection<AbstractIndexQueryTrigger> indexQueryTriggers)
			{
				this.parent = parent;
				this.indexQuery = indexQuery;
				this.shouldIncludeInResults = shouldIncludeInResults;
				this.fieldsToFetch = fieldsToFetch;
				this.indexQueryTriggers = indexQueryTriggers;

				if (fieldsToFetch.IsDistinctQuery)
					alreadyReturned = new HashSet<RavenJObject>(new RavenJTokenEqualityComparer());
			}