/** * If some or all of the children of this name are versions, returns the latest version among * them. * * @return ContentName The latest version component */ public ContentName getLatestVersionChildName() { // of the available names in _children that are version components, // find the latest one (version-wise) // names are sorted, so the last one that is a version should be the latest version // ListIterator previous doesn't work unless you've somehow gotten it to point at the end... ContentName theName = null; ContentName latestName = null; CCNTime latestTimestamp = null; Iterator<ContentName> it = _children.iterator(); // TODO these are sorted -- we just need to iterate through them in reverse order. Having // trouble finding something like C++'s reverse iterators to do that (linked list iterators // can go backwards -- but you have to run them to the end first). while (it.hasNext()) { theName = it.next(); if (VersioningProfile.isVersionComponent(theName.component(0))) { if (null == latestName) { latestName = theName; latestTimestamp = VersioningProfile.getVersionComponentAsTimestamp(theName.component(0)); } else { CCNTime thisTimestamp = VersioningProfile.getVersionComponentAsTimestamp(theName.component(0)); if (thisTimestamp.after(latestTimestamp)) { latestName = theName; latestTimestamp = thisTimestamp; } } } } return latestName; }
@Test public void testFindVersionComponent() { if (VersioningProfile.findLastVersionComponent(abnotvName) != -1) fail(); if (VersioningProfile.findLastVersionComponent(abName) != -1) fail(); if (VersioningProfile.findLastVersionComponent(abSegName) != 2) fail(); if (VersioningProfile.findLastVersionComponent(abvName) != 2) fail(); if (VersioningProfile.findLastVersionComponent(abvvName) != 3) fail(); }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#addVersion(org.ccnx.ccn.protocol.ContentName, long). */ @Test public void testVersionNameContentNameLong() { ContentName name; /* try with length 2 contentname */ name = VersioningProfile.addVersion(abName, 256); byte[][] parts = {{97}, {98}, {-3, 1, 0}}; if (!name.equals(new ContentName(parts))) fail("long encode version failed"); /* check v=0 comes out 0 length */ name = VersioningProfile.addVersion(abName, 0); if (!name.equals(ab0Name)) fail("long encode version=0 failed"); }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#cutTerminalVersion(org.ccnx.ccn.protocol.ContentName). */ @Test public void testCutTerminalVersion() { if (!VersioningProfile.cutTerminalVersion(abSegName).first().equals(abName)) fail( "Not equals: " + VersioningProfile.cutTerminalVersion(abSegName).first() + " and " + abName); if (!VersioningProfile.cutTerminalVersion(abName).first().equals(abName)) fail(); if (!VersioningProfile.cutTerminalVersion(new ContentName()).first().equals(new ContentName())) fail(); // check correct version field stripped if 2 present if (!VersioningProfile.cutTerminalVersion(abvvName).first().equals(abvName)) fail(); }
// test interface with versioning @Test public void testVersionedNameWithPipeline() { Log.info(Log.FAC_TEST, "Starting testVersionedNameWithPipeline"); long received = 0; byte[] bytes = new byte[1024]; try { vistream = new CCNVersionedInputStream(VersioningProfile.cutLastVersion(testName), readHandle); } catch (IOException e1) { Log.info(Log.FAC_TEST, "failed to open stream for pipeline test: " + e1.getMessage()); Assert.fail(); } while (!vistream.eof()) { try { received += vistream.read(bytes); } catch (IOException e) { Log.warning(Log.FAC_TEST, "failed to read segments: " + e.getMessage()); Assert.fail(); } } Log.info(Log.FAC_TEST, "read " + received + " from stream"); Assert.assertTrue(received == bytesWritten); Log.info(Log.FAC_TEST, "Completed testVersionedNameWithPipeline"); }
/** * Returns the latest version available under this prefix as a CCNTime object. * * @return CCNTime Latest child version as CCNTime */ public CCNTime getLatestVersionChildTime() { ContentName latestVersion = getLatestVersionChildName(); if (null != latestVersion) { return VersioningProfile.getVersionComponentAsTimestamp(latestVersion.component(0)); } return null; }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#addVersion(org.ccnx.ccn.protocol.ContentName, * java.sql.Timestamp). */ @Test public void testVersionNameContentNameTimestamp() { /* try with length 2 contentname */ CCNTime ts = new CCNTime(1000); ts.setNanos(15722656); ContentName name = VersioningProfile.addVersion(abName, ts); if (!name.equals(abvName)) fail("timestamp encode version failed"); }
@Test public void testPaddedVersions() throws Exception { ContentName name = ContentName.fromNative("/testme"); long v0 = 0x80FFFF; byte[] b0 = {VersioningProfile.VERSION_MARKER, (byte) 0x80, (byte) 0xFF, (byte) 0xFF}; ContentName vn0 = VersioningProfile.addVersion(name, v0); byte[] x0 = VersioningProfile.getLastVersionComponent(vn0); System.out.println("From long name : " + vn0.toString()); Assert.assertTrue(Arrays.areEqual(b0, x0)); // now do it as ccntime CCNTime t0 = CCNTime.fromBinaryTimeAsLong(v0); vn0 = VersioningProfile.addVersion(name, t0); x0 = VersioningProfile.getLastVersionComponent(vn0); System.out.println("From ccntime name: " + vn0.toString()); Assert.assertTrue(Arrays.areEqual(b0, x0)); }
/** return the URI-encoded representation and the msec representation as: "%s (%d)" */ @Override public String toString() { if (null == _asString) { synchronized (this) { _asString = String.format( "%s (%d)", VersioningProfile.printAsVersionComponent(_version), _version.getTime()); } } return _asString; }
// need a responder with objects to pipeline @BeforeClass public static void setUpBeforeClass() throws Exception { readHandle = CCNHandle.open(); writeHandle = CCNHandle.open(); ContentName namespace = testHelper.getTestNamespace("pipelineTest"); testName = new ContentName(namespace, "PipelineSegments"); testName = VersioningProfile.addVersion(testName); try { putSegments(); } catch (Exception e) { Log.info(Log.FAC_TEST, "failed to put objects for pipeline test: " + e.getMessage()); Assert.fail(); } }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#hasTerminalVersion(org.ccnx.ccn.protocol.ContentName). */ @Test public void testhasTerminalVersion() { if (VersioningProfile.hasTerminalVersion(abName)) fail("shouldn't be versioned"); if (!VersioningProfile.hasTerminalVersion(abvName)) fail("should be versioned"); if (!VersioningProfile.hasTerminalVersion(abSegName)) fail("should be versioned (with segments): " + abSegName); if (VersioningProfile.hasTerminalVersion(new ContentName())) fail("shouldn't be versioned"); /* check the sequence 0xf8 0x00 * is not treated as a version */ byte[][] parts = {{97}, {98}, {-3, 0}}; if (VersioningProfile.hasTerminalVersion(new ContentName(parts))) fail("not version component"); if (VersioningProfile.hasTerminalVersion(abnotvName)) fail(); }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#getLastVersionAsLong(org.ccnx.ccn.protocol.ContentName). * * @throws VersionMissingException */ @Test public void testGetVersionAsLong() throws VersionMissingException { if (VersioningProfile.getLastVersionAsLong(abSegName) != 0x1040) fail(); ContentName name = VersioningProfile.addVersion(abName, 1); ContentName n2 = ContentName.fromNative(name, "addon"); Assert.assertTrue(VersioningProfile.getLastVersionAsLong(name) == 1); Assert.assertTrue(VersioningProfile.getLastVersionAsLong(n2) == 1); n2 = ContentName.fromNative(n2, "addon2", "addon3"); Assert.assertTrue(VersioningProfile.getLastVersionAsLong(n2) == 1); try { VersioningProfile.getLastVersionAsLong(abName); fail(); } catch (VersionMissingException e) { return; } fail(); }
/** * Print the URI-encoded representation * * @return */ public String printAsVersionComponent() { return VersioningProfile.printAsVersionComponent(_version); }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#addVersion(org.ccnx.ccn.protocol.ContentName). * * @throws InterruptedException */ @Test public void testVersionNameContentName() throws InterruptedException { ContentName name = VersioningProfile.addVersion(abName); Thread.sleep(10); if (name == VersioningProfile.addVersion(abSegName)) fail("should be different versions"); }
/** * Given a versioned ContentName, extract the version number from the end. * * @param versionedNamed * @exception IllegalArgumentException if the name is unversioned * @throws VersionMissingException */ public VersionNumber(ContentName versionedNamed) throws VersionMissingException { _version = VersioningProfile.getLastVersionAsTimestamp(versionedNamed); _versionComponent = VersioningProfile.timeToVersionComponent(_version); _binaryTime = _version.toBinaryTimeAsLong(); }
/** * Create a VersionNumber based on the binaryTime of #version * * @param version */ public VersionNumber(CCNTime version) { _version = new CCNTime(version); _versionComponent = VersioningProfile.timeToVersionComponent(_version); _binaryTime = _version.toBinaryTimeAsLong(); }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#getLastVersionAsTimestamp(org.ccnx.ccn.protocol.ContentName). * * @throws VersionMissingException */ @Test public void testGetVersionAsTimestamp() throws VersionMissingException { CCNTime ts = VersioningProfile.getLastVersionAsTimestamp(abSegName); ContentName name = VersioningProfile.addVersion(abName, ts); if (!name.equals(abvName)) fail(); }
/** * Create a version number from the milliseconds since epoch (as per System.currentTimeMillis or * CCNTime.getTime). * * @param msecSinceEpoch */ public VersionNumber(long msecSinceEpoch) { _version = new CCNTime(msecSinceEpoch); _versionComponent = VersioningProfile.timeToVersionComponent(_version); _binaryTime = _version.toBinaryTimeAsLong(); }
/** * Test method for * org.ccnx.ccn.profiles.VersioningProfile#isVersionOf(org.ccnx.ccn.protocol.ContentName, * org.ccnx.ccn.protocol.ContentName). */ @Test public void testIsVersionOf() { if (!VersioningProfile.isVersionOf(abSegName, abName)) fail(); if (VersioningProfile.isVersionOf(abName, abSegName)) fail(); if (VersioningProfile.isVersionOf(abvName, abvvName)) fail(); }
/** * Create a VersionNumber from a byte array, such as from a ContentName component. * * @param versionComponent */ public VersionNumber(byte[] versionComponent) { _version = VersioningProfile.getVersionComponentAsTimestamp(versionComponent); _versionComponent = copyOf(versionComponent, versionComponent.length); _binaryTime = _version.toBinaryTimeAsLong(); }
@Test public void testThumbnails() throws Exception { Log.info(Log.FAC_TEST, "Starting testThumbnails"); byte[] fakeImageData1 = "xxx".getBytes(); ContentName thumbNailBase = new ContentName(testHelper.getTestNamespace("testThumbnails"), "thumbnailBaseFile"); CCNStringObject cso = new CCNStringObject( thumbNailBase, "thumbNailBase", CCNFlowControl.SaveType.REPOSITORY, putHandle); cso.save(); cso.close(); ContentName origVersion = SegmentationProfile.segmentRoot( VersioningProfile.getLatestVersion( thumbNailBase, cso.getContentPublisher(), SystemConfiguration.LONG_TIMEOUT, putHandle.defaultVerifier(), getHandle) .name()); ContentName thumbName = ThumbnailProfile.getLatestVersion( thumbNailBase, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); Log.info(Log.FAC_TEST, "Check that we can retrieve a simple thumbnail"); RepositoryFileOutputStream thumbImage1 = new RepositoryFileOutputStream(thumbName, putHandle); thumbImage1.write(fakeImageData1, 0, fakeImageData1.length); thumbImage1.close(); ContentName checkThumbName = ThumbnailProfile.getLatestVersion( thumbNailBase, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); checkData(checkThumbName, fakeImageData1); Log.info(Log.FAC_TEST, "Check that we can retrieve a second version of a thumbnail"); byte[] fakeImageData2 = "yyy".getBytes(); ContentName thumbName2 = VersioningProfile.updateVersion(checkThumbName); RepositoryFileOutputStream thumbImage2 = new RepositoryFileOutputStream(thumbName2, putHandle); thumbImage2.write(fakeImageData2, 0, fakeImageData2.length); thumbImage2.close(); checkThumbName = ThumbnailProfile.getLatestVersion( thumbNailBase, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); checkData(checkThumbName, fakeImageData2); Log.info( Log.FAC_TEST, "Check that we can retrieve a thumbnail associated with a second version of a file"); cso = new CCNStringObject( thumbNailBase, "thumbNailBase", CCNFlowControl.SaveType.REPOSITORY, putHandle); cso.save(); cso.close(); byte[] fakeImageData3 = "zzz".getBytes(); thumbName = ThumbnailProfile.getLatestVersion( thumbNailBase, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); RepositoryFileOutputStream thumbImage3 = new RepositoryFileOutputStream(thumbName, putHandle); thumbImage3.write(fakeImageData3, 0, fakeImageData3.length); thumbImage3.close(); checkThumbName = ThumbnailProfile.getLatestVersion( thumbNailBase, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); checkData(checkThumbName, fakeImageData3); Log.info( Log.FAC_TEST, "Check that we can retrieve a second thumbnail associated with a second version of a file"); byte[] fakeImageData4 = "fff".getBytes(); thumbName2 = VersioningProfile.updateVersion(checkThumbName); RepositoryFileOutputStream thumbImage4 = new RepositoryFileOutputStream(thumbName2, putHandle); thumbImage4.write(fakeImageData4, 0, fakeImageData4.length); thumbImage4.close(); checkThumbName = ThumbnailProfile.getLatestVersion( thumbNailBase, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); checkData(checkThumbName, fakeImageData4); Log.info( Log.FAC_TEST, "Check that we can retrieve the correct thumbnail associated with an arbitrary version of a file"); checkThumbName = ThumbnailProfile.getLatestVersion( origVersion, "image.png".getBytes(), SystemConfiguration.LONG_TIMEOUT, putHandle); checkData(checkThumbName, fakeImageData2); Log.info(Log.FAC_TEST, "Completed testThumbnails"); }