Example #1
0
        //	The following should be self-explanatory.
        public void DeleteEntry(String tid)
        {
            lock (this) {
                StoreEntry entry = null;

                if (table.TryGetValue(tid, out entry))
                {
                    table.Remove(tid);
                    bw.Seek(entry.index, SeekOrigin.Begin);
                    bw.Write(empty_id, 0, idSize);
                    bw.Flush();
                    freeEntries.Push(entry);
                }
            }
        }
Example #2
0
        public TidStatus CreateEntry(String tid)
        {
            lock (this) {
                if (tid == null || tid.Length != idSize)
                {
                    throw new ArgumentException("Invalid TID");
                }
                StoreEntry entry = null;
                table.TryGetValue(tid, out entry);
                if (entry != null)
                {
                    return(entry.status);
                }

                if (freeEntries.Count > 0)
                {
                    entry = freeEntries.Pop();
                }
                else
                {
                    entry       = new StoreEntry();
                    entry.index = (int)fs.Length;

                    bw.Seek(0, SeekOrigin.Begin);
                    bw.Write(++slots);
                }
                entry.status = TidStatus.Created;
                entry.tid    = tid;
                table.Add(tid, entry);

                bw.Seek(entry.index, SeekOrigin.Begin);
                bw.Write(tid.ToCharArray(), 0, idSize);
                bw.Write((byte)TidStatus.Created);
                bw.Write(EMPTY_REMAINDER, 0, REMAINDER_SIZE);
                bw.Flush();

                return(TidStatus.Created);
            }
        }
Example #3
0
        public TidStatus GetStatus(String tid, out String errorMessage)
        {
            TidStatus tidStatus = TidStatus.Created;

            lock (this) {
                errorMessage = "";
                StoreEntry entry = null;

                if (!table.TryGetValue(tid, out entry))
                {
                    throw new ArgumentException("Invalid TID");
                }

                tidStatus = entry.status;

                if (tidStatus == TidStatus.RolledBack)
                {
                    br.BaseStream.Seek(entry.index + idSize + 1, SeekOrigin.Begin);
                    byte[] err = br.ReadBytes(REMAINDER_SIZE);
                    errorMessage = Encoding.UTF8.GetString(err);
                }
            }
            return(tidStatus);
        }
Example #4
0
        // Opens an existing TidStore, or creates a fresh one, if the given file does not yet exist.
        public TidStore(String fileName, bool bgRFC)
        {
            idSize    = bgRFC ? GUID_SIZE : TID_SIZE;
            entrySize = idSize + REMAINDER_SIZE;
            empty_id  = new char[idSize];

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = "TIDStore.tid";
            }
            try {
                fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                br = new BinaryReader(fs);
                bw = new BinaryWriter(fs);
                if (fs.Length < entrySize)  // A newly created empty store file
                {
                    slots = 0;
                    bw.Seek(0, SeekOrigin.Begin);
                    bw.Write(slots);
                    bw.Flush();
                }
                else
                {
                    br.BaseStream.Seek(0, SeekOrigin.Begin);
                    slots = br.ReadInt32();
                }
                table = new Dictionary <String, StoreEntry>();

                for (int i = 0; i < slots; ++i)
                {
                    StoreEntry entry = new StoreEntry();
                    entry.index = (int)fs.Position;
                    // Read tid/uid
                    char[] tempId = br.ReadChars(idSize);
                    if (tempId == null || tempId.Length != idSize)
                    {
                        throw new IOException("Unable to read entry at position " + i);
                    }
                    // Read status
                    entry.status = (TidStatus)br.ReadByte();
                    // Skip REMAINDER
                    br.BaseStream.Seek(REMAINDER_SIZE, SeekOrigin.Current);

                    if (tempId[0] == 0)
                    {
                        freeEntries.Push(entry);
                    }
                    else
                    {
                        entry.tid        = new String(tempId);
                        table[entry.tid] = entry;
                    }
                }
            }
            catch (Exception) {
                if (fs != null)
                {
                    fs.Close();
                }
                br = null;
                bw = null;
                throw;
            }
        }