Exemple #1
0
// Filter multiple registrations
func (self *MemoryStorage) pathFilter(path, op, value string, page, perPage int) ([]Service, int, error) {
	matchedIds := []string{}
	pathTknz := strings.Split(path, ".")

	self.mutex.RLock()
	for _, svc := range self.data {
		matched, err := catalog.MatchObject(svc, pathTknz, op, value)
		if err != nil {
			self.mutex.RUnlock()
			return []Service{}, 0, err
		}
		if matched {
			matchedIds = append(matchedIds, svc.Id)
		}
	}

	keys := catalog.GetPageOfSlice(matchedIds, page, perPage, MaxPerPage)
	if len(keys) == 0 {
		self.mutex.RUnlock()
		return []Service{}, len(matchedIds), nil
	}

	svcs := make([]Service, 0, len(keys))
	for _, k := range keys {
		svcs = append(svcs, self.data[k])
	}
	self.mutex.RUnlock()
	return svcs, len(matchedIds), nil
}
Exemple #2
0
func (self *MemoryStorage) pathFilterResources(path, op, value string, page, perPage int) ([]Resource, int, error) {
	self.mutex.RLock()
	resourceIds := make([]string, 0, len(self.resources))
	pathTknz := strings.Split(path, ".")

	for _, res := range self.resources {
		matched, err := catalog.MatchObject(res, pathTknz, op, value)
		if err != nil {
			self.mutex.RUnlock()
			return []Resource{}, 0, err
		}
		if matched {
			resourceIds = append(resourceIds, res.Id)
		}
	}

	pageResourceIds := catalog.GetPageOfSlice(resourceIds, page, perPage, MaxPerPage)
	ress := make([]Resource, 0, len(pageResourceIds))
	for _, id := range pageResourceIds {
		ress = append(ress, self.resources[id])
	}

	self.mutex.RUnlock()
	return ress, len(resourceIds), nil
}
Exemple #3
0
func (self *MemoryStorage) pathFilterDevices(path, op, value string, page, perPage int) ([]Device, int, error) {
	pathTknz := strings.Split(path, ".")

	self.mutex.RLock()
	resourceIds := make([]string, 0, len(self.resources))
	for _, d := range self.devices {
		dev, _ := self.get(d.Id)
		matched, err := catalog.MatchObject(dev, pathTknz, op, value)
		if err != nil {
			self.mutex.RUnlock()
			return []Device{}, 0, err
		}
		if matched {
			// save IDs of all resources of the matched device
			for _, r := range dev.Resources {
				resourceIds = append(resourceIds, r.Id)
			}
		}
	}

	// get the slice of resources as indicated by page
	pageResourceIds := catalog.GetPageOfSlice(resourceIds, page, perPage, MaxPerPage)
	ress := make([]Resource, 0, len(pageResourceIds))
	for _, i := range pageResourceIds {
		ress = append(ress, self.resources[i])
	}

	// convert to devices
	devs := self.devicesFromResources(ress)
	self.mutex.RUnlock()
	return devs, len(resourceIds), nil
}
Exemple #4
0
// Path filtering
// Filter one registration
func (self *MemoryStorage) pathFilterOne(path string, op string, value string) (Service, error) {
	pathTknz := strings.Split(path, ".")

	self.mutex.RLock()
	// return the first one found
	for _, svc := range self.data {
		matched, err := catalog.MatchObject(svc, pathTknz, op, value)
		if err != nil {
			self.mutex.RUnlock()
			return Service{}, err
		}
		if matched {
			self.mutex.RUnlock()
			return svc, nil
		}
	}
	self.mutex.RUnlock()
	return Service{}, nil
}
Exemple #5
0
// Path filtering
func (self *MemoryStorage) pathFilterDevice(path, op, value string) (Device, error) {
	pathTknz := strings.Split(path, ".")

	self.mutex.RLock()
	// return the first one found
	for _, d := range self.devices {
		dev, _ := self.get(d.Id)
		matched, err := catalog.MatchObject(dev, pathTknz, op, value)
		if err != nil {
			self.mutex.RUnlock()
			return Device{}, err
		}
		if matched {
			self.mutex.RUnlock()
			return dev, nil
		}
	}
	self.mutex.RUnlock()
	return Device{}, nil
}
Exemple #6
0
func (self *MemoryStorage) pathFilterResource(path, op, value string) (Resource, error) {
	pathTknz := strings.Split(path, ".")

	self.mutex.RLock()
	// return the first one found
	for _, d := range self.devices {
		for _, rid := range d.Resources {
			res := self.resources[rid]
			matched, err := catalog.MatchObject(res, pathTknz, op, value)
			if err != nil {
				self.mutex.RUnlock()
				return Resource{}, err
			}
			if matched {
				self.mutex.RUnlock()
				return res, nil
			}
		}
	}
	self.mutex.RUnlock()
	return Resource{}, nil
}