<commit_before>"""ChannelDb impelements a "Channel as a Database" service for bot use."""
import json


class ChannelDb(object):
    """Flow Channel as a Database."""

    _data = {}

    def __init__(self, server, config):
        """Initialize the channel db using the server connection passed."""
        self.config = config
        self.server = server
        self.account_id = self.server.flow.account_id()
        self._data = self._get_all(config.db_keys)

    def get(self, key):
        """Get all records for the given key in the channel database.

        If the key has already been loaded into memory (self._data) then just
        fetch it from there. Otherwise, do a search for the key in the
        db-channel.
        """
        if key in self._data:
            return self._data[key]

        messages = self.server.flow.search(
            oid=self.config.org_id,
            cid=self._get_db_channel_id(),
            search=key)

        return self._get_data_from_messages(messages, key)

    def get_last(self, key):
        """Get the most recent record for the given key."""
        all_records = self.get(key)
        return all_records[-1] if all_records else None

    def new(self, key, value):
        """Save a new record in both memory (self._data) and the db-channel."""
        self._data.setdefault(key, []).append(value)

        self.server.flow.send_message(
            cid=self._get_or_create_db_channel(),
            oid=self.config.org_id,
            msg=json.dumps({key: value})
        )

    def _get_all(self, keys):
        """Load records for each of the given keys into a dict."""
        data = {}
        if keys:
            for key in keys:
                data[key] = self.get(key)
        return data
<commit_msg>Fix channel_db._data if new is called before get<commit_after>"""ChannelDb impelements a "Channel as a Database" service for bot use."""
import json


class ChannelDb(object):
    """Flow Channel as a Database."""

    _data = {}

    def __init__(self, server, config):
        """Initialize the channel db using the server connection passed."""
        self.config = config
        self.server = server
        self.account_id = self.server.flow.account_id()
        self._data = self._get_all(config.db_keys)

    def get(self, key):
        """Get all records for the given key in the channel database.

        If the key has already been loaded into memory (self._data) then just
        fetch it from there. Otherwise, do a search for the key in the
        db-channel.
        """
        if key in self._data:
            return self._data[key]

        messages = self.server.flow.search(
            oid=self.config.org_id,
            cid=self._get_db_channel_id(),
            search=key)

        return self._get_data_from_messages(messages, key)

    def get_last(self, key):
        """Get the most recent record for the given key."""
        all_records = self.get(key)
        return all_records[-1] if all_records else None

    def new(self, key, value):
        """Save a new record in both memory (self._data) and the db-channel."""
        self._data.setdefault(key, self.get(key)).append(value)

        self.server.flow.send_message(
            cid=self._get_or_create_db_channel(),
            oid=self.config.org_id,
            msg=json.dumps({key: value})
        )

    def _get_all(self, keys):
        """Load records for each of the given keys into a dict."""
        data = {}
        if keys:
            for key in keys:
                data[key] = self.get(key)
        return data

{'fraction_non_alphanumeric': 0.10289210233592881, 'fraction_numerical': 0.0005561735261401557, 'mean_word_length': 2.1608084358523727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6093249', 'n_tokens_mistral': 1034, 'n_tokens_neox': 1003, 'n_words': 333}