Esempio n. 1
0
        public BasePartition(LeoEngineConfiguration engineConfig, long partitionId, ItemConfiguration config, Func <Task <IEncryptor> > encryptorFactory, IMemoryCache cache, string cachePrefix)
        {
            _store        = new SecureStore(engineConfig.BaseStore, engineConfig.BackupQueue, engineConfig.IndexQueue, engineConfig.Compressor);
            _partitionId  = partitionId;
            _config       = config;
            _engineConfig = engineConfig;

            _options = SecureStoreOptions.KeepDeletes;
            if (config.DoBackup)
            {
                _options = _options | SecureStoreOptions.Backup;
            }
            if (config.Indexer != null)
            {
                _options = _options | SecureStoreOptions.Index;
            }
            if (config.DoCompress)
            {
                _options = _options | SecureStoreOptions.Compress;
            }

            _encryptor = new Lazy <Task <IEncryptor> >(async() => config.DoEncrypt ? await encryptorFactory().ConfigureAwait(false) : null, true);

            string container = partitionId.ToString(CultureInfo.InvariantCulture);

            _luceneIndex = new Lazy <LuceneIndex>(() => engineConfig.IndexStore == null ? null : new LuceneIndex(new SecureStore(engineConfig.IndexStore, null, null, engineConfig.Compressor), container, config.BasePath, _encryptor, cache, $"{cachePrefix}::{partitionId}"), true);
        }
        public SecureStoreDirectory(Directory cache, ISecureStore store, string container, string basePath, Lazy<Task<IEncryptor>> encryptor)
        {
            _container = container;
            _basePath = basePath ?? string.Empty;
            _cache = cache;
            _store = store;
            _encryptor = encryptor ?? new Lazy<Task<IEncryptor>>(() => Task.FromResult((IEncryptor)null));

            _options = SecureStoreOptions.None;
            if (_store.CanCompress)
            {
                _options = _options | SecureStoreOptions.Compress;
            }

            store.CreateContainerIfNotExists(container).WaitAndWrap();
        }
Esempio n. 3
0
        public async Task Delete(StoreLocation location, UpdateAuditInfo audit, SecureStoreOptions options = SecureStoreOptions.All)
        {
            var metadata = await _store.GetMetadata(location);

            if (metadata == null)
            {
                return;
            }

            if (options.HasFlag(SecureStoreOptions.KeepDeletes))
            {
                await _store.SoftDelete(location, audit);
            }
            else
            {
                await _store.PermanentDelete(location);
            }

            // The rest of the tasks are done asyncly
            var tasks = new List <Task>();

            if (options.HasFlag(SecureStoreOptions.Backup))
            {
                if (_backupQueue == null)
                {
                    throw new ArgumentException("Backup option should not be used if no backup queue has been defined", "options");
                }

                tasks.Add(_backupQueue.SendMessage(GetMessageDetails(location, metadata)));
            }

            if (options.HasFlag(SecureStoreOptions.Index))
            {
                if (_indexQueue == null)
                {
                    throw new ArgumentException("Index option should not be used if no index queue has been defined", "options");
                }

                tasks.Add(_indexQueue.SendMessage(GetMessageDetails(location, metadata)));
            }

            if (tasks.Count > 0)
            {
                await Task.WhenAll(tasks);
            }
        }
Esempio n. 4
0
        public SecureStoreDirectory(ISecureStore store, string container, string basePath, Lazy <Task <IEncryptor> > encryptor, IMemoryCache memoryCache = null, string cachePrefix = null)
        {
            _container   = container;
            _memoryCache = memoryCache ?? new MemoryCache(new MemoryCacheOptions());
            _cachePrefix = cachePrefix ?? "Lucene";
            _basePath    = basePath ?? string.Empty;
            _store       = store;
            _encryptor   = encryptor ?? new Lazy <Task <IEncryptor> >(() => Task.FromResult((IEncryptor)null));

            _lockFactory = new SecureLockFactory(store, GetLocation);

            _options = SecureStoreOptions.None;
            if (_store.CanCompress)
            {
                _options = _options | SecureStoreOptions.Compress;
            }

            SafeTask.SafeWait(() => store.CreateContainerIfNotExists(container));
        }
Esempio n. 5
0
        public BasePartition(LeoEngineConfiguration engineConfig, long partitionId, ItemConfiguration config, Func <Task <IEncryptor> > encryptorFactory)
        {
            _store        = new SecureStore(engineConfig.BaseStore, engineConfig.BackupQueue, engineConfig.IndexQueue, engineConfig.SecondaryIndexQueue, engineConfig.Compressor);
            _partitionId  = partitionId;
            _config       = config;
            _engineConfig = engineConfig;

            _options = SecureStoreOptions.KeepDeletes;
            if (config.DoBackup)
            {
                _options = _options | SecureStoreOptions.Backup;
            }
            if (config.Indexer != null)
            {
                _options = _options | SecureStoreOptions.Index;
            }
            if (config.DoCompress)
            {
                _options = _options | SecureStoreOptions.Compress;
            }

            _encryptor = new Lazy <Task <IEncryptor> >(async() => config.DoEncrypt ? await encryptorFactory() : null, true);
        }
