-
I've succeeded with creating a plugin for syncing tiddlers to a web server (basically, created a cut-down version of TiddlyWeb plugin, as per advice in this discussion) Now, to make the sync logic to work with our p2p backend, I need a unique ID on every user-created tiddler (not necessary UUID, just a random number will suffice). How do I write a plugin that will assign each tiddler a unique ID, right when a toddler is created? |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 19 replies
-
I have a wikitext and macro solution. Just increment a global variable, and save it back. You can issue them serially, If random you need to always test for collisions. More often than not the created date would suffice, and you can increment microseconds until unique. The use of a custom field with a prefix or suffix will reduce the chance of collisions across wikis; eg use a custom wikiname in the serial number, along with a string for your solution. Tony |
Beta Was this translation helpful? Give feedback.
-
Hi @ichorid
Excellent!
TW5 uses the title field as a unique ID for each tiddler. You may be able to use the title directly, but if you'd rather have a fixed length identifier you could just hash the title and take a fixed number of bits to yield the ID. The sync adaptor would probably have to maintain a mapping of ID to title. Using the title as the ID means that renaming a tiddler has to be modelled as deleting the old tiddler and creating the new one. I believe @pmario's plugin attempts to track IDs across renames. |
Beta Was this translation helpful? Give feedback.
-
I did some experiments 10 years ago with tiddler titles as UUIDs ... but that's not convenient. ... At the moment I use tiddler names in Wikitext linking is done using the "aliases" field and my I personally would go for UUIDs. They are easy to create and you don't have to think about collisions. UUIDs in general means RFC 4122 IDs, which are Hex based and need 36 chars for 5.3x10^36 possible numbers. I'd use a number which is based on 62 characters. So you get 4.4x10^37 possible "numbers" out of a string with 21 chars eg: I did some other experiments which used different elements, that are "kind of" unique eg: date in miliseconds since 1970 + domain name + local miliseconds
just some ideas |
Beta Was this translation helpful? Give feedback.
-
As Saq wrote,
If your users use the UI it should be simple to add a If you create tiddlers programmatically eg: So: How do you create new tiddlers? |
Beta Was this translation helpful? Give feedback.
-
Ok, so the question is: how to add a custom field programmatically, from a plugin? (e.g. in a sync plugin, on synchronization). I spent a few hours looking for a way to do it. |
Beta Was this translation helpful? Give feedback.
-
Issuing ids to all tiddlers can be done in the UI adding appropriate actions to the required buttons, one case to remember is cloning, you need to reset that field to a newly issued one. In the save mechanisium you can first trigger a check all "has changed" tiddlers for the ID AND GENERATE if not. This will catch tiddlers created from following camel case links or custom buttons not using a SET UID action. For a database the ID is only needed before saving. Also rather than just a unique number simply allow a static prefix with a wiki name or epoch. Still increment the number but give it a seed value. The chance of duplicate ID's is reduced and can be tested for interwiki transfers. This obviates the need to generate a UNIVERSAL UID which uses far more bytes. Regards |
Beta Was this translation helpful? Give feedback.
-
@ichorid if adding a field in the sync plugin would meet your needs, that is entirely possible. Apologies, you're getting a lot of replies that don't take into account the context of the thread, presumably out of a desire to be helpful despite the busy time of year. If you post your plugin and point out where in the flow you want to add the field, I can assist you with that.
In your case |
Beta Was this translation helpful? Give feedback.
-
I have spent a lot of time on this subject so please consider these comments. They are a result of considerable design and analysis and will save time in developing such a solution. There are a range of powerful features that become possible if we have an additional field on tiddlers, that I have being calling a Tiddler Serial Number TSN. I have built this as a complete solution already, using wikitext and macros (no plugins) but of course a feature such as this would be better at a lower level plugin or core. Whilst I support the idea of a Universal ID, I believe issuing a serial number each time one is needed, by incrementing a number and adding a configurable prefix, such as a user specified wikiname or epoch, is sufficient and requires less bytes. A utility to rename a (Serial number) prefix if necessary, provides the opportunity to adjust the unique identifier as needed. I have also built a method to add serial numbers to existing core tiddlers without modifying them by storing the system Serial Number in a separate json file. Core tiddlers will have a "name to system serial number" table that will never change. Once a shadow is cloned or renamed they are issued a TSN. There are many cases where only issuing a Serial number if required is also practical, such as for compound tiddlers, like we see in streams. If the parent tiddler has a serial number, each child can be issued there own serial number, add this to a field in the parent, and have the parent tiddlers serial number recorded in the children. The Parent and child tiddlers can then be freely renamed as many times as needed whilst retaining their relationships. This same "serial number as required mechaisium" could be used for unique ID's in a database or saver backend by simply using the same on demand serial number before saving a given tiddler the first time. A database name could also be specified, and made part of the serial number used to save tiddlers. The Serial number field once issued should not be available to edit; use $:/config/EditTemplateFields/Visibility/fieldname with the value "hide". Another trick is to create a mechaisium to increment the last serial number by one, add the current prefix and before saving convert the incremented number to a more compact base number, just as hex is more compact with its 16 characters (base 16), you can make a base 36 or base 62 using a-z A-Z and 0-9. The serial number never changes and compressing it this way consumes far fewer bytes. Starting with one byte and only growing as needed. 5 bytes can represent more than 14,700,000 tiddlers. With an modifiable prefix, one can continue issuing new serial numbers but change the prefix, this does not affect the uniqueness but does allow a different prefix to be set using the current user name, or an epoch, such as development time, then once in "production". Thus the serial number can carry other information that is related to the issuing of serial numbers. An Additional on demand serial number based on the existing ones, could always be generated using a registered wikiname to make such a serial number unique, and totally transferable between wikis. Not to mention the use of namespaces for tiddlers similar to the current plugin and macro tiddlers. Regards |
Beta Was this translation helpful? Give feedback.
-
TiddlyMap implements a hidden |
Beta Was this translation helpful? Give feedback.
@ichorid if adding a field in the sync plugin would meet your needs, that is entirely possible. Apologies, you're getting a lot of replies that don't take into account the context of the thread, presumably out of a desire to be helpful despite the busy time of year.
If you post your plugin and point out where in the flow you want to add the field, I can assist you with that.
Otherwise the things to keep in mind are that:
this.wiki.addTiddler(new $tw.Tiddler(oldTiddler,newFields));
In …