diff --git a/cstore/deltachain.h b/cstore/deltachain.h --- a/cstore/deltachain.h +++ b/cstore/deltachain.h @@ -160,39 +160,6 @@ } }; -/* - * Wrapper around python delta chain - */ -class PyDeltaChain : public DeltaChain { - private: - std::shared_ptr< std::vector > _chain; - - public: - PyDeltaChain(std::shared_ptr< std::vector > chain) : - _chain(chain) {} - - // Default destructor is used, because the destructor of _chain object - // will free the allocated memory automatically. - ~PyDeltaChain() {} - - const DeltaChainLink getlink(const size_t idx) { - return _chain->at(idx); - } - - size_t linkcount() { - return _chain->size(); - } - - get_delta_chain_code_t status() { - if (_chain->size()) { - return GET_DELTA_CHAIN_OK; - } else { - return GET_DELTA_CHAIN_NOT_FOUND; - } - } - -}; - class DeltaChainIterator { private: size_t _index; diff --git a/cstore/deltachain.cpp b/cstore/deltachain.cpp --- a/cstore/deltachain.cpp +++ b/cstore/deltachain.cpp @@ -4,7 +4,7 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. -// deltachain.h - c++ implementation of deltachain and related classes +// deltachain.cpp - c++ implementation of deltachain and related classes // no-check-code #include "cstore/deltachain.h" diff --git a/cstore/py-datapackstore.h b/cstore/py-datapackstore.h --- a/cstore/py-datapackstore.h +++ b/cstore/py-datapackstore.h @@ -25,8 +25,9 @@ #include "cstore/datastore.h" #include "cstore/key.h" #include "cstore/py-structs.h" +#include "cstore/pythondatastore.h" +#include "cstore/pythonkeyiterator.h" #include "cstore/pythonutil.h" -#include "cstore/pythonkeyiterator.h" #include "cstore/uniondatapackstore.h" // --------- DatapackStore Implementation --------- @@ -205,7 +206,8 @@ try { std::vector stores; - std::vector pySubStores; + std::vector cSubStores; + std::vector< std::shared_ptr > pySubStores; PyObject *item; PythonObj inputIterator = PyObject_GetIter(storeList); @@ -213,26 +215,34 @@ // Record the substore references, so: // A) We can decref them in case of an error. // B) They don't get GC'd while the uniondatapackstore holds on to them. - pySubStores.push_back(PythonObj(item)); + int iscdatapack = PyObject_IsInstance(item, (PyObject*)&datapackstoreType); - int isinstance = PyObject_IsInstance(item, (PyObject*)&datapackstoreType); - if (isinstance == 0) { - PyErr_SetString(PyExc_RuntimeError, "cuniondatapackstore only accepts cdatapackstore"); - return -1; - } else if (isinstance != 1) { + PythonObj store(item); + if (iscdatapack == 1) { + cSubStores.push_back(store); + py_datapackstore *subStore = (py_datapackstore*)item; + stores.push_back(&subStore->datapackstore); + } else if (iscdatapack == 0) { + // Memory management of PythonDataStore is passed to + // py_uniondatapackstore + std::shared_ptr pystore = + std::make_shared(store); + pySubStores.push_back(pystore); + stores.push_back(pystore.get()); + } else { // Error return -1; } - - py_datapackstore *pySubStore = (py_datapackstore*)item; - stores.push_back(&pySubStore->datapackstore); } // We have to manually call the member constructor, since the provided 'self' // is just zerod out memory. new(&self->uniondatapackstore) std::shared_ptr(new UnionDatapackStore(stores)); - new(&self->substores) std::vector(); - self->substores = pySubStores; + new(&self->cstores) std::vector(); + new(&self->pystores) std::vector< std::shared_ptr >(); + + self->cstores = cSubStores; + self->pystores = pySubStores; } catch (const std::exception &ex) { PyErr_SetString(PyExc_RuntimeError, ex.what()); return -1; @@ -243,7 +253,8 @@ static void uniondatapackstore_dealloc(py_uniondatapackstore *self) { self->uniondatapackstore.~shared_ptr(); - self->substores.~vector(); + self->cstores.~vector(); + self->pystores.~vector< std::shared_ptr >(); PyObject_Del(self); } diff --git a/cstore/py-structs.h b/cstore/py-structs.h --- a/cstore/py-structs.h +++ b/cstore/py-structs.h @@ -13,6 +13,7 @@ #include #include "cstore/datapackstore.h" +#include "cstore/pythondatastore.h" #include "cstore/pythonutil.h" #include "cstore/uniondatapackstore.h" @@ -28,7 +29,8 @@ std::shared_ptr uniondatapackstore; // Keep a reference to the python objects so we can decref them later. - std::vector substores; + std::vector cstores; + std::vector< std::shared_ptr > pystores; }; #endif // FBHGEXT_CSTORE_PY_STRUCTS_H diff --git a/cstore/pythondatastore.h b/cstore/pythondatastore.h new file mode 100644 --- /dev/null +++ b/cstore/pythondatastore.h @@ -0,0 +1,80 @@ +// pythondatastore.h - c++ declarations for a python data store +// +// Copyright 2017 Facebook, Inc. +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. +// +// no-check-code + +// The PY_SSIZE_T_CLEAN define must be defined before the Python.h include, +// as per the documentation. + +#ifndef FBHGEXT_PYTHONDATASTORE_H +#define FBHGEXT_PYTHONDATASTORE_H + +#define PY_SSIZE_T_CLEAN +#include +#include + +#include "cstore/datastore.h" +#include "cstore/key.h" +#include "cstore/pythonutil.h" + +/* + * Wrapper around python delta chain + */ +class PyDeltaChain : public DeltaChain { + private: + std::shared_ptr< std::vector > _chain; + std::shared_ptr< std::vector > _pythonChainLinks; + + public: + PyDeltaChain(std::shared_ptr< std::vector > chain, + std::shared_ptr< std::vector > pythonChainLinks) : + _chain(chain), + _pythonChainLinks(pythonChainLinks) {} + + // Default destructor is used, because the destructor of _chain + // and _tuples objects will free the allocated memory automatically. + ~PyDeltaChain() {} + + const DeltaChainLink getlink(const size_t idx) { + return _chain->at(idx); + } + + size_t linkcount() { + return _chain->size(); + } + + get_delta_chain_code_t status() { + if (_chain->size()) { + return GET_DELTA_CHAIN_OK; + } else { + return GET_DELTA_CHAIN_NOT_FOUND; + } + } + +}; + +class PythonDataStore : public DataStore { + private: + PythonObj _store; // pointer to python object + + public: + PythonDataStore(PythonObj store); + + ~PythonDataStore() = default; + + DeltaChainIterator getDeltaChain(const Key &key); + + std::shared_ptr getMissing(KeyIterator &missing); + + std::shared_ptr getDeltaChainRaw(const Key &key); + + bool contains(const Key &key); + + void markForRefresh(); +}; + +#endif //FBHGEXT_PYTHONDATASTORE_H diff --git a/cstore/pythondatastore.cpp b/cstore/pythondatastore.cpp new file mode 100644 --- /dev/null +++ b/cstore/pythondatastore.cpp @@ -0,0 +1,100 @@ +// pythondatastore.cpp - implementation of a python data store +// +// Copyright 2017 Facebook, Inc. +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. +// +// no-check-code + +#include "cstore/pythondatastore.h" +#include "cstore/pythonkeyiterator.h" + +PythonDataStore::PythonDataStore(PythonObj store) : _store(store) {} + +DeltaChainIterator PythonDataStore::getDeltaChain(const Key &key) { + std::shared_ptr chain = getDeltaChainRaw(key); + return DeltaChainIterator(chain); +} + +std::shared_ptr PythonDataStore::getDeltaChainRaw(const Key &key) { + // Build (name, node) tuple and call getdeltachain + // method of the underlying store + PythonObj pyKey = Py_BuildValue("(s#s#)", (key.name).c_str(), + (key.name).size(), key.node, 20); + PythonObj list = _store.callmethod("getdeltachain", pyKey); + + // Extract the delta chain from the list of tuples + // and build a DeltaChain object from them + std::shared_ptr< std::vector > links = + std::make_shared< std::vector >(); + + std::shared_ptr< std::vector > tuples = + std::make_shared< std::vector >(); + + PythonObj iter = PyObject_GetIter(list); + PyObject *item; + while ((item = PyIter_Next(iter)) != NULL) { + PythonObj tuple(item); + + const char *filename, *deltabasefilename; + const uint8_t *node, *deltabasenode, *delta; + uint16_t filenamesz, deltabasefilenamesz; + uint64_t deltasz, nodesz, deltabasenodesz; + + if (!PyArg_ParseTuple(tuple, "s#z#s#z#z#", + &filename, &filenamesz, + &node, &nodesz, + &deltabasefilename, &deltabasefilenamesz, + &deltabasenode, &deltabasenodesz, + &delta, &deltasz)) { + throw pyexception(); + } + + links->push_back(DeltaChainLink(filename, deltabasefilename, node, + deltabasenode, delta, filenamesz, + deltabasefilenamesz, deltasz)); + + tuples->push_back(tuple); + } + + return std::make_shared< PyDeltaChain >(links, tuples); +} + +std::shared_ptr PythonDataStore::getMissing(KeyIterator &missing) { + PythonObj list = PyList_New(0); + + Key *key; + while ((key = missing.next()) != NULL) { + PythonObj pyKey = Py_BuildValue("(s#s#)", key->name.c_str(), + key->name.size(), key->node, 20); + if (PyList_Append(list, (PyObject*)pyKey)) { + throw pyexception(); + } + } + + PythonObj keys = _store.callmethod("getmissing", list); + return std::make_shared(keys); +} + +void PythonDataStore::markForRefresh() { + PythonObj args = Py_BuildValue(""); + _store.callmethod("markforrefresh", args); +} + +class Single : public KeyIterator { + public: + Key *_k; + Single(Key *k) : _k(k) {} + Key *next() { + Key *tmp = _k; + _k = NULL; + return tmp; + } +}; + +bool PythonDataStore::contains(const Key &key) { + Single iter((Key*)&key); + std::shared_ptr it = getMissing(iter); + return (!it->next()); +} diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -242,6 +242,7 @@ 'cstore/deltachain.cpp', 'cstore/py-cstore.cpp', 'cstore/pythonutil.cpp', + 'cstore/pythondatastore.cpp', 'cstore/uniondatapackstore.cpp', 'ctreemanifest/manifest.cpp', 'ctreemanifest/manifest_entry.cpp', diff --git a/tests/cstore-uniondatapackstore.py b/tests/cstore-uniondatapackstore.py --- a/tests/cstore-uniondatapackstore.py +++ b/tests/cstore-uniondatapackstore.py @@ -71,6 +71,17 @@ text = unionstore.get(revisions[0][0], revisions[0][1]) self.assertEquals("content", text) + def testGetFromSingleDeltaPyStore(self): + packdir = self.makeTempDir() + + revisions = [("foo", self.getFakeHash(), nullid, "pycontent")] + fastpack = self.createPack(packdir, revisions=revisions) + + unionstore = uniondatapackstore([fastpack]) + + text = unionstore.get(revisions[0][0], revisions[0][1]) + self.assertEquals("pycontent", text) + def testGetFromChainDeltas(self): packdir = self.makeTempDir() @@ -89,6 +100,24 @@ text = unionstore.get(revisions[1][0], revisions[1][1]) self.assertEquals(rev2, text) + def testGetFromChainDeltasPyStore(self): + packdir = self.makeTempDir() + + rev1 = "pycontent1" + rev2 = "pycontent2" + firsthash = self.getFakeHash() + revisions = [ + ("foo", firsthash, nullid, rev1), + ("foo", self.getFakeHash(), firsthash, + mdiff.textdiff(rev1, rev2)), + ] + fastpack = self.createPack(packdir, revisions=revisions) + + unionstore = uniondatapackstore([fastpack]) + + text = unionstore.get(revisions[1][0], revisions[1][1]) + self.assertEquals(rev2, text) + def testGetDeltaChainSingleRev(self): """Test getting a 1-length delta chain.""" packdir = self.makeTempDir() @@ -102,6 +131,19 @@ self.assertEquals(1, len(chain)) self.assertEquals("content", chain[0][4]) + def testGetDeltaChainSingleRevPyStore(self): + """Test getting a 1-length delta chain.""" + packdir = self.makeTempDir() + + revisions = [("foo", self.getFakeHash(), nullid, "pycontent")] + fastpack = self.createPack(packdir, revisions=revisions) + + unionstore = uniondatapackstore([fastpack]) + + chain = unionstore.getdeltachain(revisions[0][0], revisions[0][1]) + self.assertEquals(1, len(chain)) + self.assertEquals("pycontent", chain[0][4]) + def testGetDeltaChainMultiRev(self): """Test getting a 2-length delta chain.""" packdir = self.makeTempDir() @@ -120,6 +162,24 @@ self.assertEquals("content2", chain[0][4]) self.assertEquals("content", chain[1][4]) + def testGetDeltaChainMultiRevPyStore(self): + """Test getting a 2-length delta chain.""" + packdir = self.makeTempDir() + + firsthash = self.getFakeHash() + revisions = [ + ("foo", firsthash, nullid, "pycontent1"), + ("foo", self.getFakeHash(), firsthash, "pycontent2"), + ] + fastpack = self.createPack(packdir, revisions=revisions) + + unionstore = uniondatapackstore([fastpack]) + + chain = unionstore.getdeltachain(revisions[1][0], revisions[1][1]) + self.assertEquals(2, len(chain)) + self.assertEquals("pycontent2", chain[0][4]) + self.assertEquals("pycontent1", chain[1][4]) + def testGetDeltaChainMultiPack(self): """Test getting chains from multiple packs.""" packdir = self.makeTempDir() @@ -141,6 +201,27 @@ self.assertEquals("content2", chain[0][4]) self.assertEquals("content", chain[1][4]) + def testGetDeltaChainMultiPackPyStore(self): + """Test getting chains from multiple packs.""" + packdir = self.makeTempDir() + + revisions1 = [ + ("foo", self.getFakeHash(), nullid, "content"), + ] + fastpack1 = self.createPack(packdir, revisions=revisions1) + + revisions2 = [ + ("foo", self.getFakeHash(), revisions1[0][1], "content2"), + ] + fastpack2 = self.createPack(packdir, revisions=revisions2) + + unionstore = uniondatapackstore([fastpack1, fastpack2]) + + chain = unionstore.getdeltachain(revisions2[0][0], revisions2[0][1]) + self.assertEquals(2, len(chain)) + self.assertEquals("content2", chain[0][4]) + self.assertEquals("content", chain[1][4]) + def testGetMissing(self): packdir = self.makeTempDir() diff --git a/tests/test-check-code-hg.t b/tests/test-check-code-hg.t --- a/tests/test-check-code-hg.t +++ b/tests/test-check-code-hg.t @@ -74,6 +74,8 @@ Skipping cstore/py-datapackstore.h it has no-che?k-code (glob) Skipping cstore/py-structs.h it has no-che?k-code (glob) Skipping cstore/py-treemanifest.h it has no-che?k-code (glob) + Skipping cstore/pythondatastore.cpp it has no-che?k-code (glob) + Skipping cstore/pythondatastore.h it has no-che?k-code (glob) Skipping cstore/pythonkeyiterator.h it has no-che?k-code (glob) Skipping cstore/pythonutil.cpp it has no-che?k-code (glob) Skipping cstore/pythonutil.h it has no-che?k-code (glob)