<commit_before>/*
Copyright 2020 The Ceph-CSI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package journal

import (
	"context"

	"github.com/ceph/ceph-csi/internal/util"

	"github.com/ceph/go-ceph/rados"
	"k8s.io/klog"
)

// listExcess is the number of false-positive key-value pairs we will
// accept from ceph when getting omap values.
var listExcess = 32

func getOMapValues(
	ctx context.Context,
	conn *Connection,
	poolName, namespace, oid, prefix string, keys []string) (map[string]string, error) {
	// fetch and configure the rados ioctx
	ioctx, err := conn.conn.GetIoctx(poolName)
	if err != nil {
		return nil, omapPoolError(poolName, err)
	}
	defer ioctx.Destroy()

	if namespace != "" {
		ioctx.SetNamespace(namespace)
	}

	results := map[string]string{}
	// want is our "lookup map" that ensures O(1) checks for keys
	// while iterating, without needing to complicate the caller.
	want := make(map[string]bool, len(keys))
	for i := range keys {
		want[keys[i]] = true
	}

	err = ioctx.ListOmapValues(
		oid, "", prefix, int64(len(want)+listExcess),
		func(key string, value []byte) {
			if want[key] {
				results[key] = string(value)
			}
		},
	)
	switch err {
	case nil:
	case rados.ErrNotFound:
		klog.Errorf(
			util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q): %v"),
			poolName, namespace, oid, err)
		return nil, util.NewErrKeyNotFound(oid, err)
	default:
		return nil, err
	}

	klog.V(4).Infof(
		util.Log(ctx, "got omap values: (pool=%q, namespace=%q, name=%q): %+v"),
		poolName, namespace, oid, results)
	return results, nil
}

func removeMapKeys(
	ctx context.Context,
	conn *Connection,
	poolName, namespace, oid string, keys []string) error {
	// fetch and configure the rados ioctx
	ioctx, err := conn.conn.GetIoctx(poolName)
	if err != nil {
		return omapPoolError(poolName, err)
	}
	defer ioctx.Destroy()

	if namespace != "" {
		ioctx.SetNamespace(namespace)
	}

	err = ioctx.RmOmapKeys(oid, keys)
	if err != nil {
		klog.Errorf(
			util.Log(ctx, "failed removing omap keys (pool=%q, namespace=%q, name=%q): %v"),
			poolName, namespace, oid, err)
	} else {
		klog.V(4).Infof(
			util.Log(ctx, "removed omap keys (pool=%q, namespace=%q, name=%q): %+v"),
			poolName, namespace, oid, keys)
	}
	return err
}

func setOMapKeys(
	ctx context.Context,
	conn *Connection,<commit_msg>journal: do not return errors from remove omap func if omap missing<commit_after>/*
Copyright 2020 The Ceph-CSI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package journal

import (
	"context"

	"github.com/ceph/ceph-csi/internal/util"

	"github.com/ceph/go-ceph/rados"
	"k8s.io/klog"
)

// listExcess is the number of false-positive key-value pairs we will
// accept from ceph when getting omap values.
var listExcess = 32

func getOMapValues(
	ctx context.Context,
	conn *Connection,
	poolName, namespace, oid, prefix string, keys []string) (map[string]string, error) {
	// fetch and configure the rados ioctx
	ioctx, err := conn.conn.GetIoctx(poolName)
	if err != nil {
		return nil, omapPoolError(poolName, err)
	}
	defer ioctx.Destroy()

	if namespace != "" {
		ioctx.SetNamespace(namespace)
	}

	results := map[string]string{}
	// want is our "lookup map" that ensures O(1) checks for keys
	// while iterating, without needing to complicate the caller.
	want := make(map[string]bool, len(keys))
	for i := range keys {
		want[keys[i]] = true
	}

	err = ioctx.ListOmapValues(
		oid, "", prefix, int64(len(want)+listExcess),
		func(key string, value []byte) {
			if want[key] {
				results[key] = string(value)
			}
		},
	)
	switch err {
	case nil:
	case rados.ErrNotFound:
		klog.Errorf(
			util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q): %v"),
			poolName, namespace, oid, err)
		return nil, util.NewErrKeyNotFound(oid, err)
	default:
		return nil, err
	}

	klog.V(4).Infof(
		util.Log(ctx, "got omap values: (pool=%q, namespace=%q, name=%q): %+v"),
		poolName, namespace, oid, results)
	return results, nil
}

func removeMapKeys(
	ctx context.Context,
	conn *Connection,
	poolName, namespace, oid string, keys []string) error {
	// fetch and configure the rados ioctx
	ioctx, err := conn.conn.GetIoctx(poolName)
	if err != nil {
		return omapPoolError(poolName, err)
	}
	defer ioctx.Destroy()

	if namespace != "" {
		ioctx.SetNamespace(namespace)
	}

	err = ioctx.RmOmapKeys(oid, keys)
	switch err {
	case nil:
	case rados.ErrNotFound:
		// the previous implementation of removing omap keys (via the cli)
		// treated failure to find the omap as a non-error. Do so here to
		// mimic the previous behavior.
		klog.V(4).Infof(
			util.Log(ctx, "when removing omap keys, omap not found (pool=%q, namespace=%q, name=%q): %+v"),
			poolName, namespace, oid, keys)
	default:
		klog.Errorf(
			util.Log(ctx, "failed removing omap keys (pool=%q, namespace=%q, name=%q): %v"),
			poolName, namespace, oid, err)
		return err
	}
	klog.V(4).Infof(
		util.Log(ctx, "removed omap keys (pool=%q, namespace=%q, name=%q): %+v"),
		poolName, namespace, oid, keys)
	return nil
}

func setOMapKeys(
	ctx context.Context,
	conn *Connection,
{'fraction_non_alphanumeric': 0.12489674541549645, 'fraction_numerical': 0.005451842061787543, 'mean_word_length': 4.057644110275689, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7426905', 'n_tokens_mistral': 2184, 'n_tokens_neox': 2064, 'n_words': 675}