Exemple #1
0
        private List <IBlock> FindBlocks(uint recordId)
        {
            var blocks  = new List <IBlock>();
            var success = false;

            try
            {
                var currentBlockId = recordId;

                do
                {
                    // Grab next block
                    var block = _storage.Find(currentBlockId);
                    if (null == block)
                    {
                        // Special case: if block #0 never created, then attempt to create it
                        if (currentBlockId == 0)
                        {
                            block = _storage.CreateNew();
                        }
                        else
                        {
                            throw new Exception("Block not found by id: " + currentBlockId);
                        }
                    }

                    blocks.Add(block);

                    // If this is a deleted block then ignore the f**k out of it
                    if (1L == block.GetHeader(KIsDeleted))
                    {
                        throw new InvalidDataException("Block not found: " + currentBlockId);
                    }

                    // Move next
                    currentBlockId = (uint)block.GetHeader(KNextBlockId);
                } while (currentBlockId != 0);

                success = true;
                return(blocks);
            }
            finally
            {
                // Incase shit happens, dispose all fetched blocks
                if (false == success)
                {
                    foreach (var block in blocks)
                    {
                        block.Dispose();
                    }
                }
            }
        }
 /// <summary>
 /// Creates a new record with empty data.
 /// </summary>
 public uint Create()
 {
     using (IBlock firstBlock = blockStorage.CreateNew()) {
         return(firstBlock.Id);
     }
 }