Ejemplo n.º 1
0
        public async Task <ulong?> ProcessSyncPacket(SyncPacket syncPacket)
        {
            if (_cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            switch (syncPacket.PacketCase)
            {
            case Items:
                _log.Info($"Received batch: '{syncPacket.Items.Batch.Count}'");
                ulong?lastPos = null;
                foreach (var responseItem in syncPacket.Items.Batch)
                {
                    if (_cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    var pos = await SyncHandler(responseItem.LogEvent);

                    lastPos = pos;
                }
                return(lastPos);

            case Item:
                //                            Console.WriteLine($"Received: '{syncPacket}'");
                return(await SyncHandler(syncPacket.Item.LogEvent));

            case SkipPos:
                await _lmdb.WriteAsync(txn =>
                {
                    _replicationTable.SetLastPos(txn, _targetReplicaId, syncPacket.SkipPos.LastPos);
                    _updateClock(txn, _targetReplicaId, syncPacket.SkipPos.LastPos);
                }, false, true);

                return(syncPacket.SkipPos.LastPos);

            case SyncFrom:
            case None:
            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 2
0
        private HashSet <string> TryAdd(IEnumerable <string> keys, Action <string, Stream> streamWriter, bool allowOverride,
                                        DateTimeOffset expiry, AvailabilityLevel requiredAvailabilityLevel)
        {
            // TODO: Add reactions to other AvailabilityLevels
            // TODO: Deal with expiration
            var batch = keys.Select(k => (k, new MemoryStream())).ToArray();

            foreach (var ks in batch)
            {
                streamWriter(ks.Item1, ks.Item2);
                ks.Item2.Position = 0;
            }

            var preparedBatch = batch.Select(ks => (new TableKey(ks.Item1), new TableValue(ks.Item2))).ToArray();

            var ret = _lmdb.WriteAsync(txn => allowOverride ? txn.AddOrUpdateBatch(_kvTable, preparedBatch) : txn.AddBatch(_kvTable, preparedBatch), false).Result;

            return(new HashSet <string>(ret.Where(kb => kb.Item2).Select(kb => kb.Item1.ToString()))); // TODO: Reconfirm if successful or failed keys should be returned
        }
Ejemplo n.º 3
0
        private bool CheckAndRemoveIfExpired(KvKey key, VectorClock currentClock, KvMetadata metadataToCheck, WriteTransaction txn = null)
        {
            if (metadataToCheck == null || metadataToCheck.Status == Active && currentClock.TicksOffsetUtc < metadataToCheck.Expiry.TicksOffsetUtc)
            {
                return(true);
            }

            if (metadataToCheck.Status == Active && currentClock.TicksOffsetUtc < metadataToCheck.Expiry.TicksOffsetUtc)
            {
                if (txn != null)
                {
                    RemoveExpired(txn, ToTableKey(key), metadataToCheck.Expiry);
                }
                else
                {
                    _lmdb.WriteAsync(tx => RemoveExpired(tx, ToTableKey(key), metadataToCheck.Expiry), false);
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
 public static Task <bool> AddOrUpdate(this LightningPersistence lmdb, Table table, TableKey key, TableValue value, bool requiresIsolation = false) =>
 lmdb.WriteAsync(txn => txn.AddOrUpdate(table, key, value), requiresIsolation);