protected function prefixLength($namespace, $key)
    {
        if (isset($this->prefixLengths[$namespace])) {
            return $this->prefixLengths[$namespace];
        }

        $firstSegment = $this->firstKeySegments($key);

        $this->failOnMissingNamespace($namespace);

        foreach ($this->addedData[$namespace] as $data) {

            try {

                // If no exception occurs this is the right length
                isset($data[$firstSegment]);

                $this->prefixLengths[$namespace] = 1;
                return $this->prefixLengths[$namespace];

            } catch (KeyLengthException $e) {
                $this->prefixLengths[$namespace] = $e->getMinSegments();
                return $this->prefixLengths[$namespace];
            }

        }

        return 1;

    }