File size: 5,474 Bytes
fab29d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
// 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
{
/// <summary>
/// Represents a dictionary retrieved from a SLOB file.
/// </summary>
public class SlobDictionaryInMemory : IWordDictionary
{
public string FileName { get; set; }
/// <summary>
/// The encoding of the keys and the content.
/// </summary>
private System.Text.Encoding _encoding;
/// <summary>
/// The compression method of the content.
/// </summary>
private string _compression;
/// <summary>
/// The dictionary of tags of this SLOB file. The tags represent metadata for the SLOB file.
/// </summary>
private Dictionary<string, string> _tags;
/// <summary>
/// The types of content. The <see cref="StoreItemInMemory"/>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.
/// </summary>
private string[] _contentTypes;
/// <summary>
/// The list of keys of this dictionary.
/// </summary>
private Reference[] _references;
/// <summary>
/// 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.
/// </summary>
private StoreItemInMemory[] _storeItems;
private byte[] _buffer = new byte[256];
/// <summary>
/// Initializes a new instance of the <see cref="SlobDictionaryInMemory"/> class.
/// </summary>
/// <param name="encoding">The encoding of the content.</param>
/// <param name="compression">The compression type of the content.</param>
/// <param name="tags">The tags (metadata of this dictionary).</param>
/// <param name="contentTypes">The content types.</param>
/// <param name="references">The references (keys of the dictionary).</param>
/// <param name="storeItems">The store items (each store item contains multiple content items).</param>
public SlobDictionaryInMemory(
System.Text.Encoding encoding,
string compression,
Dictionary<string, string> tags,
string[] contentTypes,
Reference[] references,
StoreItemInMemory[] storeItems)
{
_encoding = encoding;
_compression = compression;
_tags = tags;
_contentTypes = contentTypes;
_references = references;
_storeItems = storeItems;
}
/// <summary>Gets all keys of this dictionary.</summary>
/// <returns></returns>
public string[] GetKeys()
{
return _references.Select(x => x.Key).ToArray();
}
/// <summary>
/// Gets a number of <paramref name="count"/> keys including the key given in <paramref name="key"/> (if found).
/// </summary>
/// <param name="key">The key to search for.</param>
/// <param name="count">The number of keys following the given key.</param>
/// <returns>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 <paramref name="count"/>.</returns>
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;
}
/// <summary>
/// Try to get the content for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The key for which the content should be retrieved..</param>
/// <param name="value">If successfull, contains the retrieved content and the content identification.</param>
/// <returns>True if the content with the specified key was found; otherwise, false.</returns>
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;
}
/// <summary>
/// Gets the <see cref="System.ValueTuple{System.String, System.String}"/> with the specified key.
/// </summary>
/// <value>
/// The value <see cref="System.ValueTuple{System.String, System.String}"/> for the specified key.
/// </value>
/// <param name="key">The key.</param>
/// <returns>The value <see cref="System.ValueTuple{System.String, System.String}"/> for the specified key.</returns>
public (string Content, string ContentId) this[string key]
{
get
{
TryGetValue(key, out var value);
return value;
}
}
}
}
|