DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Juraj Linkeš" <juraj.linkes@pantheon.tech>
To: thomas@monjalon.net, david.marchand@redhat.com,
	Honnappa.Nagarahalli@arm.com, ohilyard@iol.unh.edu,
	lijuan.tu@intel.com
Cc: dev@dpdk.org, "Juraj Linkeš" <juraj.linkes@pantheon.tech>
Subject: [RFC PATCH v1 12/15] dts: merge DTS framework/serializer.py to DPDK
Date: Wed,  6 Apr 2022 14:56:03 +0000	[thread overview]
Message-ID: <20220406145606.2913834-13-juraj.linkes@pantheon.tech> (raw)
In-Reply-To: <20220406145606.2913834-1-juraj.linkes@pantheon.tech>

---
 dts/framework/serializer.py | 115 ++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100644 dts/framework/serializer.py

diff --git a/dts/framework/serializer.py b/dts/framework/serializer.py
new file mode 100644
index 0000000000..0ffa16a592
--- /dev/null
+++ b/dts/framework/serializer.py
@@ -0,0 +1,115 @@
+# BSD LICENSE
+#
+# Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#   * Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+#   * Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in
+#     the documentation and/or other materials provided with the
+#     distribution.
+#   * Neither the name of Intel Corporation nor the names of its
+#     contributors may be used to endorse or promote products derived
+#     from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""
+Wrapper class for serializer module
+"""
+import os
+import pickle
+
+
+class Singleton(type):
+    _instances = {}
+
+    def __call__(self, *args, **kwargs):
+        if self not in self._instances:
+            self._instances[self] = super(Singleton, self).__call__(*args, **kwargs)
+        return self._instances[self]
+
+
+class Serializer(object):
+
+    """
+    Two-levels cache implementation for storing/retrieving any kind of object
+    using using a key-value model. It uses the pickle module to store objects
+    into a file.
+    This class implements the Singleton pattern. Everytime its constructor
+    is called it will return a reference to the same instance.
+    """
+
+    def __init__(self):
+        self.volatile_cache = {}
+        self.filename = "serializer.cache"
+
+    def save(self, object_name, object_to_save):
+        """
+        Saves an object into the volatile dictionary cache - which
+        resides in memory.
+        """
+        self.volatile_cache[object_name] = object_to_save
+
+    def load(self, object_name):
+        """
+        Loads and returns an object from the volatile cache.
+        """
+        return self.volatile_cache.get(object_name, None)
+
+    def set_serialized_filename(self, filename):
+        """
+        Sets the name of the non-volatile cache file to be used in the future
+        """
+        self.filename = filename
+
+    def save_to_file(self):
+        """
+        Saves the volatile cache to a file (non-volatile) using the pickle
+        module. Returns True in case everything went OK, False otherwise.
+        """
+        try:
+            serialized_file = open(self.filename, "w")
+            pickle.dump(self.volatile_cache, serialized_file)
+            serialized_file.close()
+            return True
+        except:
+            return False
+
+    def load_from_file(self):
+        """
+        Reads from a pickle-like file using pickle module and populates the
+        volatile cache. Returns True in case everything went OK, False
+        otherwise.
+        """
+        try:
+            serialized_file = open(self.filename, "r")
+            self.volatile_cache = pickle.load(serialized_file)
+            serialized_file.close()
+            return True
+        except:
+            self.volatile_cache.clear()
+            return False
+
+    def discard_cache(self):
+        """
+        Discards both volatile and non-volatile cache.
+        """
+        self.volatile_cache.clear()
+        if os.path.exists(self.filename):
+            os.remove(self.filename)
-- 
2.20.1


  parent reply	other threads:[~2022-04-06 14:58 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-06 14:55 [RFC PATCH v1 00/15] merge DTS core files " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 01/15] dts: merge DTS dep/tclclient.tgz " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 02/15] dts: merge DTS dep/tgen.tgz " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 03/15] dts: merge DTS dts " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 04/15] dts: merge DTS framework/__init__.py " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 05/15] dts: merge DTS framework/asan_test.py " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 06/15] dts: merge DTS framework/checkCase.py " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 07/15] dts: merge DTS framework/dts.py " Juraj Linkeš
2022-04-06 14:55 ` [RFC PATCH v1 08/15] dts: merge DTS framework/exception.py " Juraj Linkeš
2022-04-06 14:56 ` [RFC PATCH v1 09/15] dts: merge DTS framework/logger.py " Juraj Linkeš
2022-04-06 14:56 ` [RFC PATCH v1 10/15] dts: merge DTS framework/packet.py " Juraj Linkeš
2022-04-06 14:56 ` [RFC PATCH v1 11/15] dts: merge DTS framework/project_dpdk.py " Juraj Linkeš
2022-04-06 14:56 ` Juraj Linkeš [this message]
2022-04-06 14:56 ` [RFC PATCH v1 13/15] dts: merge DTS framework/utils.py " Juraj Linkeš
2022-04-06 14:56 ` [RFC PATCH v1 14/15] dts: merge DTS main.py " Juraj Linkeš
2022-04-06 14:56 ` [RFC PATCH v1 15/15] dts: merge DTS version.py " Juraj Linkeš
2022-04-07  5:04 ` [RFC PATCH v1 00/15] merge DTS core files " Jerin Jacob
2022-04-07  7:33   ` Thomas Monjalon
2022-04-11  7:41     ` Juraj Linkeš
2022-04-11 17:55       ` Honnappa Nagarahalli
2022-04-11 18:20         ` Owen Hilyard
2022-04-11 19:06   ` Honnappa Nagarahalli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220406145606.2913834-13-juraj.linkes@pantheon.tech \
    --to=juraj.linkes@pantheon.tech \
    --cc=Honnappa.Nagarahalli@arm.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=lijuan.tu@intel.com \
    --cc=ohilyard@iol.unh.edu \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).