Beispiel #1
0
        public Receipt Store <T>(WarehouseKey key, IEnumerable <T> data, IEnumerable <LoadingDockPolicy> loadingDockPolicies)
        {
            ThrowIfNotInitialized();

            var uuid = Guid.NewGuid();

            ConcurrentBag <LoadingDockPolicy> enforcedPolicies = new ConcurrentBag <LoadingDockPolicy>();

            // resolve the appropriate store, based on the policy
            Parallel.ForEach(ResolveShelves <T>(loadingDockPolicies), (shelf) =>
            {
                shelf.Store(key, data, enforcedPolicies);
            });

            // the receipt is largely what was passed in when it was stored
            var receipt = new Receipt(enforcedPolicies.Any())
            {
                UUID  = uuid,
                Key   = key.Id,
                Scope = key.Scope,
                // add the policies that were upheld during the store, this is necessary,
                // because this warehouse might not be able to satisfy all of the policies
                Policies       = enforcedPolicies.Distinct().ToList(),
                SHA256Checksum = CalculateChecksum <T>(data)
            };

            SessionReceipts.Add(receipt);

            return(receipt);
        }
Beispiel #2
0
 public void Store(WarehouseKey key, IEnumerable <string> payload, IProducerConsumerCollection <LoadingDockPolicy> enforcedPolicies)
 {
     Records.AddOrUpdate(new MemoryShelfKey(key.Scope, key.Id), payload.ToList(), (k, a) => payload.ToList());
     foreach (var pol in SupportedPolicies)
     {
         enforcedPolicies.TryAdd(pol);
     }
 }
Beispiel #3
0
        public void Append <T>(WarehouseKey key, IEnumerable <T> data, IEnumerable <LoadingDockPolicy> loadingDockPolicies)
        {
            ThrowIfNotInitialized();

            Parallel.ForEach(ResolveShelves <T>(loadingDockPolicies), (shelf) =>
            {
                shelf.Append(key, data);
            });
        }
Beispiel #4
0
        public ShelfManifest GetManifest(WarehouseKey key)
        {
            if (CanRetrieve(key))
            {
                // the only way to get metadata from a memory shelf is to actually just retrieve it.
                // TODO: store metadata separately for items as stord
                return(new ShelfManifest(SupportedPolicies, CalculateSize(Retrieve(key))));
            }

            return(null);
        }
Beispiel #5
0
 public IEnumerable <T> Retrieve <T>(WarehouseKey key)
 {
     ThrowIfNotInitialized();
     return
         (Shelves
          // We can't just use the OfType, because we ned to handle inheritance
          // buy we do want this for speed.
          .OfType <IShelf <T> >()
          .FirstOrDefault(shelf => shelf.CanRetrieve(key))
          .Retrieve(key)
          ?? Enumerable.Empty <T>());
 }
Beispiel #6
0
        public WarehouseKeyManifest GetManifest(WarehouseKey key)
        {
            // right now, we just return the data that was sent when it was created
            var policies = SessionReceipts.FirstOrDefault(sr => sr.Key == key.Id)?.Policies;

            // if there aren't any receipts for this, the warehouse has no idea where they're stored.
            // TODO: ideally, the warehouse will eventually be able to resolve the receipts from their state
            if (policies == null)
            {
                return(new WarehouseKeyManifest());
            }

            return(new WarehouseKeyManifest
            {
                // TODO: where do we get the type from? is it passed in? Why should it matter here?
                StorageShelvesManifests = ResolveShelves <object>(policies).Where(s => s.CanRetrieve(key)).Select(shelf => shelf.GetManifest(key)).ToList(),
                StoragePolicies = policies
            });
        }
Beispiel #7
0
 public void Append(WarehouseKey key, IEnumerable <string> additionalPayload)
 {
     Records.AddOrUpdate(new MemoryShelfKey(key.Scope, key.Id), additionalPayload.ToList(), (k, a) => a.Concat(additionalPayload).ToList());
 }
Beispiel #8
0
 public IEnumerable <string> Retrieve(WarehouseKey key)
 {
     return(Records.GetValueOrDefault(new MemoryShelfKey(key.Scope, key.Id), new List <string>()));
 }
Beispiel #9
0
 public bool CanRetrieve(WarehouseKey key)
 {
     return(Records.Keys.Contains(new MemoryShelfKey(key.Scope, key.Id)));
 }