// build a query for the separate hashes-table, or the document hash array that ignores
        // the actual index of the hashbin
        private LiteDB.BsonExpression GetQueryForHashBinsIgnoreIndex(int[] hashBins, bool useSeparateTable)
        {
            // don't care about the actual index of the hasbin, only if it exists
            // See https://github.com/perivar/FindSimilar2/blob/master/Soundfingerprinting/DatabaseService.cs\
            var bsonArray = new LiteDB.BsonArray(hashBins.Select(x => new BsonValue(x)));

            if (useSeparateTable)
            {
                return(LiteDB.Query.In("HashBin", bsonArray));
            }
            else
            {
                // use the hashbin array on the main document
                return(LiteDB.Query.In("HashBins[*]", bsonArray));
            }
        }
        public List <TrackData> ReadTracksByReferences(IEnumerable <IModelReference> ids)
        {
            // get track collection
            var col = db.GetCollection <TrackDataDTO>("tracks");

            // two alternative methods
            // 1. use Query.In
            var bsonArray = new LiteDB.BsonArray(ids.Select(x => new BsonValue(x.Id)));
            var results   = col.Find(LiteDB.Query.In("_id", bsonArray));

            // 2. Use Find and Linq Contains
            // var results = col.Find(x => ids.Contains(new ModelReference<string>(x.Id)));

            if (results.Count() == 0)
            {
                return(new List <TrackData>());
            }

            return(results.Select(TrackDataDTO.CopyToTrackData).ToList());
        }
        public List <SubFingerprintData> ReadSubFingerprintDataByReference(IEnumerable <IModelReference> ids)
        {
            // Get fingerprint collection
            var col = db.GetCollection <SubFingerprintDTO>("fingerprints");

            // ensure indexes
            col.EnsureIndex(x => x.SubFingerprintId);

            // two alternative methods
            // 1. use Query.In
            var bsonArray = new LiteDB.BsonArray(ids.Select(x => new BsonValue(x.Id)));
            var results   = col.Find(LiteDB.Query.In("_id", bsonArray));

            // 2. Use Find and Linq Contains
            // var results = col.Find(x => ids.Contains(new ModelReference<string>(x.Id)));

            if (results.Count() == 0)
            {
                return(new List <SubFingerprintData>());
            }

            return(results.Select(SubFingerprintDTO.CopyToSubFingerprintData).ToList());
        }
예제 #4
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, string collection)
        {
            // get entity from list item type
            var entity = mapper.GetEntityMapper(member.UnderlyingType);

            member.Serialize = (list, m) =>
            {
                // supports null values when "SerializeNullValues = true"
                if (list == null)
                {
                    return(BsonValue.Null);
                }

                var result  = new BsonArray();
                var idField = entity.Id;

                foreach (var item in (IEnumerable)list)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var id = idField.Getter(item);

                    result.Add(new BsonDocument
                    {
                        { "$id", m.Serialize(id.GetType(), id, 0) },
                        { "$ref", collection }
                    });
                }

                return(result);
            };

            member.Deserialize = (bson, m) =>
            {
                if (bson.IsArray == false)
                {
                    return(null);
                }

                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(member.DataType, array));
                }

                // copy array changing $id to _id
                var result = new BsonArray();

                foreach (var item in array)
                {
                    var refId   = item["$id"];
                    var missing = item["$missing"] == true;

                    // if referece document are missing, do not inlcude on output list
                    if (missing)
                    {
                        continue;
                    }

                    // if refId is null was included by "include" query, so "item" is full filled document
                    if (refId.IsNull)
                    {
                        result.Add(item);
                    }
                    else
                    {
                        result.Add(new BsonDocument {
                            { "_id", refId }
                        });
                    }
                }

                return(m.Deserialize(member.DataType, result));
            };
        }
예제 #5
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, string collection)
        {
            // get entity from list item type
            var entity = mapper.GetEntityMapper(member.UnderlyingType);

            member.Serialize = (list, m) =>
            {
                // supports null values when "SerializeNullValues = true"
                if (list == null)
                {
                    return(BsonValue.Null);
                }

                var result  = new BsonArray();
                var idField = entity.Id;

                foreach (var item in (IEnumerable)list)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var id = idField.Getter(item);

                    result.Add(new BsonDocument
                    {
                        { "$id", m.Serialize(id.GetType(), id, 0) },
                        { "$ref", collection }
                    });
                }

                return(result);
            };

            member.Deserialize = (bson, m) =>
            {
                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(member.DataType, array));
                }

                var hasIdRef = array[0].AsDocument == null || array[0].AsDocument["$id"].IsNull;

                if (hasIdRef)
                {
                    // if no $id, deserialize as full (was loaded via Include)
                    return(m.Deserialize(member.DataType, array));
                }
                else
                {
                    // copy array changing $id to _id
                    var arr = new BsonArray();

                    foreach (var item in array)
                    {
                        arr.Add(new BsonDocument {
                            { "_id", item.AsDocument["$id"] }
                        });
                    }

                    return(m.Deserialize(member.DataType, arr));
                }
            };
        }