Exemple #1
0
        /// <summary>
        ///  Find key (string)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="op">0 - equal; -1 - nearest left (less than); 1 - nearest right (more than) </param>
        /// <returns>Value</returns>
        public BTResult Find(string key, int op)
        {
            int      i   = find(VSLib.ConvertStringToByte(key), op);
            BTResult ret = new BTResult();

            ret.KeyString = (i < 0) ? "" : VSLib.ConvertByteToString(BTree[i].Key);
            ret.Value     = (i < 0) ? -1 : BTree[i].Value[0];

            return(ret);
        }
Exemple #2
0
        /// <summary>
        ///  Find all keys (non-unique), string
        /// </summary>
        /// <param name="key"></param>
        /// <param name="op">0 - equal; -1 - nearest left (less than); 1 - nearest right (more than) </param>
        /// <returns></returns>
        public BTResultList FindAll(string key, int op)
        {
            int i = find(VSLib.ConvertStringToByte(key), op);

            BTResultList ret = new BTResultList();

            ret.KeyString = (i < 0) ? "" : VSLib.ConvertByteToString(BTree[i].Key);
            ret.Value     = (i < 0) ? new long[0] : BTree[i].Value.ToArray();

            return(ret);
        }
        internal const long FBQE_HEADER_LENGTH = LAST_Q_POS + LAST_Q_LEN + 4; // 4-reserve


        ///////////////////////////////////////////////////
        ///////////////////// METHODS /////////////////////
        ///////////////////////////////////////////////////
        /// <summary>
        /// Get by index
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public FBQE GetFBQE(int idx)
        {
            FBQE f = new FBQE();

            f.index   = idx;
            f.address = idx * FBQE_LENGTH;

            f.SG            = VSLib.ConvertByteToString(VSLib.GetByteArray(buffer, f.address + SG_POS, SG_LEN));
            f.ADDRESS_START = VSLib.ConvertByteToLong(VSLib.GetByteArray(buffer, f.address + ADDRESS_START_POS, ADDRESS_START_LEN));
            f.ADDRESS_END   = VSLib.ConvertByteToLong(VSLib.GetByteArray(buffer, f.address + ADDRESS_END_POS, ADDRESS_END_LEN));
            f.LENGTH        = VSLib.ConvertByteToLong(VSLib.GetByteArray(buffer, f.address + LENGTH_POS, LENGTH_LEN));
            f.PREV          = VSLib.ConvertByteToInt(VSLib.GetByteArray(buffer, f.address + PREV_POS, PREV_LEN));
            f.NEXT          = VSLib.ConvertByteToInt(VSLib.GetByteArray(buffer, f.address + NEXT_POS, NEXT_LEN));

            if (f.SG != DEFS.FBQE_SIGNATURE)
            {
                throw new VSException(DEFS.E0006_INVALID_SIGNATURE_CODE, "(FBQE)");
            }

            return(f);
        }
 /// <summary>
 /// Read string
 /// </summary>
 /// <param name="address"></param>
 /// <param name="length"></param>
 /// <returns></returns>
 public string ReadString(long address, long length)
 {
     return(VSLib.ConvertByteToString(ReadBytes(address, length)));
 }
