Before I go to bed

Sun, Feb 23, 2003

Searching google groups for "eightypercent" turns up someone who likes to post in lots of bondage forums. For the record, that isn't me. :)

Code review

Sun, Feb 23, 2003

I have the "comparison before saving" stuff working and that is cutting down my upload times dramatically. This is almost like a real blogging tool now. However, one function -- called TransformAndSaveDoc -- turned out really nasty. This function transforms an xml document with an xslt style sheet. It then compares the result to what is already on disk and only writes the new result to disk if the new version is different from the old version.

Suggestions for making this tighter? Is there some functionality (memcmp?) that I'm missing in the .Net framework? Since I don't have comments working, send mail to eightypercent@bedafamily.com and I'll post the best comments up here with credit and eternal thanks.

        private void TransformAndSaveDoc(XslTransform xslt, XmlDocument xmlSource, string fileName)
        {           
            using (MemoryStream memstrm = new MemoryStream())
            {
                bool fFileDifferent = false;

                xslt.Transform(xmlSource, null, memstrm);

                FileInfo fi = new FileInfo(fileName);

                // Check for simple things first
                if (!File.Exists(fileName) || fi.Length != memstrm.Length)
                    fFileDifferent = true;

                // Actually compare the file to the stream
                if (!fFileDifferent)
                {
                    Byte[] buffer1 = new Byte[8096];
                    Byte[] buffer2 = new Byte[8096];

                    // compare the existing file with the memory stream
                    using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        memstrm.Seek(0, SeekOrigin.Begin);
                        
                        int size1 = 0, size2 = 0;
                        while(  !fFileDifferent 
                            &&  (0 != (size1 = memstrm.Read(buffer1, 0, 8096))))
                        {
                            size2 = fileStream.Read(buffer2, 0, 8096);
                            Debug.Assert(size1 == size2);
                            
                            for (int i = 0; i < size1; i++)
                            {
                                if (buffer1[i] != buffer2[i])
                                {
                                    fFileDifferent = true;
                                    break;
                                }
                            }
                        }
                    }
                }

                // Write that puppy out
                if (fFileDifferent)
                {
                    using (FileStream fileStream = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        memstrm.Seek(0, SeekOrigin.Begin);

                        Byte[] buffer = new Byte[8096];
                        int size;
                        while(0 != (size = memstrm.Read(buffer, 0, 8096)))
                        {
                            fileStream.Write(buffer, 0, size);
                        }
                    }
                }
            }
        }

Untitled #45

Sun, Feb 23, 2003

I have the dictionary done so now I don't upload files that haven't changed. Now all I have to do is change my generation code so that it doesn't actually write to files that haven't changed.

I could do this by making sure that I track which data files have changed and figuring out which output files that means that I have to generate.

Or, I could just always generate every output file, but compare it to what is on disk before writing it. If it is exactly the same as what is already there, I can just throw away my in memory copy and not write anything.

Radio took the first approach, except it didn't get it completely right. If you added a new entry in Radio, it wouldn't update any of the archive pages. The problem is that you would always see links going further back on the calendar but never going forward, as it would never update one of the archive pages. With the second approach it may take a little longer to generate the site, but you'll be sure to update everything that needs to be updated.

I think that I'm going to go with the second approach. I can adapt and implement the first as needed (perhaps on a more granular level.

FTPUpstreaming?

Sun, Feb 23, 2003

If you see this then I uploaded everything using my new FTP support.

Right now I just upload everything every time. However, it shouldn't be difficult for me to keep a dictionary of file times so I can keep track of what is new since the last upload.

I also need to do the uploading on a different thread and report status.