Esempio n. 6
0
        public async Task <Metadata> SaveMetadata(StoreLocation location, Metadata metadata, SecureStoreOptions options = SecureStoreOptions.All)
        {
            var m = await _store.SaveMetadata(location, metadata);

            /****************************************************
             *  POST SAVE TASKS (BACKUP, INDEX)
             * ***************************************************/
            // The rest of the tasks are done asyncly
            var tasks = new List <Task>();

            if (options.HasFlag(SecureStoreOptions.Backup))
            {
                if (_backupQueue == null)
                {
                    throw new ArgumentException("Backup option should not be used if no backup queue has been defined", "options");
                }

                tasks.Add(_backupQueue.SendMessage(GetMessageDetails(location, metadata)));
            }

            if (options.HasFlag(SecureStoreOptions.Index) && !(metadata?.DoNotIndex ?? false))
            {
                if (_indexQueue == null)
                {
                    throw new ArgumentException("Index option should not be used if no index queue has been defined", "options");
                }

                var queue = _secondaryIndexQueue != null && (metadata?.UseSecondaryIndexQueue ?? false) ? _secondaryIndexQueue : _indexQueue;
                tasks.Add(queue.SendMessage(GetMessageDetails(location, metadata)));
            }

            if (tasks.Count > 0)
            {
                await Task.WhenAll(tasks);
            }

            return(m);
        }
Esempio n. 7
0
        public async Task <Metadata> SaveData(StoreLocation location, Metadata mdata, UpdateAuditInfo audit, Func <IWriteAsyncStream, Task> savingFunc, CancellationToken token, IEncryptor encryptor = null, SecureStoreOptions options = SecureStoreOptions.All)
        {
            LeoTrace.WriteLine("Saving: " + location.Container + ", " + location.BasePath + ", " + (location.Id.HasValue ? location.Id.Value.ToString() : "null"));
            var metadata = new Metadata(mdata);

            /****************************************************
             *  SETUP METADATA
             * ***************************************************/
            if (encryptor != null)
            {
                metadata[MetadataConstants.EncryptionMetadataKey] = encryptor.Algorithm;
            }
            else
            {
                metadata.Remove(MetadataConstants.EncryptionMetadataKey);
            }

            if (options.HasFlag(SecureStoreOptions.Compress))
            {
                if (_compressor == null)
                {
                    throw new ArgumentException("Compression option should not be used if no compressor has been implemented", "options");
                }
                metadata[MetadataConstants.CompressionMetadataKey] = _compressor.Algorithm;
            }
            else
            {
                metadata.Remove(MetadataConstants.CompressionMetadataKey);
            }

            /****************************************************
             *  PREPARE THE SAVE STREAM
             * ***************************************************/
            var m = await _store.SaveData(location, metadata, audit, async (stream) =>
            {
                LengthCounterStream counter = null;
                stream = stream.AddTransformer(s =>
                {
                    // Encrypt just before writing to the stream (if we need)
                    if (encryptor != null)
                    {
                        s = encryptor.Encrypt(s, false);
                    }

                    // Compression comes right before encryption
                    if (options.HasFlag(SecureStoreOptions.Compress))
                    {
                        s = _compressor.CompressWriteStream(s);
                    }

                    // Always place the length counter stream
                    counter = new LengthCounterStream(s);
                    return(counter);
                });

                await savingFunc(stream);
                await stream.Complete(token);
                return(counter.Length);
            }, token);

            /****************************************************
             *  POST SAVE TASKS (BACKUP, INDEX)
             * ***************************************************/
            // The rest of the tasks are done asyncly
            var tasks = new List <Task>();

            if (options.HasFlag(SecureStoreOptions.Backup))
            {
                if (_backupQueue == null)
                {
                    throw new ArgumentException("Backup option should not be used if no backup queue has been defined", "options");
                }

                tasks.Add(_backupQueue.SendMessage(GetMessageDetails(location, metadata)));
            }

            if (options.HasFlag(SecureStoreOptions.Index))
            {
                tasks.Add(ForceIndex(location, mdata));
            }

            if (tasks.Count > 0)
            {
                await Task.WhenAll(tasks);
            }

            return(m);
        }
Esempio n. 8
0
        public async Task <Metadata> SaveObject <T>(StoreLocation location, ObjectWithMetadata <T> obj, UpdateAuditInfo audit, IEncryptor encryptor = null, SecureStoreOptions options = SecureStoreOptions.All)
            where T : ObjectWithAuditInfo
        {
            // Serialise to json as more cross platform
            obj.Data.HideAuditInfo = true;
            var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj.Data));

            obj.Data.HideAuditInfo = false;
            obj.Metadata[MetadataConstants.TypeMetadataKey] = typeof(T).FullName;

            var ct       = CancellationToken.None;
            var metadata = await SaveData(location, obj.Metadata, audit, (s) => s.WriteAsync(data, 0, data.Length, ct), ct, encryptor, options);

            obj.Data.Audit = metadata.Audit;
            return(metadata);
        }