Exemple #5
0
        /// <summary>
        /// Load cahce fields
        /// </summary>
        private void deserialize()
        {
            CURRENT_ALLOC = base.ALLOC;

            // Create/cleanup fields cache
            if (FCache == null)
            {
                FCache = new List <FieldCache>(32);
            }
            else
            {
                FCache.Clear();
            }

            // Create cache binary tree
            FCacheTree = new VSBBTree(FCT_NAME, 32, true);

            // Read variable data into the buffer
            byte[] data = base.ReadBytes(base.FIXED, (long)FIELDS_SIZE);

            int offset = FIRST_FIELD_OFFSET_REL;                                    // Offset inside buffer (relative, doesn't include fixed part)

            int n = (int)FIELDS_NUMBER;                                             // Number of fields

            // Load fields
            for (int i = 0; i < n; i++)
            {
                // Create field
                FieldCache f = new FieldCache();
                f.OFFSET = offset;                                                                                              // Field offset in object
                f.TYPE   = data[offset + FIELD_TYPE_POS];                                                                       // Field type

                byte name_length = data[offset + FIELD_NAME_LENGTH_POS];                                                        // Name length
                f.NAME = VSLib.ConvertByteToString(VSLib.GetByteArray(data, (int)(offset + FIELD_NAME_POS), (int)name_length)); // Name

                int value_offset = FIELD_FIXED_LENGTH + name_length;                                                            // Value offset

                // Get value depending on type
                if ((f.TYPE == FIELD_TYPE_BYTE) | (f.TYPE == FIELD_TYPE_SHORT) | (f.TYPE == FIELD_TYPE_DATETIME))
                {
                    f.DATA_OFFSET = value_offset;                                   // Shift to value offset
                    f.LENGTH      = FIELD_LENGTHS[f.TYPE];
                }
                else if ((f.TYPE == FIELD_TYPE_INT) | (f.TYPE == FIELD_TYPE_LONG))
                {
                    f.DATA_OFFSET = value_offset + 1;                               // Shift to value offset (+ 1 byte length)
                    f.LENGTH      = (int)data[offset + value_offset];
                }
                else if ((f.TYPE == FIELD_TYPE_BYTES) | (f.TYPE == FIELD_TYPE_STRING))
                {
                    int l = (int)data[offset + value_offset];                       // Read number of length bytes
                    if (l > 0)
                    {
                        byte[] ba = VSLib.GetByteArray(data, (int)(offset + value_offset + 1), (int)l);      // Read length
                        f.LENGTH = VSLib.ConvertByteToInt(decompress(ba, FIELD_TYPE_LONG));
                    }
                    else
                    {
                        f.LENGTH = 0;
                    }

                    f.DATA_OFFSET = value_offset + 1 + l;
                }
                else
                {
                    throw new VSException(DEFS.E0029_INVALID_FIELD_TYPE_CODE, "- " + f.TYPE.ToString());
                }

                f.OLDLENGTH   = f.LENGTH;
                f.STATE       = STATE_LOADED;
                f.FULL_LENGTH = f.DATA_OFFSET + f.LENGTH;

                if (f.LENGTH > 0)
                {
                    f.VALUE = VSLib.GetByteArray(data, (int)(offset + f.DATA_OFFSET), (int)f.LENGTH);
                }
                else
                {
                    f.VALUE = new byte[0];
                }

                offset += f.FULL_LENGTH;

                f.DELETED = false;

                // Add to cache
                FCache.Add(f);

                // Add to the tree
                FCacheTree.Insert(f.NAME, i);
            }
            FreeSpace = (int)(this.Size - base.FIXED - offset);
        }
Exemple #6
0
 /// <summary>
 /// Get string value
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public string GetString(string name)
 {
     byte[] b = get_field(name, FIELD_TYPE_STRING);
     return((b == null) ? DEFAULT_STRING : VSLib.ConvertByteToString(b));
 }
Exemple #7
0
 /// <summary>
 /// Read string
 /// </summary>
 /// <param name="offset"></param>
 /// <param name="len"></param>
 /// <returns></returns>
 public string ReadString(long offset, int len)
 {
     return(VSLib.ConvertByteToString(this.ReadBytes(offset, len)));
 }
