Esempio n. 1
0
        async Task <Result <MdFileInfo> > FindFileInfoAsync(FileSystemPath path)
        {
            if (!_path.IsParentOf(path))
            {
                return(new InvalidOperation <MdFileInfo>("Incorrect path"));
            }
            if (!path.IsFile)
            {
                return(new InvalidOperation <MdFileInfo>("Path is not a file"));
            }

            // a stored MdFileInfo is a reference (i.e. the locator) to the actual Md holding data
            var(data, errors) = await _indexer.GetAllValuesAsync <MdFileInfo>(path.Path).ConfigureAwait(false);

            var list = data.ToList();

            if (list.Count == 0)
            {
                return(new KeyNotFound <MdFileInfo>(string.Join(',', errors)));
            }
            if (list.Count > 1)
            {
                return(new MultipleResults <MdFileInfo>($"Expected 1 result, found: {list.Count}."));
            }

            var md = await MdAccess.LocateAsync(list.Single().Locator); // finally locate the actual md, which our stored MdFileInfo uses to

            return(Result.OK(SetupFileInfo(path, md.Value)));           // Get / Set actions will be injected into the encapsulating MdFileInfo, which acts directly upon its Md
        }
Esempio n. 2
0
        public static async Task <TypeStoreInfo> GetOrAddTypeStoreAsync(IMd dbInfoMd, string dbId)
        {
            IMd typeStoreHead;
            var typeStoreResult = await dbInfoMd.GetValueAsync(TYPE_STORE_HEAD_KEY).ConfigureAwait(false);

            if (!typeStoreResult.HasValue)
            {
                typeStoreHead = await MdAccess.CreateAsync(0)
                                .ConfigureAwait(false);

                await dbInfoMd.AddAsync(TYPE_STORE_HEAD_KEY, new StoredValue(typeStoreHead.MdLocator))
                .ConfigureAwait(false);
            }
            else
            {
                var typeStoreHeadLocation = typeStoreResult.Value.Payload.Parse <MdLocator>();
                typeStoreHead = (await MdAccess.LocateAsync(typeStoreHeadLocation)
                                 .ConfigureAwait(false)).Value;
            }

            Task onHeadChange(MdLocator newLocation) => dbInfoMd.SetAsync(TYPE_STORE_HEAD_KEY, new StoredValue(newLocation));

            var dataTree = new DataTree(typeStoreHead, onHeadChange);

            return(new TypeStoreInfo(dataTree));
        }
Esempio n. 3
0
        async Task <Result <DirectoryInfo> > FindDirectoryInfoAsync(FileSystemPath path)
        {
            if (!_path.IsParentOf(path))
            {
                return(new InvalidOperation <DirectoryInfo>("Incorrect path"));
            }
            if (!path.IsDirectory)
            {
                return(new InvalidOperation <DirectoryInfo>("Path is not a directory"));
            }

            var(data, errors) = await _indexer.GetAllValuesAsync <DirectoryInfo>(path.Path).ConfigureAwait(false);

            var list = data.ToList();

            if (list.Count == 0)
            {
                return(new KeyNotFound <DirectoryInfo>(string.Join(',', errors)));
            }
            if (list.Count > 1)
            {
                return(new MultipleResults <DirectoryInfo>($"Expected 1 result, found: {list.Count}."));
            }

            var md = await MdAccess.LocateAsync(list.Single().Locator);

            return(Result.OK(SetupDirectoryInfo(path, md.Value)));
        }
Esempio n. 4
0
        public async Task <Result <DirectoryInfo> > DeleteDirectory(FileSystemPath path)
        {
            var findResult = await FindDirectoryInfoAsync(path);

            if (!findResult.HasValue)
            {
                return(findResult);
            }

            var mdResult = await MdAccess.LocateAsync(findResult.Value.Locator).ConfigureAwait(false);

            if (!mdResult.HasValue)
            {
                return(Result.Fail <DirectoryInfo>(mdResult.ErrorCode.Value, mdResult.ErrorMsg));
            }

            var deleteResult = await mdResult.Value.DeleteAsync(path.Path).ConfigureAwait(false);

            if (!deleteResult.HasValue)
            {
                return(Result.Fail <DirectoryInfo>(deleteResult.ErrorCode.Value, deleteResult.ErrorMsg));
            }

            return(Result.OK(findResult.Value));

            // TODO: re-index
            //_indexer.Delete(key);
        }
