// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SlobViewer.Slob
{
///
/// Represents a dictionary retrieved from a SLOB file.
///
public class SlobDictionaryInMemory : IWordDictionary
{
public string FileName { get; set; }
///
/// The encoding of the keys and the content.
///
private System.Text.Encoding _encoding;
///
/// The compression method of the content.
///
private string _compression;
///
/// The dictionary of tags of this SLOB file. The tags represent metadata for the SLOB file.
///
private Dictionary _tags;
///
/// The types of content. The s contain a table which stores for each content item an index into this list of content types,
/// so that each content item has a designated content type.
///
private string[] _contentTypes;
///
/// The list of keys of this dictionary.
///
private Reference[] _references;
///
/// The store items, i.e. the list of values. Each store item contains multiple values, in order to have a better compression factor of the content.
///
private StoreItemInMemory[] _storeItems;
private byte[] _buffer = new byte[256];
///
/// Initializes a new instance of the class.
///
/// The encoding of the content.
/// The compression type of the content.
/// The tags (metadata of this dictionary).
/// The content types.
/// The references (keys of the dictionary).
/// The store items (each store item contains multiple content items).
public SlobDictionaryInMemory(
System.Text.Encoding encoding,
string compression,
Dictionary tags,
string[] contentTypes,
Reference[] references,
StoreItemInMemory[] storeItems)
{
_encoding = encoding;
_compression = compression;
_tags = tags;
_contentTypes = contentTypes;
_references = references;
_storeItems = storeItems;
}
/// Gets all keys of this dictionary.
///
public string[] GetKeys()
{
return _references.Select(x => x.Key).ToArray();
}
///
/// Gets a number of keys including the key given in (if found).
///
/// The key to search for.
/// The number of keys following the given key.
/// A array of keys, including the key given (if it is found in the dictionary). The size of the array is equal to or less than .
public string[] GetKeys(string key, int count)
{
int i;
for (i = 0; i < _references.Length; ++i)
{
if (0 >= string.Compare(key, _references[i].Key))
break;
}
if (i == _references.Length)
return null;
var len = Math.Min(count, _references.Length - i);
var result = new string[len];
for (int k = 0; k < len; ++k)
result[k] = _references[i + k].Key;
return result;
}
///
/// Try to get the content for the specified .
///
/// The key for which the content should be retrieved..
/// If successfull, contains the retrieved content and the content identification.
/// True if the content with the specified key was found; otherwise, false.
public bool TryGetValue(string key, out (string Content, string ContentId) value)
{
int i;
for (i = 0; i < _references.Length; ++i)
{
if (0 == string.Compare(key, _references[i].Key))
break;
}
if (i == _references.Length)
{
value = (null, null);
return false;
}
var blobIndex = _references[i].BinIndex;
var itemIndex = _references[i].ItemIndex;
var storeItem = _storeItems[blobIndex];
var item = storeItem.GetAt(itemIndex, _encoding, _compression, _buffer);
value = (item.Content, _contentTypes[item.ContentId]);
return true;
}
///
/// Gets the with the specified key.
///
///
/// The value for the specified key.
///
/// The key.
/// The value for the specified key.
public (string Content, string ContentId) this[string key]
{
get
{
TryGetValue(key, out var value);
return value;
}
}
}
}