public void RemoveAllBefore(string name, Etag etag)
        {
            var listsByName       = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var nameKey      = CreateKey(name);
            var nameKeySlice = (Slice)nameKey;

            var iterator = listsByName.MultiRead(Snapshot, nameKeySlice);

            try
            {
                if (!iterator.Seek(Slice.BeforeAllKeys))
                {
                    return;
                }
                bool skipMoveNext;
                do
                {
                    skipMoveNext = false;
                    var currentEtag = Etag.Parse(iterator.CurrentKey.ToString());

                    if (currentEtag.CompareTo(etag) > 0)
                    {
                        break;
                    }

                    ushort version;
                    var    value = LoadJson(tableStorage.Lists, iterator.CurrentKey, writeBatch.Value, out version);

                    var key       = value.Value <string>("key");
                    var etagSlice = (Slice)currentEtag.ToString();

                    tableStorage.Lists.Delete(writeBatch.Value, etagSlice);
                    listsByName.MultiDelete(writeBatch.Value, nameKeySlice, etagSlice);
                    listsByNameAndKey.Delete(writeBatch.Value, (Slice)AppendToKey(nameKey, key));

                    if (generalStorageActions.MaybePulseTransaction(iterator))
                    {
                        iterator = listsByName.MultiRead(Snapshot, nameKeySlice);
                        if (!iterator.Seek(Slice.BeforeAllKeys))
                        {
                            break;
                        }
                        skipMoveNext = true;
                    }
                } while (skipMoveNext || iterator.MoveNext());
            }
            finally
            {
                if (iterator != null)
                {
                    iterator.Dispose();
                }
            }
        }
Example #2
0
        public void RemoveAllOlderThan(string name, DateTime dateTime)
        {
            var listsByName       = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var nameKey      = CreateKey(name);
            var nameKeySlice = (Slice)nameKey;

            using (var iterator = listsByName.MultiRead(Snapshot, nameKeySlice))
            {
                if (!iterator.Seek(Slice.BeforeAllKeys))
                {
                    return;
                }

                do
                {
                    ushort version;
                    var    value     = LoadJson(tableStorage.Lists, iterator.CurrentKey, writeBatch.Value, out version);
                    var    createdAt = value.Value <DateTime>("createdAt");

                    if (createdAt > dateTime)
                    {
                        break;
                    }

                    var key       = value.Value <string>("key");
                    var etag      = Etag.Parse(iterator.CurrentKey.ToString());
                    var etagSlice = (Slice)etag.ToString();

                    tableStorage.Lists.Delete(writeBatch.Value, etagSlice);
                    listsByName.MultiDelete(writeBatch.Value, nameKeySlice, etagSlice);
                    listsByNameAndKey.Delete(writeBatch.Value, AppendToKey(nameKey, key));

                    generalStorageActions.MaybePulseTransaction();
                }while (iterator.MoveNext());
            }
        }
        private void RemoveDocumentReference(Func <IIterator> createIterator, bool tryPulseTransaction, CancellationToken token)
        {
            var iterator = createIterator();

            try
            {
                if (iterator.Seek(Slice.BeforeAllKeys) == false)
                {
                    return;
                }

                var documentReferencesByKey        = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByKey);
                var documentReferencesByRef        = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByRef);
                var documentReferencesByView       = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByView);
                var documentReferencesByViewAndKey = tableStorage.DocumentReferences.GetIndex(Tables.DocumentReferences.Indices.ByViewAndKey);

                bool skipMoveNext;
                do
                {
                    skipMoveNext = false;
                    // TODO: Check if we can avoid the clone.
                    var id = iterator.CurrentKey.Clone();

                    ushort version;
                    var    value = LoadStruct(tableStorage.DocumentReferences, id, writeBatch.Value, out version);
                    if (value == null)
                    {
                        continue;
                    }
                    var reference = value.ReadString(DocumentReferencesFields.Reference);
                    var view      = value.ReadInt(DocumentReferencesFields.IndexId).ToString(CultureInfo.InvariantCulture);
                    var key       = value.ReadString(DocumentReferencesFields.Key);

                    var viewKey = CreateKey(view);

                    tableStorage.DocumentReferences.Delete(writeBatch.Value, id);
                    documentReferencesByKey.MultiDelete(writeBatch.Value, (Slice)CreateKey(key), id);
                    documentReferencesByRef.MultiDelete(writeBatch.Value, (Slice)CreateKey(reference), id);
                    documentReferencesByView.MultiDelete(writeBatch.Value, (Slice)viewKey, id);
                    documentReferencesByViewAndKey.MultiDelete(writeBatch.Value, (Slice)AppendToKey(viewKey, key), id);

                    if (tryPulseTransaction)
                    {
                        if (generalStorageActions.MaybePulseTransaction(iterator))
                        {
                            iterator = createIterator();
                            if (iterator.Seek(Slice.BeforeAllKeys) == false)
                            {
                                break;
                            }
                            skipMoveNext = true;
                        }
                    }
                }while ((skipMoveNext || iterator.MoveNext()) && token.IsCancellationRequested == false);
            }
            finally
            {
                if (iterator != null)
                {
                    iterator.Dispose();
                }
            }
        }