1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package Torello.JavaDoc.SyntaxHiLite;

import Torello.Java.FileNode;
import Torello.Java.FileRW;
import Torello.Java.FileTransfer;
import Torello.Java.StorageWriter;
import Torello.Java.CacheError;

import java.io.File;

import java.util.TreeSet;


// These are just some simple methods that allow for reading and writing a Java TreeSet that 
// contains all of the Hash-Codes which have been saved to this Cache.  When a file is written to 
// the Cache, a simple Hash-Code is generated using the String-Contents of that file.  Currently,
// Java's Standard String.hashCode() is used to produce the Hash-Code.
// 
// This means summing the value of every character in the String 

class CacheMethodsInternal64
{
    private static final String HASH_SAVE_TREE = "HILITED_STRINGS_HASH_CODE.ts";

    static String checkCSD(String cacheSaveDirectory)
    {
        cacheSaveDirectory = cacheSaveDirectory.trim();

        if (! cacheSaveDirectory.endsWith(File.separator))
            cacheSaveDirectory = cacheSaveDirectory + File.separator;

        File f  = new File(cacheSaveDirectory);

        if (! f.exists()) throw new CacheError(
            "The specified cache-directory does not exist on the file-system: " +
            "[" + cacheSaveDirectory + "]"
        );

        return cacheSaveDirectory;
    }

    // the "return TreeSet<Long>" complains about an unchecked cast.
    @SuppressWarnings("unchecked") 
    static TreeSet<Long> checkTS(String cacheSaveDirectory)
    {
        String  fName   = cacheSaveDirectory + HASH_SAVE_TREE;
        File    f       = new File(fName);

        if (! f.exists()) throw new CacheError(
            "The current-cache directory does not contain a primary-cache file: " +
            "[" + fName + "]"
        );

        Object o;

        try
            { o = FileRW.readObjectFromFile(fName, true); }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error attempting to read the following primary-cache file.  " + 
                "It appears to be corrupted: [" + fName + "]",
                e
            );
        }

        if (! (o instanceof TreeSet)) throw new CacheError(
            "There primary cache file loaded, but does not contain the correct " +
            "data-structure.  It appears to be corrupted. [" + fName + "]"
        );

        return (TreeSet<Long>) o;
    }


    // Just saves a brand-new (empty) Hash-Code List (a java.util.TreeSet) to disk, using
    // Standard Java Object Serialization.

    private static TreeSet<Integer> writeNewTS(String cacheSaveDirectory)
    {
        TreeSet<Integer> hashCodes = new TreeSet<>();

        try
            { FileRW.writeObjectToFile(hashCodes, cacheSaveDirectory + HASH_SAVE_TREE, true); }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error writing the Cache Hash-Code File to disk.  " +
                "[" + cacheSaveDirectory + HASH_SAVE_TREE + "].  " ,
                e
            );
        }

        return hashCodes;
    }


    // This will save the hash-code {@code TreeSet<Integer>} to disk.
    // It uses standard Java Object-Serialization

    static void persistMasterHashToDisk(HiLiteCache64 cache) throws CacheError
    {
        try
        {
            FileRW.writeObjectToFile
                (cache.hashCodes, cache.cacheSaveDirectory + HASH_SAVE_TREE, true);
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error writing the Master Hash-Code table to disk. " +
                "File [" + cache.cacheSaveDirectory + HASH_SAVE_TREE + "] was not saved. " +
                "The cache-file will have to be refreshed at some point.  New Files " +
                "Cache-Hash not saved.",
                e
            );
        }
    }


    // This will initialize a cache in the file-system directory identified by parameter
    // 'cacheSaveDirectory'.  This method will traverse the File-System Directory-Tree and clobber
    // the contents of all sub-directories, and the files that they contain.
    // 
    // If the directory specified does not exist, a {@code CacheError} is thrown.
    // 
    // At the end a new Hash-Code => File-Location 

    static HiLiteCache64 initializeOrClear(String cacheSaveDirectory, StorageWriter sw)
        throws CacheError
    {
        cacheSaveDirectory = checkCSD(cacheSaveDirectory);

        final String tempStrForStupidLambdaFinal = cacheSaveDirectory;

        try
        {
            File f = new File(cacheSaveDirectory);

            if (f.isDirectory())
                FileTransfer.deleteFilesRecursive(
                    FileNode.createRoot(cacheSaveDirectory).loadTree(), null,
                    (FileNode fn) -> fn.getFullPathName().equals(tempStrForStupidLambdaFinal),
                    sw
                );

            f.mkdirs();
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error emptying/clearing the directory " +
                "[" + cacheSaveDirectory + "] of it's contents, please see cause " +
                "throwable.getCause() for details.",
                e
            );
        }

        try
            { writeNewTS(cacheSaveDirectory); }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error saving/creating the new cache-file " +
                "[" + cacheSaveDirectory + "], please see cause chain throwable.getCause(), " +
                "for more details.",
                e
            );
        }

        return new HiLiteCache64(cacheSaveDirectory);
    }

    static long computeCacheKey(
            final String    codeTypeParam,
            final boolean   includeLineNumbers,
            final byte      styleNum,
            final String    sourceCodeAsString
        )
    {
        final long FNV_PRIME = 0x100000001b3L;
        long hash = 0xcbf29ce484222325L;
    
        // Mix in codeTypeParam
        for (int i = 0; i < codeTypeParam.length(); i++) {
            hash ^= codeTypeParam.charAt(i);
            hash *= FNV_PRIME;
        }
    
        // Mix in includeLineNumbers
        hash ^= (includeLineNumbers ? 1 : 0);
        hash *= FNV_PRIME;
    
        // Mix in styleNum
        hash ^= styleNum;
        hash *= FNV_PRIME;
    
        // Mix in sourceCodeAsString
        for (int i = 0; i < sourceCodeAsString.length(); i++) {
            hash ^= sourceCodeAsString.charAt(i);
            hash *= FNV_PRIME;
        }
    
        return hash;
    }
}