Esempio n. 5
0
        // Returns existing or creates new
        public async Task <Result <MdFileStream> > CreateFile(FileSystemPath path)
        {
            if (!_path.IsParentOf(path))
            {
                return(new InvalidOperation <MdFileStream>("Incorrect path"));
            }
            if (!path.IsFile)
            {
                return(new InvalidOperation <MdFileStream>("Path is not a file"));
            }

            var fileResult = await FindFileAsync(path);

            if (fileResult.HasValue)
            {
                return(Result.OK(fileResult.Value));
            }

            await AddOrLoad(FILE_INFO_KEY);

            var md = await MdAccess.CreateAsync(0);

            var info    = SetupFileInfo(path, md);
            var value   = new StoredValue(info);
            var pointer = await _dataTreeCache[FILE_INFO_KEY].AddAsync(path.Path, value);

            if (!pointer.HasValue)
            {
                return(Result.Fail <MdFileStream>(pointer.ErrorCode.Value, pointer.ErrorMsg));
            }
            await _indexer.IndexAsync(path.Path, pointer.Value);

            //ScheduleIndexing(path, pointer.Value);
            return(Result.OK(new MdFileStream(info)));
        }
        public static async Task <DataTree> CreateAsync(Func <MdLocator, Task> onHeadAddressChange)
        {
            var head = await MdAccess.CreateAsync(level : 0);

            var dataTree = new DataTree(head, onHeadAddressChange);

            return(dataTree);
        }
        public static async Task <Result <FileSystem> > CreateForSAFEApp(SafeApp.Session session, string appId, string root)
        {
            var manager = new MdHeadManager(session, appId, DataProtocol.DEFAULT_PROTOCOL);
            await manager.InitializeManager();

            MdAccess.SetCreator((level) => manager.CreateNewMdOps(level, DataProtocol.DEFAULT_PROTOCOL));
            MdAccess.SetLocator(manager.LocateMdOps);

            var dbResult = FileSystem.GetOrAdd(root);

            return(dbResult);
        }
Esempio n. 8
0
        public static async Task <Result <IIndexer> > GetOrAddAsync(string directoryPath)
        {
            var indexLocation = new MdLocator(Encoding.UTF8.GetBytes($"{directoryPath}_indexer"), DataProtocol.DEFAULT_PROTOCOL);
            var indexMd       = await MdAccess.LocateAsync(indexLocation).ConfigureAwait(false);

            if (!indexMd.HasValue)
            {
                indexMd = Result.OK(await MdAccess.CreateAsync(0));
            }
            var indexHead = new MdHead(indexMd.Value, directoryPath);
            var indexer   = await Indexer.GetOrAddAsync(indexHead);

            return(Result.OK((IIndexer)indexer));
        }
Esempio n. 9
0
        public static async Task <Result <Database> > CreateForApp(SafeApp.Session session, string appId, string databaseId)
        {
            var manager = new MdHeadManager(session, appId, DataProtocol.DEFAULT_PROTOCOL);
            await manager.InitializeManager();

            MdAccess.SetCreator((level) => manager.CreateNewMdOps(level, DataProtocol.DEFAULT_PROTOCOL));
            MdAccess.SetLocator(manager.LocateMdOps);

            var indexerDbId   = $"{databaseId}_indexer";
            var indexerMdHead = await manager.GetOrAddHeadAsync(indexerDbId);

            var indexer = await Indexer.GetOrAddAsync(indexerMdHead);

            var databaseMdHead = await manager.GetOrAddHeadAsync(databaseId);

            var dbResult = await Database.GetOrAddAsync(databaseMdHead, indexer);

            return(dbResult);
        }
