Naanou.ResourceExchange
Class BlockStorage

Manages the partial uncomplete download blocks

Field Summary
System.Collections.ArrayList blocks
         Blocks that have been finished and are stored
long diskUsageKB
         Total disk usage
bool gcDue
bool inited
System.Collections.ArrayList inUseBlocks
         Blocks that are currently being written too
System.Threading.ReaderWriterLock rwl

Constructor Summary
BlockStorage()
        Initializes a new instance of the BlockStorage class.

Property Summary
long DiskUsage
         Gets the disk usage (in KB) of blocks
bool GcDue
         Gets whether a GC is due
bool Oversize
         Is storage oversize?
double Utilisation
         Gets utilisation of block storage
string UtilStats
         Gets a short string indicating utilisation details

Method Summary
Naanou.ResourceExchange.Block Create(long start, long end, long length, string contentHash)
         Creates a new block for a resource
void DecrementDiskUsage(long amountKb)
         Decrements the disk usage counter
void Delete(Naanou.ResourceExchange.Block block)
         Deletes a block from disk and internal db
void Delete(Naanou.ResourceExchange.Block block, bool force)
         Deletes a block from memory and disk
void DeleteFile(System.IO.FileInfo file)
         Deletes a file from disk
void Gc(bool startupGc)
         Garbage collect block storage
long GcDiskOrphans(Naanou.Common.PriorityQueue list, bool startupGc)
         Find blocks on disk that we don't know about. If startupGc is true, will also delete any incomplete blocks found
void GcMemoryOrphans(bool startupGc)
         Remove all orphans in memory.
void GcStale(Naanou.Common.PriorityQueue pq)
         Goes thru the sorted list {tick => block} and deletes blocks that are too old and incomplete
void GcUnpopular()
         Sorts blocks by accessed frequency, and removes least accessed blocks first
string GetBlockFilename(string contentHash, string range)
         Gets the absolute filename for a block
void IncrementDiskUsage(long amountKb)
         Increments the disk usage counter
void Init()
void Load()
         Loads block info from disk, forgetting current state. This is called on program startup
void PromoteBlock(Naanou.ResourceExchange.Block block)
         Promotes a block from `in use' to idle. Do not call this method directly, use Naanou.ResourceExchange.Block.Retire.
Naanou.ResourceExchange.Block[] Query(string contentHash)
         Find all complete blocks that are part of a certain file
long Query(string contentHash, System.Collections.ArrayList output, System.Collections.ArrayList source)
Naanou.ResourceExchange.Block[] QueryAny(string contentHash)
         Finds all blocks complete or not
void Save()
         Saves current block info to disk

Methods inherited from class System.Object
Equals, Finalize, GetHashCode, GetType, MemberwiseClone, ToString


Field Detail

blocks

private System.Collections.ArrayList blocks

Blocks that have been finished and are stored


diskUsageKB

private long diskUsageKB

Total disk usage


gcDue

private bool gcDue


inited

private bool inited


inUseBlocks

private System.Collections.ArrayList inUseBlocks

Blocks that are currently being written too


rwl

private System.Threading.ReaderWriterLock rwl

Constructor Detail

BlockStorage

public BlockStorage()

Initializes a new instance of the BlockStorage class.

Property Detail

DiskUsage

internal long DiskUsage

Gets the disk usage (in KB) of blocks


GcDue

public bool GcDue

Gets whether a GC is due


Oversize

internal bool Oversize

Is storage oversize?


Utilisation

internal double Utilisation

Gets utilisation of block storage


UtilStats

internal string UtilStats

Gets a short string indicating utilisation details

Method Detail

Create

internal Naanou.ResourceExchange.Block Create(long start,
                                              long end,
                                              long length,
                                              string contentHash)

Creates a new block for a resource

Parameters:
contentHash - Hash of contents
end - Ending byte number (0-based)
length - Total length of resource
start - Starting byte number (0-based)
Returns:
New block

DecrementDiskUsage

private void DecrementDiskUsage(long amountKb)

Decrements the disk usage counter

Parameters:
amountKb - Amount in kilobytes to decrement by

Delete

internal void Delete(Naanou.ResourceExchange.Block block)

Deletes a block from disk and internal db

Parameters:
block - Block to delete
Throws:
System.ArgumentException - If block is marked Naanou.ResourceExchange.Block.RequiredLocally

Delete

internal void Delete(Naanou.ResourceExchange.Block block,
                     bool force)

Deletes a block from memory and disk

Parameters:
block - Block to delete
force - If true, block will be removed even if block's RequiredLocally flag is set

DeleteFile

private void DeleteFile(System.IO.FileInfo file)

Deletes a file from disk

Parameters:
file -

Gc

public void Gc(bool startupGc)

Garbage collect block storage

Uses four methods to clean up on disk and memory usage. The fourth method is only performed when the first three fail to clean up enough space. Method - Description

  • 1. Disk orphans - Finds blocks stored on disk that are no longer being kept track of (i.e. not in memory)
  • 2. Memory orphans - Finds blocks that we are keeping track of in memory that no longer have a data on disk
  • 3. Stale - Finds blocks on disk that are stale. Stale blocks are ones that are incomplete, and haven't been written to for a while (based on Naanou.ResourceExchange.Config.BlockStorageTimeout)
  • 4. LRU - Deletes blocks in Least Recently Used order until enough space is cleared

  • GcDiskOrphans

    private long GcDiskOrphans(Naanou.Common.PriorityQueue list,
                               bool startupGc)

    Find blocks on disk that we don't know about. If startupGc is true, will also delete any incomplete blocks found

    Parameters:
    list - Returns a priority queue of blocks sorted in order of last write
    startupGc - Is Gc running due to a program startup?
    Returns:
    Disk usage of blocks in KB

    GcMemoryOrphans

    private void GcMemoryOrphans(bool startupGc)

    Remove all orphans in memory.


    GcStale

    private void GcStale(Naanou.Common.PriorityQueue pq)

    Goes thru the sorted list {tick => block} and deletes blocks that are too old and incomplete

    Parameters:
    pq - Priority queue in last write ticks order

    GcUnpopular

    private void GcUnpopular()

    Sorts blocks by accessed frequency, and removes least accessed blocks first


    GetBlockFilename

    private string GetBlockFilename(string contentHash,
                                    string range)

    Gets the absolute filename for a block

    Parameters:
    contentHash - Hash
    range - Range
    Returns:
    Filename

    IncrementDiskUsage

    private void IncrementDiskUsage(long amountKb)

    Increments the disk usage counter

    Parameters:
    amountKb - Amount in kilobytes to increment by

    Init

    internal void Init()


    Load

    internal void Load()

    Loads block info from disk, forgetting current state. This is called on program startup


    PromoteBlock

    internal void PromoteBlock(Naanou.ResourceExchange.Block block)

    Promotes a block from `in use' to idle. Do not call this method directly, use Naanou.ResourceExchange.Block.Retire.

    Parameters:
    block -

    Query

    internal Naanou.ResourceExchange.Block[] Query(string contentHash)

    Find all complete blocks that are part of a certain file

    Parameters:
    contentHash - Contents hash
    Returns:
    Matching blocks

    Query

    private long Query(string contentHash,
                       System.Collections.ArrayList output,
                       System.Collections.ArrayList source)


    QueryAny

    internal Naanou.ResourceExchange.Block[] QueryAny(string contentHash)

    Finds all blocks complete or not

    Parameters:
    contentHash - Hash

    Save

    internal void Save()

    Saves current block info to disk