View script | License | Download documentation as: HTML or editable |
Download script | History | Other scripts by: sunanda |
[0.045] 27.46k
Documentation for: tsn.rscript: tsn.r title: Tranched Serial Numbers purpose: Quick, safe way of allocating categorized unique serial numbers author: Sunanda date: 30-apr-2007 Version: 0.0.1 1. purposetsn.r is a tool for allocating two-level unique serial numbers. The numbers are unique as each time you call tsn.r (for the same tsn set) you will receive a new number that has not been issued before (for that tsn set). They are two level as on each call to tsn.r you supply a category id. The serial number issued also includes a unique category serial number. That may make more sense if you read the quick example (below) and the section what could I possibly want with a script like this? (much later on). 2. quick example[1] do %tsn.r ;; install the utility [2] my-set: tsn-api/make-new-set ;; create a new number set [3] my-cats: copy ["green" "red" "blue"] [4] my-tsns: make block! 50000 [5] loop 50000 [ [6] append my-tsns third tsn-api/get-tsn my-set random/only my-cats ]
my-tsns now contains 50,000 unique serial numbers. You can use these as short, efficient, keys in database. You can also use them to get back to the category id and unique category serial number. See following example: [1] length? my-tsns [2] length? unique my-tsns [3] for nn 1 10 1 [ [4] print mold tsn-api/get-cat-id my-set my-tsns/:nn ]
The whole data structure to map 50,000 numbers back to the their categories is highly compact: >> length? mold my-set == 476 (Your precise number will vary as the example uses randomised data). 3. installingdo %tsn.r tsn.r also requires rse-ids.r (also in the Library) to be in the same folder. 4. api functionsThe Application Programmer Interface has these functions:
All the API functions (except make-new-set require the tsn set as a parameter. You can have multiple tsn sets; the serial numbers created will be unique only within that tsn set 4.1. make-new-setReturns you an object that defines the tsn set USAGE: my-set: tsn-api/make-new-set You are responsible for:
examples: tsn-set-1: tsn-api/make-new-set tsn-set-2: tsn-api/make-new-set You now have two separate tsn sets. Each will return its own series of unique serial numbers. But note that the numbers are not unique across the two tsn sets: both may (for example) have a serial number 101. 4.1.1. saving and reloading a tsn setIf you wish to access or create unique serial numbers across multiple sessions, you need to save the tsn set object at the end of one session and reload it at the start of the next: write %saved-tsn-set.txt mold tsn-set-1 . . . tsn-set-1: first reduce load/all %saved-tsn-set.txt To reduce the risk of users editing or corrupting the saved file, consider saving it as binary or encloaking it: write/binary %saved-tsn-set.txt encloak compress mold tsn-set-1 "secret key" . . . first reduce load/all decompress decloak read/binary %saved-tsn-set.txt "secret key" 4.2. get-tsnGiven a category id, returns a unique serial number and a unique category serial number. USAGE: tsn-api/get-tsn tsn-set cat-id ARGUMENTS: tsn-set -- the tsn-set in use (Type: object) cat-id -- the category id (Type: string integer) The function returns a block! with four entries:
example: print new-usn: third tsn-api/get-tsn my-set "red" == 4776 4.3. get-all-categoriesReturns a block of all the defined categories USAGE: tsn-api/get-all-categories tsn-set ARGUMENTS: tsn-set -- the tsn-set in use (Type: object) example: probe tsn-api/get-all-categories my-set == ["blue" "green" "red"] 4.4. get-cat-idGiven a unique serial number, it returns the category id and unique category serial number. USAGE: tsn-api/get-cat-id tsn-set usn ARGUMENTS: tsn-set -- the tsn-set in use (Type: object) usn -- unique serial number (Type: integer) The function returns either:
example: probe tsn-api/get-cat-id my-set 1888 == ["red" 629 1888] 4.5. get-all-tsns-for-catGiven a category, returns all unique serial numbers allocated to that category: USAGE: tsn-api/get-all-tsns-for-cat tsn-set cat-id ARGUMENTS: tsn-set -- the tsn-set in use (Type: object) cat-id -- category-id (Type string integer) The function returns either:
example: probe tsn-api/get-all-tsns-for-cat my-set "red" == [1 2 3 4 ... 600 601 602 .... 1009 1010 .... ] 4.6. remove-categoryRemoves a category and all unique serial numbers allocated to it. This does not mean the serial numbers will be reissued later. The publicly released version of tsn.r does not reuse numbers, ever. It does mean:
USAGE: tsn-api/remove-category tsn-set cat-id ARGUMENTS: tsn-set -- the tsn-set in use (Type: object) cat-id -- category-id (Type string integer) example: probe tsn-api/remove-category my-set "green" == true 5. what could I possibly want with a script like this?Good question! It is either something you need very badly, or it seems completely pointless. 5.1. historicalThe original usage was for an experiment database-type application. The database supported various entities (let's call some of the entities users, files, programs, memos, to make a concrete example). Each entity type had unique entity numbers: eg users were numbered 1,2,3,4; Files were numbered 1,2,3,4,5 etc. That meant we could have made unique serial numbers like this: U1, U2, U3 (for users), F1, F2, F3, F4 for files. But (reasoning omitted for simplicity) we needed (wanted!) all identifiers to be integers. tsn.r was an easy way to make that possible. It also enabled a simple (almost flat) database structure with records like this: 20 [5775 377 4477 388] meaning entity 20 is associated with entities 5775, 377, 4477 and 388 without us having to know what types of entity [5775 377 4477 388] are. That was a sort of advance on the many-to-many relationship that many databases support....More of an any-to-any relationship. And that came in very handy for some data manipulations. 5.2. present daytsn.r is a component in the (soon to be released) Altme World Indexer. It allow us a simple way of flattening [Group-id Post-id] into a single, relatively low, integer for indexing purposes. 6. configuringThere are two magic numbers you can change in a tsn set. Changing them may affect performance. They adjust how unique serial number ranges are reserved for categories. You can change the settings globally (the changes will affect all tsn-sets allocated after the change); or locally (the changes affect only the specific tsn-set. 6.1. global changetsn-api/tsn-template/smallest-tranche: 25 tsn-api/tsn-template/growth-factor: 1.1 6.2. local changemy-set: tsn-api/make-new-set my-set/smallest-tranche: 25 my-set/growth-factor: 1.1 6.3. smallest-trancheDefines the smallest range reserved for a category. 6.4. growth-factorDefines the size of the second and subsequent ranges reserved for a category. If a category exhausts a range, then tsn.r reserves a range equal to the current size of the category times the growth factor. Example: category red has a highest unique category serial number of 950 and we need to allocate another range to it. If the growth-factor is 1.4, then tsn.r allocates a range that is 1330 wide (950 * 1.4). |