コード例 #1
0
        public static ContentDatabaseSession OpenDatabase(string path)
        {
            //Open file
            DatabaseFile file = DatabaseFile.LoadFile(path);

            //Open DB
            var db = new ContentDatabaseSession(file);

            //Read TOC
            if (db.tocPage != -1)
            {
                //Read blob
                byte[]           tocBytes = file.ReadBlob(db.tocPage);
                int              count    = tocBytes.Length / TOC_ENTRY_LENGTH;
                BufferBinaryTool reader   = new BufferBinaryTool(tocBytes);

                //Add all
                for (int i = 0; i < count; i++)
                {
                    int offset = TOC_ENTRY_LENGTH * i;
                    DatabaseObjectHeader entry = new DatabaseObjectHeader();
                    entry.object_id   = reader.ReadUInt64(offset + 0);
                    entry.page_id     = reader.ReadUInt64(offset + 8);
                    entry.commit_id   = reader.ReadUInt64(offset + 16);
                    entry.group_id    = reader.ReadInt32(offset + 24);
                    entry.commit_type = tocBytes[offset + 28];
                    entry.flags       = tocBytes[offset + 29];
                    entry.reserved    = reader.ReadUInt16(offset + 30);
                    db.objects.Add(entry);
                }
            }

            //Read Name Table
            if (db.nameTablePage != -1)
            {
                //Read blob
                byte[]           ntBytes = file.ReadBlob(db.nameTablePage);
                BufferBinaryTool reader  = new BufferBinaryTool(ntBytes);

                //Begin reading
                int index = 0;
                while (index < ntBytes.Length)
                {
                    //Read length
                    int length = reader.ReadInt32(index);

                    //Read string
                    db.nameTable.Add(Encoding.ASCII.GetString(ntBytes, index + 4, length));

                    //Update
                    index += length + 4;
                }
            }

            return(db);
        }
コード例 #2
0
        /* File store */

        private void LowLevelUpsertObject(ulong id, ulong commit_id, int group_id, byte commit_type, byte[] payload)
        {
            //Look for an existing entry with this ID
            DatabaseObjectHeader entry = null;

            foreach (var e in objects)
            {
                if (e.object_id == id)
                {
                    entry = e;
                }
            }

            //Update or add the payload data
            long payloadPage;

            if (entry == null)
            {
                payloadPage = file.WriteNewBlob(PAGE_TYPE_OBJECT, payload); //Make a new blob
            }
            else
            {
                payloadPage = file.UpdateBlob((long)entry.page_id, PAGE_TYPE_OBJECT, payload); //Update existing blob
            }
            //Update or create entry
            if (entry == null)
            {
                entry = new DatabaseObjectHeader
                {
                    object_id   = id,
                    page_id     = (ulong)payloadPage,
                    commit_id   = commit_id,
                    group_id    = group_id,
                    commit_type = commit_type,
                    flags       = 0,
                    reserved    = 0
                };
                objects.Add(entry);
            }
            else
            {
                entry.page_id     = (ulong)payloadPage;
                entry.commit_id   = commit_id;
                entry.group_id    = group_id;
                entry.commit_type = commit_type;
                entry.flags       = 0;
                entry.reserved    = 0;
            }

            //Note that we do NOT update the TOC. That will be done when this is fully commited
        }