private string CreatePackageForSend(ReplicationPacket packet, out int last)
        {
            int      maxc = INTERNALLIMIT;
            WhatItem what = GetBranchConfig(packet.branchname).what;

            if (what.PackageItemLimit > 0)
            {
                maxc = what.PackageItemLimit;
            }
            string outFolder     = _OutboxPath;
            int    packageNumber = packet.lastrecord;
            int    i             = packet.lastrecord;
            string filename      = outFolder + _S + packet.branchname + _S + packageNumber.ToString("0000000000") + ".mgdat";

            if (i < _docs.RecordCount())
            {
                StorageFile <Guid> package = new StorageFile <Guid>(filename, SF_FORMAT.JSON, true);
                while (maxc > 0)
                {
                    var meta = _docs.GetMeta(i);
                    if (meta == null)
                    {
                        break;
                    }
                    if (meta.isReplicated == false && MatchType(meta.typename, what))
                    {
                        if (meta.isDeleted == false || what.PropogateHQDeletes)
                        {
                            object obj = _docs.GetObject(i, out meta);
                            package.WriteObject(meta.key, obj);
                            maxc--;
                        }
                    }

                    i++;
                }
                package.Shutdown();
                packageNumber++;
                // compress the file
                using (FileStream read = File.OpenRead(filename))
                    using (FileStream outp = File.Create(filename + ".gz"))
                        CompressForBackup(read, outp);

                // delete uncompressed file
                File.Delete(filename);
            }

            last = i;
            return(filename + ".gz");
        }
        private bool MatchType(string typename, WhatItem what)
        {
            // match type filter
            foreach (var i in what.HQ2Btypes)
            {
                // do wildcard search
                Regex reg = new Regex("^" + i.Replace("*", ".*").Replace("?", "."), RegexOptions.IgnoreCase);
                if (reg.IsMatch(typename))
                {
                    return(true);
                }
            }

            return(false);
        }
        private ClientWhatWhenConfig GetBranchConfig(string branchname)
        {
            WhatItem ret = _config.What.Find((WhatItem w) => { return(w.Name.ToLower() == branchname.ToLower()); });

            if (ret == null)
            {
                ret = _config.What.Find((WhatItem w) => { return(w.Name.ToLower() == "default"); });
            }

            ClientWhatWhenConfig c = new ClientWhatWhenConfig();

            c.what    = ret;
            var where = _config.Where.Find(w => { return(w.BranchName.ToLower() == branchname.ToLower()); });
            if (where != null)
            {
                c.whencron = where.When;
            }
            else
            {
                c.whencron = "* * * * *";
            }

            return(c);
        }
        private bool MatchType(string typename, WhatItem what)
        {
            // match type filter
            foreach (var i in what.HQ2Btypes)
            {
                // do wildcard search
                Regex reg = new Regex("^" + i.Replace("*", ".*").Replace("?", "."), RegexOptions.IgnoreCase);
                if (reg.IsMatch(typename))
                    return true;
            }

            return false;
        }