public DynamoDBEntry ToEntry (object value)
		{
			Coordinate cord = value as Coordinate;
			var pList = new PrimitiveList (DynamoDBEntryType.Numeric);
			if (cord != null) {
				pList.Add (cord.Latitude);
				pList.Add (cord.Longitude);
				pList.Add (cord.AccuracyInMeters);
			}
			return pList;
		}
Beispiel #2
0
        public DynamoDBEntry ToEntry(object value)
        {
            Coordinate cord  = value as Coordinate;
            var        pList = new PrimitiveList(DynamoDBEntryType.Numeric);

            if (cord != null)
            {
                pList.Add(cord.Latitude);
                pList.Add(cord.Longitude);
                pList.Add(cord.AccuracyInMeters);
            }
            return(pList);
        }
Beispiel #3
0
        public void Compile()
        {
            if (root.PrimitiveCount == 0)
                // nothing to compile
                return;

            // calc bounding box
            root.boundingBox = surroundingBox(root.primitives);

            bigObjects = new PrimitiveList();
            for (int i = 0; i < root.primitives.Count; i++)
            {
                if (isBigBoundingBox(root.primitives[i].BoundingBox, root.boundingBox))
                {
                    Primitive bigObject = root.primitives[i];
                    bigObjects.Add(bigObject);
                    root.primitives.Remove(bigObject);
                    i--;
                }
            }

            // recalc bounding box
            root.boundingBox = surroundingBox(root.primitives);

            // split root
            root.SplitRecursively(MaxObjectsInNode, 0, MaxTreeDepth);

            compiled = true;
        }
Beispiel #4
0
            public DynamoDBEntry ToEntry(object value)
            {
                if (value == null)
                {
                    return(null);
                }

                var dictionary    = (IDictionary <string, TimeSpan>)value;
                var primitiveList = new PrimitiveList(DynamoDBEntryType.String);

                foreach (var keyValuePair in dictionary)
                {
                    primitiveList.Add(new Primitive(string.Format("{0}@{1}", keyValuePair.Key, keyValuePair.Value)));
                }

                return(primitiveList);
            }
Beispiel #5
0
        private static void TestBinaryDecoding()
        {
            // Test data
            var data                = "Hello world!";
            var binaryData          = Encoding.UTF8.GetBytes(data);
            var base64Data          = Convert.ToBase64String(binaryData);
            var ms                  = new MemoryStream(binaryData);
            var binaryDataPrimitive = new Primitive(ms);

            var data2                = "Big River";
            var binaryData2          = Encoding.UTF8.GetBytes(data2);
            var base64Data2          = Convert.ToBase64String(binaryData2);
            var ms2                  = new MemoryStream(binaryData2);
            var binaryData2Primitive = new Primitive(binaryData2);

            var binarySet = new PrimitiveList(DynamoDBEntryType.Binary);

            binarySet.Add(binaryDataPrimitive);
            binarySet.Add(binaryData2Primitive);
            var binarySetContents = binarySet.AsListOfByteArray();

            var binaryList = new DynamoDBList();

            binaryList.Add(binaryDataPrimitive);
            binaryList.Add(binaryData2Primitive);
            var binaryListContents = binaryList.AsListOfByteArray();

            var base64Set = new PrimitiveList(DynamoDBEntryType.String);

            base64Set.Add(base64Data);
            base64Set.Add(base64Data2);
            var base64SetContents = new List <string> {
                base64Data, base64Data2
            };

            // Populate document
            var doc = new Document();

            doc["Binary"]       = binaryData;
            doc["BinarySet"]    = binarySet;
            doc["BinaryList"]   = binaryList;
            doc["BinaryString"] = base64Data;

            // Convert document to JSON
            var json       = doc.ToJson();
            var prettyJson = doc.ToJsonPretty();

            CompareJson(json, prettyJson);

            // Back to a document
            var rt = Document.FromJson(json);

            // Test to make sure binary data is strings
            var p = rt["Binary"] as Primitive;

            Assert.IsNotNull(p);
            Assert.AreEqual(DynamoDBEntryType.String, p.Type);

            p = rt["BinaryString"] as Primitive;
            Assert.IsNotNull(p);
            Assert.AreEqual(DynamoDBEntryType.String, p.Type);

            var s = rt["BinarySet"] as DynamoDBList;

            Assert.IsNotNull(s);
            Assert.AreEqual(2, s.Entries.Count);
            for (int i = 0; i < s.Entries.Count; i++)
            {
                var entry = s.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.String, (entry as Primitive).Type);
                Assert.AreEqual(base64SetContents[i], entry.AsString());
            }

            var l = rt["BinaryList"] as DynamoDBList;

            Assert.IsNotNull(l);
            Assert.AreEqual(2, l.Entries.Count);
            for (int i = 0; i < l.Entries.Count; i++)
            {
                var entry = l.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.String, (entry as Primitive).Type);
                Assert.AreEqual(base64SetContents[i], entry.AsString());
            }

            // Add a base64 set (SS, with base64 items)
            rt["BinarySet2"] = base64Set;

            // Decode all base64-encoded attributes
            rt.DecodeBase64Attributes("Binary", "BinarySet", "BinaryList", "BinarySet2", "BinaryString", "FakeAttribute");

            // Test to make sure attributes are binary
            var dp = rt["Binary"] as Primitive;

            Assert.IsNotNull(dp);
            Assert.AreEqual(DynamoDBEntryType.Binary, dp.Type);
            CollectionAssert.AreEqual(binaryData, dp.AsByteArray());

            dp = rt["BinaryString"] as Primitive;
            Assert.IsNotNull(dp);
            Assert.AreEqual(DynamoDBEntryType.Binary, dp.Type);
            CollectionAssert.AreEqual(binaryData, dp.AsByteArray());

            var dl = rt["BinaryList"] as DynamoDBList;

            Assert.IsNotNull(dl);
            Assert.AreEqual(2, dl.Entries.Count);
            for (int i = 0; i < dl.Entries.Count; i++)
            {
                var entry = dl.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.Binary, (entry as Primitive).Type);
                CollectionAssert.AreEqual(entry.AsByteArray(), binarySetContents[i]);
            }

            dl = rt["BinarySet"] as DynamoDBList;
            Assert.IsNotNull(dl);
            Assert.AreEqual(2, dl.Entries.Count);
            for (int i = 0; i < dl.Entries.Count; i++)
            {
                var entry = dl.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.Binary, (entry as Primitive).Type);
                CollectionAssert.AreEqual(entry.AsByteArray(), binarySetContents[i]);
            }

            var ds = rt["BinarySet2"] as PrimitiveList;

            Assert.IsNotNull(ds);
            Assert.AreEqual(2, ds.Entries.Count);
            Assert.AreEqual(DynamoDBEntryType.Binary, ds.Type);
            for (int i = 0; i < ds.Entries.Count; i++)
            {
                var entry = ds.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.Binary, (entry as Primitive).Type);
                CollectionAssert.AreEqual(entry.AsByteArray(), binarySetContents[i]);
            }

            rt["FakeBinaryData"] = "this is fake";
            AssertExtensions.ExpectException(() => rt.DecodeBase64Attributes("FakeBinaryData"));
        }
