Uploaded image for project: 'Clojure'
  1. CLJ-1408

Add transient keyword to cached toString() value in _str

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Completed
    • Affects versions: Release 1.6
    • Fix versions: Release 1.7
    • Labels:
    • Approval:
      Ok
    • Patch:
      Code and Test

      Description

      _str field in Keyword and Symbol classes lazily caches result of toString(). Because this field is not transient, serializing (using Java serialization) any keyword or symbol before and after calling toString() for the first time yields different results:

      (import (java.io ByteArrayOutputStream ObjectOutputStream
                       ByteArrayInputStream  ObjectInputStream))
      
      (defn- serialize [obj]
        (with-open [bos (ByteArrayOutputStream.)
                    stream (ObjectOutputStream. bos)]
          (.writeObject stream obj)
          (-> bos .toByteArray seq)))
      
      ;; keyword example
      
      (def k1 (serialize :k))
      (println :k)
      (def k2 (serialize :k))
      
      (= k1 k2) ;;=> false 
      
      ;; symbol example
      
      (def sym 'a)
      
      (def s1 (serialize sym))
      (println sym)
      (def s2 (serialize sym))
      
      (= s1 s2) ;;=> false
      

      This issue came up when I was trying to use keywords as key in [Hazelcast](https://github.com/hazelcast/hazelcast) map. Hazelcast uses serialized keys in various scenarios, thus if I first put something to map under key :k and then print :k, I can no longer find such key.

      Approach: Add transient keyword to _str field in Keyword and Symbol classes

      Patch: CLJ-1408-3.patch

      Screened by: Brenton Ashworth

        Attachments

        1. CLJ-1408.patch
          1 kB
          Tomasz Nurkiewicz
        2. CLJ-1408-2.patch
          1 kB
          Tomasz Nurkiewicz
        3. CLJ-1408-3.patch
          2 kB
          Alex Miller

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              nurkiewicz Tomasz Nurkiewicz
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: