Generation of unique data identifiers

Random GUID

Most people will be familiair with GUIDs, and when picking a new GUID they will likely use the static NewGuid() function that returns a (practically unique) GUID. For NFileStorage we would like to discourage people to use this function to generate a unique data identifier; you will quickly find out the index file is not optimized to store random GUIDs. The end result is that your index file will rapidly grow when you store a large amount of items in the store. Instead, use one of the following alternatives

var dataIdentifier = Guid.NewGuid(); // NOTE, not the recommended way!

Based upon a filename, or string

Import the FileStorage.Helper namespace, and use the overloaded method ToNFileStorageDataIdentifier function of any string to produce a GUID, like so:

using FileStorage.Helper;
...
string filename = "myfilename.jpg";
var dataIdentifier = filename.ToNFileStorageDataIdentifier();

A big advantage of using the string to GUID conversion, is that it allows readability of the files using the verbose 'dir' command; in the example below you can see I have stored information about all Dutch municipalities (gemeenten) in a File storage. The filename of each entry is the unique code that the CBS (Centraal Bureau van de Statistiek/ Central Bureau of Statistics) has assigned to that municipality.

H:\Proj\Kadaster\Kadaster\NFileStorages\CBS>FileStorageCmd.exe dir cbs_gemeenten verbose
1
. 443 (443 files/sec, 00:00:00 mins) Dir............................................................
.
Data identifier                      | Text identifier  | Creation date     | Size
-------------------------------------+------------------+-------------------+-----------------
5f5f5f5f-5f5f-5f5f-5f5f-676d30303033 | __________gm0003 | 20090317 03:33:59 | 1.811
5f5f5f5f-5f5f-5f5f-5f5f-676d30303035 | __________gm0005 | 20090317 03:33:59 | 2.123
5f5f5f5f-5f5f-5f5f-5f5f-676d30303037 | __________gm0007 | 20090317 03:33:59 | 2.475
5f5f5f5f-5f5f-5f5f-5f5f-676d30303039 | __________gm0009 | 20090317 03:33:59 | 1.651
5f5f5f5f-5f5f-5f5f-5f5f-676d30303130 | __________gm0010 | 20090317 03:33:59 | 5.119
...
5f5f5f5f-5f5f-5f5f-5f5f-676d31393136 | __________gm1916 | 20090317 03:34:05 | 2.747
5f5f5f5f-5f5f-5f5f-5f5f-676d31393236 | __________gm1926 | 20090317 03:34:05 | 2.423
5f5f5f5f-5f5f-5f5f-5f5f-676d31393535 | __________gm1955 | 20090317 03:34:05 | 4.261
5f5f5f5f-5f5f-5f5f-5f5f-676d31393837 | __________gm1987 | 20090317 03:34:05 | 3.657
443 files found (1.281.702 bytes)
This operation took 23719 msecs

H:\Proj\Kadaster\Kadaster\NFileStorages\CBS>

Based upon a unique moment in time

var dataIdentifier = FileStorage.Helper.DataIdentifierHelper.CreateDataIdentifierByDateTimeUtcTimeTicks();

Note that this function is by itself not human readable at first sight.

Based upon a unique moment in time (2)

var dataIdentifier = FileStorage.Helper.DataIdentifierHelper.CreateDataIdentifierByReadableDateTimeUtc();

Note that this function is by itself human readable when you 'dir' the contents of the filestorage (format 'yyyymmddhhmmssmmmm').

Your custom routine ...

Use your imagination about what's the best GUID structure for specific situation. Maybe you the entry in the FileStorage refers to some object in your application that is stored in a database, which is already identified by, for example, an identity field (auto incrementing int32 for example). In that case you could decide to do the following;

using FileStorage.Helper;
...
int someReference = 4324;
string someReferenceStringValue = someReferenc.ToString();
var dataIdentifier = someReferenceStringValue.ToNFileStorageDataIdentifier();

Last edited Jul 2, 2009 at 10:01 AM by barkgj, version 6

Comments

No comments yet.