Esempio n. 10
0
        public static async Task <Result <Directory> > GetOrAddAsync(string directoryPath, MdLocator dirLocator)
        {
            var indexer = await IndexerFactory.GetOrAddAsync(directoryPath);

            if (!indexer.HasValue)
            {
                return(Result.Fail <Directory>(indexer.ErrorCode.Value, indexer.ErrorMsg));
            }

            var dirMd = await MdAccess.LocateAsync(dirLocator).ConfigureAwait(false);

            if (!dirMd.HasValue)
            {
                return(Result.Fail <Directory>(dirMd.ErrorCode.Value, dirMd.ErrorMsg));
            }
            var dirHead   = new MdHead(dirMd.Value, directoryPath);
            var dirResult = await Directory.GetOrAddAsync(dirHead, indexer.Value);

            return(dirResult);
        }
Esempio n. 11
0
        async Task LoadStoreAsync(string type)
        {
            if (!_dataTreeAddresses.ContainsKey(type))
            {
                throw new InvalidOperationException($"Store does not exist! {type}");
            }
            // check if already loaded? or is this a refresh function also?

            Task onHeadChange(MdLocator newXOR) => UpdateTypeStores(type, newXOR);

            var headResult = await MdAccess.LocateAsync(_dataTreeAddresses[type]).ConfigureAwait(false);

            if (!headResult.HasValue)
            {
                throw new Exception($"Error code: {headResult.ErrorCode.Value}. {headResult.ErrorMsg}");
            }
            var dataTree = new DataTree(headResult.Value, onHeadChange);

            _dataTreeCache[type] = dataTree;
        }
Esempio n. 12
0
        public static async Task <Result <Directory> > GetOrAddAsync(string directoryPath)
        {
            var indexer = await IndexerFactory.GetOrAddAsync(directoryPath);

            if (!indexer.HasValue)
            {
                return(Result.Fail <Directory>(indexer.ErrorCode.Value, indexer.ErrorMsg));
            }

            var dirLocation = new MdLocator(Encoding.UTF8.GetBytes(directoryPath), DataProtocol.DEFAULT_PROTOCOL);
            var dirMd       = await MdAccess.LocateAsync(dirLocation).ConfigureAwait(false);

            if (!dirMd.HasValue)
            {
                dirMd = Result.OK(await MdAccess.CreateAsync(0));
            }
            var dirHead   = new MdHead(dirMd.Value, directoryPath);
            var dirResult = await Directory.GetOrAddAsync(dirHead, indexer.Value);

            return(dirResult);
        }
Esempio n. 13
0
        public async Task <IDatabase> GetOrAddDataBaseAsync(string dbName)
        {
            var indexLocation = new MdLocator(System.Text.Encoding.UTF8.GetBytes($"{dbName}_indexer"), DataProtocol.DEFAULT_PROTOCOL, null, null);
            var indexMd       = await MdAccess.LocateAsync(indexLocation).ConfigureAwait(false);

            if (!indexMd.HasValue)
            {
                throw new System.Exception(indexMd.ErrorMsg);
            }
            var indexHead = new MdHead(indexMd.Value, dbName);
            var indexer   = await Indexer.GetOrAddAsync(indexHead);

            var dbLocation = new MdLocator(System.Text.Encoding.UTF8.GetBytes(dbName), DataProtocol.DEFAULT_PROTOCOL, null, null);
            var dbMd       = await MdAccess.LocateAsync(dbLocation).ConfigureAwait(false);

            if (!dbMd.HasValue)
            {
                throw new System.Exception(dbMd.ErrorMsg);
            }
            var dbHead   = new MdHead(dbMd.Value, dbName);
            var dbResult = await Database.GetOrAddAsync(dbHead, indexer);

            return(dbResult.Value);
        }
Esempio n. 14
0
 public InMemClient()
 {
     MdAccess.UseInMemoryDb();
 }
Esempio n. 15
0
 public LocalDbClient()
 {
     MdAccess.SetCreator((level) => Task.FromResult(LocalMd.Create(level)));
     MdAccess.SetLocator((locator) => Task.FromResult(Result.OK(LocalMd.Locate(locator))));
 }
Esempio n. 16
0
 public static Result <FileSystem> CreateInMemory(string root = "/")
 {
     MdAccess.UseInMemoryDb();
     return(FileSystem.GetOrAdd(root));
 }