Package io.jsondb.io

Class JsonWriter


  • public class JsonWriter
    extends java.lang.Object
    A special File Writer to write to the .json DB files that ensures proper character encoding is used and the necessary File Locks are created.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> boolean appendToJsonFile​(java.util.Collection<T> collection, java.lang.Object objectToSave)
      A utility method that appends the provided object to the end of collection file in a atomic way
      <T> boolean appendToJsonFile​(java.util.Collection<T> collection, java.util.Collection<? extends T> batchToSave)
      A utility method that appends the provided collection of objects to the end of collection file in a atomic way
      <T> boolean removeFromJsonFile​(java.util.Map<java.lang.Object,​T> collection, java.lang.Object id)
      A utility method that substracts the provided Ids and writes rest of the collection to file in a atomic way
      <T> boolean removeFromJsonFile​(java.util.Map<java.lang.Object,​T> collection, java.util.Set<java.lang.Object> removeIds)
      A utility method that subtracts the provided Ids and writes rest of the collection to file in a atomic way
      <T> boolean renameKeyInJsonFile​(java.util.Collection<T> collection, boolean ignoreReadonly, java.lang.String oldKey, java.lang.String newKey)
      A utility method renames a particular key for the entire contents of .json in a atomic way
      <T> boolean reWriteJsonFile​(java.util.Collection<T> collection, boolean ignoreReadonly)
      A utility method that completely replaces the contents of .json with the provided collection in a atomic way
      <T> boolean updateInJsonFile​(java.util.Map<java.lang.Object,​T> collection, java.lang.Object id, T objectToSave)
      A utility method that updates the provided collection of objects into the existing collection file in a atomic way
      <T> boolean updateInJsonFile​(java.util.Map<java.lang.Object,​T> collection, java.util.Map<java.lang.Object,​T> modifiedObjects)
      A utility method that updates the provided collection of objects into the existing collection file in a atomic way
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JsonWriter

        public JsonWriter​(JsonDBConfig dbConfig,
                          CollectionMetaData cmd,
                          java.lang.String collectionName,
                          java.io.File collectionFile)
                   throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • appendToJsonFile

        public <T> boolean appendToJsonFile​(java.util.Collection<T> collection,
                                            java.lang.Object objectToSave)
        A utility method that appends the provided object to the end of collection file in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        objectToSave - new Object that is being inserted or updated.
        Returns:
        true if success
      • appendToJsonFile

        public <T> boolean appendToJsonFile​(java.util.Collection<T> collection,
                                            java.util.Collection<? extends T> batchToSave)
        A utility method that appends the provided collection of objects to the end of collection file in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        batchToSave - collection of objects to append.
        Returns:
        true if success
      • removeFromJsonFile

        public <T> boolean removeFromJsonFile​(java.util.Map<java.lang.Object,​T> collection,
                                              java.lang.Object id)
        A utility method that substracts the provided Ids and writes rest of the collection to file in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        id - id of objects to be removed.
        Returns:
        true if success
      • removeFromJsonFile

        public <T> boolean removeFromJsonFile​(java.util.Map<java.lang.Object,​T> collection,
                                              java.util.Set<java.lang.Object> removeIds)
        A utility method that subtracts the provided Ids and writes rest of the collection to file in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        removeIds - ids of objects to be removed.
        Returns:
        true if success
      • updateInJsonFile

        public <T> boolean updateInJsonFile​(java.util.Map<java.lang.Object,​T> collection,
                                            java.lang.Object id,
                                            T objectToSave)
        A utility method that updates the provided collection of objects into the existing collection file in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        id - the id of object to save
        objectToSave - the actual object to save.
        Returns:
        true if success
      • updateInJsonFile

        public <T> boolean updateInJsonFile​(java.util.Map<java.lang.Object,​T> collection,
                                            java.util.Map<java.lang.Object,​T> modifiedObjects)
        A utility method that updates the provided collection of objects into the existing collection file in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        modifiedObjects - objects to update.
        Returns:
        true if success
      • reWriteJsonFile

        public <T> boolean reWriteJsonFile​(java.util.Collection<T> collection,
                                           boolean ignoreReadonly)
        A utility method that completely replaces the contents of .json with the provided collection in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        ignoreReadonly - force rewrite even if the collection is marked readonly this is necessary for schemaupdate.
        Returns:
        true if success
      • renameKeyInJsonFile

        public <T> boolean renameKeyInJsonFile​(java.util.Collection<T> collection,
                                               boolean ignoreReadonly,
                                               java.lang.String oldKey,
                                               java.lang.String newKey)
        A utility method renames a particular key for the entire contents of .json in a atomic way
        Type Parameters:
        T - Type annotated with Document annotation and member of the baseScanPackage
        Parameters:
        collection - existing collection
        ignoreReadonly - force rewrite even if the collection is marked readonly this is necessary for schemaupdate.
        oldKey - String representing the old key/field
        newKey - String representing the new key/field
        Returns:
        true if success