Beispiel #6
0
        // Attempts to decode a particular DynamoDBEntry.
        // May throw exceptions, in particular if the data is not base64 encoded
        private static bool TryDecodeBase64(DynamoDBEntry entry, out DynamoDBEntry decodedEntry)
        {
            decodedEntry = null;

            // Convert string primitive (S) to binary primitive (B)
            var primitive = entry as Primitive;
            if (primitive != null && primitive.Type == DynamoDBEntryType.String)
            {
                // Decode the contents
                var base64 = primitive.Value as string;
                byte[] bytes;
                if (!TryDecodeBase64(base64, out bytes))
                    return false;

                // Store as binary primitive (B)
                decodedEntry = new Primitive(bytes);
                return true;
            }

            // Convert string set (SS) to binary set (BS)
            var primitiveList = entry as PrimitiveList;
            if (primitiveList != null && primitiveList.Type == DynamoDBEntryType.String)
            {
                var decodedList = new PrimitiveList(DynamoDBEntryType.Binary);
                foreach(var item in primitiveList.Entries)
                {
                    // Attempt to decode
                    DynamoDBEntry decodedItem;
                    if (!TryDecodeBase64(item, out decodedItem))
                        return false;

                    // The decoded item must be a Primitive
                    Primitive decodedPrimitive = decodedItem as Primitive;
                    if (decodedPrimitive == null)
                        return false;

                    decodedList.Add(decodedPrimitive);
                }

                decodedEntry = decodedList;
                return true;
            }

            // In a given list (L), convert every string primitive (S) to binary primitive (B)
            // Non-strings and strings that cannot be converted will be left as-is
            var dynamoDBList = entry as DynamoDBList;
            if (dynamoDBList != null)
            {
                var decodedList = new DynamoDBList();
                foreach(var item in dynamoDBList.Entries)
                {
                    DynamoDBEntry decodedItem;
                    if (!TryDecodeBase64(item, out decodedItem))
                    {
                        // if decoding could not succeed, store same item
                        decodedItem = item;
                    }

                    decodedList.Add(decodedItem);
                }

                decodedEntry = decodedList;
                return true;
            }

            return false;
        }
Beispiel #7
0
			public DynamoDBEntry ToEntry(object value)
			{
                if (value == null)
                {
                    return null;
                }

				var dictionary = (IDictionary<string, TimeSpan>)value;
				var primitiveList = new PrimitiveList(DynamoDBEntryType.String);
				foreach (var keyValuePair in dictionary)
				{
					primitiveList.Add(new Primitive(string.Format("{0}@{1}", keyValuePair.Key, keyValuePair.Value)));
				}

				return primitiveList;
			}