Skip to content

Commit

Permalink
we push now, enabled restore, still an error
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Sep 29, 2023
1 parent fc34f7a commit 3d1d970
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 123 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/org/polypheny/db/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public static Catalog getInstance() {

public abstract void updateSnapshot();

public abstract void change();

public abstract void commit();

public abstract void rollback();
Expand Down Expand Up @@ -275,6 +277,9 @@ public static Snapshot snapshot() {
public abstract void removeAdapterTemplate( long templateId );


public abstract PropertyChangeListener getChangeListener();


public abstract void restore();

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
GraphCatalog.class })
public interface LogicalCatalog {


LogicalCatalog withLogicalNamespace( LogicalNamespace namespace );

LogicalNamespace getLogicalNamespace();
Expand Down
83 changes: 83 additions & 0 deletions core/src/main/java/org/polypheny/db/catalog/impl/Persister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.catalog.impl;

import com.drew.lang.Charsets;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.util.PolyphenyHomeDirManager;

public class Persister {

ExecutorService service = Executors.newSingleThreadExecutor();


private final File backup;


public Persister() {
this.backup = initBackupFile();
}


private static File initBackupFile() {
if ( !PolyphenyHomeDirManager.getInstance().checkIfExists( "catalog" ) ) {
PolyphenyHomeDirManager.getInstance().registerNewFolder( "catalog" );
}
File folder = PolyphenyHomeDirManager.getInstance().getFileIfExists( "catalog" );
if ( !folder.isDirectory() ) {
throw new GenericRuntimeException( "There is an error with the catalog folder in the .polypheny folder." );
}
return PolyphenyHomeDirManager.getInstance().registerNewFile( "catalog/catalog.poly" );
}


public synchronized void write( String data ) {
service.execute( () -> {
try {
BufferedWriter writer = new BufferedWriter( new FileWriter( backup, Charsets.ISO_8859_1 ) );
writer.write( data );
writer.flush();
writer.close();
} catch ( IOException e ) {
throw new GenericRuntimeException( e );
}
} );
}


public synchronized String read() {
String data;
try {
BufferedReader reader = new BufferedReader( new FileReader( backup, Charsets.ISO_8859_1 ) );
data = reader.lines().collect( Collectors.joining() );
reader.close();
} catch ( IOException e ) {
throw new GenericRuntimeException( e );
}
return data;
}

}
54 changes: 47 additions & 7 deletions core/src/main/java/org/polypheny/db/catalog/impl/PolyCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.annotations.Deserialize;
import io.activej.serializer.annotations.Serialize;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.polypheny.db.adapter.AbstractAdapterSetting;
Expand Down Expand Up @@ -98,15 +100,22 @@ public class PolyCatalog extends Catalog implements PolySerializable {
public final Map<Long, StoreCatalog> storeCatalogs;

private final IdBuilder idBuilder = IdBuilder.getInstance();
private final Persister persister;

@Getter
private Snapshot snapshot;
private String backup;

private final AtomicBoolean dirty = new AtomicBoolean( false );

@Getter
PropertyChangeListener changeListener = evt -> {
dirty.set( true );
};


public PolyCatalog() {
this(
new HashMap<>(),
this( new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
Expand All @@ -129,13 +138,17 @@ public PolyCatalog(
this.interfaces = new ConcurrentHashMap<>( interfaces );
this.adapterTemplates = new ConcurrentHashMap<>();
this.storeCatalogs = new ConcurrentHashMap<>();
updateSnapshot();

this.persister = new Persister();


}


@Override
public void init() {
//new DefaultInserter();
updateSnapshot();
}


Expand Down Expand Up @@ -167,7 +180,9 @@ public void updateSnapshot() {

// update with newly generated physical entities
this.snapshot = SnapshotBuilder.createSnapshot( idBuilder.getNewSnapshotId(), this, logicalCatalogs, allocationCatalogs );

this.listeners.firePropertyChange( "snapshot", null, this.snapshot );
this.dirty.set( false );
}


Expand All @@ -187,17 +202,25 @@ private void addNamespaceIfNecessary( AllocationEntity entity ) {
}


private void change() {
@Override
public void change() {
// empty for now
updateSnapshot();
this.dirty.set( true );
}


public void commit() {
public synchronized void commit() {
if ( !this.dirty.get() ) {
log.debug( "Nothing changed" );
return;
}

log.debug( "commit" );
this.backup = serialize();

updateSnapshot();
persister.write( backup );
this.dirty.set( false );
}


Expand Down Expand Up @@ -374,6 +397,7 @@ public void deleteNamespace( long id ) {
public long addAdapter( String uniqueName, String clazz, AdapterType type, Map<String, String> settings, DeployMode mode ) {
long id = idBuilder.getNewAdapterId();
adapters.put( id, new CatalogAdapter( id, uniqueName, clazz, type, mode, settings ) );
change();
return id;
}

Expand All @@ -384,12 +408,14 @@ public void updateAdapterSettings( long adapterId, Map<String, String> newSettin
return;
}
adapters.put( adapterId, adapters.get( adapterId ).toBuilder().settings( ImmutableMap.copyOf( newSettings ) ).build() );
change();
}


@Override
public void deleteAdapter( long id ) {
adapters.remove( id );
change();
}


Expand All @@ -399,28 +425,42 @@ public long addQueryInterface( String uniqueName, String clazz, Map<String, Stri

interfaces.put( id, new CatalogQueryInterface( id, uniqueName, clazz, settings ) );

updateSnapshot();
change();
return id;
}


@Override
public void deleteQueryInterface( long id ) {
interfaces.remove( id );
change();
}


@Override
public long addAdapterTemplate( Class<? extends Adapter<?>> clazz, String adapterName, String description, List<DeployMode> modes, List<AbstractAdapterSetting> settings, Function4<Long, String, Map<String, String>, Adapter<?>> deployer ) {
long id = idBuilder.getNewAdapterTemplateId();
adapterTemplates.put( id, new AdapterTemplate( id, clazz, adapterName, settings, modes, description, deployer ) );
change();
return id;
}


@Override
public void removeAdapterTemplate( long templateId ) {
adapterTemplates.remove( templateId );
change();
}


@Override
public void restore() {
this.backup = persister.read();
if ( this.backup == null ) {
log.warn( "No file found to restore" );
return;
}
rollback();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.annotations.Deserialize;
import io.activej.serializer.annotations.Serialize;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Getter;
import lombok.Value;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.IdBuilder;
import org.polypheny.db.catalog.catalogs.AllocationDocumentCatalog;
import org.polypheny.db.catalog.entity.allocation.AllocationCollection;
Expand Down Expand Up @@ -72,6 +74,15 @@ public PolyAllocDocCatalog(
this.collections = new ConcurrentHashMap<>( collections );
this.placements = new ConcurrentHashMap<>( placements );
this.partitions = new ConcurrentHashMap<>( partitions );
listeners.addPropertyChangeListener( Catalog.getInstance().getChangeListener() );
}


PropertyChangeSupport listeners = new PropertyChangeSupport( this );


public void change() {
listeners.firePropertyChange( "change", null, null );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.annotations.Deserialize;
import io.activej.serializer.annotations.Serialize;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Getter;
import lombok.Value;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.IdBuilder;
import org.polypheny.db.catalog.catalogs.AllocationGraphCatalog;
import org.polypheny.db.catalog.entity.allocation.AllocationGraph;
Expand Down Expand Up @@ -74,6 +76,15 @@ public PolyAllocGraphCatalog(
this.graphs = new ConcurrentHashMap<>( graphs );
this.placements = new ConcurrentHashMap<>( placements );
this.partitions = new ConcurrentHashMap<>( partitions );
listeners.addPropertyChangeListener( Catalog.getInstance().getChangeListener() );
}


PropertyChangeSupport listeners = new PropertyChangeSupport( this );


public void change() {
listeners.firePropertyChange( "change", null, null );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.annotations.Deserialize;
import io.activej.serializer.annotations.Serialize;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,6 +29,7 @@
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.polypheny.db.catalog.Catalog;
import org.polypheny.db.catalog.IdBuilder;
import org.polypheny.db.catalog.catalogs.AllocationRelationalCatalog;
import org.polypheny.db.catalog.entity.allocation.AllocationColumn;
Expand Down Expand Up @@ -109,6 +111,15 @@ public PolyAllocRelCatalog(
this.partitions = new ConcurrentHashMap<>( partitions );
this.properties = new ConcurrentHashMap<>( properties );
this.placements = new ConcurrentHashMap<>( placements );
listeners.addPropertyChangeListener( Catalog.getInstance().getChangeListener() );
}


PropertyChangeSupport listeners = new PropertyChangeSupport( this );


public void change() {
listeners.firePropertyChange( "change", null, null );
}


Expand Down
Loading

0 comments on commit 3d1d970

Please sign in to comment.