Exemple #1
0
func (self ReadableCatalogAPI) paginatedDeviceFromDevice(d Device, page, perPage int) *PaginatedDevice {
	pd := &PaginatedDevice{
		&d,
		make([]Resource, 0, len(d.Resources)),
		page,
		perPage,
		len(d.Resources),
	}

	resourceIds := make([]string, 0, len(d.Resources))
	for _, r := range d.Resources {
		resourceIds = append(resourceIds, r.Id)
	}

	pageResourceIds := catalog.GetPageOfSlice(resourceIds, page, perPage, MaxPerPage)
	for _, id := range pageResourceIds {
		for _, r := range d.Resources {
			if r.Id == id {
				pd.Resources = append(pd.Resources, r.ldify(self.apiLocation))
			}
		}
	}

	return pd
}
Exemple #2
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 #3
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 #4
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 #5
0
func (self *MemoryStorage) getMany(page int, perPage int) ([]Service, int, error) {
	self.mutex.RLock()
	total := len(self.data)
	keys := catalog.GetPageOfSlice(self.index, page, perPage, MaxPerPage)

	if len(keys) == 0 {
		self.mutex.RUnlock()
		return []Service{}, total, nil
	}

	svcs := make([]Service, 0, len(keys))
	for _, k := range keys {
		svcs = append(svcs, self.data[k])
	}
	self.mutex.RUnlock()
	return svcs, total, nil
}
Exemple #6
0
// Utility
func (self *MemoryStorage) getMany(page int, perPage int) ([]Device, int, error) {
	self.mutex.RLock()
	keys := catalog.GetPageOfSlice(self.index, page, perPage, MaxPerPage)
	total := len(self.resources)

	if len(keys) == 0 {
		self.mutex.RUnlock()
		return []Device{}, total, nil
	}

	ress := make([]Resource, 0, len(keys))
	for _, k := range keys {
		ress = append(ress, self.resources[k])
	}
	devs := self.devicesFromResources(ress)

	self.mutex.RUnlock()
	return devs, total, nil
}