public void addResourcesToGroup(Resource[] newResources)
 {
     foreach (Resource newResource in newResources)
     {
         resourceList[newResource.header.resourceID] = newResource;
     }
 }
        public static void sendResourceInfo(IPEndPoint destination, RingInfo ringInfo, Resource[] resources)
        {
            byte[] message = new byte[Constants.WRITEBUFFSIZE];
            MemoryStream stream;

            try
            {
                User user = User.getInstance();
                BinaryFormatter serializer = new BinaryFormatter();
                stream = new MemoryStream(message);

                NetLib.insertEntropyHeader(serializer, stream);

                serializer.Serialize(stream, Constants.MessageTypes.MSG_QUERYHIT);
                serializer.Serialize(stream, ringInfo.ring.ringID);
                serializer.Serialize(stream, ringInfo.token);
                serializer.Serialize(stream, new Peer(user.node, user.publicUserInfo, ringInfo.IE));
                serializer.Serialize(stream, resources.Length);

                foreach (Resource resource in resources)
                {
                    serializer.Serialize(stream, resource.header);
                }

                NetLib.communicateAsync(destination, message, false);
            }
            catch (Exception e)
            {
                int x = 2;
            }
        }
        public void addResources(Resource[] resources)
        {
            QueryProcessor queryProcessor = QueryProcessor.getInstance();
            float partialWeight;  /*
                                   * This is partial because only after all resources have been inserted we can update the
                                   * second part of the weight formula N/dfk
                                   */
            try
            {
                foreach (Resource resource in resources)
                {
                    foreach (InformationEntropy IE in resource.IE)
                    {
                        string keyword = IE.keyword;

                        //note that IE.weight contains the frequency of keyword in resource (FDk)
                        partialWeight = IE.weight/resource.IE.Length;
                        RRLib.SortedList invertedList = (RRLib.SortedList)resourcesIndex.getInvertedList(keyword);
                        if (invertedList == null)
                        {
                            invertedList = new RRLib.SortedList();
                            resourcesIndex.add(keyword, invertedList);
                        }

                        invertedList.Add(new ResourceDescriptor(resource, partialWeight,
                            (uint)(partialWeight*resource.IE.Length), (uint)resource.IE.Length));
                    }
                    updateUI(1);
                }
            }
            catch (Exception e)
            {
                int x = 2;
            }
        }
 public ResourceDescriptor(Resource __resource, float __weight, uint __frequency, uint __numKeywords)
 {
     resource = __resource;
     weight = __weight;
     frequencyOfKeyword = __frequency;
     numKeywords = __numKeywords;
 }
        public static string getFileExtention(Resource resource)
        {
            if (resource.data == null || !(resource.data is FileInfo))
                return resource.header.name.Substring(resource.header.name.LastIndexOf("."));

            FileInfo file = (FileInfo)resource.data;
            return file.Extension;
        }
 public override bool resourceBelongsToThisGroup(Resource resource)
 {
     string extention = FileLib.getFileExtention(resource);
     foreach (string filterExtention in this.filters)
     {
         if (filterExtention.CompareTo(extention) == 0)
             return true;
     }
     return false;
 }
 public FileGroup(string groupName, Resource[] files, string[] __filters)
     : base(groupName, files)
 {
     filters = __filters;
 }
 public virtual bool resourceBelongsToThisGroup(Resource resource)
 {
     return false;
 }
 public void addResourceToGroup(Resource newResource)
 {
     resourceList[newResource.header.resourceID] = newResource;
 }
 public ResourceGroup(string __groupName, Resource[] __resources)
 {
     groupName = __groupName;
     resourceList = new Hashtable();
     addResourcesToGroup(__resources);
 }
 public ResourceGroup GetResourceGroup(Resource resource)
 {
     foreach (ResourceGroup group in this.groups)
     {
         if (group.resourceBelongsToThisGroup(resource))
             return group;
     }
     return null;
 }
        public Resource[] rankDatabase(InvertedIndex db, string[] query, string[] columns)
        {
            RRLib.SortedList resources;
            float Wkd, Wkq = 1.0f;
            float existingNumerator, existingResourceLength, existingQueryLength;
            Resource resource;
            ResourceMatch existingMatch;
            Hashtable results = new Hashtable(Constants.MAX_SEARCH_RESULTS_FROM_PEER, 0.9f);

            foreach (string keyword in query)
            {
                if ((resources = (RRLib.SortedList)db.getInvertedList(keyword)) != null)
                {
                    int resourceToConsider = Constants.MAX_RESOURCES_TO_CONSIDER_FOR_KEYWORD-1 > resources.Count ?  resources.Count-1 : Constants.MAX_RESOURCES_TO_CONSIDER_FOR_KEYWORD-1;
                    for (;resourceToConsider >= 0; resourceToConsider--)
                    {
                        ResourceDescriptor rd = (ResourceDescriptor)resources[resourceToConsider];
                        resource = rd.resource;
                        Wkd = rd.weight;
                        if ((existingMatch = (ResourceMatch)results[resource.header.resourceID]) != null)
                        {
                            existingMatch.numerator = existingMatch.numerator +(Wkd*Wkq);
                            existingMatch.resourceLength = existingMatch.resourceLength + (float)Math.Pow(Wkd, 2.0);
                            existingMatch.queryLength  = existingMatch.queryLength + (float)Math.Pow(Wkq, 2.0);

                        }
                        else
                        {
                            existingNumerator = (Wkd*Wkq);
                            existingResourceLength = (float)Math.Pow(Wkd, 2);
                            existingQueryLength = (float)Math.Pow(Wkq, 2);

                            results[rd.resource.header.resourceID] = new ResourceMatch(rd, existingNumerator,
                                existingResourceLength, existingQueryLength);
                        }
                    }
                }
            }
            ResourceMatch[] ret_results = new ResourceMatch[results.Count];
            int insertIndex = 0;
            IDictionaryEnumerator enumerator = results.GetEnumerator();
            float score;
            ResourceMatch match;
            while (enumerator.MoveNext())
            {
                match = (ResourceMatch)enumerator.Value;

                //now the numerator contains the commulative score
                match.numerator = match.numerator/(float)(Math.Sqrt(match.resourceLength) * Math.Sqrt(match.queryLength));
                ret_results[insertIndex++] = match;
            }

            Array.Sort(ret_results, 0, insertIndex);

            //Remove any extra matches beyond the max acceptable
            int clearRange = Math.Min(insertIndex, Constants.MAX_QUERY_MATCHES);
            Array.Clear(ret_results, clearRange, results.Count- clearRange);
            Resource[] final_results = new Resource[ret_results.Length];
            insertIndex = 0;
            foreach (ResourceMatch rMatch in ret_results)
            {
                final_results[insertIndex++] = rMatch.rd.resource;
            }

            return  final_results;
        }
        public static Resource[] filesToResources(FileInfo[] files)
        {
            InformationEntropy[] IE;
            Resource[] resources = new Resource[files.Length];
            int index = 0;
            Constants.ResourceType type = Constants.ResourceType.UNKNOWN_RESOURCE_TYPE;

            foreach (FileInfo file in files)
            {
                if (FileLib.isASCIIDocument(file))
                {
                    type = Constants.ResourceType.FILE_TYPE_DOC;
                    IE = InvertedIndex.extractInformationEntropy(file);
                }
                else
                {
                    type = Resource.determineType(file);
                    //just use the file name as an indicator of info in the file
                    IE = InvertedIndex.extractInformationEntropy(file.Name);
                }

                Debug.Assert((IE != null), "Information Entropy from " + file.Name + " is null");
                resources[index++] = new Resource(type, Constants.DEFAULT_TOKENS, file.Length,
                                                  InformationEntropy.Detokenize(IE, ' '), file.Name, file.FullName, IE, file);
            }
            return resources;
        }