Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Large Persistent Hash Table Stored on Filesystem
See more Misc Examples
Demonstrates how to implement a large, persistent hash table that is stored on the filesystem and allows for quick retrieval using a hash key.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Cache;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
cache: TCache;
key: string;
eTag: string;
itemValue: string;
begin
success := False;
// Let's say we would like to implement a persistent hash table with approximately 200,000 entries.
// We want something simple and straightforward.
//
// The Chilkat Cache class is a solution that might fit.
//
// Each hash table entry is a file. Depending on the number of anticipated hash entries,
// the files can be contained in a single directory, or a collection of 256 directories,
// or a collection of 256x256 directories.
// There are 3 options:
//
// Level 0: All cache files are in a single directory (the cache root).
// Level 1: Cache files are located in 256 sub-directories numbered 0 .. 255 directly under the cache root.
// Level 2: There are two levels of sub-directories under the cache root.
// The 1st level has 256 sub-directories numbered 0 .. 255 directly under the cache root.
// The 2nd level allows for up to 256 sub-directories (0..255) under each level-1 directory.
// Cache files are stored in the leaf directories.
// For this example, given that we anticipate a larger number of hash entries, we choose a "level 2" cache.
cache := TCache.Create;
// We can also spread the cache among several root directories, but for this example we'll only use one root directory.
// Call AddRoot once for each root directory.
cache.AddRoot('c:/temp/myCache');
cache.Level := 2;
// Add some key/values to the persisted hash table (i.e. the cache).
// The eTag is optional metadata.
key := 'apple';
eTag := '';
itemValue := 'macos';
success := cache.SaveTextNoExpire(key,eTag,itemValue);
// Add more items..
success := cache.SaveTextNoExpire('microsoft','','windows');
success := cache.SaveTextNoExpire('google','','android');
// Lookup items:
key := 'microsoft';
itemValue := cache.FetchText(key);
WriteLn(key + ': ' + itemValue);
key := 'apple';
itemValue := cache.FetchText(key);
WriteLn(key + ': ' + itemValue);
cache.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.