1dea18910SJim Walker/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2dea18910SJim Walker/*
3dea18910SJim Walker *     Copyright 2018 Couchbase, Inc
4dea18910SJim Walker *
5dea18910SJim Walker *   Licensed under the Apache License, Version 2.0 (the "License");
6dea18910SJim Walker *   you may not use this file except in compliance with the License.
7dea18910SJim Walker *   You may obtain a copy of the License at
8dea18910SJim Walker *
9dea18910SJim Walker *       http://www.apache.org/licenses/LICENSE-2.0
10dea18910SJim Walker *
11dea18910SJim Walker *   Unless required by applicable law or agreed to in writing, software
12dea18910SJim Walker *   distributed under the License is distributed on an "AS IS" BASIS,
13dea18910SJim Walker *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dea18910SJim Walker *   See the License for the specific language governing permissions and
15dea18910SJim Walker *   limitations under the License.
16dea18910SJim Walker */
17dea18910SJim Walker
18dea18910SJim Walker#include "collections/collections_dockey.h"
19dea18910SJim Walker
20a2622a01STim Bradgate#include <gsl/gsl>
21a2622a01STim Bradgate
22dea18910SJim Walker/**
23dea18910SJim Walker * Factory method to create a Collections::DocKey from a DocKey
24dea18910SJim Walker */
25dea18910SJim WalkerCollections::DocKey Collections::DocKey::make(const ::DocKey& key) {
26dea18910SJim Walker    if (key.getDocNamespace() != DocNamespace::System) {
27dea18910SJim Walker        throw std::invalid_argument("DocKey::make incorrect namespace:" +
28dea18910SJim Walker                                    std::to_string(int(key.getDocNamespace())));
29dea18910SJim Walker    }
30dea18910SJim Walker    const uint8_t* collection = findCollection(key, SystemSeparator);
31dea18910SJim Walker    if (collection) {
32a2622a01STim Bradgate        return DocKey(key, gsl::narrow<uint8_t>(collection - key.data()), 1);
33dea18910SJim Walker    } else {
34dea18910SJim Walker        // No collection found, not an error - ok for DefaultNamespace.
35dea18910SJim Walker        return DocKey(key, 0, 0);
36dea18910SJim Walker    }
37dea18910SJim Walker}