Exemple #8
0
        /// <summary>
        /// Load catalog content
        /// </summary>
        public void Load()
        {
            int    pos = 0;
            string s   = "";


            err_line = -1;

            if (!File.Exists(catalog_file_name))
            {
                File.WriteAllBytes(catalog_file_name, VSLib.ConvertStringToByte(sg_ok + DEFS.VSTORAGE_VERSION + "$" + "U" + "$" + DEFS.DELIM_NEWLINE));         // 'U'  dont encrypt (+19)
            }
            s = VSLib.ConvertByteToString(File.ReadAllBytes(catalog_file_name));
            // Parse file
            pos = s.IndexOf(DEFS.DELIM_NEWLINE);
            if (pos < 0)
            {
                return;
            }
            // Get encryption status
            string sth = s.Substring(0, pos);

            if (sth.Length < 21)
            {
                err_line = 1;
            }
            else
            {
                // Encryption
                ste     = sth.Substring(19, 1).ToUpper();
                ENCRYPT = (sth.Substring(19, 1) == "E");

                pos += DEFS.DELIM_NEWLINE.Length;
                line = 1;
                VSCatalogDescriptor desc = null;

                while ((pos < s.Length) & (err_line < 0))
                {
                    string par     = "";
                    int    ln      = 0;
                    int    new_pos = s.IndexOf(DEFS.DELIM_NEWLINE, pos);
                    if (new_pos < 0)
                    {
                        ln = s.Length - pos;
                    }
                    else
                    {
                        ln = new_pos - pos;
                    }

                    if (ln > 0)
                    {
                        par = s.Substring(pos, ln).Trim();
                    }
                    line++;
                    pos += (ln + DEFS.DELIM_NEWLINE.Length);

                    if (par != "")
                    {
                        if (par.Substring(0, 1) == "[")
                        { // Parse new descriptor
                            desc = null;
                            string sname = "";
                            int    eb    = par.IndexOf("]", 1);
                            if (eb < 0)
                            {
                                err_line = line;
                            }
                            else
                            {
                                if (eb > 1)
                                {
                                    sname = par.Substring(1, eb - 1);
                                }
                                if (sname != "")
                                {
                                    sname = sname.Trim().ToLower();
                                    for (int i = 0; i < dl.Count; i++)
                                    {
                                        if (dl[i].Name == sname)
                                        {
                                            desc = dl[i];
                                            break;
                                        }
                                    }
                                    if (desc == null)
                                    {
                                        desc      = new VSCatalogDescriptor(this);
                                        desc.name = sname;
                                        dl.Add(desc);
                                    }
                                }
                                else
                                {
                                    err_line = line;
                                }
                            }
                        }
                        else
                        { // Parse parameters
                            string s_val = "";
                            long   n_val = 0;
                            if (desc == null)
                            {
                                err_line = line;
                            }
                            else
                            {
                                par += "                            ";

                                if (parse_long(ref par, DEF_ID, out n_val))
                                {
                                    if (n_val >= 0)
                                    {
                                        desc.id = (short)n_val;
                                    }
                                }
                                else if (parse_long(ref par, DEF_SIZE, out n_val))
                                {
                                    if (n_val >= 0)
                                    {
                                        desc.space_size_pg = n_val;
                                    }
                                }
                                else if (parse_long(ref par, DEF_EXTENSION, out n_val))
                                {
                                    if (n_val >= 0)
                                    {
                                        desc.extension_pg = n_val;
                                    }
                                }
                                else if (parse_long(ref par, DEF_PAGESIZE, out n_val))
                                {
                                    if (n_val >= 0)
                                    {
                                        desc.page_size_kb = n_val;
                                    }
                                }
                                else if (parse_long(ref par, DEF_PARTITIONS, out n_val))
                                {
                                    if (n_val >= 0)
                                    {
                                        desc.partitions = n_val;
                                    }
                                }
                                else if (parse_string(ref par, DEF_PATH, out s_val))
                                {
                                    if (s_val != DEF_ERROR)
                                    {
                                        desc.path = s_val;
                                    }
                                }
                                else if (parse_string(ref par, DEF_INDEXSPACE, out s_val))
                                {
                                    if (s_val != DEF_ERROR)
                                    {
                                        desc.indexspace = s_val;
                                    }
                                }
                                else if (parse_string(ref par, DEF_TIMESTAMP, out s_val))
                                {
                                    if (s_val != DEF_ERROR)
                                    {
                                        desc.creation_timestamp = s_val;
                                    }
                                }
                                else if (parse_string(ref par, DEF_SIGNATURE, out s_val))
                                {
                                    if (s_val != DEF_ERROR)
                                    {
                                        desc.signature = s_val;
                                    }
                                }
                                else
                                {
                                    err_line = line;
                                }
                            }
                        }
                    }
                }
            }
            if (err_line >= 0)
            {
                throw new VSException(DEFS.E0016_OPEN_STORAGE_ERROR_CODE, "- invalid catalog entry at line " + err_line.ToString());
            }

            for (int i = 0; i < dl.Count; i++)
            {
                string sg = dl[i].Signature;
                dl[i].CalculateSignature();
                if (dl[i].Signature != sg)
                {
                    throw new VSException(DEFS.E0016_OPEN_STORAGE_ERROR_CODE, "- missing or invalid space signature for '" + dl[i].Name);
                }
            }
        }